Skip to content

Commit

Permalink
[wip]
Browse files Browse the repository at this point in the history
  • Loading branch information
kimo-k committed Oct 31, 2024
1 parent 3f9638e commit ceb8495
Show file tree
Hide file tree
Showing 3 changed files with 238 additions and 82 deletions.
76 changes: 28 additions & 48 deletions src/re_com/nested_grid.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
(:require
[clojure.string :as str]
[re-com.util :as u :refer [px deref-or-value]]
[re-com.nested-grid.util :as ngu]
[reagent.core :as r]
[re-com.debug :as debug]
[re-com.config :as config :refer [include-args-desc?]]
Expand Down Expand Up @@ -492,27 +493,6 @@
0
"-4px")}))}])))

(defn path->grid-line-name [path]
(str "line__" (hash path) "-start"))

(defn grid-template
([tokens & more-tokens]
(grid-template (apply concat tokens more-tokens)))
([tokens]
(let [rf (fn [s group]
(str s " "
(cond (number? (first group))
(str/join " " (map px group))
(string? (first group))
(str/join " " group)
:else
(str "[" (str/join " " (map path->grid-line-name group)) "]"))))]
(str
(->> tokens
(partition-by (some-fn number? string?))
(reduce rf ""))
" [end]"))))

(defn cell [{:keys [value]}]
(str value))

Expand All @@ -521,8 +501,8 @@
[:div
(-> {:on-click (debug/log-on-alt-click props)
:class class
:style (merge {:grid-column (path->grid-line-name column-path)
:grid-row (path->grid-line-name row-path)}
:style (merge {:grid-column (ngu/path->grid-line-name column-path)
:grid-row (ngu/path->grid-line-name row-path)}
style)}
(merge attr))]
children))
Expand Down Expand Up @@ -898,8 +878,8 @@
:display :grid
:grid-column-start 2
:grid-row-start 2
:grid-template-columns (grid-template cell-grid-columns)
:grid-template-rows (grid-template cell-grid-rows)}})]
:grid-template-columns (ngu/grid-template cell-grid-columns)
:grid-template-rows (ngu/grid-template cell-grid-rows)}})]
column-header-cells (for [path column-paths
:let [edge (cond-> #{}
(start-branch? path column-paths) (conj :left)
Expand Down Expand Up @@ -928,7 +908,7 @@
(theme/apply props {:part ::column-header} theme)
:default re-com.nested-grid/column-header)]]]
^{:key [::column (or path (gensym))]}
[:div {:style {:grid-column-start (path->grid-line-name path)
[:div {:style {:grid-column-start (ngu/path->grid-line-name path)
:grid-column-end (str "span " (cond-> path
:do (header-cross-span column-paths)
(not show?) dec))
Expand Down Expand Up @@ -979,7 +959,7 @@
(theme/apply props {:part ::row-header} theme)
:default re-com.nested-grid/row-header)]]]
^{:key [::row (or path (gensym))]}
[:div {:style {:grid-row-start (path->grid-line-name path)
[:div {:style {:grid-row-start (ngu/path->grid-line-name path)
:grid-row-end (str "span " (cond-> path
:do (header-cross-span showing-row-paths)
(not show?) dec))
Expand Down Expand Up @@ -1027,8 +1007,8 @@
cells (if-not theme-cells?
(for [row-path showing-row-paths
column-path showing-column-paths]
[cell (cond-> {:style {:grid-column (path->grid-line-name column-path)
:grid-row (path->grid-line-name row-path)}
[cell (cond-> {:style {:grid-column (ngu/path->grid-line-name column-path)
:grid-row (ngu/path->grid-line-name row-path)}
:row-path row-path
:column-path column-path}
cell-value
Expand Down Expand Up @@ -1136,10 +1116,10 @@
(merge
{:position :relative
:display :grid
:grid-template-columns (grid-template [(px row-header-total-width)
(px column-header-total-width)])
:grid-template-rows (grid-template [(px column-header-total-height)
"1fr"])}
:grid-template-columns (ngu/grid-template [(px row-header-total-width)
(px column-header-total-width)])
:grid-template-rows (ngu/grid-template [(px column-header-total-height)
"1fr"])}
(when-not sticky?
{:max-width (or max-width (when remove-empty-column-space? native-width))
:max-height (or max-height
Expand All @@ -1155,8 +1135,8 @@
:grid-column-start 1
:grid-row-start 1
:z-index 3
:grid-template-columns (grid-template max-row-widths)
:grid-template-rows (grid-template max-column-heights)}})]
:grid-template-columns (ngu/grid-template max-row-widths)
:grid-template-rows (ngu/grid-template max-column-heights)}})]
header-spacer-cells)
column-headers (into [:div (themed ::column-header-grid-container
{:style {:position :sticky
Expand All @@ -1166,8 +1146,8 @@
:display :grid
:grid-column-start 2
:grid-row-start 1
:grid-template-columns (grid-template cell-grid-columns)
:grid-template-rows (grid-template max-column-heights)}})]
:grid-template-columns (ngu/grid-template cell-grid-columns)
:grid-template-rows (ngu/grid-template max-column-heights)}})]
column-header-cells)
row-headers (into [:div (themed ::row-header-grid-container
{:style {:position :sticky
Expand All @@ -1176,8 +1156,8 @@
:display :grid
:grid-column-start 1
:grid-row-start 2
:grid-template-columns (grid-template max-row-widths)
:grid-template-rows (grid-template cell-grid-rows)}})]
:grid-template-columns (ngu/grid-template max-row-widths)
:grid-template-rows (ngu/grid-template cell-grid-rows)}})]
row-header-cells)
cells (-> cell-grid-container
(into cells)
Expand All @@ -1187,15 +1167,15 @@
(conj (when show-selection-box? box-selector)))]
[:div (debug/->attr
(themed ::wrapper
{:src src
:style (merge {:flex-direction :column}
(when-not sticky?
(merge {:flex "0 0 auto"
:display :flex}
(when remove-empty-column-space?
{:max-width :fit-content})
(when remove-empty-row-space?
{:max-height :fit-content}))))}))
{:src src
:style (merge {:flex-direction :column}
(when-not sticky?
(merge {:flex "0 0 auto"
:display :flex}
(when remove-empty-column-space?
{:max-width :fit-content})
(when remove-empty-row-space?
{:max-height :fit-content}))))}))
(when show-export-button? control-panel)
(conj
outer-grid-container
Expand Down
131 changes: 112 additions & 19 deletions src/re_com/nested_grid/util.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,28 @@
#?@(:cljs [[reagent.core :as r]
[re-com.util :as u]])))

