Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diff-based text replacing #209

Merged
merged 9 commits into from
Mar 7, 2024
25 changes: 16 additions & 9 deletions src/web/parserUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,19 +179,26 @@ function parseText(

const markdownRanges: MarkdownRange[] = ranges as MarkdownRange[];

targetElement.innerHTML = '';
targetElement.innerText = '';

// We don't want to parse text with single '\n', because contentEditable represents it as invisible <br />
if (!!text && text !== '\n') {
const dom = parseRangesToHTMLNodes(text, markdownRanges, markdownStyle);
target.appendChild(dom);
}

if (alwaysMoveCursorToTheEnd) {
CursorUtils.moveCursorToEnd(target);
} else if (isFocused && cursorPosition !== null) {
CursorUtils.setCursorPosition(target, cursorPosition, disableNewLinesInCursorPositioning);
const rootSpan = targetElement.firstChild as HTMLElement | null;
if (!rootSpan || rootSpan.innerHTML !== dom.innerHTML) {
if (rootSpan) {
rootSpan.replaceWith(dom);
} else {
targetElement.innerHTML = '';
targetElement.innerText = '';
target.appendChild(dom);
}

if (alwaysMoveCursorToTheEnd) {
CursorUtils.moveCursorToEnd(target);
} else if (isFocused && cursorPosition !== null) {
CursorUtils.setCursorPosition(target, cursorPosition, disableNewLinesInCursorPositioning);
}
}
}

return {text: target.innerText, cursorPosition: cursorPosition || 0};
Expand Down
Loading