fix: avoid reentrancy when pressing ESC to quit codemirror

pull/4750/head
Tienson Qin 2022-03-31 21:40:47 +08:00
parent 019160f32a
commit 1682dc3c7e
3 changed files with 8 additions and 31 deletions

View File

@ -110,7 +110,6 @@
"react-textarea-autosize": "8.3.3",
"react-tippy": "1.4.0",
"react-transition-group": "4.3.0",
"react-virtuoso": "^2.8.5",
"reakit": "0.11.1",
"remove-accents": "0.4.2",
"send-intent": "3.0.11",

View File

@ -188,14 +188,10 @@
:else
nil))))
;; Avoid reentrancy
(def *code-saving (atom false))
(defn save-file-or-block-when-blur-or-esc!
(defn- save-file-or-block-when-blur-or-esc!
[editor textarea config state]
(when-not @*code-saving
(reset! *code-saving true)
(save-file-or-block! editor textarea config state)
(reset! *code-saving false)))
(state/set-block-component-editing-mode! false)
(save-file-or-block! editor textarea config state))
(defn- text->cm-mode
([text]
@ -237,6 +233,8 @@
{:mode mode
:readOnly (if ui-config/publishing? "nocursor" false)
:extraKeys #js {"Esc" (fn [cm]
;; Avoid reentrancy
(gobj/set cm "escPressed" true)
(save-file-or-block-when-blur-or-esc! cm textarea config state)
(when-let [block-id (:block/uuid config)]
(let [block (db/pull [:block/uuid block-id])]
@ -251,10 +249,10 @@
(.on editor "change" (fn [_cm _e]
(let [new-code (.getValue editor)]
(reset! (:calc-atom state) (calc/eval-lines new-code))))))
(.on editor "blur" (fn [_cm e]
(.on editor "blur" (fn [cm e]
(when e (util/stop e))
(state/set-block-component-editing-mode! false)
(save-file-or-block-when-blur-or-esc! editor textarea config state)))
(when-not (gobj/get cm "escPressed")
(save-file-or-block-when-blur-or-esc! editor textarea config state))))
(.addEventListener element "mousedown"
(fn [e]
(state/clear-selection!)

View File

@ -1271,18 +1271,6 @@
dependencies:
"@types/node" "*"
"@virtuoso.dev/react-urx@^0.2.12":
version "0.2.13"
resolved "https://registry.yarnpkg.com/@virtuoso.dev/react-urx/-/react-urx-0.2.13.tgz#e2cfc42d259d2a002695e7517d34cb97b64ee9c4"
integrity sha512-MY0ugBDjFb5Xt8v2HY7MKcRGqw/3gTpMlLXId2EwQvYJoC8sP7nnXjAxcBtTB50KTZhO0SbzsFimaZ7pSdApwA==
dependencies:
"@virtuoso.dev/urx" "^0.2.13"
"@virtuoso.dev/urx@^0.2.12", "@virtuoso.dev/urx@^0.2.13":
version "0.2.13"
resolved "https://registry.yarnpkg.com/@virtuoso.dev/urx/-/urx-0.2.13.tgz#a65e7e8d923cb03397ac876bfdd45c7f71c8edf1"
integrity sha512-iirJNv92A1ZWxoOHHDYW/1KPoi83939o83iUBQHIim0i3tMeSKEh+bxhJdTHQ86Mr4uXx9xGUTq69cp52ZP8Xw==
acorn-node@^1.6.1:
version "1.8.2"
resolved "https://registry.yarnpkg.com/acorn-node/-/acorn-node-1.8.2.tgz#114c95d64539e53dede23de8b9d96df7c7ae2af8"
@ -6838,14 +6826,6 @@ react-transition-group@4.3.0:
loose-envify "^1.4.0"
prop-types "^15.6.2"
react-virtuoso@^2.8.5:
version "2.8.5"
resolved "https://registry.yarnpkg.com/react-virtuoso/-/react-virtuoso-2.8.5.tgz#92f22d22255b444380dd0a29004d9bf678df834a"
integrity sha512-ayFESqgt++or9NLZ5XZR9Pta5W9jiT9pf9cYa/FYX5BoDuWMFYhou7xCal624JY6CzOOnwUlCGck95dtxsVDiA==
dependencies:
"@virtuoso.dev/react-urx" "^0.2.12"
"@virtuoso.dev/urx" "^0.2.12"
react@17.0.2:
version "17.0.2"
resolved "https://registry.yarnpkg.com/react/-/react-17.0.2.tgz#d0b5cc516d29eb3eee383f75b62864cfb6800037"