(defn path->grid-line-name [path]
(str "line__" (hash path) "-start"))

#?(:cljs
(defn grid-template
([tokens & more-tokens]
(grid-template (apply concat tokens more-tokens)))
([tokens]
(let [rf (fn [s group]
(str s " "
(cond (number? (first group))
(str/join " " (map u/px group))
(string? (first group))
(str/join " " group)
:else
(str "[" (str/join " " (map path->grid-line-name group)) "]"))))]
(str
(->> tokens
(partition-by (some-fn number? string?))
(reduce rf ""))
" [end]")))))

(def spec? (some-fn vector? seq?))
(def item? (complement spec?))
(def ascend pop)
Expand Down Expand Up @@ -67,49 +89,83 @@
(keyword? node) 20)))

(defn walk-size [{:keys [window-start window-end tree size-cache]}]
(println)
(let [sum-size (volatile! 0)
windowed-nodes (volatile! [])
collect! (fn [path] (vswap! windowed-nodes conj path))
cache! (fn [node size] (vswap! size-cache assoc node size))
intersection? (fn [[x1 x2]]
(and (<= x1 window-end)
(>= x2 window-start)))
level->space (volatile! {})
windowed-paths (volatile! [])
windowed-sizes (volatile! [])
windowed-sums (volatile! [])
collect-space! (fn [level space]
(vswap! level->space
(fn [m] (cond-> m
(not (get m level)) (assoc level space)))))
collect-size! (fn [size] (vswap! windowed-sizes conj size))
forget-size! #(vswap! windowed-sizes pop)
collect-sum! (fn [sum] (vswap! windowed-sums conj sum))
forget-sum! #(vswap! windowed-sums pop)
collect-path! (fn [size] (vswap! windowed-paths conj size))
forget-path! #(vswap! windowed-paths pop)
cache! (if-not size-cache
(constantly nil)
(fn [node size] (vswap! size-cache assoc node size)))
lookup (if-not size-cache
(constantly nil)
#(get @size-cache %))
intersection? (if-not (and window-start window-end)
(constantly true)
(fn [[x1 x2]]
(and (<= x1 window-end)
(>= x2 window-start))))
walk
(fn walk [path node & {:keys [collect-me?] :or {collect-me? true}}]
(cond
(leaf? node) (let [sum @sum-size
leaf-size (leaf-size node)
leaf-path (conj path node)
level (count leaf-path)
bounds [sum (+ sum leaf-size)]]
(when (and (intersection? bounds) collect-me?)
(collect! leaf-path))
(collect-path! leaf-path)
(collect-sum! sum)
(collect-size! leaf-size)
(collect-space! level sum))
(vreset! sum-size (+ sum leaf-size))
leaf-size)
(branch? node) (let [sum @sum-size
csize (get @size-cache node)
csize (lookup node)
cbounds (when csize [sum (+ sum csize)])
skippable? (and csize (not (intersection? cbounds)))]
(if skippable?
(let [new-sum (+ sum csize)]
(vreset! sum-size new-sum)
csize)
(let [own-path (conj path (first node))
own-size (walk path (own-leaf node) {:collect-me? false})
_ (when :always (collect! own-path))
descend-tx (map (partial walk own-path))
total-size (+ own-size
(transduce descend-tx + (children node)))
(let [own-path (conj path (own-leaf node))
level (count own-path)
own-size (walk path (own-leaf node) {:collect-me? false})
_ (collect-path! own-path)
_ (collect-size! own-size)
_ (collect-sum! sum)
descend-tx (map (partial walk own-path))
total-size (+ own-size
(transduce descend-tx + (children node)))
total-bounds [sum (+ sum total-size)]]
(when-not (intersection? total-bounds)
(vswap! windowed-nodes pop))
(if (intersection? total-bounds)
(collect-space! level sum)
(do
(forget-path!)
(forget-sum!)
(forget-size!)))
(when-not csize (cache! node total-size))
total-size)))))]
(walk [] tree)
{:sum-size @sum-size
:windowed-nodes @windowed-nodes}))
:level->space (into (sorted-map) @level->space)
:windowed-sums @windowed-sums
:windowed-paths @windowed-paths
:windowed-sizes @windowed-sizes
:window-start window-start
:window-end window-end}))

