Fix custom query loop rendering

pull/645/head
Tienson Qin 2020-06-19 14:48:06 +08:00
parent a74452e3fe
commit e8e40e707c
7 changed files with 95 additions and 51 deletions

View File

@ -60,10 +60,16 @@
(let [content (db/get-file path)]
(content/content encoded-path {:content content
:format format
:on-hide (save-file-handler content)})))]
:on-hide (save-file-handler content)})))
page (db/get-file-page path)]
[:div.file
[:h1.title
path]
(when page
[:div.text-sm.mb-4.ml-1 "Page: "
[:a.bg-base-2.p-1.ml-1 {:style {:border-radius 4}
:href (str "/page/" (util/url-encode page))}
(util/capitalize-all page)]])
(cond
;; image type
(and format (contains? (config/img-formats) format))

View File

@ -816,23 +816,30 @@
(declare ->hiccup)
(rum/defc custom-query < rum/reactive
{:did-mount (fn [state]
(rum/defcs custom-query < rum/reactive
{:will-mount (fn [state]
(let [[config _options content] (:rum/args state)
current-heading-uuid (:heading/uuid (:heading config))
remove-headings (if current-heading-uuid [current-heading-uuid] nil)
result (db/custom-query content remove-headings)]
(assoc state :query-result result)))
:did-mount (fn [state]
(when-let [query (last (:rum/args state))]
(state/add-custom-query-component! query (:rum/react-component state)))
state)}
[config options content]
[:div.custom-query.my-4
[:code (or (:query-title options)
"Query result: ")]
(let [result (db/custom-query content)]
[state config options content]
;; exclude the current one, otherwise it'll loop forever
(let [result (:query-result state)]
[:div.custom-query.my-2
[:code (or (:query-title options)
"Query result: ")]
(if (seq result)
(->hiccup result (assoc config
:custom-query? true
:group-by-page? true)
{:style {:margin-top "0.25rem"
:margin-left "0.25rem"}})
[:div "Empty"]))])
[:div "Empty"])]))
(rum/defc admonition
[config type options result]

View File

@ -28,7 +28,7 @@
(when-let [template (state/get-journal-template)]
(handler/insert-new-heading!
(first headings)
(str (:heading/content (first headings)) "\n" template)
template
false))))))
state)}
[[title format]]

View File

@ -113,7 +113,8 @@
(svg/star-outline "stroke-current h-5 w-5"))]])
(when (and file-path (not sidebar?) (not journal?))
[:div.text-sm.mb-2.ml-1 "File: "
[:div.text-sm.ml-1
"File: "
[:a.bg-base-2.p-1.ml-1 {:style {:border-radius 4}
:href (str "/file/" (util/url-encode file-path))}
file-path]])
@ -129,7 +130,7 @@
[:span.mr-1 (util/capitalize-all item)]])])))
;; content before headings, maybe directives or summary it can be anything
(when (and (not journal?) (not heading?))
(when (and (not journal?) (not heading?) (not sidebar?))
(when-let [path (let [file-id (:db/id (:page/file page))]
(:file/path (db/entity repo file-id)))]
(let [encoded-path (util/url-encode path)
@ -142,7 +143,7 @@
(let [encoded-content (utf8/encode content)
heading-start-pos (or heading-start-pos (utf8/length encoded-content))
content-before-heading (string/trim (utf8/substring encoded-content 0 heading-start-pos))]
[:div.before-heading.ml-1.mt-4
[:div.before-heading.ml-1.mt-5.mb-3
(content/content
encoded-path
{:content content-before-heading

View File

@ -38,7 +38,7 @@
(fn []
(handler/redirect! {:to :home}))
500))}
"Rebuild index"]
"Rebuild the db"]
[:a.text-gray-400.ml-4 {:on-click (fn []
(handler/remove-repo! repo))}
"Remove"]]])]]

View File

@ -36,9 +36,11 @@
:page
(let [page-name (get-in block-data [:page :page/name])]
[page-name (page/page {:parameters {:path {:name page-name}}
:sidebar? true
:repo repo})])
[page-name
[:div.ml-2
(page/page {:parameters {:path {:name page-name}}
:sidebar? true
:repo repo})]])
["" [:span]]))

View File

@ -247,7 +247,7 @@
(let [match (:route-match @state/state)
route-name (get-in match [:data :name])]
(when (= route-name :tag)
(get-in match [:path-params :name]))))
(get-in match [:path-params :name]))))
(defn pull
([eid]
@ -318,18 +318,20 @@
(if (= k :page/refed-headings)
[:page/ref-pages page-id]))
handler-keys)
custom-queries (some->>
(filter (fn [v]
(and (= (first v) (state/get-current-repo))
(= (second v) :custom)))
(keys @query-state))
(map (fn [v]
(vec (drop 1 v)))))]
;; custom-queries (some->>
;; (filter (fn [v]
;; (and (= (first v) (state/get-current-repo))
;; (= (second v) :custom)))
;; (keys @query-state))
;; (map (fn [v]
;; (vec (drop 1 v)))))
]
(->>
(util/concat-without-nil
handler-keys
refed-pages
custom-queries)
;; custom-queries
)
distinct)))
[[key]])))
@ -400,26 +402,42 @@
(sort-by-pos)
(group-by :heading/page)))
(defn- with-repo
[repo headings]
(map (fn [heading]
(assoc heading :heading/repo repo))
headings))
(defn custom-query
[query-string]
(try
(let [query (reader/read-string query-string)
[query inputs] (if (vector? (first query))
[`~(first query) (rest query)]
[`~query nil])
inputs (map resolve-input inputs)
repo (state/get-current-repo)
k [:custom query-string]
result (-> (apply q repo k {} query inputs)
react
seq-flatten)
heading? (:heading/uuid (first result))]
(if heading?
(group-by-page result)
result))
(catch js/Error e
(println "Query parsing failed: ")
(js/console.dir e))))
([query-string]
(custom-query query-string nil))
([query-string remove-headings]
(try
(let [query (reader/read-string query-string)
[query inputs] (if (vector? (first query))
[`~(first query) (rest query)]
[`~query nil])
inputs (map resolve-input inputs)
repo (state/get-current-repo)
k [:custom query-string]
result (-> (apply q repo k {} query inputs)
react
seq-flatten)
heading? (:heading/uuid (first result))]
(if heading?
(let [result (if (seq remove-headings)
(let [remove-headings (set remove-headings)]
(remove (fn [h]
(contains? remove-headings (:heading/uuid h)))
result))
result)]
(some->> result
(with-repo repo)
(group-by-page)))
result))
(catch js/Error e
(println "Query parsing failed: ")
(js/console.dir e)))))
(defn refresh-query-result!
[repo query inputs]
@ -723,12 +741,6 @@
(let [aliases (get-page-alias repo-url page)]
(set (conj aliases page))))
(defn- with-repo
[repo headings]
(map (fn [heading]
(assoc heading :heading/repo repo))
headings))
(defn get-page-headings
([page]
(get-page-headings (state/get-current-repo)
@ -1348,6 +1360,22 @@
(get-heading-end-pos-rec (last children))
(get-in heading [:heading/meta :end-pos]))))
(defn get-file-page
[file-path]
(when-let [repo (state/get-current-repo)]
(when-let [conn (get-conn repo)]
(some->
(d/q
'[:find ?page-name
:in $ ?path
:where
[?file :file/path ?path]
[?page :page/file ?file]
[?page :page/name ?page-name]]
conn file-path)
seq-flatten
first))))
(comment
(defn debug!
[]