Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : Note SPBoucher AI8 * File : src/lib/canvas/history.ts9 * Description : undo / redo history of the canvas editor (its own,10 * independent of the browser's): immutable snapshots of the11 * document that share every untouched node (documents are12 * edited by spreading). A gesture (drag, resize, typing run)13 * is one entry: its intermediate states are `transient` and14 * only `settle` records the step. Viewport changes are never15 * history. Pure.16 * Module : lib17 * Repository : https://git.spboucher.ai/note-spboucher-ai18 * Project Website : https://note.spboucher.ai19 * Created : 2026-11-1920 * Last Modified : 2026-11-1921 *22 * Author : Simon-Pierre Boucher23 * Position : Professor of Finance24 * Institution : Université du Québec en Outaouais (UQO)25 * Organization : SP Boucher26 * Website : https://spboucher.ai27 * Git : https://git.spboucher.ai28 *29 * Preferred Contact: contact@spboucher.ai30 * Development : dev@spboucher.ai31 * Support : support@spboucher.ai32 * Security/Admin : admin@spboucher.ai33 *34 * Copyright (c) 202635 * Simon-Pierre Boucher36 * All rights reserved where applicable. This file is governed by the37 * applicable project license, copyright notices and relevant law.38 * Existing third-party rights and notices remain unaffected.39 *40 * License : None specified (no license file in this repository)41 *42 * Do not embed credentials, private keys, access tokens,43 * certificates or confidential personal information.44 *45 * =============================================================================46 * END OF SP BOUCHER SOURCE HEADER47 * =============================================================================48 */4950import type { CanvasData } from "./format";5152export interface History {53 past: CanvasData[]; present: CanvasData; future: CanvasData[];54 /** The document before the gesture in progress (null when idle). */55 gesture: CanvasData | null;56}57export const HISTORY_LIMIT = 200;58export const createHistory = (present: CanvasData): History => ({ past: [], present, future: [], gesture: null });59const push = (past: CanvasData[], d: CanvasData) => (past.length >= HISTORY_LIMIT ? [...past.slice(past.length - HISTORY_LIMIT + 1), d] : [...past, d]);60/** One undoable step. A no-op (same object) changes nothing. Ends any gesture in progress first. */61export function commit(h: History, next: CanvasData): History {62 const s = h.gesture ? settle(h) : h;63 return next === s.present ? s : { past: push(s.past, s.present), present: next, future: [], gesture: null };64}65/** An intermediate state of a gesture: shown, not recorded. */66export function transient(h: History, next: CanvasData): History {67 return next === h.present ? h : { ...h, present: next, gesture: h.gesture ?? h.present };68}69/** Ends the gesture: everything since it began becomes one step (nothing when the document is back where it started). */70export function settle(h: History): History {71 if (!h.gesture) return h;72 return h.gesture === h.present ? { ...h, gesture: null } : { past: push(h.past, h.gesture), present: h.present, future: [], gesture: null };73}74/** Abandons the gesture: back to the document before it. */75export function cancelGesture(h: History): History { return h.gesture ? { ...h, present: h.gesture, gesture: null } : h; }76export function undo(h: History): History {77 const s = h.gesture ? settle(h) : h; if (!s.past.length) return s;78 return { past: s.past.slice(0, -1), present: s.past[s.past.length - 1], future: [s.present, ...s.future], gesture: null };79}80export function redo(h: History): History {81 if (h.gesture || !h.future.length) return h;82 return { past: push(h.past, h.present), present: h.future[0], future: h.future.slice(1), gesture: null };83}84/**85 * A document that did not come from the user (the server copy, the result of a merge): it replaces the present and the history is86 * dropped — an older snapshot would not contain what the other session added, and undoing to it would delete that work on the next save.87 */88export const replaceHistory = (_h: History, next: CanvasData): History => createHistory(next);89export const canUndo = (h: History) => h.past.length > 0 || (h.gesture !== null && h.gesture !== h.present);90export const canRedo = (h: History) => h.future.length > 0 && !h.gesture;91