Skip to content

Commit

Permalink
fix: improve speculative updates
Browse files Browse the repository at this point in the history
Append text to the previous node instead of to the start of the next one, when possible
  • Loading branch information
BearToCode committed Nov 7, 2024
1 parent 0a39360 commit ddab992
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions packages/carta-md/src/lib/internal/speculative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,21 @@ export function speculativeHighlightUpdate(container: HTMLDivElement, from: stri
advance(change.value.length);
};
const addedCallback = (change: Change) => {
const node = textNodes.at(currentNodeIdx) ?? createTemporaryNode(container);
const node =
currentNodeCharIdx == 0
? textNodes.at(currentNodeIdx - 1)
: textNodes.at(currentNodeIdx) ?? createTemporaryNode(container);
if (!node) return; // Could not create a temporary node

const text = node.textContent ?? '';
const pre = text.slice(0, currentNodeCharIdx);
const post = text.slice(currentNodeCharIdx);
if (currentNodeCharIdx == 0) {
node.textContent = text + change.value;
} else {
const pre = text.slice(0, currentNodeCharIdx);
const post = text.slice(currentNodeCharIdx);
node.textContent = pre + change.value + post;
}

node.textContent = pre + change.value + post;
advance(change.value.length);
};
const removedCallback = (change: Change) => {
Expand Down

0 comments on commit ddab992

Please sign in to comment.