enhance: custom editor command trigger (#3001)

* refactor: rename slash → custom-command-trigger

* refactor: use command-menu-trigger variable throughout, rather than hard-coded

Co-authored-by: Tienson Qin <tiensonqin@gmail.com>
This commit is contained in:
Devon Zuegel
2021-11-03 05:09:33 +02:00
committed by GitHub
parent 311f9701ac
commit 54b7df65a9
4 changed files with 83 additions and 77 deletions

View File

@@ -1467,10 +1467,6 @@
(util/format "[[%s][%s]]" url file-name))
nil)))
(defn- get-asset-link
[url]
(str "/" url))
(defn ensure-assets-dir!
[repo]
(let [repo-dir (config/get-repo-dir repo)
@@ -1582,7 +1578,7 @@
(if file (.-name file) (if image? "image" "asset"))
image?)
format
{:last-pattern (if drop-or-paste? "" commands/slash)
{:last-pattern (if drop-or-paste? "" (state/get-editor-command-trigger))
:restore? true})))))
(p/finally
(fn []
@@ -1599,7 +1595,7 @@
(insert-command! id
(get-asset-file-link format signed-url file-name true)
format
{:last-pattern (if drop-or-paste? "" commands/slash)
{:last-pattern (if drop-or-paste? "" (state/get-editor-command-trigger))
:restore? true})
(reset! *asset-uploading? false)
@@ -1738,7 +1734,7 @@
last-command (and last-slash-caret-pos (subs edit-content last-slash-caret-pos pos))]
(when (> pos 0)
(or
(and (= \/ (util/nth-safe edit-content (dec pos)))
(and (= (state/get-editor-command-trigger) (util/nth-safe edit-content (dec pos)))
@commands/*initial-commands)
(and last-command
(commands/get-matched-commands last-command)))))
@@ -1864,8 +1860,7 @@
blocks-dom-nodes))]
(state/set-selection-blocks! blocks)))))))))
(defn- get-link
[format link label]
(defn- get-link [format link label]
(let [link (or link "")
label (or label "")]
(case (keyword format)
@@ -1881,27 +1876,27 @@
:markdown (util/format "![%s](%s)" label link)
:org (util/format "[[%s]]"))))
(defn handle-command-input
[command id format m]
(defn handle-command-input [command id format m]
;; TODO: Add error handling for when user doesn't provide a required field.
;; (The current behavior is to just revert back to the editor.)
(case command
:link
(let [{:keys [link label]} m]
(if (and (string/blank? link)
(string/blank? label))
nil
(insert-command! id
(get-link format link label)
format
{:last-pattern (str commands/slash "link")})))
:image-link
(let [{:keys [link label]} m]
(if (and (string/blank? link)
(string/blank? label))
nil
(insert-command! id
(get-image-link format link label)
format
{:last-pattern (str commands/slash "link")})))
:link (let [{:keys [link label]} m]
(when-not (or (string/blank? link) (string/blank? label))
(insert-command!
id
(get-link format link label)
format
{:last-pattern (str (state/get-editor-command-trigger) "link")})))
:image-link (let [{:keys [link label]} m]
(when (not (string/blank? link))
(insert-command!
id
(get-image-link format link label)
format
{:last-pattern (str (state/get-editor-command-trigger) "link")})))
nil)
(state/set-editor-show-input! nil)
@@ -1995,14 +1990,15 @@
(let [input (state/get-input)
pos (cursor/pos input)
last-input-char (util/nth-safe (.-value input) (dec pos))]
(case last-input-char
"/"
;; TODO: is it cross-browser compatible?
;; (not= (gobj/get native-e "inputType") "insertFromPaste")
(if (= last-input-char (state/get-editor-command-trigger))
(when (seq (get-matched-commands input))
(reset! commands/*slash-caret-pos (cursor/get-caret-pos input))
(reset! commands/*show-commands true))
"<"
(reset! commands/*show-commands true)))
(if (= last-input-char commands/angle-bracket)
(when (seq (get-matched-block-commands input))
(reset! commands/*angle-bracket-caret-pos (cursor/get-caret-pos input))
(reset! commands/*show-block-commands true))
@@ -2629,7 +2625,7 @@
(delete-block! repo false))
(and (> current-pos 1)
(= (util/nth-safe value (dec current-pos)) commands/slash))
(= (util/nth-safe value (dec current-pos)) (state/get-editor-command-trigger)))
(do
(util/stop e)
(reset! *slash-caret-pos nil)
@@ -2821,8 +2817,8 @@
(when (and (= "〈" c)
(= "《" (util/nth-safe value (dec (dec current-pos))))
(> current-pos 0))
(commands/handle-step [:editor/input "<" {:last-pattern "《〈"
:backward-pos 0}])
(commands/handle-step [:editor/input commands/angle-bracket {:last-pattern "《〈"
:backward-pos 0}])
(reset! commands/*angle-bracket-caret-pos (cursor/get-caret-pos input))
(reset! commands/*show-block-commands true))
@@ -2833,7 +2829,7 @@
(not= (util/nth-safe value current-pos) "]")))
(state/set-editor-show-page-search-hashtag! false)))
(when (and @*show-commands (not= key-code 191)) ; not /
(when (and @*show-commands (not= key-code 191)) ; not / TODO: is this the .charCodeAt or the event code?
(let [matched-commands (get-matched-commands input)]
(if (seq matched-commands)
(do