Simplify editor on-hide implementation

experiment/tanstack-table
Tienson Qin 2024-06-17 16:47:58 +08:00
parent 939bdc9b74
commit 500245fb95
4 changed files with 44 additions and 62 deletions

View File

@ -98,7 +98,6 @@
(defonce *drag-to-block
(atom nil))
(def *move-to (atom nil))
(defonce *next-editing-block (atom nil))
;; TODO: dynamic
(defonce max-depth-of-links 5)
@ -2213,7 +2212,6 @@
:else
(let [block (or (db/entity [:block/uuid (:block/uuid block)]) block)]
(when (:block/uuid block) (reset! *next-editing-block (:block/uuid block)))
(editor-handler/clear-selection!)
(editor-handler/unhighlight-blocks!)
(let [f #(let [cursor-range (some-> (gdom/getElement block-id)
@ -2490,21 +2488,7 @@
(editor-box {:block block
:block-id uuid
:block-parent-id block-id
:format format
:on-hide (fn [value event ^js e]
(let [select? (and (= event :esc)
(not (string/includes? value "```")))
edit-next-block? (and @*next-editing-block (not= @*next-editing-block (:block/uuid block)))
blur-editing? (and (= event :blur) (when e (util/input? (.-relatedTarget e))))]
(when-let [container (gdom/getElement "app-container")]
(dom/remove-class! container "blocks-selection-mode"))
(p/do!
(editor-handler/save-block! repo (:block/uuid block) value)
(when-not (and (or edit-next-block? blur-editing?) (not select?))
(editor-handler/escape-editing select?))
(some-> config :on-escape-editing
(apply [(str uuid) (= event :esc)]))
(reset! *next-editing-block nil))))}
:format format}
edit-input-id
config))]
[:div.flex.flex-1.w-full.block-content-wrapper {:style {:display (if (:slide? config) "block" "flex")}}
@ -3019,7 +3003,7 @@
(when-not (or (:hide-children? config) in-whiteboard?)
(let [config' (-> (update config :level inc)
(dissoc :original-block :data :first-page?))]
(dissoc :original-block :data :first-journal?))]
(block-children config' block children collapsed?)))
(when-not in-whiteboard? (dnd-separator-wrapper block children block-id slide? false false))])))
@ -3474,8 +3458,9 @@
(defn- block-list
[config blocks]
(let [virtualized? (or
(:first-page? config)
(let [first-journal? (:first-journal? config)
virtualized? (or
first-journal?
(= :page (get-in config [:data :name])))
render-item (fn [idx]
(let [top? (zero? idx)

View File

@ -2,7 +2,6 @@
(:require [clojure.string :as string]
[frontend.diff :as diff]
[frontend.handler.file :as file]
[frontend.state :as state]
[frontend.ui :as ui]
[frontend.util :as util]
[logseq.shui.ui :as shui]

View File

@ -26,6 +26,7 @@
[frontend.util.keycode :as keycode]
[goog.dom :as gdom]
[goog.string :as gstring]
[dommy.core :as dom]
[logseq.graph-parser.property :as gp-property]
[logseq.common.util :as common-util]
[promesa.core :as p]
@ -807,49 +808,60 @@
:else
nil)]))
(defn- editor-on-blur
[^js e *ref & {:keys [unmount? block *blur?]}]
(when (or (not= @*ref js/document.activeElement) ; same activeElement when switching apps
;; handle unmount if only on-blur and ESC not handled
(and unmount? (and (:db/id block) (nil? (:db/id (state/get-edit-block))))))
(defn- editor-on-hide
[state value type e]
(let [repo (state/get-current-repo)
action (state/get-editor-action)
[opts _id config] (:rum/args state)
block (:block opts)]
(cond
(let [action (state/get-editor-action)]
(or (contains?
#{:commands :block-commands
:page-search :page-search-hashtag :block-search :template-search
:property-search :property-value-search
:datepicker} action)
(and (keyword? action)
(= (namespace action) "editor.action"))))
;; FIXME: This should probably be handled as a keydown handler in editor, but this handler intercepts Esc first
(and (= type :esc) (exist-editor-commands-popup?))
nil
(or (contains?
#{:commands :block-commands
:page-search :page-search-hashtag :block-search :template-search
:property-search :property-value-search
:datepicker} action)
(and (keyword? action)
(= (namespace action) "editor.action")))
(util/stop e)
;; editor/input component handles Escape directly, so just prevent handling it here
(= :input (state/get-editor-action))
;; editor/input component handles Escape directly, so just prevent handling it here
(= :input action)
nil
:else
(let [{:keys [on-hide value]} (editor-handler/get-state)]
(when on-hide
(when *blur? (reset! *blur? true))
(on-hide value :blur e))))))
(let [select? (and (= type :esc)
(not (string/includes? value "```")))]
(when-let [container (gdom/getElement "app-container")]
(dom/remove-class! container "blocks-selection-mode"))
(p/do!
(editor-handler/save-block! repo (:block/uuid block) value)
(editor-handler/escape-editing select?)
(some-> config :on-escape-editing
(apply [(str uuid) (= type :esc)])))))))
(rum/defcs box < rum/reactive
{:init (fn [state]
(assoc state ::id (str (random-uuid))
::ref (atom nil)
::blur? (atom false)))
(assoc state
::id (str (random-uuid))
::ref (atom nil)))
:did-mount (fn [state]
(state/set-editor-args! (:rum/args state))
state)}
(mixins/event-mixin
(fn [state]
(mixins/hide-when-esc-or-outside
state
:on-hide (fn [_state e type]
(editor-on-hide state (:value (editor-handler/get-state)) type e)))))
(mixins/event-mixin setup-key-listener!)
lifecycle/lifecycle
[state {:keys [format block parent-block on-hide]} id config]
[state {:keys [format block parent-block]} id config]
(let [*ref (::ref state)
*blur? (::blur? state)
content (state/sub-edit-content (:block/uuid block))
heading-class (get-editor-style-class block content format)
on-blur (get config :on-blur editor-on-blur)
opts (cond->
{:id id
:ref #(reset! *ref %)
@ -859,20 +871,6 @@
:on-click (editor-handler/editor-on-click! id)
:on-change (editor-handler/editor-on-change! block id search-timeout)
:on-paste (paste-handler/editor-on-paste! id)
:on-blur (fn [e]
(on-blur e *ref {:*blur? *blur?}))
:on-unmount (fn []
(when-not @*blur?
(on-blur nil *ref {:unmount? true
:block block
:*blur? *blur?})))
:on-key-down (fn [e]
(if-let [on-key-down (:on-key-down config)]
(on-key-down e)
(when (and (= (util/ekey e) "Escape") on-hide)
(when-not (exist-editor-commands-popup?)
(reset! *blur? true)
(on-hide content :esc e)))))
:auto-focus true
:class heading-class}
(some? parent-block)

View File

@ -15,7 +15,7 @@
(let [repo (state/sub :git/current-repo)]
(page/page {:repo repo
:page-name (str (:block/uuid page))
:first-page? (zero? idx)}))])
:first-journal? (zero? idx)}))])
(rum/defc journals < rum/reactive
{:will-unmount (fn [state]