Skip to content

Commit

Permalink
feat: create page ref after import zotero page
Browse files Browse the repository at this point in the history
  • Loading branch information
thezjy committed Jul 20, 2021
1 parent 65bd4d5 commit d97036b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 38 deletions.
3 changes: 2 additions & 1 deletion src/main/frontend/components/editor.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,8 @@

(when (state/sub :editor/show-zotero)
(transition-cp
(zotero/zotero-search)
(zotero/zotero-search (fn [page-name]
(editor-handler/handle-command-zotero id page-name format)))
false
*slash-caret-pos))

Expand Down
19 changes: 15 additions & 4 deletions src/main/frontend/extensions/zotero.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,33 @@
[clojure.string :as str]
[frontend.extensions.zotero.api :as api]
[frontend.extensions.zotero.handler :as zotero-handler]
[frontend.extensions.zotero.extractor :as extractor]
[frontend.state :as state]
[frontend.util :as util]
[rum.core :as rum]))

(rum/defc zotero-search-item [{:keys [data title] :as item}]

(rum/defc zotero-search-item [{:keys [data] :as item} handle-command-zotero]
(let [type (:item-type data)
title (:title data)
abstract (str (subs (:abstract-note data) 0 200) "...")]

(if (= type "journalArticle")
[:div.px-2.py-4.border-b.cursor-pointer.border-solid.hover:bg-gray-100.last:border-none
{:on-click (fn [] (go (<! (zotero-handler/create-zotero-page item))))}
{:on-click (fn [] (go (<! (zotero-handler/create-zotero-page item))
(let [{:keys [page-name]} (extractor/extract item)]
(handle-command-zotero page-name)

(state/sidebar-add-block!
(state/get-current-repo)
(:db/id page-name)
:page
{:page page-name}))))}
[[:div.font-bold.mb-1 title]
[:div.text-sm abstract]]]
nil)))

(rum/defc zotero-search []
(rum/defc zotero-search [handle-command-zotero]

(let [cache-api-key (js/localStorage.getItem "zotero-api-key")
cache-user-id (js/localStorage.getItem "zotero-user-id")
Expand Down Expand Up @@ -65,5 +76,5 @@

[:div
(map
(fn [item] (rum/with-key (zotero-search-item item) (:key item)))
(fn [item] (rum/with-key (zotero-search-item item handle-command-zotero) (:key item)))
search-result)]])))
9 changes: 4 additions & 5 deletions src/main/frontend/extensions/zotero/api.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

(def config {:api-version 3
:base "https://api.zotero.org"
:api-key "api_key"
:timeout 150000
:api-key "NlJI2bAuhYcQ4UgXSwHHsWRD"
:type :user
:type-id 8234867})
:type-id 8237615})

;; taken from https://github.com/metosin/metosin-common/blob/master/src/cljc/metosin/core/async/debounce.cljc
(defn debounce
Expand Down Expand Up @@ -77,11 +77,10 @@

(comment
(get* config "/collections")
(get* config "/items" )
(get* config "/items")
(get* config "/items" {:item-type "journalArticle"})
(item "JZCIN4K5")
(item "RFYNAQTN")
(item "3V6N8ECQ")
(notes "3V6N8ECQ")
(attachments "3V6N8ECQ")
)
(attachments "3V6N8ECQ"))
16 changes: 6 additions & 10 deletions src/main/frontend/extensions/zotero/handler.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@

(<! (add page-name :attachments key))

(<! (add page-name :notes key))

(js/alert "finish"))))
(<! (add page-name :notes key)))))

