浏览代码

improve(pdf): support block ref with highlight

charlie 4 年之前
父节点
当前提交
980741ef97

文件差异内容过多而无法显示
+ 0 - 0
public/index.html


+ 1 - 1
src/main/frontend/db/model.cljs

@@ -719,7 +719,7 @@
   [page-name]
   (if (util/uuid-string? 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?
   [block]

+ 40 - 0
src/main/frontend/extensions/pdf/assets.cljs

@@ -3,6 +3,7 @@
             [frontend.util :as utils]
             [frontend.db.model :as db-model]
             [frontend.db.utils :as db-utils]
+            [frontend.handler.page :as page-handler]
             [frontend.handler.editor :as editor-handler]
             [frontend.state :as state]
             [frontend.config :as config]
@@ -48,6 +49,45 @@
           data (pr-str {:highlights highlights})]
       (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!
   [page-block files refresh-file!]
   (let [repo (state/get-current-repo)

+ 15 - 6
src/main/frontend/extensions/pdf/highlights.cljs

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

+ 2 - 2
src/main/frontend/handler/editor.cljs

@@ -619,7 +619,7 @@
    (state/set-editor-op! nil)))
 
 (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
                  before? false}}]
   (when (or page block-uuid)
@@ -648,7 +648,7 @@
                             (assoc :block/content content
                                    :block/format format)
                             (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)
                           (assoc new-block :block/page (:db/id (:block/page new-block)))
                           (assoc new-block :block/page (:db/id block)))

部分文件因为文件数量过多而无法显示