Skip to content

Commit

Permalink
enhance: properties inserting
Browse files Browse the repository at this point in the history
Multiple properties drawers are allowed for now, this PR limits the
creation of multiple drawers to make sure that each block has only
one. Call <properties multiple times will insert the property in same drawer.
  • Loading branch information
leizhe authored and tiensonqin committed Oct 20, 2021
1 parent 0be5859 commit 8f2140e
Show file tree
Hide file tree
Showing 4 changed files with 239 additions and 138 deletions.
39 changes: 33 additions & 6 deletions src/main/frontend/commands.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
[frontend.util.cursor :as cursor]
[frontend.util.marker :as marker]
[frontend.util.priority :as priority]
[frontend.util.property :as property]
[goog.dom :as gdom]
[goog.object :as gobj]
[promesa.core :as p]))
Expand Down Expand Up @@ -187,12 +188,9 @@

(defn ->properties
[]
(let [template (util/format
":PROPERTIES:\n:: \n:END:\n")
backward-pos 9]
[[:editor/input template {:type "properties"
:last-pattern angle-bracket
:backward-pos backward-pos}]]))
[[:editor/clear-current-bracket]
[:editor/insert-properties]
[:editor/move-cursor-to-properties]])

;; https://orgmode.org/manual/Structure-Templates.html
(defn block-commands-map
Expand Down Expand Up @@ -534,6 +532,19 @@
new-value
(count prefix))))))

(defmethod handle-step :editor/clear-current-bracket [[_ space?]]
(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 (cursor/pos current-input)
prefix (subs edit-content 0 current-pos)
prefix (util/replace-last angle-bracket prefix "" (boolean space?))
new-value (str prefix
(subs edit-content current-pos))]
(state/set-block-content-and-last-pos! input-id
new-value
(count prefix))))))

(defn compute-pos-delta-when-change-marker
[current-input edit-content new-value marker pos]
(let [old-marker (some->> (first (util/safe-re-find marker/bare-marker-pattern edit-content))
Expand Down Expand Up @@ -578,6 +589,22 @@
new-value (string/trim (priority/add-or-update-priority edit-content format new-priority))]
(state/set-edit-content! input-id new-value)))))

(defmethod handle-step :editor/insert-properties [[_ _] _format]
(when-let [input-id (state/get-edit-input-id)]
(when-let [current-input (gdom/getElement input-id)]
(let [format (or (db/get-page-format (state/get-current-page)) (state/get-preferred-format))
edit-content (gobj/get current-input "value")
new-value (property/insert-property format edit-content "" "")]
(state/set-edit-content! input-id new-value)))))

(defmethod handle-step :editor/move-cursor-to-properties [[_]]
(when-let [input-id (state/get-edit-input-id)]
(when-let [current-input (gdom/getElement input-id)]
(let [format (or (db/get-page-format (state/get-current-page)) (state/get-preferred-format))
edit-content (gobj/get current-input "value")]
(property/goto-properties-end format current-input)
(cursor/move-cursor-backward current-input 3)))))

(defmethod handle-step :editor/set-heading [[_ heading]]
(when-let [input-id (state/get-edit-input-id)]
(when-let [current-input (gdom/getElement input-id)]
Expand Down
82 changes: 49 additions & 33 deletions src/main/frontend/handler/editor.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2314,6 +2314,54 @@

(declare delete-and-update)

(defn- dwim-in-properties
[state]
(when-not (auto-complete?)
(let [{:keys [block]} (get-state)]
(when block
(let [input (state/get-input)
content (gobj/get input "value")
format (:block/format (:block (get-state)))
property-key (:raw-content (thingatpt/property-key-at-point input))
org? (= format :org)
move-to-pos (if org? 2 3)]
(if org?
(cond
(and property-key (not= property-key ""))
(case property-key
;; When cursor in "PROPERTIES", add :|: in a new line and move cursor to |
"PROPERTIES"
(do (cursor/move-cursor-to-line-end input)
(insert "\n:: ")
(cursor/move-cursor-backward input move-to-pos))
;; When cursor in "END", new block (respect the previous enter behavior)
"END"
(do
(cursor/move-cursor-to-end input)
(insert-new-block! state))
;; cursor in other positions of :ke|y: or ke|y::, move to line end for inserting value.
(if (property/property-key-exist? format content property-key)
(notification/show!
[:p.content
(util/format "Property key \"%s\" already exists!" property-key)]
:error)
(cursor/move-cursor-to-end input)))

;; when cursor in empty property key
(and property-key (= property-key ""))
(do (delete-and-update
input
(cursor/line-beginning-pos input)
(inc (cursor/line-end-pos input)))
(property/goto-properties-end format input)
(cursor/move-cursor-to-line-end input))
:else
;;When cursor in other place of PROPERTIES drawer, add :|: in a new line and move cursor to |
(do
(insert "\n:: ")
(cursor/move-cursor-backward input move-to-pos)))
(insert "\n")))))))

(defn- keydown-new-block
[state]
(when-not (auto-complete?)
Expand Down Expand Up @@ -2364,39 +2412,7 @@
(do (cursor/move-cursor-to-line-end input)
(insert (str "\n" indent next-bullet " " checkbox)))))
"properties-drawer"
(let [property-key (:raw-content (thingatpt/property-key-at-point input))
org? (= (:block/format block) :org)
move-to-pos (if org? 2 3)]
(if org?
(cond
(and property-key (not= property-key ""))
(case property-key
;; When cursor in "PROPERTIES", add :|: in a new line and move cursor to |
"PROPERTIES"
(do (cursor/move-cursor-to-line-end input)
(insert "\n:: ")
(cursor/move-cursor-backward input move-to-pos))
;; When cursor in "END", new block (respect the previous enter behavior)
"END"
(do
(cursor/move-cursor-to-end input)
(insert-new-block! state))
;; cursor in other positions of :ke|y: or ke|y::, move to line end for inserting value.
(cursor/move-cursor-to-line-end input))

;; when cursor in empty property key
(and property-key (= property-key ""))
(do (delete-and-update
input
(cursor/line-beginning-pos input)
(inc (cursor/line-end-pos input)))
(cursor/move-cursor-to-line-end input))
:else
;;When cursor in other place of PROPERTIES drawer, add :|: in a new line and move cursor to |
(do
(insert "\n:: ")
(cursor/move-cursor-backward input move-to-pos)))
(insert "\n"))))
(dwim-in-properties state))

(and
(string/blank? content)
Expand Down
10 changes: 10 additions & 0 deletions src/main/frontend/util/cursor.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,21 @@
[input]
(move-cursor-to input (line-beginning-pos input)))

(defn move-cursor-to-beginning
[input]
(move-cursor-to input 0))

(defn move-cursor-to-end
[input]
(let [pos (count (gobj/get input "value"))]
(move-cursor-to input pos)))

(defn move-cursor-to-thing
[input thing from]
(let [[content _pos] (get-input-content&pos input)
pos (string/index-of content thing from)]
(move-cursor-to input pos)))

(defn move-cursor-forward-by-word
[input]
(let [val (.-value input)
Expand Down
Loading

0 comments on commit 8f2140e

Please sign in to comment.