(comment
(create-zotero-page "JAHCZRNB")
Expand All @@ -71,8 +69,7 @@
;; => #:block{:properties {}, :tags [], :format :markdown, :children #{}, :path-refs (#:block{:name "notes", :original-name "notes", :journal? false}), :meta {:timestamps [], :properties [], :start-pos 0, :end-pos 11}, :unordered true, :content "[[notes]]", :refs (#:block{:name "notes", :original-name "notes", :journal? false}), :file 60, :page 58, :title [["Link" {:url ["Page_ref" "notes"], :label [["Plain" ""]], :full_text "[[notes]]", :metadata ""}]], :level 1, :journal? false, :anchor "", :uuid #uuid "60eee44e-7b9f-439d-8113-c8e5ab7a63e8", :body []}


#_
(item "3V6N8ECQ")
#_(item "3V6N8ECQ")
;; (db/get-page-blocks)
;; (page-handler/page-add-property! )
;; (editor-handler/insert-new-block-aux! )
Expand All @@ -82,8 +79,7 @@
;; (db/get-page-blocks "ccc")

;; (def block (state/get-edit-block))
#_
(editor-handler/save-block-if-changed!
block
(property-util/insert-property :markdown "xxx" :test "value")
{:force? true}))
#_(editor-handler/save-block-if-changed!
block
(property-util/insert-property :markdown "xxx" :test "value")
{:force? true}))
42 changes: 24 additions & 18 deletions src/main/frontend/handler/editor.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@
page-tx (let [id (:db/id (:block/page block))
retract-attributes (when id
(mapv (fn [attribute]
[:db/retract id attribute])
[:db/retract id attribute])
[:block/properties :block/tags :block/alias]))
tx (cond-> {:db/id id
:block/properties page-properties}
Expand Down Expand Up @@ -538,6 +538,7 @@
(defn clear-when-saved!
[]
(state/set-editor-show-input! nil)
(state/set-editor-show-zotero! false)
(state/set-editor-show-date-picker! false)
(state/set-editor-show-page-search! false)
(state/set-editor-show-block-search! false)
Expand Down Expand Up @@ -957,7 +958,7 @@
(if (string/starts-with? (string/lower-case line) key)
new-line
line))
lines)
lines)
new-lines (if (not= lines new-lines)
new-lines
(cons (first new-lines) ;; title
Expand Down Expand Up @@ -1039,8 +1040,8 @@
(let [blocks (db-utils/pull-many repo '[*] (mapv (fn [id] [:block/uuid id]) block-ids))
blocks* (flatten
(mapv (fn [b] (if (:collapsed (:block/properties b))
(vec (tree/sort-blocks (db/get-block-children repo (:block/uuid b)) b))
[b])) blocks))
(vec (tree/sort-blocks (db/get-block-children repo (:block/uuid b)) b))
[b])) blocks))
block-ids* (mapv :block/uuid blocks*)
unordered? (:block/unordered (first blocks*))
format (:block/format (first blocks*))
Expand Down Expand Up @@ -1727,7 +1728,7 @@
(seq blocks)
(do
(let [lookup-refs (->> (map (fn [block] (when-let [id (dom/attr block "blockid")]
[:block/uuid (medley/uuid id)])) blocks)
[:block/uuid (medley/uuid id)])) blocks)
(remove nil?))
blocks (db/pull-many repo '[*] lookup-refs)
blocks (reorder-blocks blocks)
Expand All @@ -1742,12 +1743,12 @@
(db/refresh! repo opts)
(let [blocks (doall
(map
(fn [block]
(when-let [id (gobj/get block "id")]
(when-let [block (gdom/getElement id)]
(dom/add-class! block "selected noselect")
block)))
blocks-dom-nodes))]
(fn [block]
(when-let [id (gobj/get block "id")]
(when-let [block (gdom/getElement id)]
(dom/add-class! block "selected noselect")
block)))
blocks-dom-nodes))]
(state/set-selection-blocks! blocks)))))))))

(defn- get-link
Expand All @@ -1759,6 +1760,11 @@
:org (util/format "[[%s][%s]]" link label)
nil)))

(defn handle-command-zotero
[id page-name format]
(state/set-editor-show-zotero! false)
(insert-command! id (str "[[" page-name "]]") format {}))

(defn handle-command-input
[command id format m pos]
(case command
Expand Down Expand Up @@ -2108,11 +2114,11 @@
tree (blocks-vec->tree result-blocks)]
(insert-command! id "" format {})
(let [last-block (paste-block-vec-tree-at-target tree [:template :template-including-parent]
(fn [content]
(->> content
(property/remove-property format "template")
(property/remove-property format "template-including-parent")
template/resolve-dynamic-template!)))]
(fn [content]
(->> content
(property/remove-property format "template")
(property/remove-property format "template-including-parent")
template/resolve-dynamic-template!)))]
(clear-when-saved!)
(db/refresh! repo {:key :block/insert :data [(db/pull db-id)]})
;; FIXME:
Expand Down Expand Up @@ -2637,8 +2643,8 @@
(let [min-level (apply min (mapv :block/level blocks))
prefix-level (if (> min-level 1) (- min-level 1) 0)]
(->> blocks
(mapv #(assoc % :level (- (:block/level %) prefix-level)))
(blocks-vec->tree))))
(mapv #(assoc % :level (- (:block/level %) prefix-level)))
(blocks-vec->tree))))

(defn- paste-text-parseable
[format text]
Expand Down

0 comments on commit d97036b

Please sign in to comment.