@ai-matrx/office 0.8.37 → 0.8.38

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.
@@ -1225,29 +1225,38 @@ function undoStacksOf(service) {
1225
1225
  if (!s || !(s._undoStacks instanceof Map) || !(s._redoStacks instanceof Map)) return null;
1226
1226
  return { undo: s._undoStacks, redo: s._redoStacks };
1227
1227
  }
1228
- function rebaseUndoStack(items, remote) {
1228
+ function threadThrough(list, remote) {
1229
+ if (!list?.length || isNoopDocsOp(remote)) return { next: null, after: remote };
1230
+ let R = remote;
1231
+ let changed = false;
1232
+ const next = list.map((m) => {
1233
+ if (m.id !== RICH_TEXT_EDITING || isNoopDocsOp(R)) return m;
1234
+ const own = m.params.actions;
1235
+ if (touchesPageSetup(own) && touchesPageSetup(R)) throw new Error("page setup");
1236
+ const x = transformDocsOp(own, R, "left");
1237
+ if (isNoopDocsOp(x.op)) throw new Error("emptied");
1238
+ R = transformDocsOp(R, own, "right").op;
1239
+ changed = true;
1240
+ const params = { ...m.params, actions: x.op };
1241
+ delete params.textRanges;
1242
+ delete params.layoutState;
1243
+ return { ...m, params };
1244
+ });
1245
+ return { next: changed ? next : null, after: R };
1246
+ }
1247
+ function rebaseUndoStack(items, remote, stack) {
1248
+ if (isNoopDocsOp(remote)) return 0;
1249
+ const first = stack === "undo" ? "undoMutations" : "redoMutations";
1250
+ const second = stack === "undo" ? "redoMutations" : "undoMutations";
1229
1251
  let dropped = 0;
1230
1252
  for (let i = items.length - 1; i >= 0; i--) {
1231
1253
  const item = items[i];
1232
- let ok3 = true;
1233
- for (const list of [item.undoMutations, item.redoMutations]) {
1234
- for (const m of list ?? []) {
1235
- if (m.id !== RICH_TEXT_EDITING) continue;
1236
- try {
1237
- if (touchesPageSetup(m.params.actions) && touchesPageSetup(remote)) throw new Error("page setup");
1238
- const r = transformDocsOp(m.params.actions, remote, "left");
1239
- if (isNoopDocsOp(r.op)) ok3 = false;
1240
- else {
1241
- m.params = { ...m.params, actions: r.op };
1242
- delete m.params.textRanges;
1243
- delete m.params.layoutState;
1244
- }
1245
- } catch {
1246
- ok3 = false;
1247
- }
1248
- }
1249
- }
1250
- if (!ok3) {
1254
+ try {
1255
+ const a = threadThrough(item[first], remote);
1256
+ const b = threadThrough(item[second], a.after);
1257
+ if (a.next) item[first] = a.next;
1258
+ if (b.next) item[second] = b.next;
1259
+ } catch {
1251
1260
  items.splice(i, 1);
1252
1261
  dropped++;
1253
1262
  }
@@ -1268,6 +1277,7 @@ function openDocsOtRoom(o) {
1268
1277
  }
1269
1278
  };
1270
1279
  const applyNow = (op) => {
1280
+ for (const t of port.undoTargets()) t.flushHistory?.();
1271
1281
  for (const b of branches(op)) {
1272
1282
  const params = { segmentId: segmentOf(b), actions: b };
1273
1283
  port.applyRemote(RICH_TEXT_EDITING, params);
@@ -1275,10 +1285,10 @@ function openDocsOtRoom(o) {
1275
1285
  for (const t of port.undoTargets()) {
1276
1286
  const stacks = undoStacksOf(t.service);
1277
1287
  if (!stacks) continue;
1278
- for (const m of [stacks.undo, stacks.redo]) {
1279
- const items = m.get(t.unitId);
1280
- if (items?.length) rebaseUndoStack(items, op);
1281
- }
1288
+ const undo = stacks.undo.get(t.unitId);
1289
+ if (undo?.length) rebaseUndoStack(undo, op, "undo");
1290
+ const redo = stacks.redo.get(t.unitId);
1291
+ if (redo?.length) rebaseUndoStack(redo, op, "redo");
1282
1292
  }
1283
1293
  port.markDirty();
1284
1294
  };