Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/editors/slate/slate-diff/handle-change-text-nodes.ts
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 { Operation, Text } from "slate";
7
import { nextPath, splitTextNodes } from "./split-text-nodes";
8
9
// Transform some text nodes into some other text nodes.
10
export function handleChangeTextNodes(
11
nodes: Text[],
12
nextNodes: Text[],
13
path: number[]
14
): Operation[] {
15
if (nodes.length == 0) throw Error("must have at least one nodes");
16
if (nextNodes.length == 0) throw Error("must have at least one nextNodes");
17
18
const operations: Operation[] = [];
19
20
let node = nodes[0];
21
if (nodes.length > 1) {
22
// join together everything in nodes first
23
for (let i = 1; i < nodes.length; i++) {
24
operations.push({
25
type: "merge_node",
26
path: nextPath(path),
27
position: 0, // make TS happy; seems ignored in source code
28
properties: {}, // make TS happy; seems ignored in source code -- probably a typescript error.
29
});
30
node = { ...node, ...{ text: node.text + nodes[i].text } }; // update text so splitTextNodes can use this below.
31
}
32
}
33
34
for (const op of splitTextNodes(node, nextNodes, path)) {
35
operations.push(op);
36
}
37
38
return operations;
39
}
40
41
export function isAllText(nodes: any[]): nodes is Text[] {
42
for (const node of nodes) {
43
if (!Text.isText(node)) return false;
44
}
45
return true;
46
}
47
48