@ggraphic/editor 0.1.0-alpha.4 → 0.1.0-alpha.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,5 +2,5 @@ import * as Y from 'yjs';
2
2
  export declare function useYjsPageStateSync(options: {
3
3
  pageYMap: Y.Map<unknown> | null;
4
4
  isSynced: boolean;
5
- onRemoteState: (partial: Record<string, unknown>, base: Record<string, unknown>) => void;
5
+ onRemoteState: (partial: Record<string, unknown>, base: Record<string, unknown>, own: boolean) => void;
6
6
  }): void;
@@ -28,7 +28,10 @@ import { PAGES_STATE_ORIGIN } from './useYjsPages.js';
28
28
  *
29
29
  * Own writes with another origin (a deck op, a deck undo's restore, a version
30
30
  * lift) reach the canvas like remote ones: another stack owns them or they
31
- * are not undoable, so the canvas history takes them in.
31
+ * are not undoable, so the canvas history takes them in. They are reported
32
+ * with `own: true` (a local Yjs transaction): the history takes them in only
33
+ * where it still holds the value they replaced, so undoing a deck op leaves
34
+ * the steps before it undoable.
32
35
  */
33
36
  /** Own values kept per key, to name the one a remote write started from. */
34
37
  const OWN_VALUES_PER_KEY = 32;
@@ -109,7 +112,7 @@ export function useYjsPageStateSync(options) {
109
112
  }
110
113
  }
111
114
  if (Object.keys(partial).length > 0)
112
- onRemoteStateRef.current(partial, base);
115
+ onRemoteStateRef.current(partial, base, events[0].transaction.local);
113
116
  };
114
117
  pageYMap.observeDeep(handler);
