Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/editors/slate/elements/paragraph/index.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, SlateElement } from "../register";
7
8
export interface Paragraph extends SlateElement {
9
type: "paragraph";
10
}
11
12
register({
13
slateType: "paragraph",
14
15
toSlate: ({ token, children, state }) => {
16
if (token.hidden) {
17
// this is how markdown-it happens to encode the
18
// idea of a "tight list"; it wraps the items
19
// in a "hidden" paragraph. Weird and annoying,
20
// but I can see why, I guess. Instead, we just
21
// set this here, and it propagates up to the
22
// enclosing list. Whichever tightness is first takes
23
// precedence.
24
state.tight = true;
25
}
26
return { type: "paragraph", children } as Paragraph;
27
},
28
29
StaticElement: ({ attributes, children, element }) => {
30
if (element.type != "paragraph") throw Error("bug");
31
// textIndent: 0 is needed due to task lists -- see slate/elements/list/list-item.tsx
32
return (
33
<p {...attributes}>
34
<span style={{ textIndent: 0 }}>{children}</span>
35
</p>
36
);
37
},
38
39
sizeEstimator({ node, fontSize }): number {
40
const numLines = Math.round(JSON.stringify(node).length / 60);
41
return numLines * 1.4 * fontSize + fontSize;
42
},
43
});
44
45