UNPKG

@dossierhq/core

Version:

The core Dossier library used by clients and server alike, used to interact with schema and entities directly, as well as remotely through a client.

135 lines 3.69 kB
/// <reference types="./RichTextUtils.d.ts" /> import { IS_UNMERGEABLE, TEXT_TYPE_TO_FORMAT, } from '../third-party/Lexical.js'; import { RichTextNodeType, } from '../Types.js'; export function createRichText(children) { return { root: { direction: 'ltr', format: '', indent: 0, type: 'root', version: 1, children, }, }; } export function createRichTextHeadingNode(tag, children) { return { direction: 'ltr', format: '', indent: 0, tag, type: 'heading', version: 1, children, }; } export function createRichTextParagraphNode(children) { return { direction: 'ltr', format: '', indent: 0, type: 'paragraph', version: 1, children, }; } export function createRichTextTextNode(text, options) { let formatValue = 0; if (options?.format) { for (const formatItem of options.format) { const formatFlag = TEXT_TYPE_TO_FORMAT[formatItem]; if (formatFlag !== undefined) { formatValue |= formatFlag; } } } return { detail: 0, format: formatValue, mode: 'normal', style: '', text, type: 'text', version: 1, }; } export function createRichTextLineBreakNode() { return { type: RichTextNodeType.linebreak, version: 1 }; } export function createRichTextTabNode() { return { detail: IS_UNMERGEABLE, format: 0, mode: 'normal', style: '', text: '\t', type: RichTextNodeType.tab, version: 1, }; } export function createRichTextTextAndWhitespaceNodes(text, options) { if (!(text.includes('\n') || text.includes('\r') || text.includes('\t'))) { return [createRichTextTextNode(text, options)]; } const linesOrNewline = text.replace(/(?:\r[^\n])|(?:\r$)/g, '').split(/(\r?\n|\t)/); return linesOrNewline .filter((it) => it.length > 0) .map((it) => { if (it === '\r\n' || it === '\n') { return createRichTextLineBreakNode(); } else if (it === '\t') { return createRichTextTabNode(); } return createRichTextTextNode(it, options); }); } export function createRichTextEntityNode(reference) { const { id } = reference; return { type: RichTextNodeType.entity, format: '', reference: { id }, version: 1 }; } export function createRichTextEntityLinkNode(reference, children) { const { id } = reference; return { direction: 'ltr', format: '', indent: 0, type: RichTextNodeType.entityLink, reference: { id }, version: 1, children, }; } export function createRichTextListNode(listType, children) { return { direction: 'ltr', format: '', indent: 0, type: RichTextNodeType.list, version: 1, listType, start: 1, tag: listType === 'number' ? 'ol' : 'ul', children, }; } export function createRichTextListItemNode(value, checked, children) { return { direction: 'ltr', format: '', indent: 0, type: RichTextNodeType.listitem, version: 1, checked, value, children, }; } export function createRichTextComponentNode(data) { return { type: RichTextNodeType.component, data, format: '', version: 1 }; } export function richTextTextNodeHasFormat(node, format) { return (node.format & TEXT_TYPE_TO_FORMAT[format]) !== 0; } //# sourceMappingURL=RichTextUtils.js.map