improve(pdf): support block ref with highlight

This commit is contained in:
charlie
2021-07-26 17:21:35 +08:00
parent cb05f431f5
commit 980741ef97
5 changed files with 110 additions and 22 deletions

File diff suppressed because one or more lines are too long

View File

@@ -719,7 +719,7 @@
[page-name] [page-name]
(if (util/uuid-string? page-name) (if (util/uuid-string? page-name)
(db-utils/entity [:block/uuid (uuid page-name)]) (db-utils/entity [:block/uuid (uuid page-name)])
(db-utils/entity [:block/name page-name]))) (db-utils/entity [:block/name (string/lower-case page-name)])))
(defn- heading-block? (defn- heading-block?
[block] [block]

View File

@@ -3,6 +3,7 @@
[frontend.util :as utils] [frontend.util :as utils]
[frontend.db.model :as db-model] [frontend.db.model :as db-model]
[frontend.db.utils :as db-utils] [frontend.db.utils :as db-utils]
[frontend.handler.page :as page-handler]
[frontend.handler.editor :as editor-handler] [frontend.handler.editor :as editor-handler]
[frontend.state :as state] [frontend.state :as state]
[frontend.config :as config] [frontend.config :as config]
@@ -48,6 +49,45 @@
data (pr-str {:highlights highlights})] data (pr-str {:highlights highlights})]
(fs/write-file! repo-cur repo-dir hls-file data {:skip-mtime? true})))) (fs/write-file! repo-cur repo-dir hls-file data {:skip-mtime? true}))))
(defn resolve-ref-page
[page-name]
(let [page-name (str "hls__" page-name)
page (db-model/get-page page-name)]
(if-not page
(do
(page-handler/create! page-name {:redirect? false :create-first-block? false})
;; refresh to file
(editor-handler/api-insert-new-block! page-name {:page page-name})
(db-model/get-page page-name))
page)))
(defn create-ref-block!
[{:keys [id content]}]
(when-let [pdf-current (:pdf/current @state/state)]
(when-let [ref-page (resolve-ref-page (:key pdf-current))]
(if-let [ref-block (db-model/get-block-by-uuid id)]
(do
(js/console.debug "[existed ref block]" ref-block)
ref-block)
(let [text (:text content)] ;; TODO: image
(editor-handler/api-insert-new-block!
text {:page (:block/name ref-page)
:custom-uuid id
:properties {:type "annotation"
:id (str id) ;; force custom uuid
}}))))))
(defn del-ref-block!
[{:keys [id]}]
(when-let [repo (state/get-current-repo)]
(when-let [block (db-model/get-block-by-uuid id)]
(editor-handler/delete-block-aux! block true))))
(defn copy-hl-ref!
[highlight]
(when-let [ref-block (create-ref-block! highlight)]
(utils/copy-to-clipboard! (str "((" (:block/uuid ref-block) "))"))))
(defn upload-asset! (defn upload-asset!
[page-block files refresh-file!] [page-block files refresh-file!]
(let [repo (state/get-current-repo) (let [repo (state/get-current-repo)

View File

@@ -87,23 +87,29 @@
:on-click (fn [^js/MouseEvent e] :on-click (fn [^js/MouseEvent e]
(when-let [action (.. e -target -dataset -action)] (when-let [action (.. e -target -dataset -action)]
(case action (case action
"ref"
(pdf-assets/copy-hl-ref! highlight)
"copy" "copy"
(do (do
(front-utils/copy-to-clipboard! (:text content)) (front-utils/copy-to-clipboard! (:text content))
(pdf-utils/clear-all-selection)) (pdf-utils/clear-all-selection))
"del" "del"
(del-hl! highlight) (do
(del-hl! highlight)
(pdf-assets/del-ref-block! highlight))
;; colors ;; colors
(let [properties {:color action}] (let [properties {:color action}]
(if-not id (if-not id
;; add highlight ;; add highlight
(do (let [highlight (merge highlight
(add-hl! (merge highlight {:id (pdf-utils/gen-uuid)
{:id (pdf-utils/gen-uuid) :properties properties})]
:properties properties})) (add-hl! highlight)
(pdf-utils/clear-all-selection)) (pdf-utils/clear-all-selection)
(pdf-assets/copy-hl-ref! highlight))
;; update highlight ;; update highlight
(do (do
@@ -115,6 +121,9 @@
(for [it ["yellow", "blue", "green", "red", "purple"]] (for [it ["yellow", "blue", "green", "red", "purple"]]
[:a {:key it :data-color it :data-action it} it])] [:a {:key it :data-color it :data-action it} it])]
(and id [:li.item {:data-action "ref"} "Copy ref"])
[:li.item {:data-action "copy"} "Copy text"] [:li.item {:data-action "copy"} "Copy text"]
(and id [:li.item {:data-action "del"} "Delete"]) (and id [:li.item {:data-action "del"} "Delete"])

View File

@@ -619,7 +619,7 @@
(state/set-editor-op! nil))) (state/set-editor-op! nil)))
(defn api-insert-new-block! (defn api-insert-new-block!
[content {:keys [page block-uuid sibling? before? properties] [content {:keys [page block-uuid sibling? before? properties custom-uuid]
:or {sibling? false :or {sibling? false
before? false}}] before? false}}]
(when (or page block-uuid) (when (or page block-uuid)
@@ -648,7 +648,7 @@
(assoc :block/content content (assoc :block/content content
:block/format format) :block/format format)
(wrap-parse-block) (wrap-parse-block)
(assoc :block/uuid (db/new-block-id))) (assoc :block/uuid (or custom-uuid (db/new-block-id))))
new-block (if (:block/page new-block) new-block (if (:block/page new-block)
(assoc new-block :block/page (:db/id (:block/page new-block))) (assoc new-block :block/page (:db/id (:block/page new-block)))
(assoc new-block :block/page (:db/id block))) (assoc new-block :block/page (:db/id block)))