Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/editors/slate/elements/references/editable.tsx
1698 views
1
/*
2
This is not actually editable. It just shows there references
3
and the hover text over the word "References" says that you need
4
to edit markdown source. Also, you can't type [Sage][2]space and
5
have it autoformat as a reference (say), since there references
6
aren't known to the autoformatter. Both of those are things
7
to do later. The point of the below is just to not mangle content
8
that uses reference links.
9
10
Another point -- it's currently possible to move the cursor after the
11
references at the bottom of the document and delete them all. Maybe that
12
is good, but it could be confusing. Undo will put them back though.
13
*/
14
15
import { register } from "../register";
16
import { CodeMirrorStatic } from "@cocalc/frontend/jupyter/codemirror-static";
17
import infoToMode from "../code-block/info-to-mode";
18
import { Tooltip } from "antd";
19
20
function fromSlate({ node }) {
21
if (!node.value) return "";
22
let v: string[] = [];
23
for (const name in node.value) {
24
const { title, href } = node.value[name];
25
let line = `[${name}]: ${href ? href : "<>"}`;
26
if (title) {
27
line += ` '${title.replace(/'/g, "\\'")}'`;
28
}
29
v.push(line);
30
}
31
return "\n" + v.join("\n") + "\n";
32
}
33
34
register({
35
slateType: "references",
36
37
Element: ({ attributes, children, element }) => {
38
if (element.type != "references") throw Error("references");
39
return (
40
<div {...attributes} contentEditable={false}>
41
<hr />
42
<div style={{ color: "#666", fontWeight: "bold", fontSize: "large" }}>
43
<Tooltip title="The references below must be edited in the markdown source.">
44
References
45
</Tooltip>
46
</div>
47
<CodeMirrorStatic
48
no_border
49
style={{ marginBottom: 0 }}
50
options={{ mode: infoToMode("md"), lineWrapping: true }}
51
value={fromSlate({ node: element })}
52
/>
53
{children}
54
</div>
55
);
56
},
57
58
fromSlate,
59
});
60
61