Page embed

Resolves #64
pull/645/head
Tienson Qin 2020-07-22 07:27:19 +08:00
parent e4e5e7254b
commit 384a0cef8b
7 changed files with 125 additions and 3250 deletions

View File

@ -27,7 +27,6 @@
"react-dom": "^16.12.0", "react-dom": "^16.12.0",
"react-textarea-autosize": "^8.0.1", "react-textarea-autosize": "^8.0.1",
"react-transition-group": "^4.3.0", "react-transition-group": "^4.3.0",
"tauri": "^0.8.2",
"webdav": "^3.3.0" "webdav": "^3.3.0"
} }
} }

View File

@ -45,10 +45,18 @@
[[:editor/input template {:last-pattern slash [[:editor/input template {:last-pattern slash
:backward-pos 2}]])) :backward-pos 2}]]))
(defn embed-page
[]
(conj
[[:editor/input "{{{embed [[]]}}}" {:last-pattern slash
:backward-pos 5}]]
[:editor/search-page :embed]))
(defn embed-block (defn embed-block
[] []
(conj (conj
(->inline "embed") [[:editor/input "{{{embed (())}}}" {:last-pattern slash
:backward-pos 5}]]
[:editor/search-block :embed])) [:editor/search-block :embed]))
;; Credits to roamresearch.com ;; Credits to roamresearch.com
@ -75,6 +83,7 @@
["Date Picker" [[:editor/show-date-picker]]] ["Date Picker" [[:editor/show-date-picker]]]
["Page Reference" [[:editor/input "[[]]" {:backward-pos 2}] ["Page Reference" [[:editor/input "[[]]" {:backward-pos 2}]
[:editor/search-page]]] [:editor/search-page]]]
["Page Embed" (embed-page)]
["Block Reference" [[:editor/input "(())" {:backward-pos 2}] ["Block Reference" [[:editor/input "(())" {:backward-pos 2}]
[:editor/search-block :reference]]] [:editor/search-block :reference]]]
["Block Embed" (embed-block)] ["Block Embed" (embed-block)]

View File

