From 9ef4bb15c664617d8fa5e25e376b72b7530322a7 Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Sat, 7 Aug 2021 03:37:03 +0200 Subject: [PATCH] Fix return behavior on Mathjax decorated element --- ts/editable/mathjax-component.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ts/editable/mathjax-component.ts b/ts/editable/mathjax-component.ts index 2be18fec6..91ec7b33d 100644 --- a/ts/editable/mathjax-component.ts +++ b/ts/editable/mathjax-component.ts @@ -40,12 +40,25 @@ function placeCaretAfter(node: Node): void { function moveNodesInsertedOutside(element: Element, allowedChild: Node): () => void { const observer = new MutationObserver(() => { + if (element.childNodes.length === 1) { + return; + } + const childNodes = [...element.childNodes]; const allowedIndex = childNodes.findIndex((child) => child === allowedChild); const beforeChildren = childNodes.slice(0, allowedIndex); const afterChildren = childNodes.slice(allowedIndex + 1); + // Special treatment for pressing return after mathjax block + if ( + afterChildren.length === 2 && + afterChildren.every((child) => (child as Element).tagName === "BR") + ) { + const first = afterChildren.pop(); + element.removeChild(first!); + } + let lastNode: Node | null = null; for (const node of beforeChildren) { @@ -140,6 +153,7 @@ export const Mathjax: DecoratedElementConstructor = class Mathjax decorate(): void { const mathjax = (this.dataset.mathjax = this.innerText); this.innerHTML = ""; + this.style.whiteSpace = "normal"; this.component = new Mathjax_svelte({ target: this, @@ -150,6 +164,7 @@ export const Mathjax: DecoratedElementConstructor = class Mathjax undecorate(): void { this.innerHTML = this.dataset.mathjax ?? ""; delete this.dataset.mathjax; + this.removeAttribute("style"); this.component?.$destroy(); this.component = undefined;