Path: blob/master/src/packages/frontend/editors/slate/padding.ts
1691 views
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { Descendant, Node } from "slate";67export function emptyParagraph() {8// returns a new object each time.9return {10type: "paragraph",11children: [{ text: "" }],12} as Descendant;13}1415export const EMPTY_PARAGRAPH = emptyParagraph(); // don't mutate this; won't work.16Object.freeze(EMPTY_PARAGRAPH);1718export function isWhitespaceParagraph(node: Node | undefined): boolean {19return (20node != null &&21node["type"] == "paragraph" &&22node["children"]?.length == 1 &&23isWhitespaceText(node["children"][0])24);25}2627export function isWhitespaceText(node: Node | undefined): boolean {28return node?.["text"]?.trim() === "";29}3031export function ensureDocNonempty(doc: Descendant[]): void {32if (doc.length == 0) {33doc.push(emptyParagraph());34}35}363738