Path: blob/master/src/packages/frontend/editors/slate/slate-diff/handle-change-text-nodes.ts
1698 views
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { Operation, Text } from "slate";6import { nextPath, splitTextNodes } from "./split-text-nodes";78// Transform some text nodes into some other text nodes.9export function handleChangeTextNodes(10nodes: Text[],11nextNodes: Text[],12path: number[]13): Operation[] {14if (nodes.length == 0) throw Error("must have at least one nodes");15if (nextNodes.length == 0) throw Error("must have at least one nextNodes");1617const operations: Operation[] = [];1819let node = nodes[0];20if (nodes.length > 1) {21// join together everything in nodes first22for (let i = 1; i < nodes.length; i++) {23operations.push({24type: "merge_node",25path: nextPath(path),26position: 0, // make TS happy; seems ignored in source code27properties: {}, // make TS happy; seems ignored in source code -- probably a typescript error.28});29node = { ...node, ...{ text: node.text + nodes[i].text } }; // update text so splitTextNodes can use this below.30}31}3233for (const op of splitTextNodes(node, nextNodes, path)) {34operations.push(op);35}3637return operations;38}3940export function isAllText(nodes: any[]): nodes is Text[] {41for (const node of nodes) {42if (!Text.isText(node)) return false;43}44return true;45}464748