Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/editors/slate/padding.ts
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
import { Descendant, Node } from "slate";
7
8
export function emptyParagraph() {
9
// returns a new object each time.
10
return {
11
type: "paragraph",
12
children: [{ text: "" }],
13
} as Descendant;
14
}
15
16
export const EMPTY_PARAGRAPH = emptyParagraph(); // don't mutate this; won't work.
17
Object.freeze(EMPTY_PARAGRAPH);
18
19
export function isWhitespaceParagraph(node: Node | undefined): boolean {
20
return (
21
node != null &&
22
node["type"] == "paragraph" &&
23
node["children"]?.length == 1 &&
24
isWhitespaceText(node["children"][0])
25
);
26
}
27
28
export function isWhitespaceText(node: Node | undefined): boolean {
29
return node?.["text"]?.trim() === "";
30
}
31
32
export function ensureDocNonempty(doc: Descendant[]): void {
33
if (doc.length == 0) {
34
doc.push(emptyParagraph());
35
}
36
}
37
38