115
118
  return () => {
@@ -165,11 +165,11 @@ function GenericCanvasWithRef(props) {
165
165
  // is measured from the values its writer started from, not from this
166
166
  // canvas: an own value not yet written, or one the write overrode, is no
167
167
  // part of the other client's edit.
168
- const handleRemotePageState = useCallback((partial, base) => {
168
+ const handleRemotePageState = useCallback((partial, base, own) => {
169
169
  const committed = committedStateRef.current;
170
170
  store
171
171
  .getState()
172
- .rebaseHistory(config.createInitialState({ ...committed, ...base }), config.createInitialState({ ...committed, ...partial }));
172
+ .rebaseHistory(config.createInitialState({ ...committed, ...base }), config.createInitialState({ ...committed, ...partial }), own);
173
173
  setStateRaw((prev) => config.createInitialState({ ...prev, ...partial }));
174
174
  setRemoteStateCount((n) => n + 1);
175
175
  }, [store, config]);
@@ -79,9 +79,12 @@ export interface CanvasEditorActions<TComponentState extends Record<string, unkn
79
79
  /**
80
80
  * Another client changed the page from `prev` to `next`: write that change
81
81
  * into every entry, so undo and redo only ever revert this client's own
82
- * steps (#160).
82
+ * steps (#160). With `own`, the write is this client's own from another
83
+ * stack (a deck op or its undo): it reaches only the entries that still hold
84
+ * the replaced value, so its inverse restores every entry and the steps
85
+ * before it stay undoable.
83
86
  */
84
- rebaseHistory: (prev: TComponentState, next: TComponentState) => void;
87
+ rebaseHistory: (prev: TComponentState, next: TComponentState, own?: boolean) => void;
85
88
  undo: () => void;
86
89
  redo: () => void;
87
90
  /** Merkt den aktuellen Verlaufsstand als Beginn einer Sitzung; `null` ohne Verlauf. */
@@ -13,7 +13,7 @@ import { immer } from 'zustand/middleware/immer';
13
13
  import { createStore } from 'zustand/vanilla';
14
14
  import { activeBrand } from '../brand/brandRegistry.js';
15
15
  import { nextHistoryStamp } from '../utils/historyStamp.js';
16
- import { applyHistoryRebase, planHistoryRebase } from '../utils/rebaseHistoryState.js';
16
+ import { applyHistoryRebase, applyOwnWrite, planHistoryRebase, } from '../utils/rebaseHistoryState.js';
17
17
  import { DEFAULT_FORMAT_ID, getCanvasFormatOrDefault } from '../formats/index.js';
18
18
  // Einzige Stelle, die die Auswahl schreibt: `selectedElement` ist genau dann
19
19
  // gesetzt, wenn ein einziges Element ausgewählt ist. Alle Einzel-Oberflächen
@@ -141,12 +141,16 @@ export function createCanvasEditorStore() {
141
141
  state.historyIndex++;
142
142
  }
143
143
  }),
144
- rebaseHistory: (prev, next) => {
145
- const plan = planHistoryRebase(prev, next);
146
- if (plan.length === 0)
144
+ rebaseHistory: (prev, next, own = false) => {
145
+ const plan = own ? null : planHistoryRebase(prev, next);
146
+ if (plan?.length === 0)
147
147
  return;
148
148
  const { history } = get();
149
- const rebased = history.map((entry) => entry.componentState ? applyHistoryRebase(entry.componentState, plan) : null);
149
+ const rebased = history.map((entry) => !entry.componentState
150
+ ? null
151
+ : plan
152
+ ? applyHistoryRebase(entry.componentState, plan)
153
+ : applyOwnWrite(entry.componentState, prev, next));
150
154
  if (rebased.every((componentState, i) => componentState === history[i].componentState))
151
155
  return;
152
156
  set((state) => {
@@ -68,4 +68,13 @@ export type HistoryRebase = KeyPlan[];
68
68
  export declare function planHistoryRebase(prev: Rec, next: Rec): HistoryRebase;
69
69
  /** `entry` with the planned change applied; `entry` itself when the change does not reach it. */
70
70
  export declare function applyHistoryRebase(entry: Rec, plan: HistoryRebase): Rec;
71
+ /**
72
+ * `entry` with an own write from another stack applied (a deck op, its undo
73
+ * by the page-level Y.UndoManager, a version lift): per key, only where the
74
+ * entry still holds the value the write replaced. An entry that holds another
75
+ * value predates an own step, and that step stays undoable. Applied twice,
76
+ * once for the write and once for its exact inverse, every entry is back
77
+ * where it was. `entry` itself when the write does not reach it.
78
+ */
79
+ export declare function applyOwnWrite(entry: Rec, prev: Rec, next: Rec): Rec;
71
80
  export {};
@@ -206,3 +206,29 @@ export function applyHistoryRebase(entry, plan) {
206
206
  }
207
207
  return result ?? entry;
208
208
  }
209
+ /**
210
+ * `entry` with an own write from another stack applied (a deck op, its undo
211
+ * by the page-level Y.UndoManager, a version lift): per key, only where the
212
+ * entry still holds the value the write replaced. An entry that holds another
213
+ * value predates an own step, and that step stays undoable. Applied twice,
214
+ * once for the write and once for its exact inverse, every entry is back
215
+ * where it was. `entry` itself when the write does not reach it.
216
+ */
217
+ export function applyOwnWrite(entry, prev, next) {
218
+ let result = null;
219
+ for (const key of new Set([...Object.keys(prev), ...Object.keys(next)])) {
220
+ if (jsonEqual(prev[key], next[key]))
221
+ continue;
222
+ const holdsPrev = Object.hasOwn(prev, key)
223
+ ? Object.hasOwn(entry, key) && jsonEqual(entry[key], prev[key])
224
+ : !Object.hasOwn(entry, key);
225
+ if (!holdsPrev)
226
+ continue;
227
+ result ??= { ...entry };
228
+ if (Object.hasOwn(next, key))
229
+ result[key] = next[key];
230
+ else
231
+ delete result[key];
232
+ }
233
+ return result ?? entry;
234
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ggraphic/editor",
3
- "version": "0.1.0-alpha.4",
3
+ "version": "0.1.0-alpha.5",
4
4
  "license": "AGPL-3.0-only",
5
5
  "repository": {
6
6
  "type": "git",