All files / collaborative-slate/src/sync toSlateNode.ts

95% Statements 76/80
80.48% Branches 33/41
100% Functions 8/8
100% Lines 66/66

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                5x 5x                     59x   59x     267x 267x 267x 267x 267x 267x       135x           84x 84x                     5x 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 143x 143x 143x 22x 22x 22x 22x 22x 22x 22x 19x   143x   118x       118x 118x 118x   17x 17x 17x 22x 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 {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 {SlateDocument, SlateElementNode, SlateTextNode} from '../types';
 
/**
 * 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();
 
  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 textChildren: SlateTextNode[] = [];
      for (let iterator = block.texts0(), inline: Inline | undefined; (inline = iterator()); ) {
        const text = inline.text();
        const attr = inline.attr();
        const attrKeys = Object.keys(attr);
        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;
          const data = slice.data();
          if (data && typeof data === 'object' && !Array.isArray(data)) Object.assign(textNode, {[tag]: data});
          else textNode[tag] = data !== undefined ? data : true;
        }
        textChildren.push(textNode);
      }
      const node: SlateElementNode = {
        type: block.tag() + '',
        children: textChildren.length ? textChildren : [{text: ''}],
      };
      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 node: SlateElementNode = {
        ...(attr && typeof attr === 'object' ? attr : {}),
        type: block.tag() + '',
        children: len ? children : [{text: ''}],
      };
      return node;
    }
  }
}