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 | 5x 5x 5x 259x 59x 59x 267x 267x 267x 267x 267x 267x 135x 84x 84x 5x 59x 59x 84x 84x 84x 84x 84x 84x 132x 132x 132x 19x 19x 113x 113x 84x 84x 135x 135x 135x 135x 135x 135x 135x 118x 118x 147x 147x 147x 147x 22x 22x 22x 22x 22x 147x 147x 143x 143x 143x 22x 22x 22x 22x 22x 22x 22x 22x 19x 143x 118x 2x 116x 116x 116x 27x 118x 118x 118x 118x 118x 17x 17x 17x 22x 17x 17x 17x 17x | /**
* Direct Peritext `Fragment` to Slate `Descendant[]` converter with caching.
*
* Implements a double-buffered cache keyed by Peritext `Block.hash`, which lets
* unchanged sub-trees be reused across renders so that React's reconciler can
* skip re-rendering unchanged components.
*/
import {Slice} from 'json-joy/lib/json-crdt-extensions/peritext/slice/Slice';
import {SliceStacking} from 'json-joy/lib/json-crdt-extensions/peritext/slice/constants';
import {type Block, LeafBlock, type Inline} from 'json-joy/lib/json-crdt-extensions/peritext';
import type {Fragment} from 'json-joy/lib/json-crdt-extensions/peritext/block/Fragment';
import type {SlateDescendantNode, SlateDocument, SlateElementNode, SlateTextNode} from '../types';
const isText = (node: SlateDescendantNode): node is SlateTextNode => typeof (node as SlateTextNode).text === 'string';
/**
* Double-buffered cache for Slate element nodes, keyed by Peritext Block hash.
* Entries that are not accessed during the current render pass are dropped at
* the next {@link SlateNodeCache.gc} call.
*/
class SlateNodeCache {
/** Entries written/read during the *current* render pass. */
private curr: Map<number, SlateElementNode> = new Map();
/** Entries from the *previous* render pass (read-only during current). */
private prev: Map<number, SlateElementNode> = new Map();
get(hash: number): SlateElementNode | undefined {
const curr = this.curr;
let node = curr.get(hash);
Iif (node !== undefined) return node;
node = this.prev.get(hash);
if (node !== undefined) curr.set(hash, node);
return node;
}
set(hash: number, node: SlateElementNode): void {
this.curr.set(hash, node);
}
/** Call once at the **end** of each render pass. Drops entries not accessed
* during this render. */
gc(): void {
this.prev = this.curr;
this.curr = new Map();
}
}
/**
* Stateful converter that turns a Peritext `Fragment` into a Slate
* `Descendant[]` document, reusing unchanged sub-trees via {@link SlateNodeCache}.
*
* Because unchanged blocks return the **same object reference**, React's
* reconciler (and Slate's `onChange` diffing) can skip re-rendering them.
*/
export class ToSlateNode {
public readonly cache = new SlateNodeCache();
constructor(public defaultBlock = 'p') {}
convert(fragment: Fragment<string>): SlateDocument {
const root = fragment.root;
const blockChildren = root.children;
const length = blockChildren.length;
const result: SlateElementNode[] = [];
const cache = this.cache;
for (let i = 0; i < length; i++) {
const block = blockChildren[i];
const cached = cache.get(block.hash);
if (cached) {
result.push(cached);
continue;
}
const node = this.convBlock(block);
result.push(node);
}
cache.gc();
return result;
}
private convBlock(block: Block | LeafBlock): SlateElementNode {
const hash = block.hash;
const cached = this.cache.get(hash);
Iif (cached) return cached;
const node = this.buildBlock(block);
this.cache.set(hash, node);
return node;
}
private buildBlock(block: Block | LeafBlock): SlateElementNode {
if (block instanceof LeafBlock) {
const inlineChildren: SlateDescendantNode[] = [];
for (let iterator = block.texts0(), inline: Inline | undefined; (inline = iterator()); ) {
const text = inline.text();
const attr = inline.attr();
const attrKeys = Object.keys(attr);
// Detect an `Atomic` slice — this run represents an inline void
// element. Emit it as a Slate inline element and skip the placeholder character.
let atomicSlice: Slice | undefined;
let atomicTag: string | undefined;
for (const tag of attrKeys) {
const stack = attr[tag];
Iif (!stack || stack.length <= 0) continue;
const slice = stack[0].slice;
Iif (!(slice instanceof Slice)) continue;
Iif (slice.stacking === SliceStacking.Atomic) {
atomicSlice = slice;
atomicTag = tag;
break;
}
}
Iif (atomicSlice && atomicTag !== undefined) {
const data = atomicSlice.data();
const inlineEl: SlateElementNode = {
type: atomicTag,
children: [{text: ''}],
};
if (data && typeof data === 'object' && !Array.isArray(data)) Object.assign(inlineEl, data);
inlineChildren.push(inlineEl);
continue;
}
if (!text && attrKeys.length === 0) continue;
const textNode: SlateTextNode = {text: text || ''};
const length = attrKeys.length;
ATTRS: for (let i = 0; i < length; i++) {
const tag = attrKeys[i];
const stack = attr[tag];
Iif (!stack || stack.length <= 0) continue ATTRS;
const slice = stack[0].slice;
Iif (!(slice instanceof Slice)) continue ATTRS;
Iif (slice.stacking === SliceStacking.Atomic) continue ATTRS;
const data = slice.data();
if (data && typeof data === 'object' && !Array.isArray(data)) Object.assign(textNode, {[tag]: data});
else textNode[tag] = data !== undefined ? data : true;
}
inlineChildren.push(textNode);
}
// Slate requires text nodes around inline elements: first and last
// child must be text, and two inline elements can't be adjacent.
if (inlineChildren.length === 0) {
inlineChildren.push({text: ''});
} else {
Iif (!isText(inlineChildren[0])) inlineChildren.unshift({text: ''});
Iif (!isText(inlineChildren[inlineChildren.length - 1])) inlineChildren.push({text: ''});
for (let i = 1; i < inlineChildren.length; i++) {
Iif (!isText(inlineChildren[i]) && !isText(inlineChildren[i - 1])) {
inlineChildren.splice(i, 0, {text: ''});
i++;
}
}
}
const tag = block.tag();
const node: SlateElementNode = {
type: tag === 0 ? this.defaultBlock : tag + '',
children: inlineChildren as any,
};
const attr = block.attr();
Eif (attr && typeof attr === 'object') Object.assign(node, attr);
return node;
} else {
const childBlocks = block.children;
const len = childBlocks.length;
const children: SlateElementNode[] = new Array(len);
for (let i = 0; i < len; i++) children[i] = this.convBlock(childBlocks[i]);
const attr = block.attr();
const tag = block.tag();
const node: SlateElementNode = {
...(attr && typeof attr === 'object' ? attr : {}),
type: tag === 0 ? this.defaultBlock : tag + '',
children: len ? children : [{text: ''}],
};
return node;
}
}
}
|