|
1 | 1 | /* The page's one DOM reading and quote resolver. */ |
2 | 2 | let publishedPassages; |
3 | | -export const alignText = (...args) => publishedPassages.alignText(...args); |
4 | 3 | export const closestAcross = (...args) => publishedPassages.closestAcross(...args); |
5 | 4 | export const inChrome = (...args) => publishedPassages.inChrome(...args); |
6 | 5 | export const inUi = (...args) => publishedPassages.inUi(...args); |
@@ -508,112 +507,6 @@ export function createPassages(dependencies) { |
508 | 507 | // pair is a character no UTF-8 file can hold, and a quote is written to one. |
509 | 508 | const cut = (text, from, to) => [...text].slice(from, to).join(""); |
510 | 509 |
|
511 | | - // One lossless text alignment for every widget that needs to explain a sequence of |
512 | | - // whole-text states. Segmenter keeps words and punctuation in the language-aware |
513 | | - // units this runtime already assumes; a linear-space Hirschberg walk supplies the |
514 | | - // ordered shared spine. Its quadratic *time* is capped: after stripping a common |
515 | | - // prefix and suffix, a very large divergent middle is one replacement instead of a |
516 | | - // page-freezing attempt at fine-grained alignment. Joining same+delete reconstructs |
517 | | - // `before`, and joining same+insert reconstructs `after`, exactly. |
518 | | - const textUnits = new Intl.Segmenter(undefined, { granularity: "word" }); |
519 | | - const ALIGN_CELLS = 1_000_000; |
520 | | - |
521 | | - function lcsRow(left, lo, hi, right, rlo, rhi, reverse) { |
522 | | - const width = rhi - rlo; |
523 | | - let previous = new Uint32Array(width + 1); |
524 | | - for (let at = 0; at < hi - lo; at++) { |
525 | | - const current = new Uint32Array(width + 1); |
526 | | - const word = reverse ? left[hi - at - 1] : left[lo + at]; |
527 | | - for (let across = 1; across <= width; across++) { |
528 | | - const other = reverse ? right[rhi - across] : right[rlo + across - 1]; |
529 | | - current[across] = |
530 | | - word === other |
531 | | - ? previous[across - 1] + 1 |
532 | | - : Math.max(previous[across], current[across - 1]); |
533 | | - } |
534 | | - previous = current; |
535 | | - } |
536 | | - return previous; |
537 | | - } |
538 | | - |
539 | | - function lcsMatches(left, lo, hi, right, rlo, rhi, matches) { |
540 | | - if (lo === hi || rlo === rhi) return; |
541 | | - if (hi - lo === 1) { |
542 | | - for (let at = rlo; at < rhi; at++) |
543 | | - if (left[lo] === right[at]) { |
544 | | - matches.push([lo, at]); |
545 | | - break; |
546 | | - } |
547 | | - return; |
548 | | - } |
549 | | - |
550 | | - const middle = lo + Math.floor((hi - lo) / 2); |
551 | | - let split = 0; |
552 | | - { |
553 | | - const forward = lcsRow(left, lo, middle, right, rlo, rhi, false); |
554 | | - const backward = lcsRow(left, middle, hi, right, rlo, rhi, true); |
555 | | - let best = -1; |
556 | | - const width = rhi - rlo; |
557 | | - for (let at = 0; at <= width; at++) { |
558 | | - const score = forward[at] + backward[width - at]; |
559 | | - if (score > best) { |
560 | | - best = score; |
561 | | - split = at; |
562 | | - } |
563 | | - } |
564 | | - } |
565 | | - lcsMatches(left, lo, middle, right, rlo, rlo + split, matches); |
566 | | - lcsMatches(left, middle, hi, right, rlo + split, rhi, matches); |
567 | | - } |
568 | | - |
569 | | - function alignText(before, after) { |
570 | | - const left = [...textUnits.segment(before)].map((part) => part.segment); |
571 | | - const right = [...textUnits.segment(after)].map((part) => part.segment); |
572 | | - const runs = []; |
573 | | - const push = (kind, text) => { |
574 | | - if (!text) return; |
575 | | - const last = runs.at(-1); |
576 | | - if (last?.kind === kind) last.text += text; |
577 | | - else runs.push({ kind, text }); |
578 | | - }; |
579 | | - |
580 | | - let prefix = 0; |
581 | | - while ( |
582 | | - prefix < left.length && |
583 | | - prefix < right.length && |
584 | | - left[prefix] === right[prefix] |
585 | | - ) |
586 | | - prefix++; |
587 | | - let suffix = 0; |
588 | | - while ( |
589 | | - prefix + suffix < left.length && |
590 | | - prefix + suffix < right.length && |
591 | | - left[left.length - suffix - 1] === right[right.length - suffix - 1] |
592 | | - ) |
593 | | - suffix++; |
594 | | - |
595 | | - push("same", left.slice(0, prefix).join("")); |
596 | | - const leftEnd = left.length - suffix; |
597 | | - const rightEnd = right.length - suffix; |
598 | | - const matches = []; |
599 | | - if ((leftEnd - prefix) * (rightEnd - prefix) <= ALIGN_CELLS) |
600 | | - lcsMatches(left, prefix, leftEnd, right, prefix, rightEnd, matches); |
601 | | - |
602 | | - let i = prefix; |
603 | | - let j = prefix; |
604 | | - for (const [li, rj] of matches) { |
605 | | - push("delete", left.slice(i, li).join("")); |
606 | | - push("insert", right.slice(j, rj).join("")); |
607 | | - push("same", left[li]); |
608 | | - i = li + 1; |
609 | | - j = rj + 1; |
610 | | - } |
611 | | - push("delete", left.slice(i, leftEnd).join("")); |
612 | | - push("insert", right.slice(j, rightEnd).join("")); |
613 | | - push("same", left.slice(leftEnd).join("")); |
614 | | - return runs; |
615 | | - } |
616 | | - |
617 | 510 | // What an element says, read the way this file reads the page everywhere else. A widget |
618 | 511 | // wanting the words in one of its own slots asks for them here rather than through |
619 | 512 | // `textContent`, because the two differ: the paint pass writes a hidden line into any text |
@@ -919,8 +812,6 @@ export function createPassages(dependencies) { |
919 | 812 | COLLAPSE, |
920 | 813 | quoteFrom, |
921 | 814 | cut, |
922 | | - textUnits, |
923 | | - alignText, |
924 | 815 | says, |
925 | 816 | wrote, |
926 | 817 | rangeOf, |
|
0 commit comments