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