UNPKG

json-joy

Version:

Collection of libraries for building collaborative editing apps.

43 lines (42 loc) 1.4 kB
import { printTree } from 'tree-dump/lib/printTree'; import { Block } from './Block'; export class LeafBlock extends Block { text() { let str = ''; for (let iterator = this.texts0(), inline = iterator(); inline; inline = iterator()) str += inline.text(); return str; } isLeaf() { return true; } // ------------------------------------------------------------------- export toJson() { const data = this.attr(); const attr = data !== void 0 ? { data } : null; const node = [this.tag(), attr]; for (const inline of this.texts()) { const child = inline.toJson(); if (child) node.push(child); } return node; } // ---------------------------------------------------------------- Printable toStringName() { return 'LeafBlock'; } toString(tab = '') { const header = this.toStringHeader(); const texts = [...this.texts()]; const hasSlices = !!texts.length; return (header + printTree(tab, [ this.marker ? (tab) => this.marker.toString(tab) : null, !hasSlices ? null : (tab) => 'nodes' + printTree(tab, texts.map((inline) => (tab) => inline.toString(tab))), ])); } }