@ -266,20 +266,12 @@
(let [uuid-string (str (:heading/uuid chosen)) (let [uuid-string (str (:heading/uuid chosen))
block-type @commands/*block-type] block-type @commands/*block-type]
;; block reference ;; block reference
(case block-type (when (= block-type :reference)
:embed
;; block embed
(insert-command! id
(str "@@embed: " uuid-string)
format
{:last-pattern "@@embed: "})
:reference
(insert-command! id (insert-command! id
(util/format "((%s))" uuid-string) (util/format "((%s))" uuid-string)
format format
{:last-pattern (str "((" q) {:last-pattern (str "((" q)
:postfix-fn (fn [s] (util/replace-first "))" s ""))}) :postfix-fn (fn [s] (util/replace-first "))" s ""))}) )
nil)
;; Save it so it'll be parsed correctly in the future ;; Save it so it'll be parsed correctly in the future
(handler/set-heading-property! (:heading/uuid chosen) (handler/set-heading-property! (:heading/uuid chosen)

View File

@ -201,7 +201,7 @@
(declare headings-container) (declare headings-container)
(rum/defc block-embed < (rum/defc block-embed < rum/reactive
(mixins/clear-query-cache (mixins/clear-query-cache
(fn [state] (fn [state]
(let [repo (state/get-current-repo) (let [repo (state/get-current-repo)
@ -209,7 +209,26 @@
[repo :heading/block heading-id]))) [repo :heading/block heading-id])))
[config id] [config id]
(let [headings (db/get-heading-and-children (state/get-current-repo) id)] (let [headings (db/get-heading-and-children (state/get-current-repo) id)]
[:div.embed-block.pt-2.px-3.bg-base-2 [:div.embed-block.py-2.my-2.px-3.bg-base-2
[:p
[:code "Embed block:"]]
(headings-container headings (assoc config :embed? true))]))
(rum/defc page-embed < rum/reactive
(mixins/clear-query-cache
(fn [state]
(let [repo (state/get-current-repo)
page-name (last (:rum/args state))
page-id (:db/id (db/entity [:page/name page-name]))]
[repo :page/headings page-id])))
[config page-name]
(let [page-name (string/lower-case page-name)
headings (db/get-page-headings (state/get-current-repo) page-name)]
[:div.embed-page.py-2.my-2.px-3.bg-base-2
[:p
[:code "Embed page:"]
[:a.ml-2 {:href (str "/page/" (util/encode-str page-name))}
(util/capitalize-all page-name)]]
(headings-container headings (assoc config :embed? true))])) (headings-container headings (assoc config :embed? true))]))
(defn inline (defn inline
@ -359,13 +378,6 @@
["Inline_Hiccup" s] ["Inline_Hiccup" s]
(reader/read-string s) (reader/read-string s)
["Export_Snippet" "embed" s]
(when-let [id (and s
(let [s (string/trim s)]
(and (util/uuid-string? s)
(uuid s))))]
(block-embed config id))
["Break_Line"] ["Break_Line"]
[:br] [:br]
["Hard_Break_Line"] ["Hard_Break_Line"]
@ -405,6 +417,33 @@
["Macro" options] ["Macro" options]
(let [{:keys [name arguments]} options] (let [{:keys [name arguments]} options]
(cond
(= name "embed")
(let [a (first arguments)]
(cond
(and (string/starts-with? a "[[")
(string/ends-with? a "]]"))
(let [page-name (-> (string/replace a "[[" "")
(string/replace "]]" "")
string/trim)]
(when-not (string/blank? page-name)
(page-embed config page-name)))
(and (string/starts-with? a "((")
(string/ends-with? a "))"))
(when-let [s (-> (string/replace a "((" "")
(string/replace "))" "")
string/trim)]
(when-let [id (and s
(let [s (string/trim s)]
(and (util/uuid-string? s)
(uuid s))))]
(block-embed config id)))
:else ;TODO: maybe collections?
nil))
:else
(when-let [heading-uuid (:heading/uuid config)] (when-let [heading-uuid (:heading/uuid config)]
(let [macro-content (or (let [macro-content (or
(-> (db/entity [:heading/uuid heading-uuid]) (-> (db/entity [:heading/uuid heading-uuid])
@ -417,9 +456,9 @@
(get-in (state/get-config) [:macros name]) (get-in (state/get-config) [:macros name])
(get-in (state/get-config) [:macros (keyword name)]))] (get-in (state/get-config) [:macros (keyword name)]))]
[:span [:span
(if (seq arguments) (if (and (seq arguments) macro-content)
(block/macro-subs macro-content arguments) (block/macro-subs macro-content arguments)
macro-content)]))) macro-content)]))))
:else :else
"")) ""))

View File

@ -1024,7 +1024,7 @@
pred (fn [data meta] pred (fn [data meta]
(>= (:pos meta) pos))] (>= (:pos meta) pos))]
(some-> (q repo [:heading/block heading-uuid] (some-> (q repo [:heading/block heading-uuid]
{:use-cache? false {:use-cache? true
:transform-fn #(heading-and-children-transform % repo heading-uuid level)} :transform-fn #(heading-and-children-transform % repo heading-uuid level)}
'[:find (pull ?heading [*]) '[:find (pull ?heading [*])
:in $ ?page ?pred :in $ ?page ?pred

View File

@ -144,6 +144,8 @@
(defn set-edit-content! (defn set-edit-content!
[input-id value] [input-id value]
(when input-id (when input-id
(when-let [input (gdom/getElement input-id)]
(gobj/set input "value" value))
(update-state! :editor/content (fn [m] (update-state! :editor/content (fn [m]
(assoc m input-id value))) (assoc m input-id value)))
;; followers ;; followers

File diff suppressed because it is too large Load Diff