(def test-tree [:a
(def test-tree [:z
[:g
[:x 20]
[:y 40]
Expand All @@ -131,3 +187,40 @@
:window-end 472
:size-cache (volatile! {})
:tree test-tree})

(def td {:level->space {1 0, 2 180, 3 240, 4 260},
:sum-size 660,
:window-end 342,
:window-start 242,
:windowed-paths [[:z] [:z :h] [:z :h :y] [:z :h :y 40] [:z :h :z] [:z :h :z 20]
[:z :i]],
:windowed-sizes [20 20 20 40 20 20 20],
:windowed-sums [0 180 240 260 300 320 340]})

(defn lazy-grid-template
[{:keys [windowed-paths windowed-sizes windowed-sums sum-size]}]
(let [grid-line (fn [path] (str "[line__" (hash path) "-start]"))]
(str/join " "
(loop [[path & rest-paths] windowed-paths
[size & rest-sizes] windowed-sizes
[sum & [next-sum :as rest-sums]] (conj windowed-sums sum-size)
result []]
(let [spacer? (not= next-sum (+ sum size))
next-result (if spacer?
(conj result
(grid-line path)
(str size "px")
"[spacer]"
(str (- next-sum size sum) "px"))
(conj result
(grid-line path)
(str size "px")))]
(if (empty? rest-sizes)
(conj next-result "[end]")
(recur rest-paths rest-sizes rest-sums next-result)))))))






Loading

0 comments on commit ceb8495

Please sign in to comment.