Fix deleting issue

pull/645/head
Tienson Qin 2020-06-06 21:52:33 +08:00
parent 58c23413e8
commit a4133956c4
6 changed files with 143 additions and 134 deletions

View File

@ -694,7 +694,7 @@
(state/add-custom-query-component! query (:rum/react-component state)))
state)}
[config options content]
[:div.custom-query.my-2
[:div.custom-query.my-4
[:code (or (:query-title options)
"Query result: ")]
(let [result (db/custom-query content)]
@ -833,7 +833,7 @@
(if (:group-by-page? config)
(for [[page headings] headings]
(let [page (db/entity (:db/id page))]
[:div {:key (str "page-" (:db/id page))}
[:div.my-2 {:key (str "page-" (:db/id page))}
(page-cp page)
(headings-cp headings config)]))
(headings-cp headings config))]))

View File

@ -48,114 +48,118 @@
heading? (util/uuid-string? page-name)
heading-id (and heading? (uuid page-name))
sidebar? (:sidebar? option)
raw-page-headings (get-headings repo page-name journal? heading?)
page-name (if heading?
(:page/name (db/entity repo (:db/id (:heading/page (first raw-page-headings)))))
page-name)
page (db/entity repo [:page/name page-name])
file (:page/file page)
file-path (and (:db/id file) (:file/path (db/entity repo (:db/id file))))
content (db/get-file-no-sub repo file-path)
page-headings (db/with-dummy-heading raw-page-headings format
(if (empty? raw-page-headings)
{:heading/page {:db/id (:db/id page)}
:heading/file {:db/id (:db/id (:page/file page))}
:heading/meta
(let [file-id (:db/id (:page/file page))]
{:pos (utf8/length (utf8/encode content))
:end-pos nil})})
journal?)
start-level (if journal? 2 1)
hiccup (hiccup/->hiccup page-headings {:id encoded-page-name
:start-level start-level
:sidebar? sidebar?})
starred? (contains? (set
(some->> (state/sub [:config repo :starred])
(map string/lower-case)))
page-name)
today? (and
journal?
(= page-name (string/lower-case (date/journal-name))))]
[:div.flex-1.page
(when-not sidebar?
[:div.flex.flex-row
[:a {:on-click (fn [e]
(util/stop e)
(when (gobj/get e "shiftKey")
(when-let [page (db/pull repo '[*] [:page/name page-name])]
(state/sidebar-add-block!
repo
(:db/id page)
:page
{:page page}))
(handler/show-right-sidebar)))}
[:h1.title
(util/capitalize-all page-name)]]
raw-page-headings (get-headings repo page-name journal? heading?)]
(if (and sidebar? (empty? raw-page-headings))
(do
(state/sidebar-remove-block! (:sidebar/idx option))
[:div "Deleted"])
(let [page-name (if heading?
(:page/name (db/entity repo (:db/id (:heading/page (first raw-page-headings)))))
page-name)
page (db/entity repo [:page/name page-name])
file (:page/file page)
file-path (and (:db/id file) (:file/path (db/entity repo (:db/id file))))
content (db/get-file-no-sub repo file-path)
page-headings (db/with-dummy-heading raw-page-headings format
(if (empty? raw-page-headings)
{:heading/page {:db/id (:db/id page)}
:heading/file {:db/id (:db/id (:page/file page))}
:heading/meta
(let [file-id (:db/id (:page/file page))]
{:pos (utf8/length (utf8/encode content))
:end-pos nil})})
journal?)
start-level (if journal? 2 1)
hiccup (hiccup/->hiccup page-headings {:id encoded-page-name
:start-level start-level
:sidebar? sidebar?})
starred? (contains? (set
(some->> (state/sub [:config repo :starred])
(map string/lower-case)))
page-name)
today? (and
journal?
(= page-name (string/lower-case (date/journal-name))))]
[:div.flex-1.page
(when-not sidebar?
[:div.flex.flex-row
[:a {:on-click (fn [e]
(util/stop e)
(when (gobj/get e "shiftKey")
(when-let [page (db/pull repo '[*] [:page/name page-name])]
(state/sidebar-add-block!
repo
(:db/id page)
:page
{:page page}))
(handler/show-right-sidebar)))}
[:h1.title
(util/capitalize-all page-name)]]
[:a.ml-1.text-gray-500.hover:text-gray-700
{:class (if starred? "text-gray-800")
:on-click (fn []
;; TODO: save to config file
(handler/star-page! page-name starred?))}
(if starred?
(svg/star-solid "stroke-current")
(svg/star-outline "stroke-current h-5 w-5"))]])
[:a.ml-1.text-gray-500.hover:text-gray-700
{:class (if starred? "text-gray-800")
:on-click (fn []
;; TODO: save to config file
(handler/star-page! page-name starred?))}
(if starred?
(svg/star-solid "stroke-current")
(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: "
[:a.bg-base-2.p-1.ml-1 {:style {:border-radius 4}
:href (str "/file/" (util/url-encode file-path))}
file-path]])
(when (and file-path (not sidebar?) (not journal?))
[:div.text-sm.mb-2.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]])
(when (and repo (not journal?))
(let [alias (db/get-page-alias repo page-name)]
(when (seq alias)
[:div.alias.ml-1.mb-1.content
[:span.font-bold.mr-1 "Page aliases: "]
(for [item alias]
[:a {:href (str "/page/" (util/url-encode item))}
[:span.mr-1 (util/capitalize-all item)]])])))
(when (and repo (not journal?))
(let [alias (db/get-page-alias repo page-name)]
(when (seq alias)
[:div.alias.ml-1.mb-1.content
[:span.font-bold.mr-1 "Page aliases: "]
(for [item alias]
[:a {:href (str "/page/" (util/url-encode item))}
[:span.mr-1 (util/capitalize-all item)]])])))
;; content before headings, maybe directives or summary it can be anything
(when (and (not journal?) (not heading?) (:page/file page))
(let [path (let [file-id (:db/id (:page/file page))]
(:file/path (db/entity repo file-id)))
encoded-path (util/url-encode path)
heading-start-pos (get-in (first raw-page-headings) [:heading/meta :pos])]
(when (or (not (zero? heading-start-pos))
(seq (:page/directives page)))
(let [encoded-content (utf8/encode content)
content-before-heading (string/trim (utf8/substring encoded-content 0 heading-start-pos))]
[:div.before-heading.ml-4
(content/content
encoded-path
{:content content-before-heading
:format format
:on-hide (fn [value]
(let [new-content (str (string/trim value)
"\n"
(when heading-start-pos
(utf8/substring encoded-content heading-start-pos)))]
(when (not= (string/trim new-content)
(string/trim content))
(handler/alter-file repo path new-content {:re-render-root? true}))))})]))))
;; headings
(content/content encoded-page-name
{:hiccup hiccup
:sidebar? sidebar?})
;; content before headings, maybe directives or summary it can be anything
(when (and (not journal?) (not heading?) (:page/file page))
(let [path (let [file-id (:db/id (:page/file page))]
(:file/path (db/entity repo file-id)))
encoded-path (util/url-encode path)
heading-start-pos (get-in (first raw-page-headings) [:heading/meta :pos])]
(when (or (not (zero? heading-start-pos))
(seq (:page/directives page)))
(let [encoded-content (utf8/encode content)
content-before-heading (string/trim (utf8/substring encoded-content 0 heading-start-pos))]
[:div.before-heading.ml-4
(content/content
encoded-path
{:content content-before-heading
:format format
:on-hide (fn [value]
(let [new-content (str (string/trim value)
"\n"
(when heading-start-pos
(utf8/substring encoded-content heading-start-pos)))]
(when (not= (string/trim new-content)
(string/trim content))
(handler/alter-file repo path new-content {:re-render-root? true}))))})]))))
;; headings
(content/content encoded-page-name
{:hiccup hiccup
:sidebar? sidebar?})
(when (and today? (not sidebar?))
(let [queries (state/sub [:config repo :default-queries :journals])]
(when (seq queries)
[:div#today-queries
(for [{:keys [title query]} queries]
[:div {:key (str "query-" title)}
(hiccup/custom-query {:start-level 2} {:query-title title}
query)])])))
(when (and today? (not sidebar?))
(let [queries (state/sub [:config repo :default-queries :journals])]
(when (seq queries)
[:div#today-queries
(for [{:keys [title query]} queries]
[:div {:key (str "query-" title)}
(hiccup/custom-query {:start-level 2} {:query-title title}
query)])])))
;; referenced headings
(when-not sidebar?
(reference/references page-name))]))
;; referenced headings
(when-not sidebar?
(reference/references page-name))]))))
(rum/defc all-pages < rum/reactive
[]

View File

@ -14,24 +14,25 @@
[clojure.string :as string]))
(rum/defc heading-cp < rum/reactive
[repo heading]
[repo idx heading]
(let [id (:heading/uuid heading)]
(page/page {:parameters {:path {:name (str id)}}
:sidebar? true
:sidebar/idx idx
:repo repo})))
(defn build-sidebar-item
[repo db-id block-type block-data]
[repo idx db-id block-type block-data]
(case block-type
:heading-ref
["Block reference"
[:div.ml-2
(heading-cp repo (:heading block-data))]]
(heading-cp repo idx (:heading block-data))]]
:heading
["Block"
[:div.ml-2
(heading-cp repo block-data)]]
(heading-cp repo idx block-data)]]
:page
(let [page-name (get-in block-data [:page :page/name])]
@ -45,7 +46,7 @@
[repo idx db-id block-type block-data]
(let [collapse? (state/sub [:ui/sidebar-collapsed-blocks db-id])]
[:div.sidebar-item.content
(let [[title component] (build-sidebar-item repo db-id block-type block-data)]
(let [[title component] (build-sidebar-item repo idx db-id block-type block-data)]
[:div.flex.flex-col
[:div.flex.flex-row.justify-between
[:div.flex.flex-row.justify-center
@ -86,7 +87,6 @@
(vis/new-network "page-graph" graph))
state)
(defonce *show-graph? (atom false))
(rum/defc graph < rum/reactive
{:did-mount render-graph
@ -116,7 +116,7 @@
[:div.mr-1.font-bold.text-sm "Graph"]
[:div.px-1
(ui/toggle show-graph? (fn []
(swap! *show-graph? not)))]]]
(swap! *show-graph? not)))]]]
(for [[idx [repo db-id block-type block-data]] (medley/indexed blocks)]
(rum/with-key

View File

@ -236,9 +236,12 @@
(pull (state/get-current-repo) selector eid))
([repo selector eid]
(when-let [conn (get-conn repo)]
(d/pull conn
selector
eid))))
(try
(d/pull conn
selector
eid)
(catch js/Error e
nil)))))
(defn pull-many
([eids]
@ -276,8 +279,7 @@
[:page/ref-pages page-id]
[:page/ref-pages current-page-id]
[:page/refed-headings current-page-id]
[:page/mentioned-pages current-page-id]
]
[:page/mentioned-pages current-page-id]]
;; refed-pages
(apply concat

View File

@ -230,7 +230,7 @@
(load-contents files nil nil false))
(when (seq diffs)
(let [filter-diffs (fn [type] (->> (filter (fn [f] (= type (:type f))) diffs)
(map :path)))
(map :path)))
remove-files (filter-diffs "remove")
modify-files (filter-diffs "modify")
add-files (filter-diffs "add")
@ -914,7 +914,8 @@
([heading value]
(insert-new-heading! heading value true))
([{:heading/keys [uuid content meta file dummy? level repo] :as heading} value create-new-heading?]
(let [repo (or repo (state/get-current-repo))
(when-not dummy?
(let [repo (or repo (state/get-current-repo))
value (string/trim value)
heading (with-heading-meta repo heading)
format (:heading/format heading)
@ -940,7 +941,7 @@
:data headings}
file-path
new-content)
[first-heading last-heading new-heading-content]))))
[first-heading last-heading new-heading-content])))))
;; TODO: utf8 encode performance
(defn check
@ -957,20 +958,21 @@
[{:heading/keys [uuid meta content file repo] :as heading} dummy?]
(when-not dummy?
(let [repo (or repo (state/get-current-repo))
heading (db/pull repo '[*] [:heading/uuid uuid])
file-path (:file/path (db/entity repo (:db/id file)))
file-content (db/get-file repo file-path)
after-headings (rebuild-after-headings repo file (:end-pos meta) (:pos meta))
new-content (utf8/delete! file-content (:pos meta) (:end-pos meta))]
(transact-react-and-alter-file!
repo
(concat
[[:db.fn/retractEntity [:heading/uuid uuid]]]
after-headings)
{:key :heading/change
:data [heading]}
file-path
new-content))))
heading (db/pull repo '[*] [:heading/uuid uuid])]
(when heading
(let [file-path (:file/path (db/entity repo (:db/id file)))
file-content (db/get-file repo file-path)
after-headings (rebuild-after-headings repo file (:end-pos meta) (:pos meta))
new-content (utf8/delete! file-content (:pos meta) (:end-pos meta))]
(transact-react-and-alter-file!
repo
(concat
[[:db.fn/retractEntity [:heading/uuid uuid]]]
after-headings)
{:key :heading/change
:data [heading]}
file-path
new-content))))))
(defn delete-headings!
[repo heading-uuids]

View File

@ -278,7 +278,8 @@
(when db-id
(update-state! :sidebar/blocks (fn [blocks]
(->> (remove #(= (first %) db-id) blocks)
(cons [repo db-id block-type block-data]))))))
(cons [repo db-id block-type block-data])
(distinct))))))
(defn sidebar-remove-block!
[idx]
(update-state! :sidebar/blocks #(util/drop-nth idx %)))