Skip to content

Commit

Permalink
feat: command group with title doc
Browse files Browse the repository at this point in the history
  • Loading branch information
thezjy authored and tiensonqin committed Jun 17, 2021
1 parent dde0fb9 commit c123f97
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 51 deletions.
100 changes: 95 additions & 5 deletions src/main/frontend/commands.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,12 @@
(defonce *matched-commands (atom nil))
(defonce *initial-commands (atom nil))

(defn init-commands!
[get-page-ref-text]
(let [commands (commands-map get-page-ref-text)]
(reset! *initial-commands commands)
(reset! *matched-commands commands)))
(defonce *first-command-group
{"Page Reference" "BASIC"
"Tomorrow" "TIME & DATE"
"LATER" "TASK"
"Query" "ADVANCED"
"Quote" "ORG-MODE"})

(defn ->block
([type]
Expand Down Expand Up @@ -260,6 +261,95 @@
(remove nil?)
(util/distinct-by-last-wins first)))

(defn commands-map
[get-page-ref-text]
(->>
(concat
;; basic
[["Page Reference" [[:editor/input "[[]]" {:backward-pos 2}]
[:editor/search-page]] "Create a backlink to a page"]
["Page Embed" (embed-page) "Embed a page here"]
["Block Reference" [[:editor/input "(())" {:backward-pos 2}]
[:editor/search-block :reference]] "Create a backlink to a blcok"]
["Block Embed" (embed-block) "Embed a block here" "Embed a block here"]
["Link" link-steps "Create a HTTP link"]
["Image Link" link-steps "Create a HTTP link to a image"]
(when (state/markdown?)
["Underline" [[:editor/input "<ins></ins>"
{:last-pattern slash
:backward-pos 6}]] "Create a underline text decoration"])
["Template" [[:editor/input "/" nil]
[:editor/search-template]] "Insert a created template here"]
(cond
(and (util/electron?) (config/local-db? (state/get-current-repo)))

["Upload an asset" [[:editor/click-hidden-file-input :id]] "Upload file types like image, pdf, docx, etc.)"]

(state/logged?)
["Upload an image" [[:editor/click-hidden-file-input :id]]])]

(markdown-headings)

;; time & date

[["Tomorrow" #(get-page-ref-text (date/tomorrow)) "Insert the date of tomorrow"]
["Yesterday" #(get-page-ref-text (date/yesterday)) "Insert the date of yesterday"]
["Today" #(get-page-ref-text (date/today)) "Insert the date of today"]
["Current Time" #(date/get-current-time) "Insert current time"]
["Date Picker" [[:editor/show-date-picker]] "Pick a date and insert here"]]

;; task management
(get-preferred-workflow)

[["Priority A" (->priority "A")]
["Priority B" (->priority "B")]
["Priority C" (->priority "C")]
["DONE" (->marker "DONE")]
["WAITING" (->marker "WAITING")]
["CANCELED" (->marker "CANCELED")]
["Deadline" [[:editor/clear-current-slash]
[:editor/show-date-picker :deadline]]]
["Scheduled" [[:editor/clear-current-slash]
[:editor/show-date-picker :scheduled]]]]

;; advanced

[["Query" [[:editor/input "{{query }}" {:backward-pos 2}]] "Create a DataScript query"]
["Draw" (fn []
(let [file (draw/file-name)
path (str config/default-draw-directory "/" file)
text (util/format "[[%s]]" path)]
(p/let [_ (draw/create-draw-with-default-content path)]
(println "draw file created, " path))
text)) "Draw a graph with Excalidraw"]






(when (util/zh-CN-supported?)
["Embed Bilibili Video" [[:editor/input "{{bilibili }}" {:last-pattern slash
:backward-pos 2}]]])
["Embed HTML " (->inline "html")]

["Embed Youtube Video" [[:editor/input "{{youtube }}" {:last-pattern slash
:backward-pos 2}]]]

["Embed Vimeo Video" [[:editor/input "{{vimeo }}" {:last-pattern slash
:backward-pos 2}]]]]

;; Allow user to modify or extend, should specify how to extend.
(state/get-commands))
(remove nil?)
(util/distinct-by-last-wins first)))

(defn init-commands!
[get-page-ref-text]
(let [commands (commands-map get-page-ref-text)]
(reset! *initial-commands commands)
(reset! *matched-commands commands)))

(defonce *matched-block-commands (atom (block-commands-map)))

(defn restore-state
Expand Down
59 changes: 36 additions & 23 deletions src/main/frontend/components/editor.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
(: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]]
:refer [*angle-bracket-caret-pos *matched-block-commands *matched-commands *show-block-commands *show-commands *slash-caret-pos *first-command-group]]
[frontend.components.datetime :as datetime-comp]
[frontend.components.search :as search]
[frontend.components.svg :as svg]
Expand All @@ -25,28 +25,41 @@
[id format]
(let [show-commands? (util/react *show-commands)]
(when (and show-commands?
@*slash-caret-pos
(not (state/sub :editor/show-page-search?))
(not (state/sub :editor/show-block-search?))
(not (state/sub :editor/show-template-search?))
(not (state/sub :editor/show-input))
(not (state/sub :editor/show-date-picker?)))
(let [matched (util/react *matched-commands)]
(ui/auto-complete
(map first matched)
{:on-chosen (fn [chosen]
(reset! commands/*current-command chosen)
(let [command-steps (get (into {} matched) chosen)
restore-slash? (or
(contains? #{"Today" "Yesterday" "Tomorrow"} chosen)
(and
(not (fn? command-steps))
(not (contains? (set (map first command-steps)) :editor/input))
(not (contains? #{"Date Picker" "Template" "Deadline" "Scheduled" "Upload an image"} chosen))))]
(editor-handler/insert-command! id command-steps
format
{:restore? restore-slash?})))
:class "black"})))))
@*slash-caret-pos
(not (state/sub :editor/show-page-search?))
(not (state/sub :editor/show-block-search?))
(not (state/sub :editor/show-template-search?))
(not (state/sub :editor/show-input))
(not (state/sub :editor/show-date-picker?)))
(let [matched (util/react *matched-commands)]
(ui/auto-complete
matched
{:get-group-name
(fn [item]
(get *first-command-group (first item)))

:item-render
(fn [item]
(let [command-name (first item)
command-doc (get item 2)]
[:div {:title command-doc} command-name]))

:on-chosen
(fn [chosen-item]
(let [command (first chosen-item)]
(reset! commands/*current-command command)
(let [command-steps (get (into {} matched) command)
restore-slash? (or
(contains? #{"Today" "Yesterday" "Tomorrow"} command)
(and
(not (fn? command-steps))
(not (contains? (set (map first command-steps)) :editor/input))
(not (contains? #{"Date Picker" "Template" "Deadline" "Scheduled" "Upload an image"} command))))]
(editor-handler/insert-command! id command-steps
format
{:restore? restore-slash?}))))
:class
"black"})))))

(rum/defc block-commands < rum/reactive
[id format]
Expand Down
4 changes: 2 additions & 2 deletions src/main/frontend/components/sidebar.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@
:exit 300}}
links
;; (custom-context-menu-content)
))))
))))

(rum/defc new-block-mode < rum/reactive
[]
Expand Down Expand Up @@ -372,4 +372,4 @@
;; :on-click (fn []
;; (state/set-left-sidebar-open! (not (state/get-left-sidebar-open?))))}
;; (if (state/sub :ui/left-sidebar-open?) "<" ">")]
)]))))
)]))))
52 changes: 32 additions & 20 deletions src/main/frontend/ui.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
[:div {:style {:margin-right "8px"}} title]
;; [:div {:style {:position "absolute" :right "8px"}}
;; icon]
]]
]]
(rum/with-key
(menu-link new-options child)
title)))
Expand Down Expand Up @@ -332,30 +332,42 @@
(rum/defcs auto-complete <
(rum/local 0 ::current-idx)
(shortcut/mixin :shortcut.handler/auto-complete)
[state matched {:keys [on-chosen
on-shift-chosen
on-enter
empty-div
item-render
class]}]
[state
matched
{:keys [on-chosen
on-shift-chosen
get-group-name
empty-div
item-render
class]}]
(let [current-idx (get state ::current-idx)]
[:div#ui__ac {:class class}
(if (seq matched)
[:div#ui__ac-inner.hide-scrollbar
(for [[idx item] (medley/indexed matched)]
(rum/with-key
(menu-link
{:id (str "ac-" idx)
:class (when (= @current-idx idx)
"chosen")
;; :tab-index -1
:on-mouse-down (fn [e]
(util/stop e)
(if (and (gobj/get e "shiftKey") on-shift-chosen)
(on-shift-chosen item)
(on-chosen item)))}
(if item-render (item-render item) item))
idx))]
[:<>
{:key idx}
(let [item-cp
[:div {:key idx}
(menu-link
{:id (str "ac-" idx)
:class (when (= @current-idx idx)
"chosen")
:on-mouse-down (fn [e]
(util/stop e)
(if (and (gobj/get e "shiftKey") on-shift-chosen)
(on-shift-chosen item)
(on-chosen item)))}
(if item-render (item-render item) item))]]

(if get-group-name
(if-let [group-name (get-group-name item)]
[:div
[:div.ui__ac-group-name group-name]
item-cp]
item-cp)

item-cp))])]
(when empty-div
empty-div))]))

Expand Down
7 changes: 6 additions & 1 deletion src/main/frontend/ui.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@
}
}

.ui__ac-group-name {
@apply p-2 text-xs;
color: var(--ls-block-ref-link-text-color);
}

.search-all #ui__ac-inner {
max-height: none;
max-height: none;
}

.ui__notifications {
Expand Down

0 comments on commit c123f97

Please sign in to comment.