Skip to content

Commit 3765711

Browse files
committed
fix: experimental(ish) patch for focus issues
For some reason MathQuill now requires a single frame to properly render a dynamic math input. So we shall give it that.
1 parent 6a19e31 commit 3765711

File tree

2 files changed

+29
-28
lines changed

2 files changed

+29
-28
lines changed

src/math-editor/editor.js

+16-14
Original file line numberDiff line numberDiff line change
@@ -671,21 +671,23 @@ class Editor {
671671
}
672672

673673
// Patch: Do not let lines deactivate, when there's only a math/image element present
674-
// TODO: Needed?
675-
if (this.activeLine.childNodes.length === 1) {
676-
if (
677-
this.activeLine.childNodes[0].nodeName.toLowerCase() === "math" ||
678-
this.activeLine.childNodes[0].nodeName.toLowerCase() === "attachment"
679-
) { // Left
680-
this.activeLine.childNodes[0].contentEditable = true
681-
}
682-
if (
683-
this.activeLine.childNodes[this.activeLine.childNodes.length - 1].nodeName.toLowerCase() === "math" ||
684-
this.activeLine.childNodes[this.activeLine.childNodes.length - 1].nodeName.toLowerCase() === "attachment"
685-
) { // Right
686-
this.activeLine.childNodes[this.activeLine.childNodes.length - 1].contentEditable = true
674+
// TODO: Needed? For now causes issues on chromium.
675+
/* if (window.browser === "firefox") {
676+
if (this.activeLine.childNodes.length === 1) {
677+
if (
678+
this.activeLine.childNodes[0].nodeName.toLowerCase() === "math" ||
679+
this.activeLine.childNodes[0].nodeName.toLowerCase() === "attachment"
680+
) { // Left
681+
this.activeLine.childNodes[0].contentEditable = true
682+
}
683+
if (
684+
this.activeLine.childNodes[this.activeLine.childNodes.length - 1].nodeName.toLowerCase() === "math" ||
685+
this.activeLine.childNodes[this.activeLine.childNodes.length - 1].nodeName.toLowerCase() === "attachment"
686+
) { // Right
687+
this.activeLine.childNodes[this.activeLine.childNodes.length - 1].contentEditable = true
688+
}
687689
}
688-
}
690+
} */
689691
}
690692
const observer = new MutationObserver(this.observerCallback)
691693
this.observerCallback()

src/math-editor/math.js

+13-14
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ const Math = {
138138
const obj = this.collection[id]
139139
if (obj.isOpen) return
140140

141+
// obj.container.contentEditable = false
142+
141143
// Open the element
142144
// TODO: Refactor this
143145
if (obj.isOpen === null) {
@@ -147,10 +149,6 @@ const Math = {
147149
obj.container.removeAttribute("style")
148150
obj.image.removeAttribute("style")
149151
obj.isOpen = true
150-
151-
// MathQuill weirdness
152-
obj.dynamicInterface.focus()
153-
obj.dynamicInterface.reflow()
154152
} else {
155153
obj.container.removeAttribute("style")
156154
obj.image.removeAttribute("style")
@@ -169,22 +167,23 @@ const Math = {
169167
obj.flags.push("ignoreInputDynamic")
170168
obj.dynamicInterface.latex(obj.data)
171169
obj.latexInterface.latex(obj.data)
172-
173-
// MathQuill weirdness
174-
obj.dynamicInterface.focus()
175-
obj.dynamicInterface.reflow()
176170
} else {
177171
throw new Error("Cache miss")
178172
}
179173
}
180174

181-
obj.container.contentEditable = false
182-
183175
if (obj.flags.includes("disableInputDynamic")) obj.labels[0].click() // Forces the raw latex view to open
184176

185-
// Todo: This should not live in here. This component is dynamic.
186-
if (window.setLatexCommandsVisibility) window.setLatexCommandsVisibility(true)
187-
this.events.dispatchEvent(new CustomEvent("focus", { detail: obj }))
177+
// NOTE: Mathquill requires a single frame (at least) to get ready
178+
requestAnimationFrame(() => {
179+
// MathQuill weirdness
180+
obj.dynamicInterface.focus()
181+
obj.dynamicInterface.reflow()
182+
183+
// Todo: This should not live in here. This component is dynamic.
184+
if (window.setLatexCommandsVisibility) window.setLatexCommandsVisibility(true)
185+
this.events.dispatchEvent(new CustomEvent("focus", { detail: obj }))
186+
})
188187
},
189188

190189
/**
@@ -333,7 +332,7 @@ const Math = {
333332
})
334333
}
335334

336-
obj.container.contentEditable = true
335+
// obj.container.contentEditable = true
337336
this.events.dispatchEvent(new CustomEvent("blur", { detail: obj }))
338337
obj.container.setAttribute("math", btoa(obj.data))
339338
},

0 commit comments

Comments
 (0)