Allow removing and changing the amount of already added rects

This commit is contained in:
heyarne 2022-01-01 19:22:15 +01:00
commit c340684b33
2 changed files with 35 additions and 8 deletions

View file

@ -51,3 +51,12 @@ input[type=number] {
.rect-dim { .rect-dim {
width: 6em; width: 6em;
} }
.inventory li a.delete-items {
display: none;
text-decoration: none;
}
.inventory li:hover a.delete-items {
display: inline;
}

View file

@ -19,7 +19,6 @@
) )
(defn pack-rects-naive (defn pack-rects-naive
"Sorts all rectangles by height and places them next to each other into frame, "Sorts all rectangles by height and places them next to each other into frame,
starting at the top left." starting at the top left."
@ -63,6 +62,9 @@
rects (repeatedly (:num order) #(rect/rect size))] rects (repeatedly (:num order) #(rect/rect size))]
(swap! state update-in [:rects size] (comp vec concat) rects))) (swap! state update-in [:rects size] (comp vec concat) rects)))
(defn event-val->number [e]
(js/parseInt (.. e -target -value) 10))
(defn order [] (defn order []
(let [base-order {:num 1 (let [base-order {:num 1
:width 100 :width 100
@ -76,32 +78,48 @@
(add-parts-to-cut! @addition) (add-parts-to-cut! @addition)
(reset! addition base-order))} (reset! addition base-order))}
[:input.num-rects {:type "number" [:input.num-rects {:type "number"
:on-change #(swap! addition assoc :num (js/parseInt (.. % -target -value) 10)) :min 1
:on-input #(swap! addition assoc :num (event-val->number %))
:value (:num to-add)}] " × " :value (:num to-add)}] " × "
[:input.rect-dim {:type "number" [:input.rect-dim {:type "number"
:on-change #(swap! addition assoc :width (js/parseInt (.. % -target -value) 10)) :min 1
:on-input #(swap! addition assoc :width (event-val->number %))
:value (:width to-add)}] " cm by " :value (:width to-add)}] " cm by "
[:input.rect-dim {:type "number" [:input.rect-dim {:type "number"
:on-change #(swap! addition assoc :height (js/parseInt (.. % -target -value) 10)) :min 1
:on-input #(swap! addition assoc :height (event-val->number %))
:value (:height to-add)}] " cm " :value (:height to-add)}] " cm "
[:button "Add"]])))) [:button "Add"]]))))
#_(defn debug [val] #_(defn debug [val]
[:pre (with-out-str (pprint val))]) [:pre (with-out-str (pprint val))])
(defn update-rect-count! [size n]
(swap! state assoc-in [:rects size] (repeatedly n #(rect/rect size))))
(defn remove-rects-of-size! [size]
(swap! state update :rects dissoc size))
(defn inventory [rects] (defn inventory [rects]
[:<> [:<>
[order] [order]
(when (seq rects) (when (seq rects)
[:<> [:<>
[:p "List of parts to cut:"] [:p "List of parts to cut:"]
[:ul [:ul.inventory
(for [[size rects] rects (for [[size rects] rects
:let [n (count rects)]] :let [n (count rects)]]
^{:key size} ^{:key size}
[:li [:input.num-rects {:type "number" [:li
#_#_ :on-change #(update-rects! rects size (js/Number. (-> % .-target .-value))) [:input.num-rects {:type "number"
:value n}] (str " × " (:x size) " cm by " (:y size) " cm ")])]])]) :min 1
:on-input #(update-rect-count! size (event-val->number %))
:value n}] (str " × " (:x size) " cm by " (:y size) " cm ")
[:a.delete-items {:href "#"
:on-click (fn [ev]
(.preventDefault ev)
(remove-rects-of-size! size))
:title (str "Remove " n " items")} "❌"]])]])])
(defn visualization [frame packed-rects] (defn visualization [frame packed-rects]
[:svg.visualization {:viewBox "-0.5 -0.5 501 501" :xmlns "http://www.w3.org/2000/svg"} [:svg.visualization {:viewBox "-0.5 -0.5 501 501" :xmlns "http://www.w3.org/2000/svg"}