Skip to content

Support cljs #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 63 additions & 57 deletions src/nextjournal/clerk_table_stats.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,12 @@

(defmethod col-filter-fn :regexp [[_ s]]
(when (and (not (nil? s)) (not= "" s))
(try
(let [re (java.util.regex.Pattern/compile s)]
(fn [value]
(re-find re (str value))))
(catch java.util.regex.PatternSyntaxException e
#_(println (.getMessage e))))))
(when-let [re #?(:clj (try (java.util.regex.Pattern/compile s)
(catch java.util.regex.PatternSyntaxException e
(println (.getMessage e))))
:cljs (js/RegExp. s))]
(fn [value]
(re-find re (str value))))))

(defmethod col-filter-fn :ranges [[_ ranges]]
(when-not (empty? ranges)
Expand Down Expand Up @@ -343,50 +343,44 @@
"(?<fullvalue>" value-qq "|" value-q "|" value ")"
")?"))))

(comment
(doseq [modifier ["" "+" "-"]
key ["key" "'a key'" "'a:key'" "\"a key\"" "\"a:key\""]
delim [nil ":"]
value (if delim
[nil "value" "a:value" "'a value'" "'a:value'" "\"a value\"" "\"a:value\""]
[nil])
:let [s (str modifier key delim value)]]
(prn s #_(match-all filter-regexp s) (parse-query s))))

(defn match-all [re s]
(let [m ^java.util.regex.Matcher (re-matcher re s)]
(loop [acc (transient [])]
(if (.find m)
(recur (conj! acc
{:groups (mapv #(.group m %) (range (.groupCount m)))
:named (persistent!
(reduce-kv
(fn [acc k i]
(assoc! acc (keyword k) (.group m ^int i)))
(transient {})
(.namedGroups m)))
:start (.start m)
:end (.end m)}))
(persistent! acc)))))
#? (:clj (let [m ^java.util.regex.Matcher (re-matcher re s)]
(loop [acc (transient [])]
(if (.find m)
(recur (conj! acc
{:groups (mapv #(.group m %) (range (.groupCount m)))
:named (persistent!
(reduce-kv
(fn [acc k i]
(assoc! acc (keyword k) (.group m ^int i)))
(transient {})
(.namedGroups m)))
:start (.start m)
:end (.end m)}))
(persistent! acc))))
:cljs (.matchAll s re)))

(defn parse-query [s]
(vec
(for [match (match-all filter-regexp s)
:let [groups (:named match)
start (:start match)
full (-> match :groups first)
modifier (-> groups :modifier not-blank)
key (or
(-> groups :keyqq not-blank)
(-> groups :keyq not-blank)
(-> groups :key not-blank))
fullkey (-> groups :fullkey not-blank)
delimeter (-> groups :delimeter not-blank)
value (or
(-> groups :valueqq not-blank)
(-> groups :valueq not-blank)
(-> groups :value not-blank))
fullvalue (-> groups :fullvalue not-blank)]]
:let [groups #?(:clj (:named match) :cljs (.-groups match))
start #?(:clj (:start match) :cljs (.-index match))
full #?(:clj (-> match :groups first) :cljs (aget match 0))
modifier #?(:clj (-> groups :modifier not-blank) :cljs (-> groups .-modifier not-blank))
key #?(:clj (or
(-> groups :keyqq not-blank)
(-> groups :keyq not-blank)
(-> groups :key not-blank))
:cljs (-> groups .-key not-blank))
fullkey #?(:clj (-> groups :fullkey not-blank) :cljs nil);;TODO
delimeter #?(:clj (-> groups :delimeter not-blank) :cljs (-> groups .-fullkey not-blank))
value #?(:clj (or
(-> groups :valueqq not-blank)
(-> groups :valueq not-blank)
(-> groups :value not-blank))
:cljs (-> groups .-value not-blank))
fullvalue #?(:clj (-> groups :fullvalue not-blank) :cljs nil) ;;TODO
]]
{:start-idx start
:modifier modifier
:key-quote (when fullkey
Expand All @@ -402,24 +396,34 @@
:value value
:end-idx (+ start (count full))})))

