Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/editors/task-editor/desc-rendered.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
Rendered view of the description of a single task
8
*/
9
10
import { React } from "../../app-framework";
11
import MostlyStaticMarkdown from "@cocalc/frontend/editors/slate/mostly-static-markdown";
12
import { header_part } from "./desc-rendering";
13
import { TaskActions } from "./actions";
14
import { MAX_HEIGHT } from "./constants";
15
16
interface Props {
17
actions?: TaskActions;
18
task_id: string;
19
desc: string;
20
read_only?: boolean;
21
selectedHashtags?: Set<string>;
22
searchWords?: string[];
23
is_current?: boolean;
24
hideBody?: boolean;
25
}
26
27
export const DescriptionRendered: React.FC<Props> = React.memo(
28
({
29
actions,
30
task_id,
31
desc,
32
read_only,
33
selectedHashtags,
34
searchWords,
35
is_current,
36
hideBody,
37
}) => {
38
function render_content() {
39
let value = desc;
40
if (!value.trim()) {
41
return <span style={{ color: "#666" }}>Description...</span>;
42
}
43
let show_more_link: boolean;
44
if (hideBody) {
45
let header = header_part(value);
46
show_more_link =
47
!!is_current && actions != null && header.trim() != value.trim();
48
value = header;
49
} else {
50
show_more_link = false;
51
}
52
return (
53
<>
54
<MostlyStaticMarkdown
55
value={value}
56
searchWords={searchWords}
57
onChange={
58
actions != null
59
? (value) => {
60
actions.set_desc(task_id, value, true);
61
}
62
: undefined
63
}
64
selectedHashtags={selectedHashtags}
65
toggleHashtag={
66
selectedHashtags != null && actions != null
67
? (tag) =>
68
actions.set_hashtag_state(
69
tag,
70
selectedHashtags.has(tag) ? undefined : 1,
71
)
72
: undefined
73
}
74
/>
75
{show_more_link && (
76
<a onClick={() => actions?.toggleHideBody(task_id)}>Show more...</a>
77
)}
78
</>
79
);
80
}
81
82
function on_click(e) {
83
const data = e.target != null ? e.target.dataset : undefined;
84
if (data == null) {
85
return;
86
}
87
if (data.checkbox != null) {
88
e.stopPropagation();
89
actions?.toggle_desc_checkbox(
90
task_id,
91
parseInt(data.index),
92
data.checkbox === "true",
93
);
94
} else if (data.hashtag != null) {
95
let new_state;
96
e.stopPropagation();
97
const state = { undefined: undefined, "1": 1, "-1": -1 }[data.state]; // do not use eval -- safer
98
if (state === 1 || state === -1) {
99
// for now negation doesn't go through clicking
100
new_state = undefined;
101
} else {
102
new_state = 1;
103
}
104
actions?.set_hashtag_state(data.hashtag, new_state);
105
}
106
}
107
108
return (
109
<div
110
style={{ paddingTop: "5px", maxHeight: MAX_HEIGHT, overflow: "auto" }}
111
onClick={!read_only && actions != null ? on_click : undefined}
112
className="cocalc-task-description"
113
>
114
{render_content()}
115
</div>
116
);
117
},
118
);
119
120