@ai-matrx/office 0.8.54 → 0.8.56

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.
@@ -65,6 +65,7 @@ __export(collab_exports, {
65
65
  invertDocsOp: () => invertDocsOp,
66
66
  isCellEditorScratch: () => isCellEditorScratch,
67
67
  isNoopDocsOp: () => isNoopDocsOp,
68
+ joiningAwareness: () => joiningAwareness,
68
69
  moveSelectionsThrough: () => moveSelectionsThrough,
69
70
  normalizeDocsOp: () => normalizeDocsOp,
70
71
  offlineTextOfGroups: () => offlineTextOfGroups,
@@ -1881,6 +1882,12 @@ var SHEET_MUTATIONS = {
1881
1882
  "sheet.mutation.set-worksheet-row-is-auto-height": covered("size", "ranges", "autoHeightInfo"),
1882
1883
  "sheet.mutation.set-worksheet-row-auto-height": covered("size", "rowsAutoHeightInfo"),
1883
1884
  "sheet.mutation.set-worksheet-col-width": covered("size", "ranges", "colWidth"),
1885
+ // a row or column's hidden flag (`hd`): per index like a size, the later of two wins (the gate's F5: Make table
1886
+ // hides its Record ID column, which was rolled back as "not shared" in a room)
1887
+ "sheet.mutation.set-row-hidden": covered("size", "ranges"),
1888
+ "sheet.mutation.set-row-visible": covered("size", "ranges"),
1889
+ "sheet.mutation.set-col-hidden": covered("size", "ranges"),
1890
+ "sheet.mutation.set-col-visible": covered("size", "ranges"),
1884
1891
  "sheet.mutation.set-worksheet-name": covered("name", "name"),
1885
1892
  // ── covered (C5c) ────────────────────────────────────────────────────────────────────────────────
1886
1893
  "sheet.mutation.set.numfmt": covered("numfmt", "values", "refMap"),
@@ -1937,10 +1944,6 @@ var SHEET_MUTATIONS = {
1937
1944
  "sheet.mutation.set-frozen": barrier("X", "absolute frozen indices: not in C5a"),
1938
1945
  "sheet.mutation.set-row-data": barrier("X", "row properties by index: not in C5a"),
1939
1946
  "sheet.mutation.set-col-data": barrier("X", "column properties by index: not in C5a"),
1940
- "sheet.mutation.set-row-hidden": barrier("X", "row visibility: not in C5a"),
1941
- "sheet.mutation.set-row-visible": barrier("X", "row visibility: not in C5a"),
1942
- "sheet.mutation.set-col-hidden": barrier("X", "column visibility: not in C5a"),
1943
- "sheet.mutation.set-col-visible": barrier("X", "column visibility: not in C5a"),
1944
1947
  "sheet.mutation.add-range-protection": barrier("X", "permissions: not in C5a"),
1945
1948
  "sheet.mutation.set-range-protection": barrier("X", "permissions: not in C5a"),
1946
1949
  "sheet.mutation.delete-range-protection": barrier("X", "permissions: not in C5a"),
@@ -2615,6 +2618,10 @@ var ID = {
2615
2618
  rowIsAuto: "sheet.mutation.set-worksheet-row-is-auto-height",
2616
2619
  rowAutoHeight: "sheet.mutation.set-worksheet-row-auto-height",
2617
2620
  colWidth: "sheet.mutation.set-worksheet-col-width",
2621
+ colHidden: "sheet.mutation.set-col-hidden",
2622
+ colVisible: "sheet.mutation.set-col-visible",
2623
+ rowHidden: "sheet.mutation.set-row-hidden",
2624
+ rowVisible: "sheet.mutation.set-row-visible",
2618
2625
  name: "sheet.mutation.set-worksheet-name",
2619
2626
  setName: "formula.mutation.set-defined-name",
2620
2627
  removeName: "formula.mutation.remove-defined-name",
@@ -2913,7 +2920,8 @@ function transformCell(X, Y4, side) {
2913
2920
  out.s = { ...outS && typeof outS === "object" ? outS : {}, n: null };
2914
2921
  } else if (side === "right" && outS && typeof outS === "object" && "n" in outS) {
2915
2922
  const rest = withoutStyleKeys(outS, /* @__PURE__ */ new Set(["n"]));
2916
- if (rest) out.s = rest;
2923
+ if (!has(Y4.s) && Y4.s !== null) out.s = { ...rest ?? {}, n: null };
2924
+ else if (rest) out.s = rest;
2917
2925
  else delete out.s;
2918
2926
  xLost = true;
2919
2927
  }
@@ -2951,6 +2959,34 @@ function transformCellsOverCells(x, y, side) {
2951
2959
  const op = passes.filter((p) => !isEmptyMatrix(p)).map((p) => cellsMutation(x, p));
2952
2960
  return ok2(op, lost, dropped);
2953
2961
  }
2962
+ var coveredByMerge = (ranges, r, c) => ranges.some((q) => q.startRow <= r && r <= q.endRow && q.startColumn <= c && c <= q.endColumn && !(r === q.startRow && c === q.startColumn));
2963
+ var EMPTY_CONTENT = Object.fromEntries(CONTENT_KEYS.filter((k) => k !== "t").map((k) => [k, null]));
2964
+ function valuesUnderMerge(x, y) {
2965
+ const ranges = mergeRanges(y);
2966
+ const out = {};
2967
+ const dropped = [];
2968
+ let changed = false;
2969
+ forEachCell(cellsOf(x), (r, c, v) => {
2970
+ if (v === null || !hasContent(v) || !coveredByMerge(ranges, r, c)) return putCell(out, r, c, v);
2971
+ changed = true;
2972
+ if (!isDerived(x)) dropped.push(cellRef(sheetOf(x), r, c));
2973
+ if ("s" in v) putCell(out, r, c, { s: v.s });
2974
+ });
2975
+ if (!changed) return ok2([x]);
2976
+ return ok2(isEmptyMatrix(out) ? [] : [cellsMutation(x, out)], [], dropped);
2977
+ }
2978
+ function mergeOverValues(x, y) {
2979
+ const ranges = mergeRanges(x);
2980
+ const clear = {};
2981
+ const lost = [];
2982
+ forEachCell(cellsOf(y), (r, c, v) => {
2983
+ if (v === null || !hasContent(v) || !coveredByMerge(ranges, r, c)) return;
2984
+ putCell(clear, r, c, { ...EMPTY_CONTENT });
2985
+ if (!isDerived(y)) lost.push(cellRef(sheetOf(y), r, c));
2986
+ });
2987
+ if (isEmptyMatrix(clear)) return ok2([x]);
2988
+ return ok2([x, { id: ID.values, params: { subUnitId: sheetOf(x), cellValue: clear, derived: true } }], lost);
2989
+ }
2954
2990
  var SIDE_CAR_PARAMS = ["refSheets", "previousName", "derived", "sheetIndex", "sheetsLeft"];
2955
2991
  var isDerived = (m) => m.params.derived === true;
2956
2992
  function derivedSide(x, y, side) {
@@ -3167,9 +3203,21 @@ function annotateFormulas(group, sheets, definedNames) {
3167
3203
  return m;
3168
3204
  });
3169
3205
  }
3206
+ var HIDE_SHOW = {
3207
+ [ID.colHidden]: { axis: "col", hidden: true },
3208
+ [ID.colVisible]: { axis: "col", hidden: false },
3209
+ [ID.rowHidden]: { axis: "row", hidden: true },
3210
+ [ID.rowVisible]: { axis: "row", hidden: false }
3211
+ };
3170
3212
  function sizeOf(m) {
3171
3213
  const p = m.params;
3172
3214
  const values = /* @__PURE__ */ new Map();
3215
+ const hs = HIDE_SHOW[m.id];
3216
+ if (hs) {
3217
+ const [a02, a12] = S(hs.axis);
3218
+ for (const r of p.ranges ?? []) for (let i = r[a02]; i <= r[a12]; i++) values.set(i, hs.hidden);
3219
+ return { axis: hs.axis, attr: "hd", values };
3220
+ }
3173
3221
  if (m.id === ID.rowAutoHeight) {
3174
3222
  for (const { row, autoHeight } of p.rowsAutoHeightInfo ?? []) values.set(row, autoHeight);
3175
3223
  return { axis: "row", attr: "ah", values };
@@ -3189,6 +3237,12 @@ function sizeOf(m) {
3189
3237
  function buildSize(template, values) {
3190
3238
  if (values.size === 0) return null;
3191
3239
  const p = template.params;
3240
+ const hs = HIDE_SHOW[template.id];
3241
+ if (hs) {
3242
+ const [a02, a12] = S(hs.axis);
3243
+ const first2 = (p.ranges ?? [])[0] ?? { startRow: 0, endRow: 0, startColumn: 0, endColumn: 0 };
3244
+ return { id: template.id, params: { ...p, ranges: runs(values.keys()).map((r) => ({ ...first2, [a02]: r.start, [a12]: r.start + r.count - 1 })) } };
3245
+ }
3192
3246
  if (template.id === ID.rowAutoHeight)
3193
3247
  return { id: template.id, params: { ...p, rowsAutoHeightInfo: [...values].sort((a, b) => a[0] - b[0]).map(([row, autoHeight]) => ({ row, autoHeight })) } };
3194
3248
  const axis = template.id === ID.colWidth ? "col" : "row";
@@ -3734,6 +3788,8 @@ function transformMutation(x, y, side) {
3734
3788
  return ok2([...pre, x, ...mid, ...add.length ? [mergeMutation(ID.addMerge, sheet, add)] : []], lostMerges);
3735
3789
  }
3736
3790
  if (fx === "cell" && fy === "cell") return transformCellsOverCells(x, y, side);
3791
+ if (x.id === ID.values && y.id === ID.addMerge) return valuesUnderMerge(x, y);
3792
+ if (x.id === ID.addMerge && y.id === ID.values) return mergeOverValues(x, y);
3737
3793
  if (fx === "merge" && fy === "merge") {
3738
3794
  const xr = mergeRanges(x);
3739
3795
  const yr = mergeRanges(y);
@@ -4346,6 +4402,8 @@ var REMOVE_SHEET2 = "sheet.mutation.remove-sheet";
4346
4402
  var FILTER_RANGE = "sheet.mutation.set-filter-range";
4347
4403
  var RESHAPE = /* @__PURE__ */ new Set(["sheet.mutation.set-worksheet-row-count", "sheet.mutation.set-worksheet-column-count", "sheet.mutation.remove-sheet", INSERT_SHEET]);
4348
4404
  var UNDO = "univer.command.undo";
4405
+ var KEYBOARD_EVENT = 4;
4406
+ var ESC_KEY = 27;
4349
4407
  var REDO = "univer.command.redo";
4350
4408
  var DEPTH_GUARD = /* @__PURE__ */ Symbol.for("ai-matrx.office.sheets.command-depth-guard");
4351
4409
  var json = (v) => v === void 0 ? v : JSON.parse(JSON.stringify(v));
@@ -4370,6 +4428,16 @@ function univerSheetsOtPort(o) {
4370
4428
  return null;
4371
4429
  }
4372
4430
  };
4431
+ const closeCellEditor = () => {
4432
+ const u = unitId();
4433
+ if (!u || !edited()) return;
4434
+ editorHold = false;
4435
+ try {
4436
+ cs.syncExecuteCommand("sheet.operation.set-cell-edit-visible", { eventType: KEYBOARD_EVENT, keycode: ESC_KEY, visible: false, unitId: u });
4437
+ } catch (err) {
4438
+ console.warn("[sheets-ot] could not close the cell editor before reloading the workbook", err);
4439
+ }
4440
+ };
4373
4441
  const holding = () => {
4374
4442
  if (editorHold && !edited()) editorHold = false;
4375
4443
  return editorHold;
@@ -4418,38 +4486,55 @@ function univerSheetsOtPort(o) {
4418
4486
  }
4419
4487
  }
4420
4488
  };
4489
+ const removedSheetRefs = (gone) => {
4490
+ const wb = o.workbook();
4491
+ const names = sheetNames();
4492
+ const name = names[gone];
4493
+ if (!wb || !name) return [];
4494
+ const refSheets = {};
4495
+ for (const [id, n] of Object.entries(names)) refSheets[n] = id;
4496
+ const owed = [];
4497
+ for (const ws of wb.getSheets()) {
4498
+ const sheet = ws.getSheetId();
4499
+ if (sheet === gone) continue;
4500
+ ws.getCellMatrix?.().forValue((r, c, cell) => {
4501
+ const f = cell?.f;
4502
+ if (typeof f !== "string" || !f.includes(name)) return;
4503
+ const out = rewriteFormula(f, { ownSheet: sheet, refSheets }, { kind: "remove-sheet", sheet: gone });
4504
+ if (typeof out !== "string" && out.changed) owed.push({ sheet, row: r, col: c, f: out.text });
4505
+ });
4506
+ }
4507
+ return owed;
4508
+ };
4421
4509
  const sweepRemovedSheetRefs = (g) => {
4422
- const removed = g.mutations.filter((m) => m.id === REMOVE_SHEET2).map((m) => String(m.params.subUnitId ?? ""));
4423
4510
  const wb = o.workbook();
4424
4511
  const u = unitId();
4425
- if (!removed.length || !wb || !u) return;
4426
- const refSheets = {};
4427
- for (const [id, name] of Object.entries(g.sheets)) refSheets[name] = id;
4428
- for (const gone of removed) {
4429
- const name = g.sheets[gone];
4430
- if (!name) continue;
4431
- for (const ws of wb.getSheets()) {
4432
- const sheet = ws.getSheetId();
4433
- const cells = {};
4434
- ws.getCellMatrix?.().forValue((r, c, cell) => {
4435
- const f = cell?.f;
4436
- if (typeof f !== "string" || !f.includes(name)) return;
4437
- const out = rewriteFormula(f, { ownSheet: sheet, refSheets }, { kind: "remove-sheet", sheet: gone });
4438
- if (typeof out !== "string" && out.changed) (cells[r] ??= {})[c] = { f: out.text, si: null };
4439
- });
4440
- if (!Object.keys(cells).length) continue;
4441
- applying = true;
4442
- try {
4443
- cs.syncExecuteCommand(VALUES, { unitId: u, subUnitId: sheet, cellValue: json(cells) });
4444
- } finally {
4445
- applying = false;
4446
- }
4447
- const done = {};
4448
- for (const r of Object.keys(cells))
4449
- for (const c of Object.keys(cells[r])) (done[r] ??= {})[c] = completeCellContent(cells[r][c], json(ws.getCellRaw(Number(r), Number(c)) ?? null));
4450
- g.mutations.push({ id: VALUES, params: { subUnitId: sheet, cellValue: done } });
4451
- g.inverses.push(void 0);
4512
+ if (!g.owedRefs?.length || !wb || !u) return;
4513
+ const written = /* @__PURE__ */ new Set();
4514
+ for (const m of g.mutations)
4515
+ if (m.id === VALUES) {
4516
+ for (const [r, row] of Object.entries(m.params.cellValue ?? {})) for (const [c, v] of Object.entries(row ?? {})) if (v && "f" in v) written.add(`${m.params.subUnitId}:${r}:${c}`);
4452
4517
  }
4518
+ const bySheet = /* @__PURE__ */ new Map();
4519
+ for (const x of g.owedRefs) {
4520
+ if (written.has(`${x.sheet}:${x.row}:${x.col}`) || !wb.getSheetBySheetId(x.sheet)) continue;
4521
+ const cells = bySheet.get(x.sheet) ?? {};
4522
+ (cells[x.row] ??= {})[x.col] = { f: x.f, si: null };
4523
+ bySheet.set(x.sheet, cells);
4524
+ }
4525
+ for (const [sheet, cells] of bySheet) {
4526
+ const ws = wb.getSheetBySheetId(sheet);
4527
+ applying = true;
4528
+ try {
4529
+ cs.syncExecuteCommand(VALUES, { unitId: u, subUnitId: sheet, cellValue: json(cells) });
4530
+ } finally {
4531
+ applying = false;
4532
+ }
4533
+ const done = {};
4534
+ for (const r of Object.keys(cells))
4535
+ for (const c of Object.keys(cells[r])) (done[r] ??= {})[c] = completeCellContent(cells[r][c], json(ws.getCellRaw(Number(r), Number(c)) ?? null));
4536
+ g.mutations.push({ id: VALUES, params: { subUnitId: sheet, cellValue: done } });
4537
+ g.inverses.push(void 0);
4453
4538
  }
4454
4539
  };
4455
4540
  const flush = () => {
@@ -4496,6 +4581,7 @@ function univerSheetsOtPort(o) {
4496
4581
  record.filter = filterOf(String(p.subUnitId ?? ""));
4497
4582
  }
4498
4583
  if (info.id === SHEET_NAME) record.name = ws.getName();
4584
+ if (info.id === REMOVE_SHEET2) record.owedRefs = removedSheetRefs(String(p.subUnitId ?? ""));
4499
4585
  pre.set(p, record);
4500
4586
  const owed = isStructuralMutation({ id: info.id, params: p }) ? mergeFixupsOwed({ id: info.id, params: p }, record.merges ?? []) : null;
4501
4587
  if (owed) {
@@ -4648,6 +4734,8 @@ function univerSheetsOtPort(o) {
4648
4734
  g.mutations.push(r.m);
4649
4735
  g.inverses.push(r.inverse);
4650
4736
  }
4737
+ const owedRefs = info.id === REMOVE_SHEET2 ? pre.get(info.params)?.owedRefs : void 0;
4738
+ if (owedRefs?.length) (g.owedRefs ??= []).push(...owedRefs);
4651
4739
  const was = info.id === VALUES ? pre.get(info.params)?.filter : void 0;
4652
4740
  if (was && !g.notShared) {
4653
4741
  const sheet = String(info.params.subUnitId ?? "");
@@ -4764,6 +4852,7 @@ function univerSheetsOtPort(o) {
4764
4852
  group = null;
4765
4853
  applying = true;
4766
4854
  try {
4855
+ closeCellEditor();
4767
4856
  const u = unitId();
4768
4857
  if (u) releaseUnitPluginState(cs, u, o.saveUnit?.(u) ?? null);
4769
4858
  o.load(snapshot);
@@ -4781,6 +4870,21 @@ function univerSheetsOtPort(o) {
4781
4870
  return () => void undoWatchers.delete(fn);
4782
4871
  },
4783
4872
  markDirty: () => o.markDirty(),
4873
+ cursor() {
4874
+ const sel = o.selections;
4875
+ if (!sel) return null;
4876
+ try {
4877
+ const sheetId = sel.sheetId();
4878
+ if (!sheetId) return null;
4879
+ const first = sel.get()?.[0];
4880
+ const p = first?.primary;
4881
+ const row = p?.actualRow ?? p?.startRow ?? first?.range.startRow ?? 0;
4882
+ const col = p?.actualColumn ?? p?.startColumn ?? first?.range.startColumn ?? 0;
4883
+ return { sheetId, row: Number.isFinite(row) ? row : 0, col: Number.isFinite(col) ? col : 0 };
4884
+ } catch {
4885
+ return null;
4886
+ }
4887
+ },
4784
4888
  dispose() {
4785
4889
  undoWatchers.clear();
4786
4890
  before.dispose();
@@ -5054,6 +5158,24 @@ async function workbookUsesOrderedRoom(workbookId, knob, isNewWorkbook, offlineK
5054
5158
  if (knob === "new") return isNewWorkbook();
5055
5159
  return false;
5056
5160
  }
5161
+ function joiningAwareness(o, port) {
5162
+ let at = null;
5163
+ try {
5164
+ at = port.cursor?.() ?? null;
5165
+ } catch {
5166
+ at = null;
5167
+ }
5168
+ return {
5169
+ uid: o.uid,
5170
+ clientId: o.clientId,
5171
+ name: o.displayName || "Someone",
5172
+ ...o.avatarUrl ? { avatarUrl: o.avatarUrl } : {},
5173
+ sheetId: at?.sheetId ?? null,
5174
+ row: at?.row ?? null,
5175
+ col: at?.col ?? null,
5176
+ ts: Date.now()
5177
+ };
5178
+ }
5057
5179
  async function startWorkbookOtRoom(port, o) {
5058
5180
  const latest = await getLatestSnapshot(o.workbookId);
5059
5181
  if (!latest.success) throw new Error(latest.error);
@@ -5066,16 +5188,7 @@ async function startWorkbookOtRoom(port, o) {
5066
5188
  }
5067
5189
  const doc = new Y3.Doc();
5068
5190
  const awareness = new import_awareness3.Awareness(doc);
5069
- awareness.setLocalState({
5070
- uid: o.uid,
5071
- clientId: o.clientId,
5072
- name: o.displayName || "Someone",
5073
- ...o.avatarUrl ? { avatarUrl: o.avatarUrl } : {},
5074
- sheetId: null,
5075
- row: null,
5076
- col: null,
5077
- ts: Date.now()
5078
- });
5191
+ awareness.setLocalState(joiningAwareness(o, port));
5079
5192
  const stopColor = (0, import_collab6.claimCollaboratorColor)(awareness, { uid: o.uid, clientId: o.clientId });
5080
5193
  const publishAwareness = () => o.onAwareness?.(new Map(awareness.getStates()));
5081
5194
  awareness.on("change", publishAwareness);