(comment
(doseq [modifier ["" "+" "-"]
key ["key" "'a key'" "'a:key'" "\"a key\"" "\"a:key\""]
delim [nil ":"]
value (if delim
[nil "value" "a:value" "'a value'" "'a:value'" "\"a value\"" "\"a:value\""]
[nil])
:let [s (str modifier key delim value)]]
(prn s #_(match-all filter-regexp s) (parse-query s))))

(defn filter-by-query [{:as data
:keys [rows visible-paths]} query]
(let [names (into {}
(for [[path idx] (map vector visible-paths (range))]
[(str/join "/" (map name path)) idx]))
[(str/join "/" (map name path)) idx]))
filters (for [{:keys [modifier key value]} (parse-query query)
:let [idx (names key)]
:when idx]
(cond
(and (= "+" modifier) (nil? value))
#(some? (nth % idx))

(and (= "-" modifier) (nil? value))
#(nil? (nth % idx))

(and (= "-" modifier) value)
#(not (str/index-of (str (nth % idx)) value))

value
#(str/index-of (str (nth % idx)) value)))
filters (remove nil? filters)]
Expand All @@ -443,22 +447,24 @@
active-filters (compute-filters-data opts)
true (compute-autocomplete-data opts)
true (update :rows #(filter row-filter-fn %))
search-query (filter-by-query search-query)))))
search-query #?(:clj (filter-by-query search-query)
:cljs identity)))))

(def table-markup-viewer
{:require-cljs true
:render-fn `render/table-markup-viewer})
(assoc viewer/table-markup-viewer
:render-fn `render/render-table-markup))

(def table-head-viewer
{:require-cljs true
:render-fn `render/table-head-viewer})
(assoc viewer/table-head-viewer
:render-fn `render/render-table-head))

(def table-body-viewer
{:render-fn '(fn [rows opts] (into [:tbody] (map-indexed (fn [idx row] (nextjournal.clerk.render/inspect-presented (update opts :path conj idx) row))) rows))})
(assoc viewer/table-body-viewer
:render-fn '(fn [rows opts] (into [:tbody] (map-indexed (fn [idx row] (nextjournal.clerk.render/inspect-presented (update opts :path conj idx) row))) rows))))

(def table-row-viewer
{:require-cljs true
:render-fn `render/table-row-viewer})
(assoc viewer/table-row-viewer
:render-fn `render/render-table-row))

(defn tabular? [xs]
(and (seqable? xs)
Expand Down
11 changes: 4 additions & 7 deletions src/nextjournal/clerk_table_stats/render.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@
[icon-checkmark])]
name])]]])))

(defn table-head-viewer
(defn render-table-head
[header-row {:as opts :keys [table-state]}]
(let [cells* (viewer/desc->values header-row)
cells (mapcat #(if (vector? %)
Expand Down Expand Up @@ -538,7 +538,7 @@
:idx idx}])])])
sub-headers)))]))

(defn table-row-viewer
(defn render-table-row
[row {:as opts :keys [path number-col?]}]
(into [:tr.print:border-b-gray-500.hover:bg-slate-200.print:hover:bg-transparent.group
{:class (str "print:border-b-[1px] "
Expand Down Expand Up @@ -834,11 +834,8 @@
label])]])
js/document.body))])))

(defn table-markup-viewer [head+body {:as opts :keys [sync-var]}]
(r/with-let [table-state (if sync-var
(deref sync-var)
#?(:clj (throw (js/Error. (str "no sync var: " sync-var)))
:cljs nil))]
(defn render-table-markup [head+body {:as opts :keys [sync-var]}]
(r/with-let [table-state (when sync-var (deref sync-var))]
[:div
[table-search head+body table-state opts]
[:div.bg-white.rounded.border.border-slate-300.shadow-sm.font-sans.text-sm.not-prose.overflow-x-auto
Expand Down