Path: blob/master/src/packages/frontend/editors/task-editor/desc-editor.tsx
1691 views
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45/*6Edit description of a single task7*/89import { Button } from "antd";10import { useCallback, useEffect, useRef } from "react";11import { useDebouncedCallback } from "use-debounce";12import MarkdownInput from "@cocalc/frontend/editors/markdown-input/multimode";13import { Icon } from "@cocalc/frontend/components/icon";14import { TaskActions } from "./actions";15import { SAVE_DEBOUNCE_MS } from "@cocalc/frontend/frame-editors/code-editor/const";16import ColorPicker from "@cocalc/frontend/components/color-picker";17import { MAX_HEIGHT } from "./constants";1819interface Props {20actions: TaskActions;21task_id: string;22desc: string;23font_size: number; // used only to cause refresh24color?: string;25}2627export default function DescriptionEditor({28actions,29task_id,30desc,31font_size,32color,33}: Props) {34const commit = useDebouncedCallback(() => {35actions.commit();36}, SAVE_DEBOUNCE_MS);3738const saveAndClose = useCallback(() => {39actions.commit();40actions.enable_key_handler();41actions.stop_editing_desc(task_id);42}, []);4344const getValueRef = useRef<any>(null);45useEffect(() => {46if (actions.syncdb == null) return;47const beforeChange = () => {48const desc = getValueRef.current();49actions.set_desc(task_id, desc, false);50commit();51};52actions.syncdb.on("before-change", beforeChange);53return () => {54actions.syncdb?.removeListener("before-change", beforeChange);55};56}, []);5758return (59<div>60<MarkdownInput61saveDebounceMs={SAVE_DEBOUNCE_MS}62cacheId={task_id}63value={desc}64onChange={(desc) => {65actions.set_desc(task_id, desc, false);66commit();67}}68getValueRef={getValueRef}69fontSize={font_size}70onShiftEnter={saveAndClose}71onFocus={actions.disable_key_handler}72enableUpload={true}73enableMentions={true}74height={MAX_HEIGHT}75placeholder={76"Enter a description. Use markdown with LaTeX. Evaluate code blocks."77}78autoFocus79onSave={() => {80actions.save();81}}82onUndo={() => {83actions.undo();84}}85onRedo={() => {86actions.redo();87}}88minimal89modeSwitchStyle={{90float: "right",91position: "relative",92}}93/>94<ColorPicker95toggle={<Button style={{ float: "right" }}>Color...</Button>}96color={color}97onChange={(color) => {98actions.set_color(task_id, color);99commit();100}}101style={{102float: "right",103border: "1px solid #ccc",104padding: "15px",105background: "white",106marginBottom: "15px",107boxShadow: "3px 3px 3px #ccc",108}}109/>110<Button111onClick={saveAndClose}112size="small"113type="link"114style={{ marginTop: "5px" }}115>116<Icon name="save" /> Done (shift+enter)117</Button>118</div>119);120}121122123