Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
tangjeff0 committed Apr 2, 2021
1 parent b02db91 commit 4361c55
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
29 changes: 16 additions & 13 deletions src/cljs/athens/listeners.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[athens.util :as util]
[cljsjs.react]
[cljsjs.react.dom]
[clojure.string :as str]
[clojure.string :as string]
[goog.events :as events]
[re-frame.core :refer [dispatch subscribe]])
(:import
Expand Down Expand Up @@ -115,14 +115,16 @@
"https://github.com/ryanguill/roam-tools/blob/eda72040622555b52e40f7a28a14744bce0496e5/src/index.js#L336-L345"
[s]
(-> s
(str/replace #"\[([^\[\]]+)\]\((\[\[|\(\()([^\[\]]+)(\]\]|\)\))\)" "$1")
(str/replace #"\[\[([^\[\]]+)\]\]" "$1")))
(string/replace #"\[([^\[\]]+)\]\((\[\[|\(\()([^\[\]]+)(\]\]|\)\))\)" "$1")
(string/replace #"\[\[([^\[\]]+)\]\]" "$1")))


(defn block-refs-to-plain-text
"If there is a valid ((uid)), find the original block's string.
If invalid ((uid)), no-op.
TODO: If deep block ref, convert deep block ref to plain-text."
TODO: If deep block ref, convert deep block ref to plain-text.
Want to put this in athens.util, but circular dependency from athens.db"
[s]
(let [replacements (->> s
(re-seq #"\(\(([^\(\)]+)\)\)")
Expand All @@ -138,27 +140,28 @@
(if (empty? replacements)
s
(recur (rest replacements)
(str/replace s orig-str replace-str)))))))
(clojure.string/replace s orig-str replace-str)))))))


(defn walk-str
(defn blocks-to-clipboard-data
"Four spaces per depth level."
([depth node]
(walk-str depth node false))
(blocks-to-clipboard-data depth node false))
([depth node unformat?]
(let [{:block/keys [string children header]} node
left-offset (apply str (repeat depth " "))
walk-children (apply str (map #(walk-str (inc depth) % unformat?) children))
walk-children (apply str (map #(blocks-to-clipboard-data (inc depth) % unformat?) children))
string (let [header-to-str (case header
1 "# "
2 "## "
3 "### "
"")]
(str header-to-str string))
string (if unformat?
(-> string unformat-double-brackets block-refs-to-plain-text)
string)]
(str left-offset "- " string "\n" walk-children))))
(-> string unformat-double-brackets athens.listeners/block-refs-to-plain-text)
string)
dash (if unformat? "" "- ")]
(str left-offset dash string "\n" walk-children))))


(defn copy
Expand All @@ -168,7 +171,7 @@
(let [uids @(subscribe [:selected/items])]
(when (not-empty uids)
(let [copy-data (->> (map #(db/get-block-document [:block/uid %]) uids)
(map #(walk-str 0 %))
(map #(blocks-to-clipboard-data 0 %))
(apply str))]
(.. e preventDefault)
(.. e -event_ -clipboardData (setData "text/plain" copy-data))))))
Expand All @@ -180,7 +183,7 @@
(let [uids @(subscribe [:selected/items])]
(when (not-empty uids)
(let [copy-data (->> (map #(db/get-block-document [:block/uid %]) uids)
(map #(walk-str 0 %))
(map #(blocks-to-clipboard-data 0 %))
(apply str))]
(.. e preventDefault)
(.. e -event_ -clipboardData (setData "text/plain" copy-data))
Expand Down
8 changes: 5 additions & 3 deletions src/cljs/athens/views/blocks.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
auto-complete-inline
auto-complete-slash
textarea-key-down]]
[athens.listeners :as listeners]
[athens.parse-renderer :refer [parse-and-render]]
[athens.router :refer [navigate-uid]]
[athens.style :refer [color DEPTH-SHADOWS OPACITIES ZINDICES]]
Expand Down Expand Up @@ -717,14 +718,15 @@


(defn handle-copy-unformatted
"If copying only a single block, dissoc children to not copy subtree."
[^js e uid state]
(let [uids @(subscribe [:selected/items])]
(if (empty? uids)
(let [block (db/get-block-document [:block/uid uid])
data (athens.listeners/walk-str 0 block true)]
(let [block (dissoc (db/get-block-document [:block/uid uid]) :block/children)
data (listeners/blocks-to-clipboard-data 0 block true)]
(.. js/navigator -clipboard (writeText data)))
(let [data (->> (map #(db/get-block-document [:block/uid %]) uids)
(map #(athens.listeners/walk-str 0 % true))
(map #(listeners/blocks-to-clipboard-data 0 % true))
(apply str))]
(.. js/navigator -clipboard (writeText data)))))
(.. e preventDefault)
Expand Down

0 comments on commit 4361c55

Please sign in to comment.