Disable template on db version

Use structure tag instead
feat/db-inferred-properties
Tienson Qin 2023-08-22 20:50:08 +08:00
parent 03ec9580c3
commit c8b88c9d8f
3 changed files with 144 additions and 138 deletions

View File

@ -9,7 +9,6 @@
[frontend.handler.draw :as draw]
[frontend.handler.notification :as notification]
[frontend.handler.plugin :as plugin-handler]
[frontend.handler.property :as property-handler]
[frontend.search :as search]
[frontend.state :as state]
[frontend.util :as util]
@ -218,101 +217,104 @@
(defn commands-map
[get-page-ref-text]
(->>
(concat
(let [db? (config/db-based-graph? (state/get-current-repo))]
(->>
(concat
;; basic
[["Page reference" [[:editor/input page-ref/left-and-right-brackets {:backward-pos 2}]
[:editor/search-page]] "Create a backlink to a page"]
["Page embed" (embed-page) "Embed a page here"]
["Block reference" [[:editor/input block-ref/left-and-right-parens {:backward-pos 2}]
[:editor/search-block :reference]] "Create a backlink to a block"]
["Block embed" (embed-block) "Embed a block here" "Embed a block here"]
["Link" (link-steps) "Create a HTTP link"]
["Image link" (image-link-steps) "Create a HTTP link to a image"]
(when (state/markdown?)
["Underline" [[:editor/input "<ins></ins>"
{:last-pattern command-trigger
:backward-pos 6}]] "Create a underline text decoration"])
["Template" [[:editor/input command-trigger nil]
[:editor/search-template]] "Insert a created template here"]
(cond
(and (util/electron?) (config/local-file-based-graph? (state/get-current-repo)))
[["Page reference" [[:editor/input page-ref/left-and-right-brackets {:backward-pos 2}]
[:editor/search-page]] "Create a backlink to a page"]
["Page embed" (embed-page) "Embed a page here"]
["Block reference" [[:editor/input block-ref/left-and-right-parens {:backward-pos 2}]
[:editor/search-block :reference]] "Create a backlink to a block"]
["Block embed" (embed-block) "Embed a block here" "Embed a block here"]
["Link" (link-steps) "Create a HTTP link"]
["Image link" (image-link-steps) "Create a HTTP link to a image"]
(when (state/markdown?)
["Underline" [[:editor/input "<ins></ins>"
{:last-pattern command-trigger
:backward-pos 6}]] "Create a underline text decoration"])
(when-not db?
["Template" [[:editor/input command-trigger nil]
[:editor/search-template]] "Insert a created template here"])
(cond
(and (util/electron?) (config/local-file-based-graph? (state/get-current-repo)))
["Upload an asset" [[:editor/click-hidden-file-input :id]] "Upload file types like image, pdf, docx, etc.)"])]
["Upload an asset" [[:editor/click-hidden-file-input :id]] "Upload file types like image, pdf, docx, etc.)"])]
;; ["Upload an image" [[:editor/click-hidden-file-input :id]]]
(headings)
(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"]]
[["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"]]
;; order list
[["Number list" [[:editor/clear-current-slash]
[:editor/toggle-own-number-list]] "Number list"]
["Number children" [[:editor/clear-current-slash]
[:editor/toggle-children-number-list]] "Number children"]]
[["Number list" [[:editor/clear-current-slash]
[:editor/toggle-own-number-list]] "Number list"]
["Number children" [[:editor/clear-current-slash]
[:editor/toggle-children-number-list]] "Number children"]]
;; task management
(get-preferred-workflow)
[["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]]]]
(get-preferred-workflow)
[["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]]]]
;; priority
[["A" (->priority "A")]
["B" (->priority "B")]
["C" (->priority "C")]]
[["A" (->priority "A")]
["B" (->priority "B")]
["C" (->priority "C")]]
;; advanced
[["Query" [[:editor/input "{{query }}" {:backward-pos 2}]
[:editor/exit]] query-doc]
["Zotero" (zotero-steps) "Import Zotero journal article"]
["Query function" [[:editor/input "{{function }}" {:backward-pos 2}]] "Create a query function"]
["Calculator" [[:editor/input "```calc\n\n```" {:type "block"
:backward-pos 4}]
[:codemirror/focus]] "Insert a calculator"]
["Draw" (fn []
(let [file (draw/file-name)
path (str gp-config/default-draw-directory "/" file)
text (page-ref/->page-ref path)]
(p/let [_ (draw/create-draw-with-default-content path)]
(println "draw file created, " path))
text)) "Draw a graph with Excalidraw"]
["Embed HTML " (->inline "html")]
[["Query" [[:editor/input "{{query }}" {:backward-pos 2}]
[:editor/exit]] query-doc]
["Zotero" (zotero-steps) "Import Zotero journal article"]
["Query function" [[:editor/input "{{function }}" {:backward-pos 2}]] "Create a query function"]
["Calculator" [[:editor/input "```calc\n\n```" {:type "block"
:backward-pos 4}]
[:codemirror/focus]] "Insert a calculator"]
["Draw" (fn []
(let [file (draw/file-name)
path (str gp-config/default-draw-directory "/" file)
text (page-ref/->page-ref path)]
(p/let [_ (draw/create-draw-with-default-content path)]
(println "draw file created, " path))
text)) "Draw a graph with Excalidraw"]
["Embed HTML " (->inline "html")]
["Embed Video URL" [[:editor/input "{{video }}" {:last-pattern command-trigger
:backward-pos 2}]]]
["Embed Video URL" [[:editor/input "{{video }}" {:last-pattern command-trigger
:backward-pos 2}]]]
["Embed Youtube timestamp" [[:youtube/insert-timestamp]]]
["Embed Youtube timestamp" [[:youtube/insert-timestamp]]]
["Embed Twitter tweet" [[:editor/input "{{tweet }}" {:last-pattern command-trigger
:backward-pos 2}]]]
["Add new property" [[:editor/clear-current-slash]
[:editor/new-property]]]
["Embed Twitter tweet" [[:editor/input "{{tweet }}" {:last-pattern command-trigger
:backward-pos 2}]]]
(when db?
["Add new property" [[:editor/clear-current-slash]
[:editor/new-property]]])
["Code block" [[:editor/input "```\n```\n" {:type "block"
:backward-pos 5
:only-breakline? true}]
[:editor/select-code-block-mode]] "Insert code block"]]
["Code block" [[:editor/input "```\n```\n" {:type "block"
:backward-pos 5
:only-breakline? true}]
[:editor/select-code-block-mode]] "Insert code block"]]
@*extend-slash-commands
@*extend-slash-commands
;; Allow user to modify or extend, should specify how to extend.
(state/get-commands)
(state/get-plugins-slash-commands))
(remove nil?)
(util/distinct-by-last-wins first)))
(state/get-commands)
(state/get-plugins-slash-commands))
(remove nil?)
(util/distinct-by-last-wins first))))
(defn init-commands!
[get-page-ref-text]

View File

@ -174,11 +174,12 @@
(rum/defc ^:large-vars/cleanup-todo block-context-menu-content <
shortcut/disable-all-shortcuts
[_target block-id]
(let [repo (state/get-current-repo)
db? (config/db-based-graph? repo)]
(when-let [block (db/entity [:block/uuid block-id])]
(let [properties (:block/properties block)
heading (or (pu/lookup properties :heading)
false)
repo (state/get-current-repo)]
false)]
[:.menu-links-wrapper
(ui/menu-background-color #(property-handler/set-block-property! repo block-id :background-color %)
#(property-handler/remove-block-property! repo block-id :background-color))
@ -243,7 +244,8 @@
[:hr.menu-separator]
(block-template block-id)
(when-not db?
(block-template block-id))
(cond
(srs/card-block? block)
@ -260,9 +262,9 @@
nil)
(ui/menu-link
{:key "Toggle number list"
:on-click #(state/pub-event! [:editor/toggle-own-number-list (state/get-selection-block-ids)])}
(t :context-menu/toggle-number-list))
{:key "Toggle number list"
:on-click #(state/pub-event! [:editor/toggle-own-number-list (state/get-selection-block-ids)])}
(t :context-menu/toggle-number-list))
[:hr.menu-separator]
@ -302,7 +304,7 @@
:on-click (fn []
(let [block (db/pull [:block/uuid block-id])]
(dev-common-handler/show-content-ast (:block/content block) (:block/format block))))}
(t :dev/show-block-ast)))])))
(t :dev/show-block-ast)))]))))
(rum/defc block-ref-custom-context-menu-content
[block block-ref-id]

View File

@ -2053,67 +2053,69 @@
([element-id db-id]
(insert-template! element-id db-id {}))
([element-id db-id {:keys [target] :as opts}]
(when-let [db-id (if (integer? db-id)
db-id
(:db/id (db-model/get-template-by-name (name db-id))))]
(let [journal? (:block/journal? target)
repo (state/get-current-repo)
target (or target (state/get-edit-block))
block (db/entity db-id)
format (:block/format block)
block-uuid (:block/uuid block)
template-including-parent? (not (false? (:template-including-parent (:block/properties block))))
blocks (db/get-block-and-children repo block-uuid)
root-block (db/pull db-id)
blocks-exclude-root (remove (fn [b] (= (:db/id b) db-id)) blocks)
sorted-blocks (tree/sort-blocks blocks-exclude-root root-block)
sorted-blocks (cons
(-> (first sorted-blocks)
(update :block/properties-text-values dissoc :template)
(update :block/properties-order (fn [keys]
(vec (remove #{:template} keys)))))
(rest sorted-blocks))
blocks (if template-including-parent?
sorted-blocks
(drop 1 sorted-blocks))]
(when element-id
(insert-command! element-id "" format {:end-pattern commands/command-trigger}))
(let [exclude-properties [:id :template :template-including-parent]
content-update-fn (fn [content]
(->> content
(file-property/remove-property-when-file-based repo format "template")
(file-property/remove-property-when-file-based repo format "template-including-parent")
template/resolve-dynamic-template!))
page (if (:block/name block) block
(when target (:block/page (db/entity (:db/id target)))))
blocks' (map (fn [block]
(paste-block-cleanup repo block page exclude-properties format content-update-fn false))
blocks)
sibling? (:sibling? opts)
sibling?' (cond
(some? sibling?)
sibling?
(let [repo (state/get-current-repo)
db? (config/db-based-graph? repo)]
(when-not db?
(when-let [db-id (if (integer? db-id)
db-id
(:db/id (db-model/get-template-by-name (name db-id))))]
(let [journal? (:block/journal? target)
target (or target (state/get-edit-block))
block (db/entity db-id)
format (:block/format block)
block-uuid (:block/uuid block)
template-including-parent? (not (false? (:template-including-parent (:block/properties block))))
blocks (db/get-block-and-children repo block-uuid)
root-block (db/pull db-id)
blocks-exclude-root (remove (fn [b] (= (:db/id b) db-id)) blocks)
sorted-blocks (tree/sort-blocks blocks-exclude-root root-block)
sorted-blocks (cons
(-> (first sorted-blocks)
(update :block/properties-text-values dissoc :template)
(update :block/properties-order (fn [keys]
(vec (remove #{:template} keys)))))
(rest sorted-blocks))
blocks (if template-including-parent?
sorted-blocks
(drop 1 sorted-blocks))]
(when element-id
(insert-command! element-id "" format {:end-pattern commands/command-trigger}))
(let [exclude-properties [:id :template :template-including-parent]
content-update-fn (fn [content]
(->> content
(file-property/remove-property-when-file-based repo format "template")
(file-property/remove-property-when-file-based repo format "template-including-parent")
template/resolve-dynamic-template!))
page (if (:block/name block) block
(when target (:block/page (db/entity (:db/id target)))))
blocks' (map (fn [block]
(paste-block-cleanup repo block page exclude-properties format content-update-fn false))
blocks)
sibling? (:sibling? opts)
sibling?' (cond
(some? sibling?)
sibling?
(db/has-children? (:block/uuid target))
false
(db/has-children? (:block/uuid target))
false
:else
true)]
(try
(outliner-tx/transact!
{:outliner-op :insert-blocks
:created-from-journal-template? journal?}
(save-current-block!)
(let [result (outliner-core/insert-blocks! blocks'
target
(assoc opts
:sibling? sibling?'))]
(edit-last-block-after-inserted! result)))
(catch :default ^js/Error e
(notification/show!
[:p.content
(util/format "Template insert error: %s" (.-message e))]
:error))))))))
:else
true)]
(try
(outliner-tx/transact!
{:outliner-op :insert-blocks
:created-from-journal-template? journal?}
(save-current-block!)
(let [result (outliner-core/insert-blocks! blocks'
target
(assoc opts
:sibling? sibling?'))]
(edit-last-block-after-inserted! result)))
(catch :default ^js/Error e
(notification/show!
[:p.content
(util/format "Template insert error: %s" (.-message e))]
:error))))))))))
(defn template-on-chosen-handler
[element-id]