diff --git a/editor/src/messages/frontend/frontend_message.rs b/editor/src/messages/frontend/frontend_message.rs index b942b37169..ea49d7077d 100644 --- a/editor/src/messages/frontend/frontend_message.rs +++ b/editor/src/messages/frontend/frontend_message.rs @@ -199,6 +199,12 @@ pub enum FrontendMessage { UpdateHistoryPanel { state: crate::messages::portfolio::history::utility_types::HistoryPanelState, }, + UpdateHistoryGraph { + graph: crate::messages::portfolio::history::utility_types::HistoryGraphState, + }, + UpdateHistoryGraphOverlay { + open: bool, + }, /// A History panel step's captured image as an SVG, sent when the GPU is unavailable, which the panel shows wherever a row names `id`. UpdateHistoryThumbnail { id: String, diff --git a/editor/src/messages/input_mapper/input_mappings.rs b/editor/src/messages/input_mapper/input_mappings.rs index cf47bebd3d..83d5d72746 100644 --- a/editor/src/messages/input_mapper/input_mappings.rs +++ b/editor/src/messages/input_mapper/input_mappings.rs @@ -345,6 +345,7 @@ pub fn input_mappings(zoom_with_scroll: bool, environment: Environment) -> Mappi // // DocumentMessage entry!(DoubleTap(Space); action_dispatch=DocumentMessage::GraphViewOverlayToggle), + entry!(KeyDown(KeyH); modifiers=[Alt], action_dispatch=HistoryMessage::GraphOverlayToggle), entry!(KeyDownNoRepeat(Escape); action_dispatch=DocumentMessage::Escape), entry!(KeyDown(Delete); action_dispatch=DocumentMessage::DeleteSelectedLayers), entry!(KeyDown(Backspace); action_dispatch=DocumentMessage::DeleteSelectedLayers), diff --git a/editor/src/messages/portfolio/document/document_message_handler.rs b/editor/src/messages/portfolio/document/document_message_handler.rs index 82ff91d1f7..8a287665d2 100644 --- a/editor/src/messages/portfolio/document/document_message_handler.rs +++ b/editor/src/messages/portfolio/document/document_message_handler.rs @@ -3859,7 +3859,7 @@ impl DocumentMessageHandler { ] } - pub fn update_document_widgets(&self, responses: &mut VecDeque, animation_is_playing: bool, time: Duration) { + pub fn update_document_widgets(&self, responses: &mut VecDeque, animation_is_playing: bool, time: Duration, history_graph_open: bool) { let mut widgets = vec![ IconButton::new("PlaybackToStart", 24) .tooltip_label("Restart Animation") @@ -3944,6 +3944,14 @@ impl DocumentMessageHandler { widgets.extend([ Separator::new(SeparatorStyle::Unrelated).widget_instance(), + TextButton::new("History") + .icon("HistoryUndo") + .tooltip_label(if history_graph_open { "Hide the history graph" } else { "Show the history graph" }) + .tooltip_description("Every step and branch of the document's history, drawn over the viewport.") + .tooltip_shortcut(action_shortcut!(HistoryMessageDiscriminant::GraphOverlayToggle)) + .on_update(|_| HistoryMessage::GraphOverlayToggle.into()) + .widget_instance(), + Separator::new(SeparatorStyle::Related).widget_instance(), TextButton::new("Node Graph") .icon(if self.graph_view_overlay_open { "GraphViewOpen" } else { "GraphViewClosed" }) .hover_icon(if self.graph_view_overlay_open { "GraphViewClosed" } else { "GraphViewOpen" }) diff --git a/editor/src/messages/portfolio/history/history_message.rs b/editor/src/messages/portfolio/history/history_message.rs index c3fd8358f6..67521a0a79 100644 --- a/editor/src/messages/portfolio/history/history_message.rs +++ b/editor/src/messages/portfolio/history/history_message.rs @@ -31,6 +31,10 @@ pub enum HistoryMessage { tag: String, on: bool, }, + GraphOverlay { + open: bool, + }, + GraphOverlayToggle, Captured { document_id: DocumentId, step: document_graph_storage::TimeStamp, diff --git a/editor/src/messages/portfolio/history/history_message_handler.rs b/editor/src/messages/portfolio/history/history_message_handler.rs index a27b3e153e..5f80437019 100644 --- a/editor/src/messages/portfolio/history/history_message_handler.rs +++ b/editor/src/messages/portfolio/history/history_message_handler.rs @@ -1,5 +1,7 @@ use super::changes::{Change, ChangedArea, close_up, tint_changes}; -use super::utility_types::{CaptureImage, DocumentCapture, HistoryBranch, HistoryDeltaRow, HistoryPanelState, HistoryProgressRow, HistoryRow, HistoryThumbnail, LineChange, Pixels}; +use super::utility_types::{ + CaptureImage, DocumentCapture, HistoryBranch, HistoryDeltaRow, HistoryGraphNode, HistoryGraphState, HistoryPanelState, HistoryProgressRow, HistoryRow, HistoryThumbnail, LineChange, Pixels, +}; use crate::messages::portfolio::document::DocumentMessageHandler; use crate::messages::portfolio::document::node_graph::document_node_definitions::DefinitionIdentifier; use crate::messages::portfolio::document::utility_types::network_interface::TransactionStatus; @@ -26,7 +28,7 @@ pub struct HistoryMessageContext<'a> { pub executor: &'a NodeGraphExecutor, } -/// Sends the History panel the active document's history and turns its actions into head moves. +/// Sends the History panel and the history graph the active document's history and turns their actions into head moves. #[derive(Debug, Default, ExtractField)] pub struct HistoryMessageHandler { ticking: bool, @@ -39,6 +41,8 @@ pub struct HistoryMessageHandler { requested: Option<(DocumentId, TimeStamp)>, /// Counts the images the frontend got, so the panel is sent again once a step's capture or its close-up arrives. images_sent: u64, + graph_open: bool, + graph_last_sent: Option, } /// What a captured step showed. The frontend keeps its images. @@ -77,20 +81,29 @@ impl MessageHandler> for HistoryMessag match message { HistoryMessage::Refresh => { self.last_sent = None; - if !self.ticking { - self.ticking = true; - responses.add(BroadcastMessage::SubscribeEvent { - on: EventMessage::AnimationFrame, - send: Box::new(HistoryMessage::Tick.into()), - }); - } + self.tick_every_frame(responses); } HistoryMessage::Tick => { - if context.panel_open { + if context.panel_open || self.graph_open { self.request_capture(&context); + } + if context.panel_open { self.send(&context, responses); } + if self.graph_open { + self.send_graph(&context, responses); + } } + HistoryMessage::GraphOverlay { open } => { + self.graph_open = open; + self.graph_last_sent = None; + if open { + self.tick_every_frame(responses); + } + responses.add(FrontendMessage::UpdateHistoryGraphOverlay { open }); + responses.add(PortfolioMessage::UpdateDocumentWidgets); + } + HistoryMessage::GraphOverlayToggle => responses.add(HistoryMessage::GraphOverlay { open: !self.graph_open }), HistoryMessage::Captured { document_id, step, capture } => { if self.requested == Some((document_id, step)) { self.requested = None; @@ -149,10 +162,24 @@ impl MessageHandler> for HistoryMessag } } - advertise_actions!(HistoryMessageDiscriminant;); + advertise_actions!(HistoryMessageDiscriminant; GraphOverlayToggle); } impl HistoryMessageHandler { + pub fn graph_open(&self) -> bool { + self.graph_open + } + + fn tick_every_frame(&mut self, responses: &mut VecDeque) { + if !self.ticking { + self.ticking = true; + responses.add(BroadcastMessage::SubscribeEvent { + on: EventMessage::AnimationFrame, + send: Box::new(HistoryMessage::Tick.into()), + }); + } + } + /// Asks for the newest step to be captured once no tool is mid-gesture, since a capture shows the document as it renders now. fn request_capture(&mut self, context: &HistoryMessageContext) { self.captures.retain(|id, _| context.documents.contains_key(id)); @@ -262,6 +289,19 @@ impl HistoryMessageHandler { .unwrap_or_default(); responses.add(FrontendMessage::UpdateHistoryPanel { state }); } + + fn send_graph(&mut self, context: &HistoryMessageContext, responses: &mut VecDeque) { + let document = context.active_document_id.and_then(|id| context.documents.get(&id)); + let gdd = document.and_then(|document| document.storage()); + let captures = context.active_document_id.and_then(|id| self.captures.get(&id)); + let fingerprint = fingerprint(context.active_document_id, gdd, 0, &BTreeSet::new(), None, self.images_sent); + if self.graph_last_sent == Some(fingerprint) { + return; + } + self.graph_last_sent = Some(fingerprint); + let graph = document.zip(gdd).map(|(document, gdd)| graph_state(gdd, document.declarations(), captures)).unwrap_or_default(); + responses.add(FrontendMessage::UpdateHistoryGraph { graph }); + } } fn active_storage<'a>(context: &'a HistoryMessageContext) -> Option<&'a GddV1> { @@ -327,13 +367,7 @@ fn panel_state(gdd: &GddV1, declarations: &Declarations, captures: Option<&HashM peers: gdd.peers().collect(), }; - let mut children: HashMap> = HashMap::new(); - for delta in session.history() { - for parent in delta.all_parents() { - children.entry(parent).or_default().push(delta.id); - } - } - + let children = children(session); let head = session.head_rev(); let (head_line, _) = line(session, head, None, usize::MAX); let head_line_revs: HashSet = head_line.iter().map(|delta| delta.id).collect(); @@ -371,6 +405,229 @@ fn panel_state(gdd: &GddV1, declarations: &Declarations, captures: Option<&HashM } } +fn children(session: &Session) -> HashMap> { + let mut children: HashMap> = HashMap::new(); + for delta in session.history() { + for parent in delta.all_parents() { + children.entry(parent).or_default().push(delta.id); + } + } + children +} + +/// Every interaction in history as a tree on first parents. Each delta goes to the interactions of the first line that +/// reaches it: the one through the undone steps and the head, then each line from an unclaimed delta, newest first. +fn graph_state(gdd: &GddV1, declarations: &Declarations, captures: Option<&HashMap>) -> HistoryGraphState { + let session = gdd.session(); + let names = Names { + registry: gdd.registry(), + declarations, + }; + let people = People { + session, + metadata: gdd.metadata(), + peers: gdd.peers().collect(), + }; + let head = session.head_rev(); + let (head_line, _) = line(session, head, None, usize::MAX); + let head_line: HashSet = head_line.iter().map(|delta| delta.id).collect(); + let checkpoint = session.redo_stack().first().copied(); + let (undone, _) = checkpoint.map(|checkpoint| line(session, Some(checkpoint), head, usize::MAX)).unwrap_or_default(); + let undone: HashSet = undone.iter().map(|delta| delta.id).collect(); + + let newest_first: Vec = session.history().map(|delta| delta.id).collect::>().into_iter().rev().collect(); + let mut step_of: HashMap = HashMap::new(); + let mut groups: Vec> = Vec::new(); + for tip in checkpoint.or(head).into_iter().chain(newest_first) { + let mut unclaimed = Vec::new(); + let mut current = Some(tip); + while let Some(delta) = current.filter(|rev| !step_of.contains_key(rev)).and_then(|rev| session.delta(rev)) { + unclaimed.push(delta); + current = delta.parent; + } + for group in interactions(&unclaimed) { + step_of.extend(group.iter().map(|delta| (delta.id, groups.len()))); + groups.push(group); + } + } + + let parents: Vec> = groups.iter().map(|group| group.last()?.parent.and_then(|rev| step_of.get(&rev).copied())).collect(); + let places = tidy_tree(&parents); + + let children = children(session); + let expanded = BTreeSet::new(); + let view = View { + children: &children, + on_line: &head_line, + head_line: &head_line, + head, + people: &people, + names: &names, + captures, + expanded: &expanded, + session, + metadata: gdd.metadata(), + }; + let nodes: Vec = groups + .iter() + .zip(&parents) + .zip(places) + .enumerate() + .map(|(index, ((group, parent), (x, depth)))| { + let closing = group[0]; + let merged: BTreeSet = group + .iter() + .flat_map(|delta| delta.all_parents().skip(1)) + .filter_map(|rev| step_of.get(&rev).copied()) + .filter(|&step| step != index) + .collect(); + let undone = undone.contains(&closing.id); + HistoryGraphNode { + id: closing.id.to_string(), + label: view.label(group), + x, + depth, + parent: parent.map(|parent| groups[parent][0].id.to_string()), + merged: merged.into_iter().map(|step| groups[step][0].id.to_string()).collect(), + head: Some(closing.id) == head, + undone, + abandoned: !undone && !head_line.contains(&closing.id), + thumbnail: captures.and_then(|captures| thumbnail(captures, closing.timestamp)), + } + }) + .collect(); + + HistoryGraphState { + width: nodes.iter().map(|node| node.x + 1.).fold(0., f64::max), + depth: nodes.iter().map(|node| node.depth + 1).max().unwrap_or(0), + nodes, + } +} + +/// The left and right edges of a subtree at each depth, deepest first, each offset by `shift`. +struct Contour { + left: Vec, + right: Vec, + shift: f64, +} + +impl Contour { + fn leaf() -> Self { + Self { + left: vec![0.], + right: vec![0.], + shift: 0., + } + } + + fn levels(&self) -> usize { + self.left.len() + } + + /// Where the level `level` steps below the top is kept. + fn index(&self, level: usize) -> usize { + self.left.len() - 1 - level + } + + fn left(&self, level: usize) -> f64 { + self.left[self.index(level)] + self.shift + } + + fn right(&self, level: usize) -> f64 { + self.right[self.index(level)] + self.shift + } + + /// Adds a level on top holding one node at `x`. + fn push(&mut self, x: f64) { + self.left.push(x - self.shift); + self.right.push(x - self.shift); + } +} + +/// Places subtrees side by side, each as close to the ones before as their contours allow at every shared depth. Gives +/// each subtree's offset from the first and their joint contour, built on the deepest so a long line isn't copied. +fn pack(subtrees: impl IntoIterator) -> (Vec, Contour) { + let mut subtrees = subtrees.into_iter(); + let mut packed = subtrees.next().unwrap_or_else(Contour::leaf); + let mut offsets = vec![0.]; + for mut next in subtrees { + let shared = packed.levels().min(next.levels()); + let gap = (0..shared).map(|level| packed.right(level) - next.left(level)).fold(f64::MIN, f64::max) + 1.; + next.shift += gap; + offsets.push(gap); + // The shared depths take their left edge from the subtrees before and their right edge from the next + if next.levels() > packed.levels() { + for level in 0..shared { + let index = next.index(level); + next.left[index] = packed.left(level) - next.shift; + } + packed = next; + } else { + for level in 0..shared { + let index = packed.index(level); + packed.right[index] = next.right(level) - packed.shift; + } + } + } + (offsets, packed) +} + +/// Lays out a forest given by each node's parent, children in index order, as each node's column and depth. Siblings' +/// subtrees sit side by side as close as they fit, each parent centred over its children, and the roots in a row. +fn tidy_tree(parents: &[Option]) -> Vec<(f64, usize)> { + let mut children = vec![Vec::new(); parents.len()]; + let mut roots = Vec::new(); + for (node, parent) in parents.iter().enumerate() { + match parent { + Some(parent) => children[*parent].push(node), + None => roots.push(node), + } + } + // Breadth first, so each node comes after its parent + let mut order = roots.clone(); + let mut depth = vec![0; parents.len()]; + let mut next = 0; + while let Some(&node) = order.get(next) { + for &child in &children[node] { + depth[child] = depth[node] + 1; + order.push(child); + } + next += 1; + } + + let mut contours: Vec> = parents.iter().map(|_| None).collect(); + let mut offset = vec![0.; parents.len()]; + for &node in order.iter().rev() { + let contour = match children[node].as_slice() { + [] => Contour::leaf(), + kids => { + let (offsets, mut packed) = pack(kids.iter().filter_map(|&child| contours[child].take())); + let middle = (offsets[0] + offsets[offsets.len() - 1]) / 2.; + for (&child, child_offset) in kids.iter().zip(offsets) { + offset[child] = child_offset - middle; + } + packed.shift -= middle; + packed.push(0.); + packed + } + }; + contours[node] = Some(contour); + } + + let mut x = vec![0.; parents.len()]; + let (offsets, _) = pack(roots.iter().filter_map(|&root| contours[root].take())); + for (&root, root_offset) in roots.iter().zip(offsets) { + x[root] = root_offset; + } + for &node in &order { + if let Some(parent) = parents[node] { + x[node] = x[parent] + offset[node]; + } + } + let left = x.iter().copied().fold(f64::INFINITY, f64::min); + x.into_iter().zip(depth).map(|(x, depth)| (x - left, depth)).collect() +} + /// Open interactions first, then closed ones waiting to retire, newest first. fn progress_rows(session: &Session, people: &People, names: &Names, captures: Option<&HashMap>) -> Vec { hot_steps(session) @@ -861,6 +1118,76 @@ mod tests { assert_eq!(panel_state(&gdd, &Declarations::new(), None, PAGE, &BTreeSet::new(), None).rows.len(), 3); } + /// A document with an empty network added in each of `steps` interactions. + fn document_of_steps(steps: u64) -> GddV1 { + use document_container::AnyContainer; + use document_container::backends::memory::MemoryBackend; + use document_format::GddV1Layout; + + let mut gdd = GddV1::create_in(AnyContainer::Memory(MemoryBackend::new()), GddV1Layout, PeerId(1), UserId(1), 1, "editor".into(), "stdlib".into()).expect("create"); + (1..=steps).for_each(|id| add_step(&mut gdd, id)); + gdd + } + + fn add_step(gdd: &mut GddV1, id: u64) { + use document_graph_storage::{Network, NetworkId}; + + let network = Network::default(); + gdd.stage_ops([RegistryDelta::AddNetwork { id: NetworkId(id), network }]).expect("stage"); + gdd.retire_pending_interaction().expect("retire"); + } + + /// The graph's steps oldest first, as (depth, column, head, undone, abandoned). + fn graph_of(gdd: &GddV1) -> Vec<(usize, f64, bool, bool, bool)> { + let mut nodes: Vec<_> = graph_state(gdd, &Declarations::new(), None) + .nodes + .into_iter() + .map(|node| (node.depth, node.x, node.head, node.undone, node.abandoned)) + .collect(); + nodes.sort_by(|a, b| a.0.cmp(&b.0).then(a.1.total_cmp(&b.1))); + nodes + } + + #[test] + fn a_fork_places_its_branches_side_by_side() { + let mut gdd = document_of_steps(3); + gdd.undo().expect("undo"); + add_step(&mut gdd, 4); + + let nodes = graph_of(&gdd); + let depths: Vec = nodes.iter().map(|node| node.0).collect(); + assert_eq!(depths, [0, 1, 2, 2], "both steps after the second sit one step further out"); + let [(_, left, ..), (_, right, ..)] = [nodes[2], nodes[3]]; + assert!(right - left >= 1., "the branches don't overlap: {nodes:?}"); + assert_eq!(nodes[1].1, (left + right) / 2., "the fork is centred over its branches"); + let heads = nodes.iter().filter(|node| node.2).count(); + let abandoned = nodes.iter().filter(|node| node.4).count(); + assert_eq!((heads, abandoned), (1, 1), "the new step is the head and the undone one was left behind"); + } + + #[test] + fn undone_steps_sit_beyond_the_head() { + let mut gdd = document_of_steps(3); + gdd.undo().expect("undo"); + gdd.undo().expect("undo"); + + let nodes = graph_of(&gdd); + assert_eq!(nodes, [(0, 0., true, false, false), (1, 0., false, true, false), (2, 0., false, true, false)]); + } + + #[test] + fn packed_subtrees_never_overlap() { + // A root with two forks below it, the first one deeper + let parents = [None, Some(0), Some(0), Some(1), Some(1), Some(3), Some(3), Some(2), Some(2)]; + let places = tidy_tree(&parents); + for (a, &(x, depth)) in places.iter().enumerate() { + for &(other, other_depth) in &places[a + 1..] { + assert!(depth != other_depth || (x - other).abs() >= 1., "two steps overlap: {places:?}"); + } + } + assert!(places[3].0 < places[4].0 && places[4].0 < places[7].0, "children keep their order left to right: {places:?}"); + } + #[test] fn a_run_of_only_markers_is_named_by_them() { let registry = Registry::default(); diff --git a/editor/src/messages/portfolio/history/utility_types.rs b/editor/src/messages/portfolio/history/utility_types.rs index 30459e68e0..fe56289f86 100644 --- a/editor/src/messages/portfolio/history/utility_types.rs +++ b/editor/src/messages/portfolio/history/utility_types.rs @@ -109,6 +109,38 @@ pub struct HistoryDeltaRow { pub time: Option, } +/// Every step in history as a tree on first parents, for the history graph drawn over the viewport. +#[cfg_attr(feature = "wasm", derive(tsify::Tsify))] +#[derive(Clone, Debug, Default, PartialEq, serde::Serialize, serde::Deserialize)] +pub struct HistoryGraphState { + pub nodes: Vec, + /// The widest column plus one. + pub width: f64, + /// The deepest step's depth plus one. + pub depth: usize, +} + +/// One interaction, placed in the tree. +#[cfg_attr(feature = "wasm", derive(tsify::Tsify))] +#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)] +pub struct HistoryGraphNode { + /// The rev closing the interaction, in decimal. + pub id: String, + pub label: String, + /// The column, in step widths from the left. A parent sits centred over its children. + pub x: f64, + /// Steps from the root. + pub depth: usize, + pub parent: Option, + /// Steps joined into this one by a merge. + pub merged: Vec, + pub head: bool, + pub undone: bool, + /// Off the head's line and not undone. + pub abandoned: bool, + pub thumbnail: Option, +} + /// A person with unretired work in the room's hot log. #[cfg_attr(feature = "wasm", derive(tsify::Tsify))] #[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)] diff --git a/editor/src/messages/portfolio/portfolio_message_handler.rs b/editor/src/messages/portfolio/portfolio_message_handler.rs index 2ef540cd29..ce85f4b3f5 100644 --- a/editor/src/messages/portfolio/portfolio_message_handler.rs +++ b/editor/src/messages/portfolio/portfolio_message_handler.rs @@ -1142,7 +1142,7 @@ impl MessageHandler> for Portfolio } PortfolioMessage::UpdateDocumentWidgets => { if let Some(document) = self.active_document() { - document.update_document_widgets(responses, animation.is_playing(), timing_information.animation_time); + document.update_document_widgets(responses, animation.is_playing(), timing_information.animation_time, self.history.graph_open()); } } PortfolioMessage::UpdateOpenDocumentsList => { @@ -1177,6 +1177,7 @@ impl MessageHandler> for Portfolio PrevDocument, )); common.extend(actions!(IngestMessageDiscriminant; Import)); + common.extend(self.history.actions()); } common diff --git a/frontend/src/components/panels/Document.svelte b/frontend/src/components/panels/Document.svelte index b2d7a1f7d9..7d77c298e6 100644 --- a/frontend/src/components/panels/Document.svelte +++ b/frontend/src/components/panels/Document.svelte @@ -5,6 +5,7 @@ import LayoutCol from "/src/components/layout/LayoutCol.svelte"; import LayoutRow from "/src/components/layout/LayoutRow.svelte"; import Graph from "/src/components/views/Graph.svelte"; + import HistoryGraph from "/src/components/views/HistoryGraph.svelte"; import RemoteCursor from "/src/components/views/RemoteCursor.svelte"; import RulerInput from "/src/components/widgets/inputs/RulerInput.svelte"; import ScrollbarInput from "/src/components/widgets/inputs/ScrollbarInput.svelte"; @@ -12,6 +13,7 @@ import WidgetLayout from "/src/components/widgets/WidgetLayout.svelte"; import type { AppWindowStore } from "/src/stores/app-window"; import type { DocumentStore } from "/src/stores/document"; + import type { HistoryStore } from "/src/stores/history"; import type { SubscriptionsRouter } from "/src/subscriptions-router"; import type { MessageBody } from "/src/subscriptions-router"; import { fillChoiceColor, createSRgba8, sRgba8FromCSS } from "/src/utility-functions/colors"; @@ -31,6 +33,7 @@ const editor = getContext("editor"); const appWindow = getContext("appWindow"); const document = getContext("document"); + const historyGraphOpen = getContext("history").graphOpen; const remoteCursors = document.remoteCursorsStore; // Interactive text editing @@ -768,6 +771,9 @@
+ {#if $historyGraphOpen} + + {/if} + import { getContext, tick } from "svelte"; + import HistoryShot from "/src/components/views/HistoryShot.svelte"; + import IconButton from "/src/components/widgets/buttons/IconButton.svelte"; + import TextLabel from "/src/components/widgets/labels/TextLabel.svelte"; + import type { HistoryStore } from "/src/stores/history"; + import type { EditorWrapper, HistoryGraphNode } from "/wrapper/pkg/graphite_wasm_wrapper"; + + const editor = getContext("editor"); + const history = getContext("history"); + const graph = history.graph; + const thumbnails = history.thumbnails; + + // Each step's card and the room around it + const NODE_WIDTH = 168; + const NODE_HEIGHT = 44; + const COLUMN = 184; + const ROW = 72; + const PADDING = 24; + + let body: HTMLDivElement | undefined; + let scrolledToHead = false; + + $: byId = new Map($graph.nodes.map((node) => [node.id, node])); + $: head = $graph.nodes.find((node) => node.head); + $: if (head && !scrolledToHead) scrollToHead(); + + function left(node: HistoryGraphNode): number { + return PADDING + node.x * COLUMN; + } + + function top(node: HistoryGraphNode): number { + return PADDING + node.depth * ROW; + } + + // From the bottom of the parent's card to the top of the child's + function edge(parent: HistoryGraphNode, child: HistoryGraphNode): string { + const [x1, y1] = [left(parent) + NODE_WIDTH / 2, top(parent) + NODE_HEIGHT]; + const [x2, y2] = [left(child) + NODE_WIDTH / 2, top(child)]; + const middle = (y1 + y2) / 2; + return `M ${x1} ${y1} C ${x1} ${middle}, ${x2} ${middle}, ${x2} ${y2}`; + } + + function onHeadLine(node: HistoryGraphNode): boolean { + return !node.undone && !node.abandoned; + } + + function tooltip(node: HistoryGraphNode): string { + if (node.head) return "Where the document is now"; + if (node.undone) return "Redo up to here"; + if (node.abandoned) return "Show this branch in the History panel"; + return "Go back to here"; + } + + function choose(node: HistoryGraphNode) { + if (node.abandoned) editor.followHistoryBranch(node.id); + else if (!node.head) editor.goBackToHistoryInteraction(node.id); + } + + async function scrollToHead() { + scrolledToHead = true; + await tick(); + body?.querySelector("[data-head]")?.scrollIntoView({ block: "center", inline: "center" }); + } + + + +
+
+ History + editor.setHistoryGraphOpen(false)} /> +
+
+ {#if $graph.nodes.length === 0} + Nothing in the history yet. + {/if} +
+ + {#each $graph.nodes as node (node.id)} + {@const parent = node.parent ? byId.get(node.parent) : undefined} + {#if parent} + + {/if} + {#each node.merged as merged} + {@const other = byId.get(merged)} + {#if other}{/if} + {/each} + {/each} + + {#each $graph.nodes as node (node.id)} +
+ + {#if node.abandoned} + editor.bringBackHistoryInteraction(node.id)} + /> + {/if} + {#if node.head}Now{/if} +
+ {/each} +
+
+
+ + diff --git a/frontend/src/stores/history.ts b/frontend/src/stores/history.ts index f3d1676b64..99837eb4ca 100644 --- a/frontend/src/stores/history.ts +++ b/frontend/src/stores/history.ts @@ -1,7 +1,7 @@ import { writable } from "svelte/store"; import type { Writable } from "svelte/store"; import type { SubscriptionsRouter } from "/src/subscriptions-router"; -import type { HistoryPanelState } from "/wrapper/pkg/graphite_wasm_wrapper"; +import type { HistoryGraphState, HistoryPanelState } from "/wrapper/pkg/graphite_wasm_wrapper"; export type HistoryStore = ReturnType; @@ -11,9 +11,14 @@ const initialState: HistoryPanelState = { rows: [], progress: [], more: false, f const store: Writable = import.meta.hot?.data?.store || writable(initialState); // The captured images by the ID rows name them with, sent once each const thumbnails: Writable> = import.meta.hot?.data?.thumbnails || writable>({}); +// The history graph drawn over the viewport, sent only while it is open +const graph: Writable = import.meta.hot?.data?.graph || writable({ nodes: [], width: 0, depth: 0 }); +const graphOpen: Writable = import.meta.hot?.data?.graphOpen || writable(false); if (import.meta.hot) { import.meta.hot.data.store = store; import.meta.hot.data.thumbnails = thumbnails; + import.meta.hot.data.graph = graph; + import.meta.hot.data.graphOpen = graphOpen; } const { subscribe, set } = store; @@ -79,7 +84,15 @@ export function createHistoryStore(subscriptions: SubscriptionsRouter) { keepThumbnail(data.id, encodePixels(data.pixels, data.width, data.height)); }); - return { subscribe, thumbnails: { subscribe: thumbnails.subscribe } }; + subscriptions.subscribeFrontendMessage("UpdateHistoryGraph", (data) => { + graph.set(data.graph); + }); + + subscriptions.subscribeFrontendMessage("UpdateHistoryGraphOverlay", (data) => { + graphOpen.set(data.open); + }); + + return { subscribe, thumbnails: { subscribe: thumbnails.subscribe }, graph: { subscribe: graph.subscribe }, graphOpen: { subscribe: graphOpen.subscribe } }; } export function destroyHistoryStore() { @@ -89,5 +102,7 @@ export function destroyHistoryStore() { subscriptions.unsubscribeFrontendMessage("UpdateHistoryPanel"); subscriptions.unsubscribeFrontendMessage("UpdateHistoryThumbnail"); subscriptions.unsubscribeFrontendMessage("UpdateHistoryThumbnailPixels"); + subscriptions.unsubscribeFrontendMessage("UpdateHistoryGraph"); + subscriptions.unsubscribeFrontendMessage("UpdateHistoryGraphOverlay"); subscriptionsRouter = undefined; } diff --git a/frontend/wrapper/src/editor_commands.rs b/frontend/wrapper/src/editor_commands.rs index 94390d28ca..05baac789a 100644 --- a/frontend/wrapper/src/editor_commands.rs +++ b/frontend/wrapper/src/editor_commands.rs @@ -300,6 +300,11 @@ mod editor_commands { HistoryMessage::Tag { id, tag, on }.into() } + /// Show or hide the history graph over the viewport + fn set_history_graph_open(open: bool) -> Message { + HistoryMessage::GraphOverlay { open }.into() + } + fn split_panel_group(target_group: u64, direction: Ts, tabs: Vec>, active_tab_index: usize) -> Message { WorkspaceMessage::SplitPanelGroup { target_group: PanelGroupId(target_group),