Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/editors/slate/elements/break/editable.tsx
1698 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
import { register } from "../register";
7
import { useCollapsed, useFocused, useSelected } from "../hooks";
8
9
register({
10
slateType: "softbreak",
11
12
// A softbreak creates a new line without creating
13
// a new paragraph.
14
Element: ({ attributes, children }) => {
15
const focused = useFocused();
16
const selected = useSelected();
17
const collapsed = useCollapsed();
18
const reveal = focused && selected && collapsed;
19
return (
20
<span {...attributes}>
21
<span
22
style={{
23
whiteSpace: "normal",
24
borderRight: reveal ? "1px solid #333" : undefined,
25
color: reveal ? "lightgrey" : undefined,
26
}}
27
contentEditable={false}
28
>
29
{reveal ? "↵\n" : " "}
30
</span>
31
{children}
32
</span>
33
);
34
},
35
36
fromSlate: ({ children }) => {
37
// Just in case somehow the children were edited
38
// (it doesn't seem they can be), we still won't
39
// loose information:
40
return children + "\n";
41
},
42
});
43
44
register({
45
slateType: "hardbreak",
46
47
fromSlate: ({ children }) => {
48
return children + " \n";
49
},
50
51
Element: ({ attributes, children }) => {
52
const focused = useFocused();
53
const selected = useSelected();
54
const collapsed = useCollapsed();
55
const reveal = focused && selected && collapsed;
56
return (
57
<span {...attributes}>
58
<span
59
style={{
60
whiteSpace: "pre",
61
borderRight: reveal ? "1px solid #333" : undefined,
62
color: reveal ? "lightgrey" : undefined,
63
}}
64
contentEditable={false}
65
>
66
{reveal ? "↵\n" : "\n"}
67
</span>
68
{children}
69
</span>
70
);
71
},
72
});
73
74