🐛 Fix not quitting v3 editor with Esc + Undo transactions (#11293)

* 🐛 Fix Esc key not quitting editor v3

* 🐛 Fix undo transactions being split in editor v3
This commit is contained in:
Belén Albeza 2026-08-20 14:45:43 +02:00 committed by GitHub
parent 2dcf1a8a0a
commit f29a94058a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -11,13 +11,16 @@
[app.common.data.macros :as dm]
[app.common.types.text :as txt]
[app.main.data.helpers :as dsh]
[app.main.data.workspace :as dw]
[app.main.data.workspace.texts :as dwt]
[app.main.data.workspace.undo :as dwu]
[app.main.refs :as refs]
[app.main.store :as st]
[app.main.ui.css-cursors :as cur]
[app.render-wasm.api :as wasm.api]
[app.render-wasm.text-editor :as text-editor]
[app.util.dom :as dom]
[app.util.keyboard :as kbd]
[cuerdas.core :as str]
[rumext.v2 :as mf]))
@ -294,12 +297,7 @@
(and ctrl? (= (str/lower key) "a")))
(text-editor/clear-pending-caret-styles!))
(cond
;; Escape: finalize and stop
(= key "Escape")
(do
(dom/prevent-default event)
(when-let [node (mf/ref-val contenteditable-ref)]
(.blur node)))
;; NOTE: Escape is handled in a document key-up listener (see effect below).
;; Ctrl+A: select all (key is "a" or "A" depending on platform)
(and ctrl? (= (str/lower key) "a"))
@ -516,6 +514,18 @@
"--editor-container-height" (dm/str height "px")
"--fallback-families" (if (seq fallback-families) (dm/str (str/join ", " fallback-families)) "sourcesanspro")}]
;; Exit on Escape via a document key-up listener (like v2). On key-down the trailing
;; key-up is read as a non-editing Escape and deselects the shape.
(mf/use-effect
(mf/deps)
(fn []
(let [on-key-up (fn [event]
(when (kbd/esc? event)
(dom/stop-propagation event)
(st/emit! (dw/clear-edition-mode))))]
(.addEventListener js/document "keyup" on-key-up)
#(.removeEventListener js/document "keyup" on-key-up))))
;; Register the native `beforeinput` listener. React's synthetic
;; `onBeforeInput` does not expose `getTargetRanges()`, even with
;; nativeEvent (it's fully synthetic, composed of other two events).
@ -533,6 +543,9 @@
(mf/use-effect
(mf/deps contenteditable-ref)
(fn []
;; Group the whole editing session (edits, reflow resizes, finalize) into a single
;; undo entry. Nested transactions (e.g. style shortcuts) are ref-counted and fold in.
(st/emit! (dwu/start-undo-transaction shape-id :timeout nil))
(when-let [node (mf/ref-val contenteditable-ref)]
;; Focus and select all text on mount (this will trigger on-focus)
(.focus node)
@ -543,6 +556,7 @@
;; it was not being reliable (timing issues, Firefox issues…)
(fn []
(on-blur)
(st/emit! (dwu/commit-undo-transaction shape-id))
(text-editor/text-editor-dispose)
(wasm.api/request-render-preserving-target "text-editor-dispose"))))