UNPKG

json-joy

Version:

Collection of libraries for building collaborative editing apps.

95 lines 3.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ProseMirrorNode = void 0; const peritext_1 = require("../peritext"); const constants_1 = require("../constants"); const constants_2 = require("./constants"); const ExtNode_1 = require("../../json-crdt/extensions/ExtNode"); const Slice_1 = require("../peritext/slice/Slice"); class ProseMirrorNode extends ExtNode_1.ExtNode { constructor(data) { super(data); this.data = data; // ------------------------------------------------------------------ ExtNode this.extId = constants_1.ExtensionId.prosemirror; this._view = null; this._viewHash = -1; this.txt = new peritext_1.Peritext(data.doc, this.text(), this.slices()); } text() { return this.data.get(0); } slices() { return this.data.get(1); } name() { return constants_2.MNEMONIC; } toPM(block) { const content = []; const node = { type: block.tag() + '' }; if (block instanceof peritext_1.LeafBlock) { for (let iterator = block.texts0(), inline; (inline = iterator());) { const text = inline.text(); if (!text) continue; const textNode = { type: 'text', text, }; const slices = inline.p1.layers; const length = slices.length; if (length > 0) { const marks = []; for (let i = 0; i < length; i++) { const slice = slices[i]; if (slice instanceof Slice_1.Slice) { const tag = slice.type() + ''; const data = slice.data(); const mark = { type: tag }; if (data && typeof data === 'object' && !Array.isArray(data)) mark.attrs = data; marks.push(mark); } } textNode.marks = marks; } content.push(textNode); } } else { const children = block.children; const length = children.length; for (let i = 0; i < length; i++) content.push(this.toPM(children[i])); } if (content.length) node.content = content; const data = block.attr(); if (data && typeof data === 'object') { for (const _ in data) { node.attrs = data; break; } } return node; } view() { const { txt } = this; const hash = txt.refresh(); if (this._view && hash === this._viewHash) return this._view; const content = []; const node = { type: 'doc', content }; const block = txt.blocks.root; const children = block.children; const length = children.length; for (let i = 0; i < length; i++) content.push(this.toPM(children[i])); this._viewHash = hash; this._view = node; return node; } } exports.ProseMirrorNode = ProseMirrorNode; //# sourceMappingURL=ProseMirrorNode.js.map