diff --git a/clips/templates/clips/index.html b/clips/templates/clips/index.html index d73c62e..a2d79b5 100644 --- a/clips/templates/clips/index.html +++ b/clips/templates/clips/index.html @@ -18,14 +18,31 @@ and PUTs with ordinary CSRF protection — no endpoint in this app is exempt. - + arthur + + + + + + {% csrf_token %}
+ diff --git a/clips/tests/test_api.py b/clips/tests/test_api.py index 5d2aa52..84fed0b 100644 --- a/clips/tests/test_api.py +++ b/clips/tests/test_api.py @@ -760,6 +760,17 @@ class PageTests(TestCase): # save path reads to write the X-CSRFToken header. self.assertIn("csrfmiddlewaretoken", body) + def test_the_service_worker_is_served_from_the_root(self): + # At the root so its scope is every page — a project is /p/ — and + # never cached, or a browser would keep an old worker for good. + response = self.client.get("/sw.js") + self.assertEqual(200, response.status_code) + self.assertEqual("application/javascript", response["Content-Type"]) + self.assertEqual("no-cache", response["Cache-Control"]) + body = self.client.get("/").content.decode() + self.assertIn("manifest.webmanifest", body) + self.assertIn("serviceWorker.register('/sw.js')", body) + def test_the_detector_reports_a_version_derived_from_the_model(self): # The version is the package version plus the model asset's own hash, # because a version string in the client is one somebody has to remember to diff --git a/clips/views.py b/clips/views.py index 1f6306e..ba12b18 100644 --- a/clips/views.py +++ b/clips/views.py @@ -37,7 +37,7 @@ from django.contrib.auth import login as auth_login, logout as auth_logout from django.core.exceptions import ValidationError from django.db import transaction from django.db.models import Q -from django.http import FileResponse, HttpResponse, JsonResponse +from django.http import FileResponse, Http404, HttpResponse, JsonResponse from django.shortcuts import render from django.views.decorators.http import require_http_methods @@ -146,6 +146,22 @@ def page(request, project_id=None, slug=None): }) +def service_worker(request): + """`static/arthur/sw.js`, at the root of the site. + + A service worker controls only the paths under where it is served from, and + a project is `/p/`, so `/static/arthur/sw.js` would control nothing that + matters. Never cached itself: a browser compares this file to decide whether + there is a new worker, so a stale copy would keep the old one for good.""" + for root in settings.STATICFILES_DIRS: + path = Path(root) / "arthur" / "sw.js" + if path.exists(): + response = HttpResponse(path.read_bytes(), content_type="application/javascript") + response["Cache-Control"] = "no-cache" + return response + raise Http404("no service worker") + + # --------------------------------------------------------------------------- # the detector # diff --git a/frontend/src/arthur/domain/clip.cljs b/frontend/src/arthur/domain/clip.cljs index 9bb0257..96eaa1f 100644 --- a/frontend/src/arthur/domain/clip.cljs +++ b/frontend/src/arthur/domain/clip.cljs @@ -66,10 +66,55 @@ [sym] (= :trace (:type sym))) -(defn symbol-name - "What to call a symbol: its `:name`, or its id when it has none." +(defonce ^:private placed-cache (atom nil)) + +(defn placements + "Where every symbol is placed: `{sid [[owner frame] ...]}`, earliest first, + where `owner` is the symbol whose node places it and `frame` is where in the + owner that node starts. + + Cached by IDENTITY of the document, as `raster` caches a palette: a document + is a value, so the same object means the same answer, and names and the pool + ask this once per symbol per render." + [clip] + (let [cached @placed-cache] + (if (identical? clip (:clip cached)) + (:index cached) + (let [index (-> (reduce (fn [m [owner sym]] + (reduce (fn [m [_ n]] + (if-let [sid (node/source n)] + (update m sid (fnil conj []) + [owner (:at (node/time-of n))]) + m)) + m (:nodes sym))) + {} (:symbols clip)) + (update-vals #(vec (sort-by second %))))] + (reset! placed-cache {:clip clip :index index}) + index)))) + +(defn owners + "The symbols that place `sid`, each once." [clip sid] - (or (:name (symbol clip sid)) (name sid))) + (into [] (comp (map first) (distinct)) (get (placements clip) sid))) + +(defn symbol-name + "What to call a symbol: its `:name`, or where it is when it has none. + + A DRAWING IS NAMED BY WHERE IT IS DRAWN. One placed by exactly one symbol + reads as that symbol's name and the frame it starts on — `body 12` — worked + out on every read, so renaming the lane renames its drawings and moving one + renames it. A symbol placed nowhere, or in two places, has no one place to be + named by and reads as its id." + ([clip sid] (symbol-name clip sid #{})) + ([clip sid seen] + (let [given (:name (symbol clip sid)) + [owner :as os] (owners clip sid)] + (cond + given given + (and (= 1 (count os)) (not (seen owner))) + (str (symbol-name clip owner (conj seen sid)) " " + (js/Math.floor (second (first (get (placements clip) sid))))) + :else (name sid))))) (defn node-label "What to call node `n` on screen. diff --git a/frontend/src/arthur/domain/nest.cljs b/frontend/src/arthur/domain/nest.cljs index cccfdcd..c6f084c 100644 --- a/frontend/src/arthur/domain/nest.cljs +++ b/frontend/src/arthur/domain/nest.cljs @@ -253,6 +253,23 @@ [clip sid id] (clip/update-symbol clip sid update :nodes #(apply dissoc % (subtree % id)))) +(defn delete-symbol + "Symbol `sid` gone from the library, with every node that places it — as + deleting a library item in Flash takes its instances with it. What it placed + stays, placed nowhere now, for whoever deletes it next. `{:clip}`, or + `{:refused why}` for the root, which is what the document opens on." + [clip sid] + (cond + (not (clip/symbol clip sid)) {:refused "there is no such symbol"} + (= sid (clip/opens-on clip)) {:refused "the project opens on this symbol; it cannot be deleted"} + :else + {:clip (reduce (fn [c [owner _]] + (reduce (fn [c [id n]] + (if (= sid (node/source n)) (delete-node c owner id) c)) + c (:nodes (clip/symbol c owner)))) + (update clip :symbols dissoc sid) + (:symbols clip))})) + (defn- transplant "Move node `id` from symbol `host`, where frame `frame` is showing, into symbol `target`, keeping where it is on screen and when. `carry` is the matrix from diff --git a/frontend/src/arthur/domain/span.cljs b/frontend/src/arthur/domain/span.cljs index 26d56f2..2863b6d 100644 --- a/frontend/src/arthur/domain/span.cljs +++ b/frontend/src/arthur/domain/span.cljs @@ -701,7 +701,7 @@ (when (clip/symbol clip drawing-id) "the new drawing ID is already used"))] {:refused why} (place (assoc-in clip [:symbols drawing-id] - {:id drawing-id :name (name drawing-id) :frames 1 :nodes {}}) + {:id drawing-id :frames 1 :nodes {}}) sid id drawing-id at extent true))) (defn reuse-drawing @@ -774,7 +774,7 @@ (clip/symbol clip drawing-id) "the new drawing ID is already used")] {:refused why} (let [c (assoc-in clip [:symbols drawing-id] - {:id drawing-id :name (name drawing-id) :frames 1 :nodes {}}) + {:id drawing-id :frames 1 :nodes {}}) nodes (assoc nodes id (held id drawing-id at))] (claim c sid nodes id extent remainder-id))))) @@ -810,10 +810,10 @@ :else (let [c (assoc-in clip [:symbols drawing-id] (if from - ;; Its own name: it is a new drawing, and two of one - ;; name in the library is a question nobody asked. - (assoc (clip/symbol clip from) :id drawing-id :name (name drawing-id)) - {:id drawing-id :name (name drawing-id) :frames 1 :nodes {}}))] + ;; No name: it is a new drawing, named by where it + ;; is drawn, and two of one name is a question nobody asked. + (-> (clip/symbol clip from) (assoc :id drawing-id) (dissoc :name)) + {:id drawing-id :frames 1 :nodes {}}))] (if (= f next-lo) ;; A cel starts right here, so there is no room: make some. (place c sid new-id drawing-id f extent true) diff --git a/frontend/src/arthur/events/project.cljs b/frontend/src/arthur/events/project.cljs index b9121ba..860fcc7 100644 --- a/frontend/src/arthur/events/project.cljs +++ b/frontend/src/arthur/events/project.cljs @@ -694,9 +694,9 @@ ;; a rename coalesce with whatever edit happened next. ;; ;; BLANK REMOVES THE NAME rather than storing an empty one. `clip/symbol-name` - ;; falls back to the id, so a symbol cleared of its name reads as `main` - ;; again instead of as a row with nothing on it — and the document carries no - ;; field it did not need. + ;; falls back to where the symbol is drawn, or its id, so a symbol cleared of + ;; its name reads as `body 12` or `main` again instead of as a row with + ;; nothing on it — and the document carries no field it did not need. (let [value (not-empty (str/trim (str value)))] (if-not (clip/symbol (:clip (store/entry (:clip/current db))) sid) db diff --git a/frontend/src/arthur/events/ui.cljs b/frontend/src/arthur/events/ui.cljs index 9815690..c7093a8 100644 --- a/frontend/src/arthur/events/ui.cljs +++ b/frontend/src/arthur/events/ui.cljs @@ -584,11 +584,31 @@ (edit/transaction (constantly (:clip r))) (select-results [])))))))) +(defn- paste-anchor + "The selection a paste goes by: the selection, except in Figma's two cases. + + A paste goes into what is selected and selects what it made. Pasting again + onto your own paste therefore means another one beside it, not one inside the + last — so while the selection is still exactly what the last paste made, it + goes where that one went. And pasting with the very thing you copied still + selected means a copy of it beside it: inside itself is a cycle, and was + refused. Select anything else and paste follows the selection again." + [db] + (let [{:keys [made from]} (get-in db [:ui :paste]) + primary (get-in db [:ui :selection]) + path (nth primary 3 nil) + copied (into #{} (map :path) (get-in db [:ui :clipboard :items]))] + (cond + (and made (= made (current-selections db))) from + (and (seq path) (contains? copied path)) (let [up (pop (vec path))] + [:node nil (peek up) up]) + :else primary))) + (defn paste-destination "The effective creation context at the playhead, in the destination's frames." [db clip st] (creation/target clip st (get-in db [:ui :open]) - (get-in db [:ui :selection]) (editing-frame db clip))) + (paste-anchor db) (editing-frame db clip))) (rf/reg-event-db ::paste @@ -602,10 +622,11 @@ r (clipboard/paste clip payload sid frame {:fresh-id random-uuid})] (if-let [why (:refused r)] (command-refused db why) - (-> db - (edit/transaction (constantly (:clip r))) - (select-results - (mapv (fn [id] [:node sid id (conj path id)]) (:roots r)))))))))) + (let [made (mapv (fn [id] [:node sid id (conj path id)]) (:roots r))] + (-> db + (edit/transaction (constantly (:clip r))) + (select-results made) + (assoc-in [:ui :paste] {:from (paste-anchor db) :made made}))))))))) (defn- duplicate-selected [db unique?] (let [clip (:clip (store/entry (:clip/current db))) @@ -690,12 +711,8 @@ (fn [db [_ size]] (assoc-in db [:ui :brush] (-> size js/Math.round (max 1) (min 64))))) (defn new-lane-drawing - "Create the one-frame drawing cel implied by drawing with a lane row active. - - A lane is an insertion surface, so this always makes a new cel at the - playhead. Existing content at that frame is claimed and trimmed by - `overwrite-drawing`. Selecting an existing cel resolves to that cel's ordinary - source symbol before this function is reached, and therefore draws inside it. + "Create the one-frame drawing cel implied by drawing with a lane row active, + where the lane shows nothing at the playhead — see `polygon-landing`. `overwrite-drawing` rather than `append-drawing`, because a gap already has the room. Appending RIPPLES everything after it later by the new clip's @@ -714,17 +731,31 @@ made {:clip (:clip made) :sid sid :path (conj (vec path) cel-id)})))) +(defn- showing + "The id of the clip of lane `sid` that is on screen at the lane's frame `f`, + or nil where the lane shows nothing." + [clip sid f] + (some (fn [[id n]] + (when-let [[lo hi] (node/placed-span n)] + (when (and (node/source n) (<= lo f) (< f hi)) id))) + (get-in clip [:symbols sid :nodes]))) + (defn polygon-landing "Choose the document and row path a finished polygon is drawn into. - A resolved lane always gets a new drawing cel. A selected cel resolves to its - source symbol instead, so drawing there adds a child to that existing cel." + DRAWING IN A LANE DRAWS INTO WHAT IT SHOWS. With the lane as the target, the + drawing on screen at the playhead is what you are drawing on, so the shape + goes into it — exactly as if that cel were selected. Only where the lane shows + nothing is a new cel made; a new drawing over an old one is K's job, and ⇧K's, + not a side effect of the brush." [clip st db] (let [into (creation/target clip st (get-in db [:ui :open]) - (get-in db [:ui :selection]) (editing-frame db clip))] - (if (= :lane (:kind into)) - (assoc (new-lane-drawing clip into) :lane? true) - {:clip clip :sid (:sid into) :path (:path into) :lane? false}))) + (get-in db [:ui :selection]) (editing-frame db clip)) + cel (when (= :lane (:kind into)) (showing clip (:sid into) (:frame into)))] + (cond + cel {:clip clip :path (conj (vec (:path into)) cel) :lane? false} + (= :lane (:kind into)) (assoc (new-lane-drawing clip into) :lane? true) + :else {:clip clip :sid (:sid into) :path (:path into) :lane? false}))) (defn beginning-polygon "Enter polygon mode, first materializing a new drawing at the playhead when @@ -970,7 +1001,7 @@ (or (nil? end) (>= at end)) {:refused "the playhead is past the end of this symbol"} :else - (let [new-symbol (cond-> {:id sid :name (name sid) + (let [new-symbol (cond-> {:id sid ;; A fresh cel is one frame for ;; frame-by-frame work. In an ;; ordinary composition the new @@ -1163,10 +1194,8 @@ (if (:refused where) (update db :project merge {:status (:refused where)}) (let [seeded (assoc-in document [:symbols sid] - (cond-> {:id sid - :name (if palette? "palette transition" (name sid)) - :frames 1 :nodes {}} - palette? (assoc :type :palette))) + (cond-> {:id sid :frames 1 :nodes {}} + palette? (assoc :type :palette :name "palette transition"))) result (span/place-symbol seeded st (:sid where) uuid sid (:at where) {:extent :grow-symbol :remainder-id (random-uuid) @@ -1441,30 +1470,73 @@ (boolean (get-in db [:ui :auto-key?])) st)) % edits)))))) -(rf/reg-event-db +(rf/reg-fx + ::confirm + (fn [{:keys [message then]}] + (when (js/confirm message) (rf/dispatch then)))) + +(rf/reg-event-fx + ::delete-symbol + ;; Out of the library, with its placements; see `nest/delete-symbol`. Its tab + ;; closes, and if it was on screen the root takes its place. + ;; + ;; ASKED FIRST WHEN IT IS PLACED, because then the delete reaches past the row + ;; you pointed at, into every symbol that uses it. One placed nowhere goes + ;; without a question; undo is there either way. + (fn [{:keys [db]} [_ sid sure?]] + (let [clip (:clip (store/entry (:clip/current db))) + {gone :clip why :refused} (nest/delete-symbol clip sid) + uses (clip/uses clip sid)] + (cond + why + {:db (refused db why)} + + (and (pos? uses) (not sure?)) + {::confirm {:message (str "Delete " (clip/symbol-name clip sid) "? It is used " + (if (= 1 uses) "once" (str uses " times")) ", in " + (str/join ", " (map #(clip/symbol-name clip %) + (clip/owners clip sid))) + ", and every use of it goes too.") + :then [::delete-symbol sid true]}} + + :else + (cond-> {:db (-> db + (edit/transaction (constantly gone)) + (update-in [:ui :tabs] #(vec (remove #{sid} %))) + (update :ui (fn [ui] + (cond-> ui + (= [:symbol sid] (:selection ui)) + (assoc :selection nil :selections [])))))} + (= sid (get-in db [:ui :open])) + (assoc :dispatch [::playback/open-symbol (clip/opens-on gone)])))))) + +(rf/reg-event-fx ::delete-selected ;; Backspace while drawing takes the last point back, as in Illustrator; on - ;; the pen, with a point of the selected shape picked, it takes that point. - (fn [db _] + ;; the pen, with a point of the selected shape picked, it takes that point. A + ;; symbol picked in the pool is deleted from the library. + (fn [{:keys [db]} _] (let [[vsid vid i] (get-in db [:ui :vertex]) primary (get-in db [:ui :selection])] (cond (seq (get-in db [:ui :draft])) - (update-in db [:ui :draft] #(subvec % 0 (- (count %) 2))) + {:db (update-in db [:ui :draft] #(subvec % 0 (- (count %) 2)))} (and i (= :pen (get-in db [:ui :tool])) (= [:node vsid vid] (take 3 primary))) - (deleting-vertex db vsid vid i) + {:db (deleting-vertex db vsid vid i)} + + (= :symbol (first primary)) + {:dispatch [::delete-symbol (second primary)]} :else (let [many (vec (get-in db [:ui :selections])) selections (if (some #{primary} many) many (if primary [primary] [])) nodes (distinct (keep (fn [[kind sid id]] (when (= :node kind) [sid id])) selections))] - (if (seq nodes) - (-> db - (edit/transaction #(reduce (fn [c [sid id]] (nest/delete-node c sid id)) % nodes)) - (assoc-in [:ui :selection] nil) - (assoc-in [:ui :selections] [])) - db)))))) + (when (seq nodes) + {:db (-> db + (edit/transaction #(reduce (fn [c [sid id]] (nest/delete-node c sid id)) % nodes)) + (assoc-in [:ui :selection] nil) + (assoc-in [:ui :selections] []))})))))) (rf/reg-event-db ::restack diff --git a/frontend/src/arthur/subs/ui.cljs b/frontend/src/arthur/subs/ui.cljs index 0336a9a..25fd988 100644 --- a/frontend/src/arthur/subs/ui.cljs +++ b/frontend/src/arthur/subs/ui.cljs @@ -10,6 +10,7 @@ [arthur.domain.nest :as nest] [arthur.domain.node :as node] [arthur.domain.pick :as pick] + [arthur.domain.symbol :as symbol] [arthur.footage.store :as store] [arthur.subs.playback :as playback] [arthur.subs.render :as render] @@ -108,6 +109,30 @@ (when clip (creation/target clip (:store (store/entry clip-id)) open selection f)))) +(rf/reg-sub + ::onion-scope + :<- [::selection] + :<- [::render/clip] + :<- [::render/clip-id] + :<- [::render/open] + :<- [::render/open-frame] + ;; WHAT ONION SKIN GHOSTS: the selection's row, or the lane it is in. A cel, + ;; and anything drawn in one, is on screen for its own few frames, so ghosting + ;; it ghosts nothing either side — and drawing selects the shape just drawn, + ;; which put the onion out at the first stroke. A lane's neighbours are its + ;; other drawings, which is what onion skin is for. The nearest lane, so a + ;; lane drawn inside a cel of another ghosts its own drawings. + (fn [[[kind _ id path] clip clip-id open f] _] + (if-not (= :node kind) + [] + (let [path (vec (or path [id])) + st (some-> clip-id store/entry :store) + lane? #(when-let [{:keys [sid]} (and clip (nest/inside clip st open % f))] + (symbol/lane? (clip/symbol clip sid)))] + (or (some #(let [up (pop %)] (when (and (seq up) (lane? up)) up)) + (take-while seq (iterate pop path))) + path))))) + (rf/reg-sub ::creation-placement :<- [::creation-target] diff --git a/frontend/src/arthur/ui/icon.cljs b/frontend/src/arthur/ui/icon.cljs index 7c986b4..056836f 100644 --- a/frontend/src/arthur/ui/icon.cljs +++ b/frontend/src/arthur/ui/icon.cljs @@ -30,6 +30,7 @@ (def ^:private glyphs {:start {:fill ["M2 2.5h1.3v7H2z" ; the bar it stops against "M10 2.5v7L4.2 6z"]} ; and the triangle into it + :end {:fill ["M8.7 2.5H10v7H8.7z" "M2 2.5v7L7.8 6z"]} :play {:fill ["M3.4 2.3L10 6 3.4 9.7z"]} :pause {:fill ["M3.3 2.4h1.9v7.2H3.3z" "M6.8 2.4h1.9v7.2H6.8z"]} @@ -60,7 +61,19 @@ :hold {:fill ["M1.5 8h2v2h-2z" "M8.5 2h2v2h-2z"] :stroke ["M3.5 9H9.5V4"]} ;; How an instance plays: through once into a stop, round, or one frame. :once {:fill ["M2.4 2.6 7.6 6 2.4 9.4z" "M8.4 2.6h1.4v6.8H8.4z"]} - :still {:stroke ["M3.5 2.5h5v7h-5z"] :fill ["M5 4.5h2v3H5z"]}}) + :still {:stroke ["M3.5 2.5h5v7h-5z"] :fill ["M5 4.5h2v3H5z"]} + ;; A new drawing in a lane: a sheet, with a line on it — the one at the + ;; playhead carried on — or bare. + :drawing {:stroke ["M3 1.5h4.5l2 2v7h-6.5z" "M4.6 7.8 5.9 6.1 7.4 7.3"]} + :blank {:stroke ["M3 1.5h4.5l2 2v7h-6.5z"]} + ;; The clipboard, as every editor draws it: two sheets, scissors, a board, + ;; and two sheets with one more. + :copy {:stroke ["M4.5 4.5h5v5h-5z" "M2.5 7.5v-5h5"]} + :cut {:stroke ["M2 9a1.5 1.5 0 1 0 3 0a1.5 1.5 0 1 0 -3 0" + "M7 9a1.5 1.5 0 1 0 3 0a1.5 1.5 0 1 0 -3 0" + "M4.4 7.8 8.5 2" "M7.6 7.8 3.5 2"]} + :paste {:stroke ["M3.8 2.5H2.5v8h7v-8H8.2" "M4.5 1.5h3v2h-3z"]} + :duplicate {:stroke ["M4.5 4.5h5v5h-5z" "M2.5 7.5v-5h5" "M7 5.8v2.4" "M5.8 7h2.4"]}}) (defn view "The glyph named `k`, sized by CSS and inked in `currentColor` — so a button's diff --git a/frontend/src/arthur/ui/player.cljs b/frontend/src/arthur/ui/player.cljs index a31a1d8..fe7cd4d 100644 --- a/frontend/src/arthur/ui/player.cljs +++ b/frontend/src/arthur/ui/player.cljs @@ -140,7 +140,8 @@ ;; symbol's stacking context. :target (rf/subscribe [::ui-sub/creation-target]) :onion (rf/subscribe [::layout/onion]) - :selection (rf/subscribe [::ui-sub/selection])}) + :selection (rf/subscribe [::ui-sub/selection]) + :ghosting (rf/subscribe [::ui-sub/onion-scope])}) (fn [{:keys [draft hover tone fit target] :as s} _] (-> s (dissoc :draft :hover :tone :fit :target) @@ -163,8 +164,8 @@ ;; in the editor's own state, so it changes what the canvases should ;; show without touching the frame number OR the resolver. (when-not (and (identical? (:resolver before) (:resolver now)) - (= (select-keys before [:tracing :pen :mat :onion :selection :playing? :preview :solo?]) - (select-keys now [:tracing :pen :mat :onion :selection :playing? :preview :solo?]))) + (= (select-keys before [:tracing :pen :mat :onion :selection :ghosting :playing? :preview :solo?]) + (select-keys now [:tracing :pen :mat :onion :selection :ghosting :playing? :preview :solo?]))) (repaint!)))))) ;; The brush or eraser stroke being painted: its mask, mutated in place as the @@ -195,14 +196,14 @@ (declare op-path reach) (defn- paint-onions! - "Ghost the picture `onion/frames` either side of `f`: the selected node's ops - when a node is selected, everything otherwise. Each ghost is rasterised as + "Ghost the picture `onion/frames` either side of `f`: the ops of + `::ui-sub/onion-scope` — the selection's row, or the lane it is in — + everything when nothing is selected. Each ghost is rasterised as far as it reaches inside `[vx vy wx wy]`, what is on screen, as the picture is, and the canvas covers them all." [f [vx vy wx wy]] (let [{:keys [onion-canvas]} @state - {:keys [onion playing? resolver frames selection width height]} @snapshot - path (if (= :node (first selection)) (or (nth selection 3 nil) [(nth selection 2)]) []) + {:keys [onion playing? resolver frames width height] path :ghosting} @snapshot ghosts (when (and onion-canvas (:on? onion) (not playing?)) (into [] (keep (fn [{:keys [frame direction]}] diff --git a/frontend/src/arthur/ui/pool.cljs b/frontend/src/arthur/ui/pool.cljs index e574bbc..65f32c5 100644 --- a/frontend/src/arthur/ui/pool.cljs +++ b/frontend/src/arthur/ui/pool.cljs @@ -15,13 +15,12 @@ looking at and puts the number on its tab. THE MAIN TIMELINE LEADS, and it is `clip/opens-on`'s answer rather than a - name: the longest symbol nothing else places is the one the project plays and - the one the work happens in, so it gets the top of the pane and a row drawn at - a size you can read a pose off. Every other symbol is the list under it. That - is one list and not two — every symbol here IS a timeline, and heading a - section TIMELINES and the next one SYMBOLS would name the same thing twice. - None of it is `:main` being special either: rename it, place it inside - something else, and the pool follows the document. + name: the symbol the project plays and the one the work happens in, so it gets + the top of the pane and a row drawn at a size you can read a pose off. Every + other symbol is the tree under it: what nothing places beside it at the top, + and the rest under whatever places it — see `this-project`. None of it is + `:main` being special either: rename it, place it inside something else, and + the pool follows the document. Every row is a drag source, and what it carries says what it is: `symbol:` for a symbol of this document, `import:||` for @@ -199,13 +198,27 @@ its own under the name — `sub2` — rather than one of them out at the edge. `rename` is `{:key :value :begin! :commit!}` and is what makes a row - renameable at all: a row without one has no pencil and does not answer F2." - [{:keys [label sub sub2 title thumb on? open? main? disabled? rename] :as opts}] + renameable at all: a row without one has no pencil and does not answer F2. + + `depth` puts a row in a tree: indented that far, with a twisty ahead of it. + `inside` is `{:count :open? :flip!}` for a row with rows under it; a row with + none keeps the twisty's room, so names line up down a level. + + `delete!` puts a cross beside the pencil, on hover as the pencil is." + [{:keys [label sub sub2 title thumb on? open? main? disabled? rename depth inside delete!] + :as opts}] (let [{:keys [key value begin! commit!]} rename editing? (boolean (and key (= key (:editing rename))))] [:div.pool-row {:class (str (when main? "main ") (when on? "on ") (when open? "open ") (when disabled? "disabled ") - (when editing? "editing"))} + (when editing? "editing")) + :style (when depth {:padding-left (+ 4 (* 10 depth))})} + (when depth + (if-let [{:keys [count open? flip!]} inside] + [:span.pool-twisty {:title (str count " inside") + :on-click (fn [^js e] (.stopPropagation e) (flip!))} + (if open? "\u25BE" "\u25B8")] + [:span.pool-twisty])) (if editing? ;; The input REPLACES the row rather than floating over it, so the list ;; does not change height while you type in it. @@ -235,7 +248,7 @@ (.preventDefault e) (begin! key))))} (dissoc opts :label :sub :sub2 :title :on? :open? :main? - :disabled? :thumb :rename)) + :disabled? :thumb :rename :depth :inside :delete!)) thumb [:span.text ;; The label is `.text`'s first child and stays that way: it is how @@ -250,13 +263,19 @@ ;; being avoided, and F2 already reaches it from the keyboard. :on-click (fn [^js e] (.stopPropagation e) (begin! key))} "\u270E"]) + (when delete! + [:span.pool-rename.pool-delete + {:title (str "delete " label " (Delete)") + :on-click (fn [^js e] (.stopPropagation e) (delete!))} + "\u00D7"]) ;; The main row says both its facts under the name instead. (when (and sub (not main?)) [:span.sub sub])]])])) ;; --------------------------------------------------------------------------- ;; the rows of each kind -(defn- symbol-row [document sid {:keys [clip-id store palette selection open main rename]}] +(defn- symbol-row [document sid {:keys [clip-id store palette selection open main rename + depth inside]}] (let [sym (clip/symbol document sid) label (clip/symbol-name document sid) nodes (count (:nodes sym))] @@ -276,6 +295,9 @@ :on? (= selection [:symbol sid]) :open? (= sid open) :main? (= sid main) + :depth depth + :inside inside + :delete! (when-not (= sid main) #(rf/dispatch [::ui/delete-symbol sid])) :rename (assoc rename :key [:symbol sid] :value label @@ -427,23 +449,62 @@ [:div.pool-empty "no match in this scope"] (into [:<>] (map section) children))) +(def ^:private own-section? + "Symbols with a section of their own, kept out of the tree." + #{:palette :palette-track :trace}) + +(defn- listed? [document sid] + (not (own-section? (:type (clip/symbol document sid))))) + +(defn- placed-in + "What each symbol places, as `{owner [sid ...]}`: each once, in the order they + first start in it." + [document] + (reduce-kv (fn [m sid at] + (if (listed? document sid) + (reduce-kv (fn [m owner [[_ f]]] (update m owner (fnil conj []) [f sid])) + m (group-by first at)) + m)) + {} (clip/placements document))) + +(defn- tree + "`sid`'s row and, while it is open, the rows of what it places under it. A + symbol placed in two symbols is under both. `path` is what this row is under, + which keys the row and stops a cycle." + [{:keys [document placed expanded flip! main] :as ctx} sid depth path] + (let [kids (->> (get placed sid) (sort-by (juxt first (comp str second))) + (map second) (remove (set (conj path sid)))) + root? (and (= main sid) (empty? path)) + open? (and (seq kids) (or root? (contains? expanded [path sid])))] + (into [(with-meta (symbol-row document sid + (assoc ctx :depth depth + :inside (when (and (seq kids) (not root?)) + {:count (count kids) :open? open? + :flip! #(flip! [path sid])}))) + {:key (str (conj path sid))})] + (when open? + (mapcat #(tree ctx % (inc depth) (conj path sid)) kids))))) + (defn- this-project - "The open document's own, with the main timeline at the top and every other - symbol under it. + "The open document's own: its symbols as the tree of what places what, then + each kind with a section of its own. - NOT \"TIMELINES AND SYMBOLS\". Every symbol in this model IS a timeline — it - has frames and nodes and you open it and work in it — so a pair of headings - naming those two things names one thing twice, and invites a reader to go - looking for the difference. There is one list of symbols. What is true is that - exactly one of them is where the work happens: `clip/opens-on`'s answer, the - longest symbol nothing else places. That gets the top of the pane and a row - drawn like the thing it is, and the rest of the library is the list below it. + WHAT NOTHING PLACES IS AT THE TOP, and everything else is under what places + it. Drawing makes a symbol per drawing, and a flat list of every one buries the + few that matter; a drawing is under its lane, its lane under its scene. What + is left at the top is the root the project opens on, drawn big and first with + what it places always showing under it, and + whatever is placed nowhere — a scene, something made to be placed later, or + something left behind — which is what wants looking at: keep it, place it, or + delete it. Nothing is ever cleared away for you. - Still not `:main` being special. The document says which symbol that is by its - structure; rename it, place it inside something else, and the pool follows." + One rule for every symbol, worked out from the document on each render and + stored nowhere: place it, unplace it, and the pool follows. + + A SEARCH IS FLAT: you asked for it by name, and a hit inside a closed twisty is + a hit you cannot see." [{:keys [document query searching? media sounds images chosen rename main selection] :as ctx}] (let [named? #(hit? query (clip/symbol-name document %)) - top (when (and main (named? main)) main) symbol-ids (sort-by str (keys (:symbols document))) transition-ids (into #{} (mapcat (fn [sym] (when (= :palette-track (:type sym)) @@ -454,22 +515,24 @@ transitions (filterv #(and (named? %) (transition? %)) symbol-ids) tracing (filterv #(and (named? %) (clip/trace? (clip/symbol document %))) symbol-ids) - rest (filterv #(and (named? %) (not= main %) - (not (#{:palette :palette-track :trace} - (:type (clip/symbol document %))))) - symbol-ids) + listed (filterv #(listed? document %) symbol-ids) + symbols (if searching? + (mapv #(symbol-row document % ctx) (filterv named? listed)) + (let [ctx (assoc ctx :placed (placed-in document)) + top (cond->> (filterv #(and (not= main %) + (empty? (clip/owners document %))) + listed) + (contains? (:symbols document) main) (into [main]))] + (into [] (mapcat #(tree ctx % 0 [])) top))) media (filterv #(hit? query (:label %)) media) sounds (filterv #(hit? query (:label %)) sounds) images (filterv #(hit? query (:label %)) images)] [sections searching? - (+ (if top 1 0) (count rest) (count transitions) (count tracing) + (+ (count symbols) (count transitions) (count tracing) (count media) (count sounds) (count images)) - [{:title "project" :searching? searching? + [{:title "symbols" :searching? searching? :blank "nothing to open yet" - :rows (when top [(symbol-row document top ctx)])} - {:title "symbols" :searching? searching? - :blank "nothing else in the library" - :rows (mapv #(symbol-row document % ctx) rest)} + :rows symbols} {:title "tracing layers" :searching? searching? :blank "no tracing layers yet" :rows (mapv #(symbol-row document % ctx) tracing)} @@ -559,7 +622,10 @@ ;; Which row is being renamed, as `[kind id]`. One atom rather ;; than a flag per row: exactly one name is ever being edited, and ;; opening a second input has to close the first. - editing (r/atom nil)] + editing (r/atom nil) + ;; Which tree rows are open, as `[path sid]`. Everything under + ;; the root's own children starts shut — see `tree`. + expanded (r/atom #{})] (let [{:keys [loading? status available sounds images uploaded chosen]} @(rf/subscribe [::playback/footage]) document @(rf/subscribe [::sub/settled-clip]) @@ -596,7 +662,9 @@ ;; else places is the timeline the work happens in, and it is the ;; one row in this pane drawn big enough to read a pose off. :main (clip/opens-on document) - :rename {:editing @editing :begin! #(reset! editing %)}} + :rename {:editing @editing :begin! #(reset! editing %)} + :expanded @expanded + :flip! #(swap! expanded (fn [open] (if (open %) (disj open %) (conj open %))))} tallies (counts ctx) files? (fn [^js e] (some #{"Files"} (array-seq (.. e -dataTransfer -types))))] [:section.pane.pool diff --git a/frontend/src/arthur/ui/shortcuts.cljs b/frontend/src/arthur/ui/shortcuts.cljs index 8b9045d..64b2c75 100644 --- a/frontend/src/arthur/ui/shortcuts.cljs +++ b/frontend/src/arthur/ui/shortcuts.cljs @@ -24,7 +24,6 @@ (:require [arthur.events.history :as history] [arthur.events.playback :as pb] [arthur.events.ui :as ui] - [arthur.footage.store :as store] [arthur.ui.layout :as layout] [arthur.ui.timeline :as timeline] [clojure.string :as str] @@ -32,27 +31,6 @@ [re-frame.db :as db] [reagent.core :as r])) -(defn stops - "The frames ↑ and ↓ stop on: every key and every clip edge the timeline shows, - as frames of the open symbol. In a lane that is drawing to drawing, on a tween - key to key — one rule, read off the rows the timeline draws, so a jump lands - on something you can see." - [db] - (let [{document :clip} (store/entry (:clip/current db)) - selection (get-in db [:ui :selection]) - n (pb/frames db)] - (into (sorted-set) - (comp (mapcat (fn [{:keys [keys span cels]}] - (concat keys span (mapcat (juxt :span :keys) cels)))) - (mapcat #(if (number? %) [%] %)) - ;; The first whole frame that shows it, forgiving the rounding - ;; a mapped key picks up on its way through a clock. - (map #(js/Math.ceil (- % 1e-6))) - (filter #(< -1 % n))) - (when document - (timeline/rows document (get-in db [:ui :open]) (get-in db [:ui :expanded]) - (when (= :node (first selection)) (nth selection 3))))))) - (defn- frame [db] (get-in db [:playback :frame] 0)) (defn- seek [f] [::pb/seek f]) @@ -62,12 +40,10 @@ :note "hold and drag on the stage to pan"} {:label "Previous frame" :keys ["ArrowLeft" "a"] :run #(seek (dec (frame %)))} {:label "Next frame" :keys ["ArrowRight" "d"] :run #(seek (inc (frame %)))} - {:label "Previous key" :keys ["ArrowDown" "s"] - :run #(some-> (first (rsubseq (stops %) < (frame %))) seek)} - {:label "Next key" :keys ["ArrowUp" "w"] - :run #(some-> (first (subseq (stops %) > (frame %))) seek)} + {:label "Previous key" :keys ["ArrowDown" "s"] :run timeline/prev-key} + {:label "Next key" :keys ["ArrowUp" "w"] :run timeline/next-key} {:label "First frame" :keys ["Home" "shift+ArrowLeft" "shift+a"] :run (constantly (seek 0))} - {:label "Last frame" :keys ["End" "shift+ArrowRight" "shift+d"] :run #(seek (dec (pb/frames %)))} + {:label "Last frame" :keys ["End" "shift+ArrowRight" "shift+d"] :run timeline/last-frame} {:label "Mark in" :keys ["i"] :run (constantly [::pb/mark :in]) :note "playback loops between the marks"} {:label "Mark out" :keys ["o"] :run (constantly [::pb/mark :out])} @@ -83,12 +59,12 @@ ["Animate" [{:label "Key here" :keys ["k"] :note "in a lane, a copy of the drawing from the playhead on" - :run #(if (ui/lane-here %) [::ui/drawing-here true] [::ui/key-selected])} + :run timeline/key-here} {:label "Blank drawing here" :keys ["shift+k"] :run (constantly [::ui/drawing-here false])} {:label "One frame longer" :keys ["=" "+"] :note "moves the end of the selection's span" - :run #(when-let [p (ui/span-path %)] [::ui/slide p 1 :out true])} + :run (timeline/hold 1)} {:label "One frame shorter" :keys ["-"] - :run #(when-let [p (ui/span-path %)] [::ui/slide p -1 :out true])}]] + :run (timeline/hold -1)}]] ["Make" [{:label "New symbol" :keys ["n"] :run (constantly [::ui/new :inside false]) :note "in the selected symbol; in a lane, a blank drawing"} diff --git a/frontend/src/arthur/ui/stage.cljs b/frontend/src/arthur/ui/stage.cljs index ea11433..3a2461a 100644 --- a/frontend/src/arthur/ui/stage.cljs +++ b/frontend/src/arthur/ui/stage.cljs @@ -601,6 +601,14 @@ [:circle {:class (str "first" (when (and hover (closes? draft hover)) " closing")) :cx fx :cy fy :r 1.8}]]))) +(defn- abandon! + "Whatever a pointer was doing on the stage, dropped rather than finished: the + browser took the pointer back, or a second finger came down to move the view." + [] + (reset! dragging nil) (reset! marquee nil) + (reset! painting nil) (player/stroke! nil) + (let-go! false)) + (defn- overlay [w h zoom [x0 y0 x1 y1]] (let [tool @(rf/subscribe [::sub/tool]) draft @(rf/subscribe [::sub/draft]) @@ -634,10 +642,16 @@ :view-box (str x0 " " y0 " " (- x1 x0) " " (- y1 y0)) :tab-index -1 :on-pointer-down + ;; ONE POINTER DRAWS: the first finger down. A second is the view's — + ;; see `navigation` — and fed into a stroke it zigzags between the two. (fn [^js event] + (when (.-isPrimary event) (let [svg (.-currentTarget event) p (xy svg event w h)] - (.focus svg) + ;; Without scrolling it into view: the overlay is the whole work + ;; area, and a phone scrolled to show its corner, which threw the + ;; stage off screen on a tap and every point after it sideways. + (.focus svg #js {:preventScroll true}) (case tool :pen (let [q (snapped draft (stage-point event w h) event) @@ -688,7 +702,7 @@ (.setPointerCapture svg (.-pointerId event)) (if (and (> (count placements) 1) (contains? selected-paths path)) (begin-many! ctx :move placements p) - (begin! ctx :move path p))))))) + (begin! ctx :move path p)))))))) :on-double-click (fn [^js event] (when (= :select tool) (let [p (xy (.-currentTarget event) event w h) @@ -706,6 +720,7 @@ :on-pointer-leave (fn [_] (reset! pointer nil)) :on-pointer-move (fn [^js event] + (when (.-isPrimary event) (let [p (xy (.-currentTarget event) event w h)] (reset! pointer p) (cond @@ -719,8 +734,9 @@ (when (not= q hover) (rf/dispatch [::ui/hover q]))) @repivot (swap! repivot assoc :p p) @marquee (swap! marquee assoc :p p) - @gesture (drag! p event)))) - :on-pointer-up (fn [_] + @gesture (drag! p event))))) + :on-pointer-up (fn [^js event] + (when (.-isPrimary event) (reset! dragging nil) (stroke-end!) ;; One edit, at the end: see `::ui/repivot`. @@ -735,11 +751,8 @@ old (if more? selections [])] (reset! marquee nil) (rf/dispatch [::ui/select-many (into old addresses)])) - (let-go! true))) - :on-pointer-cancel (fn [_] - (reset! dragging nil) (reset! marquee nil) - (reset! painting nil) (player/stroke! nil) - (let-go! false))} + (let-go! true)))) + :on-pointer-cancel (fn [_] (abandon!))} [ghost] (when (seq selected-pts) [:polygon.selected-paint-outline @@ -766,6 +779,18 @@ (defonce pinch-start-zoom (atom nil)) +;; Two fingers on the stage: `{:zoom :spread :at}` as they came down, and where +;; their middle was last. +(defonce ^:private two (atom nil)) + +(defn- middle [^js touches] + (let [a (aget touches 0) b (aget touches 1)] + [(/ (+ (.-clientX a) (.-clientX b)) 2) (/ (+ (.-clientY a) (.-clientY b)) 2)])) + +(defn- apart [^js touches] + (let [a (aget touches 0) b (aget touches 1)] + (js/Math.hypot (- (.-clientX a) (.-clientX b)) (- (.-clientY a) (.-clientY b))))) + (defn- navigation [] {:title "Two-finger scroll to pan · Pinch or ⌘/Ctrl-wheel to zoom · Space-drag or middle-drag to pan" ;; The picture is what is on screen, so moving or resizing the view paints @@ -790,11 +815,48 @@ (do (set! (.-scrollLeft el) (+ (.-scrollLeft el) dx)) (set! (.-scrollTop el) (+ (.-scrollTop el) dy)))))))) - ;; Safari exposes pinch as GestureEvents, with cumulative scale. + ;; ON A PHONE ONE FINGER IS THE PEN AND TWO ARE THE VIEW. Nothing on + ;; the stage scrolls under one finger — `touch-action` says so, and + ;; is not trusted on an SVG in iOS — and two pan by their middle and + ;; zoom by their spread, with whatever the first had begun dropped. + ;; Properties rather than listeners, as `onwheel` is: this ref runs on + ;; every render. Not passive, being on an element, so they can stop + ;; the page. + (set! (.-ontouchstart el) + (fn [^js e] + (let [ts (.-touches e)] + (when (<= 2 (.-length ts)) + (.preventDefault e) + (abandon!) + (reset! two {:zoom @(rf/subscribe [::layout/zoom :stage]) + :spread (apart ts) :at (middle ts)}))))) + (set! (.-ontouchmove el) + (fn [^js e] + (let [ts (.-touches e)] + (cond + (and @two (<= 2 (.-length ts))) + (let [{:keys [zoom at] s0 :spread} @two + [x y :as m] (middle ts) + z (* zoom (/ (apart ts) (max 1 s0)))] + (.preventDefault e) + (if (< (js/Math.abs (- 1 (/ z @(rf/subscribe [::layout/zoom :stage])))) 0.02) + (do (set! (.-scrollLeft el) (+ (.-scrollLeft el) (- (first at) x))) + (set! (.-scrollTop el) (+ (.-scrollTop el) (- (second at) y)))) + (layout/zoom-at! el x y z)) + (swap! two assoc :at m)) + + (.closest (.-target e) ".paint-overlay") + (.preventDefault e))))) + (set! (.-ontouchend el) + (fn [^js e] (when (< (.-length (.-touches e)) 2) (reset! two nil)))) + (set! (.-ontouchcancel el) (.-ontouchend el)) + ;; Safari exposes pinch as GestureEvents, with cumulative scale. On a + ;; phone the touches above have it already. (set! (.-ongesturestart el) (fn [e] (.preventDefault e) - (reset! pinch-start-zoom @(rf/subscribe [::layout/zoom :stage])))) + (when-not @two + (reset! pinch-start-zoom @(rf/subscribe [::layout/zoom :stage]))))) (set! (.-ongesturechange el) (fn [e] (.preventDefault e) diff --git a/frontend/src/arthur/ui/timeline.cljs b/frontend/src/arthur/ui/timeline.cljs index 93e5af4..73dd02b 100644 --- a/frontend/src/arthur/ui/timeline.cljs +++ b/frontend/src/arthur/ui/timeline.cljs @@ -41,6 +41,7 @@ [arthur.ui.layout :as layout] [arthur.ui.player :as player] [re-frame.core :as rf] + [re-frame.db :as rf-db] [reagent.core :as r])) ;; --------------------------------------------------------------------------- @@ -497,9 +498,71 @@ (aget cel "arthurCel"))) (array-seq (.elementsFromPoint js/document (.-clientX event) (.-clientY event))))) +(defn stops + "The frames ↑ and ↓ stop on: every key and every clip edge the timeline shows, + as frames of the open symbol. In a lane that is drawing to drawing, on a tween + key to key — one rule, read off the rows the timeline draws, so a jump lands + on something you can see." + [db] + (let [{document :clip} (store/entry (:clip/current db)) + selection (get-in db [:ui :selection]) + n (pb/frames db)] + (into (sorted-set) + (comp (mapcat (fn [{:keys [keys span cels]}] + (concat keys span (mapcat (juxt :span :keys) cels)))) + (mapcat #(if (number? %) [%] %)) + ;; The first whole frame that shows it, forgiving the rounding + ;; a mapped key picks up on its way through a clock. + (map #(js/Math.ceil (- % 1e-6))) + (filter #(< -1 % n))) + (when document + (rows document (get-in db [:ui :open]) (get-in db [:ui :expanded]) + (when (= :node (first selection)) (nth selection 3))))))) + +(defn- at [db] (get-in db [:playback :frame] 0)) + +;; WHAT A KEY AND ITS BUTTON BOTH SEND, as functions of the db answering the +;; event, or nil for nothing to do. `ui/shortcuts` puts them on keys and the +;; toolbar on buttons, so the two cannot come to mean different things. + +(defn prev-key [db] (some->> (first (rsubseq (stops db) < (at db))) (vector ::pb/seek))) +(defn next-key [db] (some->> (first (subseq (stops db) > (at db))) (vector ::pb/seek))) +(defn last-frame [db] [::pb/seek (dec (pb/frames db))]) + +(defn key-here + "K: in a lane, a new drawing from the playhead on, copying the one there; on + anything else, a key of what is selected." + [db] + (if (ui/lane-here db) [::ui/drawing-here true] [::ui/key-selected])) + +(defn hold + "= and −: the end of the selection's span, `delta` frames later." + [delta] + (fn [db] (when-let [p (ui/span-path db)] [::ui/slide p delta :out true]))) + +(defn- places-lane? + "Does the row at `path` in the open symbol place a lane? Holding a lane + longer is not lengthening a drawing, which is what = and − are on the bar for." + [db path] + (let [{document :clip st :store} (store/entry (:clip/current db)) + f (ui/editing-frame db document) + host (nest/inside document st (get-in db [:ui :open]) (pop path) f) + n (get-in document [:symbols (:sid host) :nodes (peek path)])] + (boolean (some->> (node/source n) (clip/symbol document) symbol/lane?)))) + +(rf/reg-sub + ::here + ;; Which of the toolbar's commands mean something for the selection right now. + (fn [db _] + (let [p (ui/span-path db)] + {:lane? (some? (ui/lane-here db)) + :span? (and (some? p) (not (places-lane? db p)))}))) + ;; --------------------------------------------------------------------------- ;; the panes +(def ^:private speeds [[0.25 "¼×"] [0.5 "½×"] [1 "1×"] [2 "2×"] [4 "4×"]]) + (defn- transport [] (let [playing? @(rf/subscribe [::playback/playing?]) rate @(rf/subscribe [::playback/rate]) @@ -536,14 +599,28 @@ host (when (number? owner-frame) (span/host-frame clip sid id owner-frame)) cuttable? (and span? (integer? host) (let [[lo hi] (node/placed-span n)] (< lo host hi))) - act (fn [event] #(rf/dispatch event))] + act (fn [event] #(rf/dispatch event)) + ;; A command that reads the db when it is pressed, as a key does. + run (fn [f] #(some-> (f @rf-db/app-db) rf/dispatch)) + here @(rf/subscribe [::here]) + marks @(rf/subscribe [::playback/range]) + selections @(rf/subscribe [::sub/selections]) + clipboard @(rf/subscribe [::sub/clipboard]) + ;; A peg goes over ONE node: it takes that node's place in the + ;; hierarchy, and there is no one place a pair of them would go. + one-node (when (= 1 (count selections)) + (let [[kind _ _ path] (first selections)] + (when (and (= :node kind) (seq path)) path)))] [:div.pane-head ;; ----------------------------------------------------------------- time ;; Where the playhead is and how it moves. Nothing here depends on a ;; selection, which is why it is the one group that is never greyed. + ;; Blender's timeline header: the ends, the keys, a frame, play. [:div.group [:button.ico {:aria-label "go to start" :title "go to the first frame (Home)" :on-click (act [::pb/seek 0])} [icon/view :start]] + [:button.ico {:aria-label "previous key" :title "back to the key or drawing before (↓ or S)" + :on-click (run prev-key)} [icon/view :prev]] [:button {:aria-label "−1" :title "step back one frame (← or A)" :on-click (act [::pb/step -1])} "−1"] [:button.ico.ico-play {:aria-label (if playing? "pause" "play") @@ -551,7 +628,11 @@ :on-click (act [::pb/toggle])} [icon/view (if playing? :pause :play)]] [:button {:aria-label "+1" :title "step forward one frame (→ or D)" - :on-click (act [::pb/step 1])} "+1"]] + :on-click (act [::pb/step 1])} "+1"] + [:button.ico {:aria-label "next key" :title "on to the key or drawing after (↑ or W)" + :on-click (run next-key)} [icon/view :next]] + [:button.ico {:aria-label "go to end" :title "go to the last frame (End)" + :on-click (run last-frame)} [icon/view :end]]] [:div.group [:button.ico {:class (when loop? "on") :aria-label "loop" :title "loop playback" :on-click (act [::pb/toggle-loop])} @@ -563,38 +644,87 @@ ;; read WHILE scrubbing, and tabular figures keep it from twitching as it ;; counts. [:span.readout.at-frame (str frame " / " frames)] - ;; Five buttons for one mutually exclusive choice that is 1× almost always. - ;; A select says "pick one of these" in the width of the answer. - ;; playbackRate on the audio element and nothing else: the sound slows, - ;; currentTime advances proportionally, and the derived frame follows. Slow - ;; motion cannot desync by construction. - [:span.pick - [:select {:value rate :aria-label "playback speed" :title "playback speed" - :on-change #(rf/dispatch [::pb/set-rate - (js/parseFloat (.. % -target -value))])} - (doall (for [[r label] [[0.25 "¼×"] [0.5 "½×"] [1.0 "1×"] [2.0 "2×"] [4.0 "4×"]]] - ^{:key r} [:option {:value r} label]))]] - [:span.sep] - ;; The same direct timing operations apply to any selected symbol clip, - ;; whether it is placed freely or lives in a lane. - [:div.group.timing-controls {:aria-label "timing"} - [:button {:disabled (not cuttable?) :aria-label "split" - :title "split the selected clip at the playhead" - :on-click (act [::ui/split])} "split"] - [:button {:disabled (not cuttable?) :aria-label "trim in" - :title "move the selected clip's start to the playhead" - :on-click (act [::ui/trim :in])} "in"] - [:button {:disabled (not cuttable?) :aria-label "trim out" - :title "move the selected clip's end to the playhead" - :on-click (act [::ui/trim :out])} "out"]] + ;; ONE BUTTON THAT SAYS THE SPEED AND STEPS IT, rather than a menu: it is + ;; 1× almost always, and a click is all the other four are away. Shift goes + ;; back. playbackRate on the audio element and nothing else: the sound + ;; slows, currentTime advances proportionally, and the derived frame + ;; follows. Slow motion cannot desync by construction. + (let [i (or (first (keep-indexed #(when (= rate (first %2)) %1) speeds)) 2)] + [:button.speed {:class (when (not= 1 rate) "on") :aria-label "playback speed" + :title "playback speed · click for faster, shift-click for slower" + :on-click (fn [^js e] + (let [j (mod (+ i (if (.-shiftKey e) -1 1)) (count speeds))] + (rf/dispatch [::pb/set-rate (first (nth speeds j))])))} + (second (nth speeds i))]) + ;; Resolve's I and O: where playback loops in the open tab. Lit while set, + ;; and shift takes one away, as on the keys. + [:div.group {:aria-label "loop marks"} + (doall + (for [[which word k] [[:in "in" "I"] [:out "out" "O"]]] + ^{:key which} + [:button {:class (when (get marks which) "on") + :title (str "mark " word " at the playhead (" k ")" + " · shift-click clears it (⇧" k ")") + :on-click (fn [^js e] + (rf/dispatch (if (.-shiftKey e) + [::pb/clear-mark which] + [::pb/mark which])))} + word]))] [:span.sep] + ;; WHAT THE SELECTION CAN HAVE DONE TO IT, and nothing else. A drawing in + ;; a lane takes a new drawing and a longer hold; a symbol placed anywhere + ;; takes a cut and a longer span; a node takes a peg. Shown, not + ;; greyed, so the strip says what is possible here, and each says its key. + (let [{:keys [lane?] hold? :span?} here] + [:div.group.context-controls {:aria-label "selection"} + ;; In a lane, K is a new drawing, which is Flash's F6 and has nothing + ;; to do with a channel's key. Keying a node stays on the key: auto-key + ;; and the inspector's marks already do it where the value is. + (when lane? + [:<> + [:button {:title "a new drawing from the playhead on, carrying on the one there (K)" + :on-click (run key-here)} + [icon/view :drawing] " drawing"] + [:button {:title "a blank drawing from the playhead on (⇧K)" + :on-click (act [::ui/drawing-here false])} + [icon/view :blank] " blank"]]) + (when hold? + [:<> + [:button {:aria-label "one frame shorter" :title "one frame shorter (−)" + :on-click (run (hold -1))} "−"] + [:button {:aria-label "one frame longer" :title "one frame longer (=)" + :on-click (run (hold 1))} "="]]) + (when cuttable? + [:button {:title "split the selected clip at the playhead" + :on-click (act [::ui/split])} "split"]) + ;; THE CLIPBOARD, which has keys everywhere but a phone. Copy, cut and + ;; duplicate while something is selected, paste while something is + ;; copied. + (when (seq selections) + [:<> + [:button.ico {:aria-label "copy" :title "copy (⌘C)" + :on-click (act [::ui/copy])} [icon/view :copy]] + [:button.ico {:aria-label "cut" :title "cut (⌘X)" + :on-click (act [::ui/cut])} [icon/view :cut]] + [:button.ico {:aria-label "duplicate" :title "duplicate beside it (⌘D)" + :on-click (act [::ui/duplicate])} [icon/view :duplicate]]]) + (when clipboard + [:button.ico {:aria-label "paste" :title "paste at the playhead (⌘V)" + :on-click (act [::ui/paste])} [icon/view :paste]]) + (when (and one-node (not lane?)) + ;; The only way to keep a pivot, and the only way to transform a + ;; measured part by hand — which is what `gesture/refusal` tells you + ;; to do, so it has to be reachable from somewhere. + [:button {:title "a transform node over the selection · ⌃/⌘-drag its cross to place the pivot" + :on-click (act [::ui/peg one-node])} "peg"])]) + [:span.spacer] [:div.group.creation-controls {:aria-label "create"} - [:button {:title "create a symbol in the selected location" + [:button {:title "create a symbol in the selected location (N)" :on-click (act [::ui/new-symbol :inside])} - [icon/view :symbol] " symbol"] - [:button {:title "create a lane in the selected location" + [icon/view :symbol] " + symbol"] + [:button {:title "create a lane in the selected location (⇧N)" :on-click (act [::ui/new-lane])} - [icon/view :lane] " lane"]] + [icon/view :lane] " + lane"]] [:button.auto-key {:class (when auto? "on") :aria-pressed auto? :title (if auto? @@ -602,7 +732,7 @@ "auto-key edited parameters at this frame") :on-click (act [::ui/toggle-auto-key])} "◆ auto key"] - [:span.spacer] + [:span.sep] ;; How many pixels a frame is. The tracks are positioned in percentages of ;; their own width, so this is one custom property on the grid and no ;; arithmetic here — see `ui/layout`. @@ -852,7 +982,7 @@ (cond (= k "Escape") (reset! chosen []) (#{"Delete" "Backspace"} k) - (do (.focus (.closest (.-currentTarget e) "section.time")) + (do (.focus (.closest (.-currentTarget e) "section.time") #js {:preventScroll true}) (rf/dispatch [::ui/edit-keyframes @chosen nil]) (reset! chosen [])) :else (let [df (* (if (= k "ArrowLeft") -1 1) (if (.-shiftKey e) 10 1)) xs @chosen] @@ -861,7 +991,7 @@ :on-pointer-down (fn [e] (.stopPropagation e) - (.focus (.-currentTarget e)) + (.focus (.-currentTarget e) #js {:preventScroll true}) (let [ids (set (map keyframes/identity-of items)) selected? (some (:ids @key-state) (map keyframes/identity-of items))] (cond @@ -1053,7 +1183,7 @@ begin! (fn [^js e actual-path gesture-kind actual-select other drag] (let [track (.closest (.-currentTarget e) ".tl-track")] (.stopPropagation e) - (.focus track) + (.focus track #js {:preventScroll true}) ;; SELECTING WAITS FOR THE RELEASE. Selecting on the press ;; changed what the timeline was showing before the gesture ;; had said anything: an expanded lane follows the @@ -1172,6 +1302,8 @@ :on-click (when (and select unmapped?) (fn [^js e] (.stopPropagation e) (choose-row! e select))) + :ref (when (and select (not unmapped?)) + (fn [^js el] (when el (aset el "arthurSelect" select)))) :on-pointer-down (when (and select (not unmapped?)) #(begin! % (or slides path) :slide select nil nil))} @@ -1348,6 +1480,29 @@ (when (pos? lo) [:div.tl-outside {:style {:left 0 :width (edge% lo frames)}}]) (when (< hi frames) [:div.tl-outside {:style {:left (edge% hi frames) :right 0}}])])))) +(defn- swept + "The clips and bars inside `tracks` that the box from `[x y]` to `[x2 y2]`, in + client pixels, touches, as their selection addresses with their elements." + [^js tracks x y x2 y2] + (let [l (min x x2) r (max x x2) t (min y y2) b (max y y2)] + (keep (fn [^js el] + (when-let [select (or (some-> (aget el "arthurCel") :select) + (aget el "arthurSelect"))] + (let [box (.getBoundingClientRect el)] + (when (and (< (.-left box) r) (< l (.-right box)) + (< (.-top box) b) (< t (.-bottom box))) + [el select])))) + (array-seq (.querySelectorAll tracks ".tl-cel, .tl-span"))))) + +(defn- mark-swept! + "Show what the box holds by a class on the blocks themselves, not by + selecting: selecting re-lays the pane — an expanded lane follows the + selection — and the rows would move under the box being drawn over them." + [^js tracks els] + (doseq [^js el (array-seq (.querySelectorAll tracks ".swept"))] + (.remove (.-classList el) "swept")) + (doseq [^js el els] (.add (.-classList el) "swept"))) + (defn- key-marquee [marquee] (when-let [{:keys [x y x2 y2]} @marquee] (when (and x2 y2) @@ -1400,7 +1555,13 @@ key-context (atom nil) key-anchor (atom nil) row-anchor (atom nil) - marquee (r/atom nil)] + marquee (r/atom nil) + ;; A box dragged from empty track space over clips and bars: + ;; `{:x :y :base}` pressed, then `:x2 :y2 :hits` once it has moved + ;; far enough to be a drag and not a click. `swept?` swallows the + ;; click a drag ends in, which would otherwise select nothing. + sweep (r/atom nil) + swept? (atom false)] (let [clip-id @(rf/subscribe [::render/clip-id]) clip @(rf/subscribe [::render/clip]) ;; THE RULER IS THE OPEN SYMBOL'S OWN FRAME SPACE. Every span, key and @@ -1510,7 +1671,37 @@ ;; own events; the target check keeps ordinary row/bar clicks local. {:on-click (fn [^js e] (when (= (.-target e) (.-currentTarget e)) - (rf/dispatch [::ui/select nil])))} + (rf/dispatch [::ui/select nil]))) + ;; ON A PHONE ONE FINGER IS THE POINTER AND TWO SCROLL, as on the + ;; stage: one drags a clip, a box or the playhead, and the rows only + ;; move under two, by their middle. Properties, not listeners, since + ;; this runs on every render; on an element, so not passive. + :ref (fn [^js el] + (when el + (let [middle (fn [^js ts] + (let [a (aget ts 0) b (aget ts 1)] + [(/ (+ (.-clientX a) (.-clientX b)) 2) + (/ (+ (.-clientY a) (.-clientY b)) 2)]))] + (set! (.-ontouchstart el) + (fn [^js e] + (when (<= 2 (.. e -touches -length)) + (when @sweep + (reset! sweep nil) + (mark-swept! el [])) + (aset el "arthurTwo" (middle (.-touches e)))))) + (set! (.-ontouchmove el) + (fn [^js e] + (when-let [[x y] (and (<= 2 (.. e -touches -length)) + (aget el "arthurTwo"))] + (.preventDefault e) + (let [[x2 y2 :as m] (middle (.-touches e))] + (set! (.-scrollLeft el) (+ (.-scrollLeft el) (- x x2))) + (set! (.-scrollTop el) (+ (.-scrollTop el) (- y y2))) + (aset el "arthurTwo" m))))) + (set! (.-ontouchend el) + (fn [^js e] + (when (< (.. e -touches -length) 2) + (aset el "arthurTwo" nil)))))))} [:div.tl-labels ;; Empty label space takes a row back out to the top of the open symbol. {:on-click (fn [^js e] @@ -1538,11 +1729,21 @@ [:div.tl-tracks {:on-pointer-down-capture (fn [e] + ;; DRAG TO SELECT, FROM EMPTY SPACE: anywhere a press would + ;; otherwise do nothing. Not yet a box — a press that does not move + ;; is still a click, and a double-click in a lane still makes a + ;; drawing — so nothing is stopped until it moves. + (reset! swept? false) + (when (and (= 0 (.-button e)) (.-isPrimary e) + (not (.closest (.-target e) ".tl-track.automation")) + (not (.closest (.-target e) "button, .tl-span, .tl-edge, .tl-junction, .tl-ruler, input"))) + (reset! sweep {:x (.-clientX e) :y (.-clientY e) + :base (if (or (.-metaKey e) (.-ctrlKey e) (.-shiftKey e)) selections [])})) (when (and (= 0 (.-button e)) (.closest (.-target e) ".tl-track.automation") (not (.closest (.-target e) "button, .movable, .unmapped, .tl-edge"))) (.stopPropagation e) (.preventDefault e) - (.focus (.-currentTarget e)) + (.focus (.-currentTarget e) #js {:preventScroll true}) (reset! marquee {:x (.-clientX e) :y (.-clientY e) :hits (let [container (.-currentTarget e)] (vec (mapcat @@ -1559,6 +1760,16 @@ (try (.setPointerCapture (.-currentTarget e) (.-pointerId e)) (catch :default _ nil)))) :on-pointer-move (fn [e] + (when-let [{:keys [x y x2]} @sweep] + (let [cx (.-clientX e) cy (.-clientY e) tracks (.-currentTarget e)] + ;; Four pixels, as a drag begins anywhere else in the app. + (when (or x2 (< 4 (js/Math.hypot (- cx x) (- cy y)))) + (.stopPropagation e) + (when-not x2 + (try (.setPointerCapture tracks (.-pointerId e)) (catch :default _ nil))) + (let [hits (swept tracks x y cx cy)] + (mark-swept! tracks (map first hits)) + (swap! sweep assoc :x2 cx :y2 cy :hits (mapv second hits)))))) (when-let [{:keys [x y base hits]} @marquee] (.stopPropagation e) (let [x2 (.-clientX e) y2 (.-clientY e)] @@ -1568,8 +1779,20 @@ (mapcat (fn [{cx :x cy :y :keys [items]}] (when (and (<= (min x x2) cx (max x x2)) (<= (min y y2) cy (max y y2))) items)) hits)))))))) - :on-pointer-up (fn [e] (when @marquee (.stopPropagation e) (reset! marquee nil))) - :on-pointer-cancel (fn [_] (when @marquee (reset! key-selection (:base @marquee)) (reset! marquee nil))) + :on-pointer-up (fn [e] + (when-let [{:keys [x2 base hits]} @sweep] + (reset! sweep nil) + (when x2 + (.stopPropagation e) + (mark-swept! (.-currentTarget e) []) + (reset! swept? true) + (rf/dispatch [::ui/select-many (into (vec base) hits)]))) + (when @marquee (.stopPropagation e) (reset! marquee nil))) + :on-pointer-cancel (fn [e] + (when @sweep + (reset! sweep nil) + (mark-swept! (.-currentTarget e) [])) + (when @marquee (reset! key-selection (:base @marquee)) (reset! marquee nil))) :tab-index 0 :on-key-down (fn [e] @@ -1588,8 +1811,9 @@ (rf/dispatch [::ui/edit-keyframes xs df]) (reset! key-selection (keyframes/shifted xs df)))))))) :on-click (fn [^js e] - (when (= (.-target e) (.-currentTarget e)) - (rf/dispatch [::ui/select nil]))) + (cond + @swept? (reset! swept? false) + (= (.-target e) (.-currentTarget e)) (rf/dispatch [::ui/select nil]))) :on-drag-enter (fn [^js e] (when (drag/accepts?) (.preventDefault e))) :on-drag-over (fn [^js e] (when (drag/accepts?) @@ -1661,7 +1885,8 @@ [:div.tl-grid {:class (when-not fine "coarse")}] [outside-marks frames false] [playhead :div.tl-playhead frames] - [key-marquee marquee]]] + [key-marquee marquee] + [key-marquee sweep]]] [cursor-hint hint]]) (finally (.disconnect sizer)))) diff --git a/frontend/src/arthur/ui/topbar.cljs b/frontend/src/arthur/ui/topbar.cljs index f7510e4..0ccb234 100644 --- a/frontend/src/arthur/ui/topbar.cljs +++ b/frontend/src/arthur/ui/topbar.cljs @@ -8,11 +8,8 @@ (:require [arthur.events.collab :as collab] [arthur.events.export :as export] [arthur.events.project :as project] - [arthur.events.ui :as ui] [arthur.subs.playback :as playback] - [arthur.subs.ui :as sub] [arthur.ui.layout :as layout] - [arthur.ui.menu :as menu] [arthur.ui.openmenu :as openmenu] [arthur.ui.settings :as settings] [arthur.ui.share :as share] @@ -57,14 +54,6 @@ (let [{project-name :name project-seq :seq :keys [busy? status]} @(rf/subscribe [::playback/project]) {footage-status :status} @(rf/subscribe [::playback/footage]) {export-status :status} @(rf/subscribe [::export/state]) - selections @(rf/subscribe [::sub/selections]) - clipboard @(rf/subscribe [::sub/clipboard]) - selected? (boolean (seq selections)) - ;; A peg goes over ONE node: it takes that node's place in the - ;; hierarchy, and there is no one place a pair of them would go. - one-node (when (= 1 (count selections)) - (let [[kind _ _ path] (first selections)] - (when (and (= :node kind) (seq path)) path))) title (or project-name "untitled") commit! (fn [] (rf/dispatch [::project/rename @draft]) @@ -79,27 +68,6 @@ [settings/new-project "new"] [openmenu/view] [undo/view] - [menu/view - {:label "edit" :title "selection and clipboard" - :items [{:label "copy" :sub "Copy selection · ⌘/Ctrl C" - :disabled? (not selected?) :on-click #(rf/dispatch [::ui/copy])} - {:label "cut" :sub "Cut selection · ⌘/Ctrl X" - :disabled? (not selected?) :on-click #(rf/dispatch [::ui/cut])} - {:label "paste" :sub "Paste at playhead into the active row · ⌘/Ctrl V" - :disabled? (nil? clipboard) :on-click #(rf/dispatch [::ui/paste])} - {:label "duplicate" :sub "Duplicate beside selection; lanes repeat forward · ⌘/Ctrl D" - :disabled? (not selected?) :on-click #(rf/dispatch [::ui/duplicate])} - {:label "duplicate unique" - :sub "Duplicate with a private nested symbol graph · ⇧⌘/Ctrl D" - :disabled? (not selected?) - :on-click #(rf/dispatch [::ui/duplicate-unique])} - ;; The only way to keep a pivot, and the only way to transform a - ;; measured part by hand — which is what `gesture/refusal` tells - ;; you to do, so it has to be reachable from somewhere. - {:label "add peg" - :sub "A transform node over the selection · ⌃/⌘-drag its cross to place the pivot" - :disabled? (nil? one-node) - :on-click #(rf/dispatch [::ui/peg one-node])}]}] [snapshots/view] (if @renaming? [:input.project-name {:auto-focus true :value @draft diff --git a/frontend/test/arthur/domain/instance_test.cljs b/frontend/test/arthur/domain/instance_test.cljs index f4585e9..ff58fb1 100644 --- a/frontend/test/arthur/domain/instance_test.cljs +++ b/frontend/test/arthur/domain/instance_test.cljs @@ -267,6 +267,24 @@ (is (= (get-in c [:symbols :outer :nodes]) (get-in (leaf/clip "c" (leaf/leaves "c" c)) [:symbols :outer :nodes])))))) +(deftest a-drawing-is-named-by-where-it-is-drawn + (let [c (assoc-in (nested) [:symbols :outer :name] "body")] + (testing "placed by one symbol: that symbol's name and the frame it starts on" + (is (= [:outer] (clip/owners c :inner))) + (is (= "body 5" (clip/symbol-name c :inner))) + (is (= "lane 5" (clip/symbol-name (assoc-in c [:symbols :outer :name] "lane") :inner)) + "renaming the lane renames its drawings")) + (testing "a name of its own wins" + (is (= "hand" (clip/symbol-name (assoc-in c [:symbols :inner :name] "hand") :inner)))) + (testing "placed nowhere, or in two symbols, it is its id" + (is (= "loose" (clip/symbol-name c :loose))) + (is (= "inner" (clip/symbol-name (clip/place-symbol c nil :loose :inner 0 (random-uuid) nil) + :inner)))) + (testing "a cycle of unnamed symbols ends rather than recurring" + (let [loop (clip/place-symbol (update-in c [:symbols :outer] dissoc :name) + nil :inner :outer 0 (random-uuid) nil)] + (is (string? (clip/symbol-name loop :inner))))))) + (deftest an-instance-is-labelled-by-what-it-places (let [c (-> (nested) (assoc-in [:symbols :inner :name] "mouth")) @@ -278,9 +296,9 @@ (is (= "mouth" (clip/node-label c id n))) (is (= "jaw" (clip/node-label (assoc-in c [:symbols :inner :name] "jaw") id n)) "renaming the symbol renames every row that shows an instance of it")) - (testing "an unnamed symbol falls back to its id, not to the node's uuid" - (is (= "inner" (clip/node-label (update-in c [:symbols :inner] dissoc :name) - id n)))) + (testing "an unnamed symbol is named by where it is drawn, not by the node's uuid" + (is (= "outer 5" (clip/node-label (update-in c [:symbols :inner] dissoc :name) + id n)))) (testing "and a name left on the instance is not read: it has none of its own" (is (= "mouth" (clip/node-label c id (assoc n :name "left eye"))))) (testing "every placement of a symbol is counted" diff --git a/frontend/test/arthur/domain/nest_test.cljs b/frontend/test/arthur/domain/nest_test.cljs index 2d3473b..13f29ec 100644 --- a/frontend/test/arthur/domain/nest_test.cljs +++ b/frontend/test/arthur/domain/nest_test.cljs @@ -560,3 +560,16 @@ (is (near? [10 90] (piv c))) (is (near? [10 90] (piv grown)) "the pivot moved because something else was drawn, which is the bug"))) + +(deftest deleting-a-symbol-takes-its-placements-and-leaves-what-it-placed + (let [c (-> (clip/blank) + (assoc-in [:symbols :lane] {:id :lane :frames 10 :nodes {}}) + (assoc-in [:symbols :cel] {:id :cel :frames 1 :nodes {}}) + (clip/place-symbol nil :main :lane 0 (random-uuid) nil) + (clip/place-symbol nil :lane :cel 3 (random-uuid) nil)) + {gone :clip} (nest/delete-symbol c :lane)] + (is (nil? (clip/symbol gone :lane))) + (is (empty? (:nodes (clip/symbol gone :main))) "its instance in the root went with it") + (is (clip/symbol gone :cel) "what it placed stays") + (is (empty? (clip/owners gone :cel)) "placed nowhere now") + (is (:refused (nest/delete-symbol c :main)) "the root cannot be deleted"))) diff --git a/frontend/test/arthur/events/export_test.cljs b/frontend/test/arthur/events/export_test.cljs index a92c334..75e933f 100644 --- a/frontend/test/arthur/events/export_test.cljs +++ b/frontend/test/arthur/events/export_test.cljs @@ -67,11 +67,11 @@ :kind :instance :parent :root :z "a1" :source {:symbol :sym/face}} :a-rect {:id :a-rect :kind :rect :parent :root :z "a3"}}} - :sym/face {:frames 40 :nodes {:root {:id :root :kind :group :z "a1"}}}}}) + :sym/face {:name "head" :frames 40 :nodes {:root {:id :root :kind :group :z "a1"}}}}}) (deftest the-picker-offers-every-symbol-then-every-instance-in-the-open-one (let [ts (export/targets clip :main)] - (is (= ["main" "face" "face 1" "face 2"] (mapv :label ts)) + (is (= ["main" "face" "head 1" "head 2"] (mapv :label ts)) "every symbol, then the instances in the open one") (testing "the instances isolate a node in the open symbol and the symbols do not" (is (= [nil nil] (mapv :isolate (take 2 ts)))) diff --git a/frontend/test/arthur/events/lane_test.cljs b/frontend/test/arthur/events/lane_test.cljs index aefaa67..fcadbea 100644 --- a/frontend/test/arthur/events/lane_test.cljs +++ b/frontend/test/arthur/events/lane_test.cljs @@ -1,5 +1,5 @@ (ns arthur.events.lane-test - (:require [cljs.test :refer [deftest is]] + (:require [cljs.test :refer [deftest is testing]] [arthur.domain.clip :as clip] [arthur.domain.creation :as creation] [arthur.domain.history :as history] @@ -316,6 +316,56 @@ (select-keys (ui/paste-destination db doc nil) [:sid :path :frame])) "an inactive selected symbol falls back to the open symbol"))) +(deftest pasting-onto-your-own-paste-goes-beside-it + (let [doc {:fps 24 :width 20 :height 20 + :symbols + {:main {:id :main :frames 40 + :nodes {:drawing {:id :drawing :kind :instance :z "a" + :span [0 10] :time {:at 0 :rate 1} + :source {:symbol :inside} + :playback {:in 0 :speed 1 :end :stop}} + :other {:id :other :kind :instance :z "b" + :span [0 10] :time {:at 0 :rate 1} + :source {:symbol :inside} + :playback {:in 0 :speed 1 :end :stop}}}} + :inside {:id :inside :frames 10 :nodes {}}}} + id (store/install! {:clip doc :store {}} "paste-beside") + instances #(count (get-in (store/entry id) [:clip :symbols % :nodes]))] + (reset! rf-db/app-db {:clip/current id :paint/revision 0 + :ui {:open :main :selection [:node :main :drawing [:drawing]]} + :playback {:frame 2}}) + (rf/dispatch-sync [::ui/copy]) + (rf/dispatch-sync [::ui/select [:node :main nil []]]) + (rf/dispatch-sync [::ui/paste]) + (is (= 3 (instances :main))) + (rf/dispatch-sync [::ui/paste]) + (rf/dispatch-sync [::ui/paste]) + (is (= 5 (instances :main)) + "the selected copy is a symbol under the playhead, and the paste still goes beside it") + (is (= 0 (instances :inside))) + (testing "selecting something else makes paste follow the selection again" + (rf/dispatch-sync [::ui/select [:node :main :other [:other]]]) + (is (= :inside (:sid (ui/paste-destination @rf-db/app-db + (:clip (store/entry id)) {}))))))) + +(deftest pasting-onto-what-you-copied-goes-beside-it + (let [doc {:fps 24 :width 20 :height 20 + :symbols + {:main {:id :main :frames 40 + :nodes {:drawing {:id :drawing :kind :instance :z "a" + :span [0 10] :time {:at 0 :rate 1} + :source {:symbol :inside} + :playback {:in 0 :speed 1 :end :stop}}}} + :inside {:id :inside :frames 10 :nodes {}}}} + id (store/install! {:clip doc :store {}} "paste-beside-copied")] + (reset! rf-db/app-db {:clip/current id :paint/revision 0 + :ui {:open :main :selection [:node :main :drawing [:drawing]]} + :playback {:frame 2}}) + (rf/dispatch-sync [::ui/copy]) + (rf/dispatch-sync [::ui/paste]) + (is (= 2 (count (get-in (store/entry id) [:clip :symbols :main :nodes]))) + "a symbol pasted with itself selected lands beside it, not refused as a cycle"))) + (deftest multi-paste-uses-one-current-primary-target-and-selects-the-batch (let [doc (fixture/document) id (store/install! {:clip doc :store {}} "multi-paste-target") @@ -342,10 +392,14 @@ (deftest polygon-landing-obeys-the-destination-symbol-mode (let [lane (fixture/document) + gap (update-in lane [:symbols :main :nodes] dissoc :b) ordinary (update-in lane [:symbols :main] dissoc :display) db {:ui {:open :main :selection [:node :main :plate [:plate]]} :playback {:frame 5}}] - (is (true? (:lane? (ui/polygon-landing lane {} db)))) + (is (= {:lane? false :path [:b]} (select-keys (ui/polygon-landing lane {} db) [:lane? :path])) + "a lane draws into the cel it shows at the playhead") + (is (true? (:lane? (ui/polygon-landing gap {} db))) + "and makes a new one only where it shows nothing") (is (false? (:lane? (ui/polygon-landing ordinary {} db)))))) (deftest beginning-a-polygon-does-not-invent-a-lane @@ -361,18 +415,27 @@ (is (nil? (get-in saved [:symbols :main :display]))) (is (nil? (get-in (store/entry (:clip/current after)) [:history :done]))))) -(deftest drawing-with-the-lane-row-active-creates-a-new-cel +(deftest drawing-with-the-lane-row-active-draws-into-what-it-shows (let [doc (fixture/document) - id (store/install! {:clip doc :store {}} "draw-new-lane-cel") + id (store/install! {:clip doc :store {}} "draw-into-lane-cel") db {:clip/current id :paint/revision 0 :ui {:open :main :selection [:node :main nil []]} :playback {:frame 1}} + after (ui/beginning-polygon db)] + (is (= doc (:clip (store/entry id))) "no cel is made over the one showing") + (is (= [] (get-in after [:ui :draft]))))) + +(deftest drawing-with-the-lane-row-active-in-a-gap-creates-a-new-cel + (let [doc (update-in (fixture/document) [:symbols :main :nodes] dissoc :b) + id (store/install! {:clip doc :store {}} "draw-new-lane-cel") + db {:clip/current id :paint/revision 0 + :ui {:open :main :selection [:node :main nil []]} + :playback {:frame 5}} after (ui/beginning-polygon db) [_ sid cel-id] (get-in after [:ui :selection]) saved (:clip (store/entry id))] (is (= :main sid)) - (is (not= :a cel-id) "the occupied cel is not reused") - (is (= [1 2] (node/placed-span (get-in saved [:symbols sid :nodes cel-id])))) + (is (= [5 6] (node/placed-span (get-in saved [:symbols sid :nodes cel-id])))) (is (= 1 (clip/frames saved (node/source (get-in saved [:symbols sid :nodes cel-id]))))) (is (= [] (get-in after [:ui :draft]))))) diff --git a/frontend/test/arthur/ui/shortcuts_test.cljs b/frontend/test/arthur/ui/shortcuts_test.cljs index 9eeb46e..75784f9 100644 --- a/frontend/test/arthur/ui/shortcuts_test.cljs +++ b/frontend/test/arthur/ui/shortcuts_test.cljs @@ -2,7 +2,7 @@ (:require [cljs.test :refer [deftest is]] [arthur.domain.sequence-test :as fixture] [arthur.footage.store :as store] - [arthur.ui.shortcuts :as shortcuts])) + [arthur.ui.timeline :as timeline])) (deftest the-key-jumps-stop-on-every-cel-edge-and-key-the-timeline-shows ;; Cels at 0, 4 and 8; `:b` keyed on its own frames 0 and 1, which it shows @@ -10,4 +10,4 @@ ;; last frame and is not somewhere to stand. (let [id (store/install! {:clip (fixture/document) :store {}} "shortcut-stops")] (is (= [0 4 5 8 11] - (vec (shortcuts/stops {:clip/current id :ui {:open :main}})))))) + (vec (timeline/stops {:clip/current id :ui {:open :main}})))))) diff --git a/frontend/test/browser/automation.mjs b/frontend/test/browser/automation.mjs index a748c4d..78e2e76 100644 --- a/frontend/test/browser/automation.mjs +++ b/frontend/test/browser/automation.mjs @@ -88,7 +88,7 @@ try { k('source'), m(k('symbol'), k('box')), k('span'), v(0, 60), k('time'), m(k('at'), 0, k('rate'), 1), k('playback'), m(k('in'), 0, k('speed'), 1, k('end'), k('stop'))); - c = cljs.core.assoc_in(c, v(k('symbols'), k('box')), m(k('id'), k('box'), k('frames'), 60, k('nodes'), m())); + c = cljs.core.assoc_in(c, v(k('symbols'), k('box')), m(k('id'), k('box'), k('name'), 'box', k('frames'), 60, k('nodes'), m())); c = cljs.core.assoc_in(c, v(k('symbols'), k('main'), k('nodes')), m(k('a'), rect('a',0,true), k('b'), rect('b',4,false), k('target'), target)); const id = arthur.footage.store.install_BANG_(m(k('clip'), c, k('store'), m()), 'timeline-browser'); const a = v(k('node'),k('main'),k('a'),v(k('a'))), b = v(k('node'),k('main'),k('b'),v(k('b'))); diff --git a/index.html b/index.html index 31b4e0c..7d048e6 100644 --- a/index.html +++ b/index.html @@ -2,7 +2,7 @@ - + arthur