From 1c8586e2ac454391d17eccbc029c6a224e272aa0 Mon Sep 17 00:00:00 2001 From: Gabriel Horner Date: Thu, 27 Jan 2022 14:13:42 -0500 Subject: [PATCH] Wrap up carving --- .carve/ignore | 58 ++----- src/main/frontend/state.cljs | 49 ------ src/main/frontend/storage.cljs | 16 -- src/main/frontend/text.cljs | 6 - src/main/frontend/tools/html_export.cljs | 49 ------ src/main/frontend/ui.cljs | 11 -- src/main/frontend/ui/date_picker.cljs | 8 +- src/main/frontend/utf8.cljs | 14 -- src/main/frontend/util.cljc | 204 ----------------------- src/main/frontend/util/cursor.cljs | 11 -- src/main/frontend/util/pool.cljs | 5 - src/main/frontend/util/property.cljs | 5 - 12 files changed, 11 insertions(+), 425 deletions(-) delete mode 100644 src/main/frontend/tools/html_export.cljs diff --git a/.carve/ignore b/.carve/ignore index 317041c57..27322f10d 100644 --- a/.carve/ignore +++ b/.carve/ignore @@ -86,61 +86,23 @@ frontend.modules.outliner.datascript/transact! frontend.page/route-view ;; placeholder fn frontend.publishing/stop -frontend.state/get-changed-files -frontend.state/online? -frontend.state/reset-published-pages +;; comment? frontend.state/set-db-persisted! -frontend.state/persist-transaction! -frontend.state/get-repo-latest-txs -frontend.state/get-editor-cp -frontend.state/get-favorites-name +;; repl fn frontend.state/remove-watch-state +;; Future use? frontend.state/get-visual-viewport-state -frontend.storage/get-json -frontend.storage/set-json -frontend.text/remove-lines-level-spaces -frontend.tools.html-export/export-page +;; Future use? frontend.ui/reset-ios-whole-page-offset! -frontend.ui/badge -frontend.ui/get-doc-scroll-top -frontend.ui/modal-show? -frontend.ui.date-picker/date-format -frontend.ui.date-picker/iso8601->date -frontend.utf8/delete! -frontend.util/indexed -frontend.util/dissoc-in -frontend.util/remove-nils-or-empty -frontend.util/index-by -frontend.util/patch -frontend.util/minimize-html -frontend.util/speed -frontend.util/moving-frequency -frontend.util/lock-global-scroll -frontend.util/scroll-to-element-v2 -frontend.util/scroll-to-bottom -frontend.util/drop-first-line -frontend.util/safe-split-first -frontend.util/trim-only-newlines -frontend.util/join-newline -frontend.util/replace-first-ignore-case -frontend.util/node-in-viewpoint? -frontend.util/input-selected? -frontend.util/extract-uuid -frontend.util/file-page? +;; For debugging frontend.util/d -frontend.util/get-prev-block-with-same-level -frontend.util/get-next-block-with-same-level -frontend.util/sort-by-value -frontend.util/encode-str -frontend.util/->system-modifier +;; Future use? frontend.util/safe-search-normalize -frontend.util/get-element-width +;; For debugging frontend.util/trace! -frontend.util/unquote-string-if-wrapped -frontend.util.cursor/end-of-line? -frontend.util.cursor/move-cursor-to-beginning -frontend.util.pool/terminate-parser-pool! -frontend.util.property/goto-properties-beginning +;; Repl fn +frontend.util.pool/terminate-pool! +;; Repl fn frontend.util.property/add-page-properties ;; Used by shadow frontend.worker.parser/init diff --git a/src/main/frontend/state.cljs b/src/main/frontend/state.cljs index 6f17bfced..2be2f202b 100644 --- a/src/main/frontend/state.cljs +++ b/src/main/frontend/state.cljs @@ -1230,10 +1230,6 @@ [repo changed-files] (set-state! [:repo/changed-files repo] changed-files)) -(defn get-changed-files - [] - (get-in @state [:repo/changed-files (get-current-repo)])) - (defn get-wide-mode? [] (:ui/wide-mode? @state)) @@ -1246,10 +1242,6 @@ [value] (set-state! :network/online? value)) -(defn online? - [] - (:network/online? @state)) - (defn get-commands [] (:commands (get-config))) @@ -1309,15 +1301,6 @@ ;; THINK: new block, indent/outdent, drag && drop, etc. (set-editor-last-input-time! repo time)) -(defn set-published-pages - [pages] - (when-let [repo (get-current-repo)] - (set-state! [:me :published-pages repo] pages))) - -(defn reset-published-pages - [] - (set-published-pages [])) - (defn set-db-persisted! [repo value] (swap! state assoc-in [:db/persisted? repo] value)) @@ -1343,30 +1326,6 @@ [_repo files? id] (swap! state assoc-in [:db/last-persist-transact-ids :repo files?] id)) -(defn get-last-persist-transact-id - [_repo files?] - (get-in @state [:db/last-persist-transact-ids :repo files?])) - -(defn persist-transaction! - [repo files? tx-id tx-data] - (when (seq tx-data) - (let [latest-txs (:db/latest-txs @state) - last-persist-tx-id (get-last-persist-transact-id repo files?) - latest-txs (if last-persist-tx-id - (update-in latest-txs [repo files?] - (fn [result] - (remove (fn [tx] (<= (:tx-id tx) last-persist-tx-id)) result))) - latest-txs) - new-txs (update-in latest-txs [repo files?] (fn [result] - (vec (conj result {:tx-id tx-id - :tx-data tx-data}))))] - (storage/set-transit! :db/latest-txs new-txs) - (set-state! :db/latest-txs new-txs)))) - -(defn get-repo-latest-txs - [repo file?] - (get-in (:db/latest-txs @state) [repo file?])) - (defn set-nfs-refreshing! [value] (set-state! :nfs/refreshing? value)) @@ -1542,10 +1501,6 @@ [value] (set-state! [:view/components :editor] value)) -(defn get-editor-cp - [] - (get-in @state [:view/components :editor])) - (defn exit-editing-and-set-selected-blocks! ([blocks] (exit-editing-and-set-selected-blocks! blocks :down)) @@ -1556,10 +1511,6 @@ (set-selection-blocks! blocks direction) (util/select-highlight! blocks))) -(defn get-favorites-name - [] - (or (:name/favorites (get-config)) "Favorites")) - (defn add-watch-state [key f] (add-watch state key f)) diff --git a/src/main/frontend/storage.cljs b/src/main/frontend/storage.cljs index e65808bd9..bf2a9016d 100644 --- a/src/main/frontend/storage.cljs +++ b/src/main/frontend/storage.cljs @@ -20,22 +20,6 @@ (when-not util/node-test? (dt/read-transit-str ^js (.getItem js/localStorage (name key))))) -(defn set-transit! - [key value] - (when-not util/node-test? - (.setItem ^js js/localStorage (name key) (dt/write-transit-str value)))) - -(defn get-json - [key] - (when-not util/node-test? - (when-let [value (.getItem js/localStorage (name key))] - (js/JSON.parse value)))) - -(defn set-json - [key value] - (when-not util/node-test? - (.setItem ^js js/localStorage (name key) (js/JSON.stringify value)))) - (defn remove [key] (when-not util/node-test? diff --git a/src/main/frontend/text.cljs b/src/main/frontend/text.cljs index 3f09e5b55..428b5e9b6 100644 --- a/src/main/frontend/text.cljs +++ b/src/main/frontend/text.cljs @@ -221,12 +221,6 @@ :else (remove-level-space-aux! text (config/get-block-pattern format) space? trim-left?))))) -(defn remove-lines-level-spaces - [text format] - (->> (string/split-lines text) - (map #(remove-level-spaces (string/triml %) format true false)) - (string/join "\n"))) - (defn build-data-value [col] (let [items (map (fn [item] (str "\"" item "\"")) col)] diff --git a/src/main/frontend/tools/html_export.cljs b/src/main/frontend/tools/html_export.cljs deleted file mode 100644 index 92e015022..000000000 --- a/src/main/frontend/tools/html_export.cljs +++ /dev/null @@ -1,49 +0,0 @@ -(ns frontend.tools.html-export - (:require-macros [hiccups.core :as hiccups :refer [html]]) - (:require [clojure.set :as set] - [clojure.walk :as walk] - [frontend.components.block :as block] - [frontend.db :as db] - [medley.core :as medley] - [frontend.format.block :as format-block])) - -;; Consider generate a db index so that search can still works - -;; Or maybe TiddlyWiki - -;; It could be better that we can reuse some parts of this module in a nodejs tool, -;; so users don't have to use the web for exporting to htmls or publishing. - -(defn- build-block - [config block] - (let [block (format-block/parse-title-and-body block) - body (:block/body block) - block (block/build-block-title config block)] - [:div.block - block - (when (seq body) - (for [child body] - (block/markup-element-cp config child)))])) - -(defn export-page - [page-name blocks show-notification!] - (let [{:keys [slide]} (db/get-page-properties page-name) - slide? slide - blocks (if (:block/pre-block? (first blocks)) - (rest blocks) - blocks)] - (if (seq blocks) - (let [config {:html-export? true :slide? slide?} - hiccup [:div.page - (for [block blocks] - (build-block config block))] - remove-attrs #{:on-click :on-change} - hiccup (walk/postwalk (fn [f] - (if (and (map? f) - (seq (set/intersection remove-attrs (set (keys f))))) - - (medley/remove-keys remove-attrs f) - f)) - hiccup)] - (html hiccup)) - (show-notification! "The published content can't be empty." :error)))) diff --git a/src/main/frontend/ui.cljs b/src/main/frontend/ui.cljs index 7186ef7d1..59d31bfc8 100644 --- a/src/main/frontend/ui.cljs +++ b/src/main/frontend/ui.cljs @@ -249,16 +249,6 @@ [:input.form-checkbox.h-4.w-4.transition.duration-150.ease-in-out (merge {:type "checkbox"} option)]) -(defn badge - [text option] - [:span.inline-flex.items-center.px-2.5.py-0.5.rounded-full.text-xs.font-medium.leading-4.bg-purple-100.text-purple-800 - option - text]) - -;; scroll -(defn get-doc-scroll-top [] - (.-scrollTop js/document.documentElement)) - (defn main-node [] (gdom/getElement "main-content-container")) @@ -493,7 +483,6 @@ binding (or custom-binding default-binding)] (shortcut-helper/decorate-binding binding))) -(defonce modal-show? (atom false)) (rum/defc modal-overlay [state close-fn] [:div.ui__modal-overlay diff --git a/src/main/frontend/ui/date_picker.cljs b/src/main/frontend/ui/date_picker.cljs index b82a335db..c959274d2 100644 --- a/src/main/frontend/ui/date_picker.cljs +++ b/src/main/frontend/ui/date_picker.cljs @@ -1,6 +1,6 @@ (ns frontend.ui.date-picker (:require [cljs-time.core :refer [after? before? day day-of-week days first-day-of-the-month minus month months plus year]] - [cljs-time.format :refer [formatter formatters parse unparse]] + [cljs-time.format :refer [formatter unparse]] [frontend.modules.shortcut.core :as shortcut] [frontend.state :as state] [frontend.util :as util :refer [deref-or-value now->utc]] @@ -18,12 +18,6 @@ (def ^:const week-format (formatter "ww")) -(def ^:const date-format (formatter "yyyy MMM dd")) - -(defn iso8601->date [iso8601] - (when (seq iso8601) - (parse (formatters :basic-date) iso8601))) - (defn- month-label [date] (unparse month-format date)) (defn- dec-month [date] (minus date (months 1))) diff --git a/src/main/frontend/utf8.cljs b/src/main/frontend/utf8.cljs index d41db036e..200e65a8e 100644 --- a/src/main/frontend/utf8.cljs +++ b/src/main/frontend/utf8.cljs @@ -26,17 +26,3 @@ (defn length [arr] (gobj/get arr "length")) - -;; start-pos inclusive -;; end-pos exclusive -(defn insert! - [s start-pos end-pos content] - (let [arr (encode s) - end-pos (or end-pos (length arr))] - (str (substring arr 0 start-pos) - content - (substring arr end-pos)))) - -(defn delete! - [s start-pos end-pos] - (insert! s start-pos end-pos "")) diff --git a/src/main/frontend/util.cljc b/src/main/frontend/util.cljc index ee88f0cf8..694d7bc94 100644 --- a/src/main/frontend/util.cljc +++ b/src/main/frontend/util.cljc @@ -142,28 +142,10 @@ (p/do! (.setStyle StatusBar (clj->js {:style (.-Dark Style)}))))) -(defn indexed - [coll] - (map-indexed vector coll)) - (defn find-first [pred coll] (first (filter pred coll))) -(defn dissoc-in - "Dissociates an entry from a nested associative structure returning a new - nested structure. keys is a sequence of keys. Any empty maps that result - will not be present in the new structure." - [m [k & ks]] - (if ks - (if-let [nextmap (get m k)] - (let [newmap (dissoc-in nextmap ks)] - (if (seq newmap) - (assoc m k newmap) - (dissoc m k))) - m) - (dissoc m k))) - ;; (defn format ;; [fmt & args] ;; (apply gstring/format fmt args)) @@ -194,24 +176,6 @@ [nm] (into {} (remove (comp nil? second)) nm)) -(defn remove-nils-or-empty - [nm] - (walk/postwalk - (fn [el] - (if (map? el) - (not-empty (into {} (remove (comp #(or - (nil? %) - (and (coll? %) - (empty? %))) second)) el)) - el)) - nm)) - -(defn index-by - [col k] - (->> (map (fn [entry] [(get entry k) entry]) - col) - (into {}))) - (defn ext-of-image? [s] (some #(string/ends-with? s %) [".png" ".jpg" ".jpeg" ".bmp" ".gif" ".webp" ".svg"])) @@ -276,15 +240,6 @@ on-ok on-failed))) -#?(:cljs - (defn patch - [url body on-ok on-failed] - (fetch url {:method "patch" - :headers {:Content-Type "application/json"} - :body (js/JSON.stringify (clj->js body))} - on-ok - on-failed))) - #?(:cljs (defn delete [url on-ok on-failed] @@ -419,13 +374,6 @@ last-newline-pos (or (string/last-index-of value \newline (dec pos)) -1)] (- pos last-newline-pos 1))) -(defn minimize-html - [s] - (->> s - (string/split-lines) - (map string/trim) - (string/join ""))) - #?(:cljs (defn stop [e] (when e (doto e (.preventDefault) (.stopPropagation))))) @@ -435,20 +383,10 @@ (when e (.stopPropagation e)))) -(def speed 500) -(def moving-frequency 15) - #?(:cljs (defn cur-doc-top [] (.. js/document -documentElement -scrollTop))) -#?(:cljs - (defn lock-global-scroll - ([] (lock-global-scroll true)) - ([v] (js-invoke (.-classList (app-scroll-container-node)) - (if v "add" "remove") - "locked-scroll")))) - #?(:cljs (defn element-top [elem top] (when elem @@ -471,15 +409,6 @@ (- top 80))) :behavior "smooth"})))))) -#?(:cljs - (defn scroll-to-element-v2 - [elem-id] - (when elem-id - (when-let [elem (gdom/getElement elem-id)] - (.scroll (app-scroll-container-node) - #js {:top (element-top elem 0) - :behavior "auto"}))))) - #?(:cljs (defn scroll-to ([pos] @@ -508,13 +437,6 @@ ([animate?] (scroll-to (app-scroll-container-node) 0 animate?)))) -#?(:cljs - (defn scroll-to-bottom - [node] - (when-let [node ^js (or node (app-scroll-container-node))] - (let [bottom (.-scrollHeight node)] - (scroll-to node bottom false))))) - #?(:cljs (defn url-encode [string] @@ -573,11 +495,6 @@ [s substr] (string/starts-with? s substr)) -(defn drop-first-line - [s] - (let [lines (string/split-lines s)] - [(first lines)])) - (defn distinct-by [f col] (reduce @@ -607,12 +524,6 @@ [repo-url] (take-last 2 (string/split repo-url #"/"))) -(defn safe-split-first [pattern s] - (if-let [first-index (string/index-of s pattern)] - [(subs s 0 first-index) - (subs s (+ first-index (count pattern)) (count s))] - [s ""])) - (defn safe-lower-case [s] (if (string? s) @@ -637,12 +548,6 @@ [s] (.replace s #"[ \t\r]+$" "")) -(defn trim-only-newlines - [s] - (-> s - (.replace #"[\n]+$" "") - (.replace #"^[\n]+" ""))) - (defn triml-without-newlines [s] (.replace s #"^[ \t\r]+" "")) @@ -659,16 +564,6 @@ (when-not not-space? " ") (triml-without-newlines right))))) -#?(:cljs - (defn join-newline - [& col] - (let [col (remove nil? col)] - (reduce (fn [acc s] - (if (or (= acc "") (= "\n" (last acc))) - (str acc s) - (str acc "\n" - (.replace s #"^[\n]+" "")))) "" col)))) - ;; Add documentation (defn replace-first [pattern s new-value] (if-let [first-index (string/index-of s pattern)] @@ -698,16 +593,6 @@ old-value)] (string/replace s (re-pattern (str "(?i)" old-value)) new-value))) -(defn replace-first-ignore-case - [s old-value new-value & [escape-chars]] - (let [escape-chars (or escape-chars default-escape-chars) - old-value (if (string? escape-chars) - (reduce (fn [acc escape-char] - (string/replace acc escape-char (str "\\" escape-char))) - old-value escape-chars) - old-value)] - (string/replace-first s (re-pattern (str "(?i)" old-value)) new-value))) - ;; copy from https://stackoverflow.com/questions/18735665/how-can-i-get-the-positions-of-regex-matches-in-clojurescript #?(:cljs (defn re-pos [re s] @@ -873,16 +758,6 @@ (and node (rec-get-blocks-content-section (gobj/get node "parentNode")))))) -#?(:cljs - (defn node-in-viewpoint? - [node] - (let [rect (.getBoundingClientRect node) - height (or (.-innerHeight js/window) - (.. js/document -documentElement -clientHeight))] - (and - (> (.-top rect) (.-clientHeight (d/by-id "head"))) - (<= (.-bottom rect) height))))) - #?(:cljs (defn get-blocks-noncollapse [] (->> (d/by-class "ls-block") @@ -916,12 +791,6 @@ (catch js/Error _e nil)))) -#?(:cljs - (defn input-selected? - [input] - (not= (get-selection-start input) - (get-selection-end input)))) - #?(:cljs (defn get-selected-text [] @@ -942,10 +811,6 @@ [s] (safe-re-find exactly-uuid-pattern s)) -(defn extract-uuid - [s] - (safe-re-find (re-pattern uuid-pattern) s)) - (defn drop-nth [n coll] (keep-indexed #(when (not= %1 n) %2) coll)) @@ -954,10 +819,6 @@ (map string/capitalize) (string/join " "))) -(defn file-page? - [page-name] - (when page-name (safe-re-find #"\." page-name))) - #?(:cljs (defn react [ref] @@ -992,40 +853,6 @@ [title] (set! (.-title js/document) title))) -#?(:cljs - (defn get-prev-block-with-same-level - [block] - (let [id (gobj/get block "id") - prefix (safe-re-find #"ls-block-[\d]+" id)] - (when-let [blocks (d/by-class "ls-block")] - (when-let [index (.indexOf blocks block)] - (let [level (d/attr block "level")] - (when (> index 0) - (loop [idx (dec index)] - (if (>= idx 0) - (let [block (nth blocks idx) - prefix-match? (starts-with? (gobj/get block "id") prefix)] - (if (and prefix-match? - (= level (d/attr block "level"))) - block - (recur (dec idx)))) - nil))))))))) - -#?(:cljs - (defn get-next-block-with-same-level - [block] - (when-let [blocks (d/by-class "ls-block")] - (when-let [index (.indexOf blocks block)] - (let [level (d/attr block "level")] - (when (> (count blocks) (inc index)) - (loop [idx (inc index)] - (if (< idx (count blocks)) - (let [block (nth blocks idx)] - (if (= level (d/attr block "level")) - block - (recur (inc idx)))) - nil)))))))) - #?(:cljs (defn get-block-container [block-element] @@ -1077,17 +904,6 @@ (recur (inc idx)) block)))))))) -(defn sort-by-value - [order m] - (into (sorted-map-by - (fn [k1 k2] - (let [v1 (get m k1) - v2 (get m k2)] - (if (= order :desc) - (compare [v2 k2] [v1 k1]) - (compare [v1 k1] [v2 k2]))))) - m)) - (defn rand-str [n] #?(:cljs (-> (.toString (js/Math.random) 36) @@ -1105,13 +921,6 @@ (when (string? tag-name) (not (safe-re-find #"[# \t\r\n]+" tag-name)))) -#?(:cljs - (defn encode-str - [s] - (if (tag-valid? s) - s - (url-encode s)))) - (defn pp-str [x] #_:clj-kondo/ignore (with-out-str (clojure.pprint/pprint x))) @@ -1159,14 +968,6 @@ (catch js/Error _ (utils/win32 path))))) -(defn ->system-modifier - [keyboard-shortcut] - (if mac? - (-> keyboard-shortcut - (string/replace "ctrl" "meta") - (string/replace "alt" "meta")) - keyboard-shortcut)) - (defn default-content-with-title [text-format] (case (name text-format) @@ -1391,11 +1192,6 @@ [] (contains? (set (system-locales)) "zh-CN"))) -#?(:cljs - (defn get-element-width - [id] - (when-let [element (gdom/getElement id)] - (gobj/get element "offsetWidth")))) (comment (= (get-relative-path "journals/2020_11_18.org" "pages/grant_ideas.org") "../pages/grant_ideas.org") diff --git a/src/main/frontend/util/cursor.cljs b/src/main/frontend/util/cursor.cljs index 739b3fb51..798d29855 100644 --- a/src/main/frontend/util/cursor.cljs +++ b/src/main/frontend/util/cursor.cljs @@ -115,13 +115,6 @@ (when-let [pre-char (subs content (dec pos) pos)] (= pre-char \newline))))) -(defn end-of-line? - [input] - (let [[content pos] (get-input-content&pos input)] - (or (= pos (count content)) - (when-let [next-char (subs content pos (inc pos))] - (= next-char \newline))))) - (defn move-cursor-to-line-end [input] (move-cursor-to input (line-end-pos input))) @@ -130,10 +123,6 @@ [input] (move-cursor-to input (line-beginning-pos input))) -(defn move-cursor-to-beginning - [input] - (move-cursor-to input 0)) - (defn move-cursor-to-end [input] (let [pos (count (gobj/get input "value"))] diff --git a/src/main/frontend/util/pool.cljs b/src/main/frontend/util/pool.cljs index f2b50bac1..039fe2077 100644 --- a/src/main/frontend/util/pool.cljs +++ b/src/main/frontend/util/pool.cljs @@ -51,11 +51,6 @@ (p/let [_ (.completed pool)] (.terminate pool))) -(defn terminate-parser-pool! - [] - (when-let [pool @parser-pool] - (terminate-pool! pool))) - (defn add-parse-job! [content config] (when-let [pool @parser-pool] diff --git a/src/main/frontend/util/property.cljs b/src/main/frontend/util/property.cljs index 23478288b..132adb999 100644 --- a/src/main/frontend/util/property.cljs +++ b/src/main/frontend/util/property.cljs @@ -122,11 +122,6 @@ (let [key (string/upper-case key)] (contains? (set (util/remove-first #{key} (get-property-keys format content))) key))) -(defn goto-properties-beginning - [_format input] - (cursor/move-cursor-to-thing input properties-start 0) - (cursor/move-cursor-forward input (count properties-start))) - (defn goto-properties-end [_format input] (cursor/move-cursor-to-thing input properties-start 0)