Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | 64x 64x 64x 64x 64x 64x 64x 64x 64x 10697x 10697x 10697x 10697x 10697x 10697x 10697x 10697x 3094x 3090x 3090x 3090x 2212x 2212x 317x 8x 8x 8x 8x 8x 28x 4x 4x 24x 24x 24x 18x 2x 2x 16x 4x 1954x 1954x 1954x 1954x 1954x 7306x 5344x 5344x 5344x 4239x 4239x 1954x 1954x 1954x 1954x 1954x 1954x 1954x 1954x 1954x 1954x 1954x 6117x 5352x 5352x 5352x 4185x 4185x 4185x 4185x 1954x 1954x 1954x 812x 812x 812x 4155x 765x 765x 4155x 1954x 1830x 1376x 1376x 1376x 1376x 1376x 1985x 1376x 10697x 9378x 9378x 9378x 9378x 9378x 1830x 1830x 7548x 9378x 9378x | import {printTree} from 'tree-dump/lib/printTree'; import {CONST, updateJson, updateNum} from '../../../json-hash/hash'; import {MarkerOverlayPoint} from '../overlay/MarkerOverlayPoint'; import {UndEndIterator, type UndEndNext} from '../../../util/iterator'; import {Inline} from './Inline'; import {formatType, getTag} from '../slice/util'; import {Range} from '../rga/Range'; import {stringify} from '../../../json-text/stringify'; import type {Point} from '../rga/Point'; import type {OverlayPoint} from '../overlay/OverlayPoint'; import type {Printable} from 'tree-dump'; import type {Peritext} from '../Peritext'; import type {Stateful} from '../types'; import type {OverlayTuple} from '../overlay/types'; import type {PeritextMlAttributes, PeritextMlElement} from './types'; import type {SliceTypeSteps} from '../slice'; export interface IBlock { readonly path: SliceTypeSteps; readonly parent: IBlock | null; } export class Block<T = string, Attr = unknown> extends Range<T> implements IBlock, Printable, Stateful { public parent: Block<T> | null = null; public children: Block<T>[] = []; constructor( public readonly txt: Peritext<T>, public readonly path: SliceTypeSteps, public readonly marker: MarkerOverlayPoint<T> | undefined, public start: Point<T>, public end: Point<T>, ) { super(txt.str, start, end); } /** * @returns Stable unique identifier within a list of blocks. Used for React * or other rendering library keys. */ public key(): number | string { Iif (!this.marker) return this.tag(); const id = this.marker.id; return id.sid.toString(36) + id.time.toString(36); } public tag(): number | string { return getTag(this.path); } public attr(): Attr | undefined { const path = this.path; const length = path.length; if (length === 0) return; const tagStep = path[length - 1]; if (!Array.isArray(tagStep)) return; return tagStep[2] as Attr | undefined; } public isLeaf(): boolean { return false; } /** * Iterate through all overlay points of this block, until the next marker * (regardless if that marker is a child or not). */ public points0(withMarker: boolean = false): UndEndNext<OverlayPoint<T>> { const txt = this.txt; const overlay = txt.overlay; const iterator = overlay.points0(this.marker); let closed = false; return () => { if (withMarker) { withMarker = false; return this.marker ?? overlay.START; } Iif (closed) return; const point = iterator(); if (!point) return; if (point instanceof MarkerOverlayPoint) { closed = true; return; } return point; }; } public points(withMarker?: boolean): IterableIterator<OverlayPoint<T>> { return new UndEndIterator(this.points0(withMarker)); } protected tuples0(): UndEndNext<OverlayTuple<T>> { const overlay = this.txt.overlay; const marker = this.marker; const iterator = overlay.tuples0(marker); let closed = false; return () => { if (closed) return; let pair = iterator(); while (!marker && pair && pair[1] && pair[1].cmpSpatial(this.start) < 0) pair = iterator(); if (!pair) return (closed = true), void 0; if (!pair[1] || pair[1] instanceof MarkerOverlayPoint) closed = true; return pair; }; } /** * @todo Consider moving inline-related methods to {@link LeafBlock}. */ public texts0(): UndEndNext<Inline<T>> { const txt = this.txt; const overlay = txt.overlay; const iterator = this.tuples0(); const start = this.start; const end = this.end; const startIsMarker = overlay.isMarker(start.id); const endIsMarker = overlay.isMarker(end.id); let isFirst = true; let next = iterator(); let closed = false; const newIterator: UndEndNext<Inline<T>> = () => { if (closed) return; const pair = next; next = iterator(); if (!pair) return; const [overlayPoint1, overlayPoint2] = pair; let point1: Point<T> = overlayPoint1; let point2: Point<T> = overlayPoint2; if (isFirst) { isFirst = false; if (start.cmpSpatial(overlayPoint1) > 0) point1 = start; if (startIsMarker) { point1 = point1.clone(); point1.step(1); // Skip condition when inline annotations tarts immediately at th // beginning of the block. if (point1.cmp(point2) === 0) return newIterator(); } } if (!endIsMarker && end.cmpSpatial(overlayPoint2) < 0) { closed = true; point2 = end; } return new Inline(txt, overlayPoint1, overlayPoint2, point1, point2); }; return newIterator; } /** * @todo Consider moving inline-related methods to {@link LeafBlock}. */ public texts(): IterableIterator<Inline<T>> { return new UndEndIterator(this.texts0()); } public text(): string { let str = ''; const children = this.children; const length = children.length; for (let i = 0; i < length; i++) str += children[i].text(); return str; } // ------------------------------------------------------------------- export public toJson(): PeritextMlElement { const data = this.attr(); const attr: PeritextMlAttributes | null = data !== void 0 ? {data} : null; const node: PeritextMlElement = [this.tag(), attr]; const children = this.children; const length = children.length; for (let i = 0; i < length; i++) node.push(children[i].toJson()); return node; } // ----------------------------------------------------------------- Stateful public hash: number = 0; public refresh(): number { const {path, children} = this; let state = CONST.START_STATE; state = updateJson(state, path); const marker = this.marker; if (marker) { state = updateNum(state, marker.marker.refresh()); state = updateNum(state, marker.textHash); } else { state = updateNum(state, this.txt.overlay.leadingTextHash); } for (let i = 0; i < children.length; i++) state = updateNum(state, children[i].refresh()); return (this.hash = state); } // ---------------------------------------------------------------- Printable public toStringName(): string { return 'Block'; } protected toStringHeader(): string { const hash = `#${this.hash.toString(36).slice(-4)}`; const tag = this.path.map((step) => formatType(step)).join('.'); const data = stringify(this.attr()); const header = `${super.toString('', true)} ${hash} ${tag} ${data}`; return header; } public toString(tab: string = ''): string { const header = this.toStringHeader(); const hasChildren = !!this.children.length; return ( header + printTree(tab, [ this.marker ? (tab) => this.marker!.toString(tab) : null, this.marker && hasChildren ? () => '' : null, hasChildren ? (tab) => 'children' + printTree( tab, this.children.map( (child, i) => (tab) => `${i + 1}. ` + child.toString(tab + ' ' + ' '.repeat(String(i + 1).length)), ), ) : null, ]) ); } } |