Add a history graph overlay that draws every step and branch over the viewport as a tree

This commit is contained in:
Dennis Kobert committed 2026-11-07 19:46:38 +00:00
1 parent ad11756f4e
commit e9e0477de3
11 files changed
+667 -22

No files matched your search

@@ -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,
@@ -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),
@@ -3859,7 +3859,7 @@ impl DocumentMessageHandler {
]
}
pub fn update_document_widgets(&self, responses: &mut VecDeque<Message>, animation_is_playing: bool, time: Duration) {
pub fn update_document_widgets(&self, responses: &mut VecDeque<Message>, 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" })
@@ -31,6 +31,10 @@ pub enum HistoryMessage {
tag: String,
on: bool,
},
GraphOverlay {
open: bool,
},
GraphOverlayToggle,
Captured {
document_id: DocumentId,
step: document_graph_storage::TimeStamp,
@@ -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<u64>,
}
/// What a captured step showed. The frontend keeps its images.
@@ -77,20 +81,29 @@ impl MessageHandler<HistoryMessage, HistoryMessageContext<'_>> 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<HistoryMessage, HistoryMessageContext<'_>> 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<Message>) {
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<Message>) {
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<Rev, Vec<Rev>> = 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<Rev> = 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<Rev, Vec<Rev>> {
let mut children: HashMap<Rev, Vec<Rev>> = 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<TimeStamp, Capture>>) -> 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<Rev> = 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<Rev> = undone.iter().map(|delta| delta.id).collect();
let newest_first: Vec<Rev> = session.history().map(|delta| delta.id).collect::<Vec<_>>().into_iter().rev().collect();
let mut step_of: HashMap<Rev, usize> = HashMap::new();
let mut groups: Vec<Vec<&Delta>> = 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<Option<usize>> = 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<HistoryGraphNode> = groups
.iter()
.zip(&parents)
.zip(places)
.enumerate()
.map(|(index, ((group, parent), (x, depth)))| {
let closing = group[0];
let merged: BTreeSet<usize> = 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<f64>,
right: Vec<f64>,
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<Item = Contour>) -> (Vec<f64>, 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<usize>]) -> 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<Option<Contour>> = 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<TimeStamp, Capture>>) -> Vec<HistoryProgressRow> {
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<usize> = 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();
@@ -109,6 +109,38 @@ pub struct HistoryDeltaRow {
pub time: Option<f64>,
}
/// 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<HistoryGraphNode>,
/// 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<String>,
/// Steps joined into this one by a merge.
pub merged: Vec<String>,
pub head: bool,
pub undone: bool,
/// Off the head's line and not undone.
pub abandoned: bool,
pub thumbnail: Option<HistoryThumbnail>,
}
/// 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)]
@@ -1142,7 +1142,7 @@ impl MessageHandler<PortfolioMessage, PortfolioMessageContext<'_>> 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<PortfolioMessage, PortfolioMessageContext<'_>> for Portfolio
PrevDocument,
));
common.extend(actions!(IngestMessageDiscriminant; Import));
common.extend(self.history.actions());
}
common
@@ -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<EditorWrapper>("editor");
const appWindow = getContext<AppWindowStore>("appWindow");
const document = getContext<DocumentStore>("document");
const historyGraphOpen = getContext<HistoryStore>("history").graphOpen;
const remoteCursors = document.remoteCursorsStore;
// Interactive text editing
@@ -768,6 +771,9 @@
<div class="graph-view" class:open={$document.graphViewOverlayOpen} style:--fade-artwork={`${$document.fadeArtwork}%`} data-graph>
<Graph />
</div>
{#if $historyGraphOpen}
<HistoryGraph />
{/if}
</LayoutCol>
<LayoutCol class="ruler-or-scrollbar right-scrollbar">
<ScrollbarInput
@@ -0,0 +1,240 @@
<script lang="ts">
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<EditorWrapper>("editor");
const history = getContext<HistoryStore>("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" });
}
</script>
<!-- Pointer presses and wheel scrolls stay here rather than reaching the viewport's tools beneath -->
<div class="history-graph" on:pointerdown|stopPropagation on:wheel|stopPropagation>
<div class="header">
<TextLabel>History</TextLabel>
<IconButton icon="CloseX" size={16} tooltipLabel="Hide the history graph" action={() => editor.setHistoryGraphOpen(false)} />
</div>
<div class="body" bind:this={body}>
{#if $graph.nodes.length === 0}
<TextLabel italic={true}>Nothing in the history yet.</TextLabel>
{/if}
<div class="canvas" style:width={`${PADDING * 2 + Math.max(0, $graph.width - 1) * COLUMN + NODE_WIDTH}px`} style:height={`${PADDING * 2 + $graph.depth * ROW}px`}>
<svg class="edges">
{#each $graph.nodes as node (node.id)}
{@const parent = node.parent ? byId.get(node.parent) : undefined}
{#if parent}
<path d={edge(parent, node)} class:head-line={onHeadLine(node)} class:undone={node.undone} />
{/if}
{#each node.merged as merged}
{@const other = byId.get(merged)}
{#if other}<path d={edge(other, node)} class="merge" />{/if}
{/each}
{/each}
</svg>
{#each $graph.nodes as node (node.id)}
<div
class="node"
class:head={node.head}
class:head-line={onHeadLine(node)}
class:undone={node.undone}
class:abandoned={node.abandoned}
style:left={`${left(node)}px`}
style:top={`${top(node)}px`}
style:width={`${NODE_WIDTH}px`}
style:height={`${NODE_HEIGHT}px`}
data-head={node.head || undefined}
>
<button class="step" data-tooltip-label={tooltip(node)} on:click={() => choose(node)}>
{#if node.thumbnail}<HistoryShot thumbnail={node.thumbnail} images={$thumbnails} />{/if}
<span class="label">{node.label}</span>
</button>
{#if node.abandoned}
<IconButton
icon="HistoryRedo"
size={16}
tooltipLabel="Bring back"
tooltipDescription="Do this step again on top of the current line."
action={() => editor.bringBackHistoryInteraction(node.id)}
/>
{/if}
{#if node.head}<span class="marker">Now</span>{/if}
</div>
{/each}
</div>
</div>
</div>
<style lang="scss">
.history-graph {
position: absolute;
inset: 0;
display: flex;
flex-direction: column;
background: var(--color-2-mildblack);
.header {
flex: 0 0 auto;
display: flex;
align-items: center;
justify-content: space-between;
padding: 4px 8px;
border-bottom: 1px solid var(--color-4-dimgray);
}
.body {
flex: 1 1 0;
overflow: auto;
position: relative;
> .text-label {
position: absolute;
padding: 16px;
}
}
.canvas {
position: relative;
}
.edges {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
pointer-events: none;
path {
fill: none;
stroke: var(--color-5-dullgray);
stroke-width: 2;
&.head-line {
stroke: var(--color-e-nearwhite);
}
&.undone {
stroke-dasharray: 4 3;
}
&.merge {
stroke-dasharray: 2 4;
}
}
}
.node {
position: absolute;
box-sizing: border-box;
display: flex;
align-items: center;
gap: 2px;
padding: 0 4px 0 0;
border-radius: 4px;
border: 1px solid var(--color-4-dimgray);
background: var(--color-3-darkgray);
&.head-line {
border-color: var(--color-8-uppergray);
}
&.head {
border-color: var(--color-e-nearwhite);
background: var(--color-4-dimgray);
}
&.undone,
&.abandoned {
.step {
opacity: 0.6;
}
}
.step {
flex: 1 1 0;
min-width: 0;
height: 100%;
display: flex;
align-items: center;
gap: 6px;
padding: 0 0 0 6px;
border: none;
background: none;
color: var(--color-e-nearwhite);
text-align: left;
cursor: pointer;
.label {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
}
.marker {
flex: 0 0 auto;
font-size: 10px;
line-height: 14px;
padding: 0 4px;
border-radius: 2px;
background: var(--color-5-dullgray);
color: var(--color-e-nearwhite);
}
}
}
</style>
+17 -2
View File
@@ -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<typeof createHistoryStore>;
@@ -11,9 +11,14 @@ const initialState: HistoryPanelState = { rows: [], progress: [], more: false, f
const store: Writable<HistoryPanelState> = import.meta.hot?.data?.store || writable<HistoryPanelState>(initialState);
// The captured images by the ID rows name them with, sent once each
const thumbnails: Writable<Record<string, string>> = import.meta.hot?.data?.thumbnails || writable<Record<string, string>>({});
// The history graph drawn over the viewport, sent only while it is open
const graph: Writable<HistoryGraphState> = import.meta.hot?.data?.graph || writable<HistoryGraphState>({ nodes: [], width: 0, depth: 0 });
const graphOpen: Writable<boolean> = import.meta.hot?.data?.graphOpen || writable<boolean>(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;
}
+5
View File
@@ -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<DockingSplitDirection>, tabs: Vec<Ts<PanelType>>, active_tab_index: usize) -> Message {
WorkspaceMessage::SplitPanelGroup {
target_group: PanelGroupId(target_group),