diff --git a/src/main/frontend/commands.cljs b/src/main/frontend/commands.cljs index 8b696f821..1936233d6 100644 --- a/src/main/frontend/commands.cljs +++ b/src/main/frontend/commands.cljs @@ -277,7 +277,7 @@ :as option}] (when-let [input (gdom/getElement id)] (let [edit-content (gobj/get input "value") - current-pos (:pos (util/get-caret-pos input)) + current-pos (cursor/pos input) prefix (subs edit-content 0 current-pos) space? (when (and last-pattern prefix) (let [s (when-let [last-index (string/last-index-of prefix last-pattern)] @@ -314,7 +314,7 @@ :as option}] (let [input (gdom/getElement id) edit-content (gobj/get input "value") - current-pos (:pos (util/get-caret-pos input)) + current-pos (cursor/pos input) prefix (subs edit-content 0 current-pos) new-value (str prefix value @@ -334,7 +334,7 @@ :as option}] (let [input (gdom/getElement id) edit-content (gobj/get input "value") - current-pos (:pos (util/get-caret-pos input)) + current-pos (cursor/pos input) suffix (subs edit-content 0 current-pos) new-value (str value suffix @@ -355,7 +355,7 @@ (let [selected? (not (string/blank? selected)) input (gdom/getElement id) edit-content (gobj/get input "value") - current-pos (:pos (util/get-caret-pos input)) + current-pos (cursor/pos input) prefix (subs edit-content 0 current-pos) postfix (if selected? (string/replace-first (subs edit-content current-pos) @@ -378,7 +378,7 @@ [id] (let [input (gdom/getElement id) edit-content (gobj/get input "value") - current-pos (:pos (util/get-caret-pos input)) + current-pos (cursor/pos input) prefix (subs edit-content 0 (dec current-pos)) new-value (str prefix (subs edit-content (inc current-pos))) @@ -427,7 +427,7 @@ (when-let [input-id (state/get-edit-input-id)] (when-let [current-input (gdom/getElement input-id)] (let [edit-content (gobj/get current-input "value") - current-pos (:pos (util/get-caret-pos current-input)) + current-pos (cursor/pos current-input) prefix (subs edit-content 0 current-pos) prefix (util/replace-last slash prefix "") new-value (str prefix diff --git a/src/main/frontend/components/editor.cljs b/src/main/frontend/components/editor.cljs index 7e9ae1b99..bea9f01a1 100644 --- a/src/main/frontend/components/editor.cljs +++ b/src/main/frontend/components/editor.cljs @@ -1,34 +1,25 @@ (ns frontend.components.editor - (:require [rum.core :as rum] + (:require [clojure.string :as string] + [dommy.core :as d] + [frontend.commands :as commands + :refer [*angle-bracket-caret-pos *matched-block-commands *matched-commands *show-block-commands *show-commands *slash-caret-pos]] + [frontend.components.datetime :as datetime-comp] + [frontend.components.search :as search] [frontend.components.svg :as svg] - [cljs-bean.core :as bean] [frontend.config :as config] + [frontend.db :as db] [frontend.handler.editor :as editor-handler :refer [get-state]] [frontend.handler.editor.lifecycle :as lifecycle] - [frontend.util :as util :refer-macros [profile]] - [frontend.handler.block :as block-handler] - [frontend.components.block :as block] - [frontend.components.search :as search] [frontend.handler.page :as page-handler] - [frontend.components.datetime :as datetime-comp] - [frontend.state :as state] [frontend.mixins :as mixins] + [frontend.modules.shortcut.core :as shortcut] + [frontend.state :as state] [frontend.ui :as ui] - [frontend.db :as db] - [dommy.core :as d] - [goog.object :as gobj] + [frontend.util :as util] + [frontend.util.cursor :as cursor] [goog.dom :as gdom] - [clojure.string :as string] [promesa.core :as p] - [frontend.commands :as commands - :refer [*show-commands - *matched-commands - *slash-caret-pos - *angle-bracket-caret-pos - *matched-block-commands - *show-block-commands]] - ["/frontend/utils" :as utils] - [frontend.modules.shortcut.core :as shortcut])) + [rum.core :as rum])) (rum/defc commands < rum/reactive [id format] @@ -77,7 +68,7 @@ (let [pos (:editor/last-saved-cursor @state/state) input (gdom/getElement id)] (when input - (let [current-pos (:pos (util/get-caret-pos input)) + (let [current-pos (cursor/pos input) edit-content (or (state/sub [:editor/content id]) "") edit-block (state/sub :editor/block) q (or @@ -135,7 +126,7 @@ (let [pos (:editor/last-saved-cursor @state/state) input (gdom/getElement id) [id format] (:rum/args state) - current-pos (:pos (util/get-caret-pos input)) + current-pos (cursor/pos input) edit-content (state/sub [:editor/content id]) edit-block (state/get-edit-block) q (or @@ -152,7 +143,7 @@ (let [pos (:editor/last-saved-cursor @state/state) input (gdom/getElement id)] (when input - (let [current-pos (:pos (util/get-caret-pos input)) + (let [current-pos (cursor/pos input) edit-content (state/sub [:editor/content id]) edit-block (state/sub :editor/block) q (or diff --git a/src/main/frontend/handler/editor.cljs b/src/main/frontend/handler/editor.cljs index 44f292014..ec20cb1fd 100644 --- a/src/main/frontend/handler/editor.cljs +++ b/src/main/frontend/handler/editor.cljs @@ -107,7 +107,7 @@ (defn html-link-format! [] (when-let [m (get-selection-and-format)] (let [{:keys [selection-start selection-end format value block edit-id input]} m - cur-pos (:pos (util/get-caret-pos input)) + cur-pos (cursor/pos input) empty-selection? (= selection-start selection-end) selection (subs value selection-start selection-end) selection-link? (and selection (or (util/starts-with? selection "http://") @@ -1796,7 +1796,7 @@ [] (when-let [id (state/get-edit-input-id)] (when-let [input (gdom/getElement id)] - (let [current-pos (:pos (util/get-caret-pos input)) + (let [current-pos (cursor/pos input) pos (:editor/last-saved-cursor @state/state) edit-content (or (state/sub [:editor/content id]) "")] (or @@ -1812,7 +1812,7 @@ (when-let [q (get-search-q)] (let [value (gobj/get input "value") pos (:editor/last-saved-cursor @state/state) - current-pos (:pos (util/get-caret-pos input))] + current-pos (cursor/pos input)] (when (or (< current-pos pos) (string/includes? q "]") (string/includes? q ")")) @@ -2365,7 +2365,7 @@ [cut? e] (let [^js input (state/get-input) id (state/get-edit-input-id) - current-pos (:pos (util/get-caret-pos input)) + current-pos (cursor/pos input) value (gobj/get input "value") deleted (and (> current-pos 0) (util/nth-safe value (dec current-pos))) @@ -2461,7 +2461,7 @@ (cond (state/editing?) (let [input (state/get-input) - pos (:pos (util/get-caret-pos input))] + pos (cursor/pos input)] (when (and (not (state/get-editor-show-input)) (not (state/get-editor-show-date-picker?)) (not (state/get-editor-show-template-search?))) @@ -2529,7 +2529,7 @@ (= key "#")) (do (commands/handle-step [:editor/search-page-hashtag]) - (state/set-last-pos! (:pos (util/get-caret-pos input))) + (state/set-last-pos! (cursor/pos input)) (reset! commands/*slash-caret-pos (util/get-caret-pos input))) (let [sym "$"] diff --git a/src/main/frontend/handler/page.cljs b/src/main/frontend/handler/page.cljs index e9b41c24e..b1a36f9a3 100644 --- a/src/main/frontend/handler/page.cljs +++ b/src/main/frontend/handler/page.cljs @@ -426,7 +426,7 @@ (defn on-chosen-handler [input id q pos format] - (let [current-pos (:pos (util/get-caret-pos input)) + (let [current-pos (cursor/pos input) edit-content (state/sub [:editor/content id]) edit-block (state/sub :editor/block) q (or diff --git a/src/main/frontend/util/cursor.cljs b/src/main/frontend/util/cursor.cljs index c6dec97a1..25b3dde0c 100644 --- a/src/main/frontend/util/cursor.cljs +++ b/src/main/frontend/util/cursor.cljs @@ -34,6 +34,10 @@ :left js/Number.MAX_SAFE_INTEGER :top js/Number.MAX_SAFE_INTEGER})))) +(defn pos [input] + (when input + (.-selectionStart input))) + (defn move-cursor-to [input n] (.setSelectionRange input n n))