feat: add including-parent property for children-only templates

pull/1303/head
Tienson Qin 2021-02-16 21:18:25 +08:00
parent 150953fcb8
commit e862cdf081
2 changed files with 39 additions and 10 deletions

View File

@ -72,12 +72,28 @@
"#264c9b"
"#793e3e"])
(rum/defcs block-template <
(defonce *including-parent? (atom nil))
(rum/defc template-checkbox
[including-parent?]
[:div.flex.flex-row
[:span.text-medium.mr-2 "Including the parent block in the template?"]
(ui/toggle including-parent?
#(swap! *including-parent? not))])
(rum/defcs block-template < rum/reactive
(rum/local false ::edit?)
(rum/local "" ::input)
[state block-id]
(let [edit? (get state ::edit?)
input (get state ::input)]
input (get state ::input)
including-parent? (rum/react *including-parent?)
block-id (if (string? block-id) (uuid block-id) block-id)
block (db/entity [:block/uuid block-id])
has-children? (seq (:block/children block))]
(when (and (nil? including-parent?) has-children?)
(reset! *including-parent? true))
(if @edit?
(do
(state/clear-edit!)
@ -87,6 +103,8 @@
{:auto-focus true
:on-change (fn [e]
(reset! input (util/evalue e)))}]
(when has-children?
(template-checkbox including-parent?))
(ui/button "Submit"
:on-click (fn []
(let [title (string/trim @input)]
@ -97,6 +115,8 @@
:error)
(do
(editor-handler/set-block-property! block-id "template" title)
(when-not including-parent?
(editor-handler/set-block-property! block-id "including-parent" false))
(state/hide-custom-context-menu!)))))))])
(ui/menu-link
{:key "Make template"

View File

@ -202,20 +202,29 @@
chosen-handler (fn [[template db-id] _click?]
(if-let [block (db/entity db-id)]
(let [new-level (:block/level edit-block)
properties (:block/properties block)
block-uuid (:block/uuid block)
including-parent? (not= (get properties "including-parent") "false")
template-parent-level (:block/level block)
pattern (config/get-block-pattern format)
content
(block-handler/get-block-full-content
(state/get-current-repo)
(:block/uuid block)
(fn [{:block/keys [level content properties] :as block}]
(let [new-level (+ new-level (- level template-parent-level))
properties' (dissoc (into {} properties) "id" "custom_id" "template")]
(-> content
(string/replace-first (apply str (repeat level pattern))
(apply str (repeat new-level pattern)))
text/remove-properties!
(text/rejoin-properties properties')))))
(fn [{:block/keys [uuid level content properties] :as block}]
(let [parent? (= uuid block-uuid)
ignore-parent? (and parent? (not including-parent?))]
(if ignore-parent?
""
(let [new-level (+ new-level
(- level template-parent-level
(if (not including-parent?) 1 0)))
properties' (dissoc (into {} properties) "id" "custom_id" "template")]
(-> content
(string/replace-first (apply str (repeat level pattern))
(apply str (repeat new-level pattern)))
text/remove-properties!
(text/rejoin-properties properties')))))))
content (if (string/includes? (string/trim edit-content) "\n")
content
(text/remove-level-spaces content format))