Path: blob/master/src/packages/frontend/editors/slate/slate-util.ts
1691 views
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { Node, Location, Editor, Element, Range, Path } from "slate";67export function containingBlock(editor: Editor): undefined | [Node, Location] {8for (const x of Editor.nodes(editor, {9match: (node) => Element.isElement(node) && Editor.isBlock(editor, node),10})) {11return x;12}13}1415export function getNodeAt(editor: Editor, path: Path): undefined | Node {16try {17return Editor.node(editor, path)[0];18} catch (_) {19return;20}21}2223// Range that contains the entire document.24export function rangeAll(editor: Editor): Range {25const first = Editor.first(editor, []);26const last = Editor.last(editor, []);27const offset = last[0]["text"]?.length ?? 0; // TODO: not 100% that this is right28return {29anchor: { path: first[1], offset: 0 },30focus: { path: last[1], offset },31};32}3334// Range that goes from selection focus to35// end of the document.36export function rangeToEnd(editor: Editor): Range {37if (editor.selection == null) return rangeAll(editor);38const last = Editor.last(editor, []);39const offset = last[0]["text"]?.length ?? 0;40return {41anchor: editor.selection.focus,42focus: { path: last[1], offset },43};44}4546export function rangeFromStart(editor: Editor): Range {47if (editor.selection == null) return rangeAll(editor);48const first = Editor.first(editor, []);49return {50anchor: { path: first[1], offset: 0 },51focus: editor.selection.focus,52};53}545556