This commit is contained in:
evilchili 2026-05-29 14:09:35 -07:00
parent 59436a7d39
commit d23c3e7a67

View File

@ -549,14 +549,16 @@ export class RibbitEditor extends Ribbit {
#updateCurrentBlock(): void { #updateCurrentBlock(): void {
const block = this.#findCurrentBlock(); const block = this.#findCurrentBlock();
if (!block) return; if (!block) return;
const caretOffset = this.#getCaretOffset(block); const caretOffset = this.#getCaretOffset(block);
const lineText = block.textContent!.replace(/\u00A0/g, ' '); const lineText = block.textContent!.replace(/\u00A0/g, ' ');
const newBlock = this.#buildBlock(lineText); const newBlock = this.#buildBlock(lineText);
block.className = newBlock.className; block.className = newBlock.className;
block.innerHTML = ''; block.innerHTML = '';
while (newBlock.firstChild) block.appendChild(newBlock.firstChild); while (newBlock.firstChild) {
block.appendChild(newBlock.firstChild);
}
// Place caret after any prefix span, never inside it // Place caret after any prefix span, never inside it
const prefixSpan = block.firstElementChild; const prefixSpan = block.firstElementChild;
@ -578,6 +580,13 @@ export class RibbitEditor extends Ribbit {
sel.removeAllRanges(); sel.removeAllRanges();
sel.addRange(range); sel.addRange(range);
} else { } else {
// switch spaces back to non-breaking spaces to avoid a chromium bug where a trailing space is ignored when
// positioning the caret. We only do this if the last character is a space; if we do it unconditionally it
// breaks detection of header nodes etc.
if (lineText.endsWith(' ')) {
block.innerHTML = block.innerHTML.replace(/\s/g, '\u00A0');
}
this.#restoreCaret(block, caretOffset); this.#restoreCaret(block, caretOffset);
} }
} }
@ -748,6 +757,7 @@ export class RibbitEditor extends Ribbit {
let remaining = offset; let remaining = offset;
const placed = this.#walkForCaret(block, range, remaining); const placed = this.#walkForCaret(block, range, remaining);
if (!placed) { if (!placed) {
range.selectNodeContents(block); range.selectNodeContents(block);
range.collapse(false); range.collapse(false);