@univerjs-pro/bases 1.0.0-alpha.8 → 1.0.0-beta.1

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.
Files changed (127) hide show
  1. package/lib/cjs/facade.js +1 -1
  2. package/lib/cjs/index.js +1 -1
  3. package/lib/cjs/locale/ar-SA.js +1 -1
  4. package/lib/cjs/locale/ca-ES.js +1 -1
  5. package/lib/cjs/locale/de-DE.js +1 -1
  6. package/lib/cjs/locale/en-US.js +1 -1
  7. package/lib/cjs/locale/es-ES.js +1 -1
  8. package/lib/cjs/locale/fa-IR.js +1 -1
  9. package/lib/cjs/locale/fr-FR.js +1 -1
  10. package/lib/cjs/locale/id-ID.js +1 -1
  11. package/lib/cjs/locale/it-IT.js +1 -1
  12. package/lib/cjs/locale/ja-JP.js +1 -1
  13. package/lib/cjs/locale/ko-KR.js +1 -1
  14. package/lib/cjs/locale/pl-PL.js +1 -1
  15. package/lib/cjs/locale/pt-BR.js +1 -1
  16. package/lib/cjs/locale/ru-RU.js +1 -1
  17. package/lib/cjs/locale/sk-SK.js +1 -1
  18. package/lib/cjs/locale/vi-VN.js +1 -1
  19. package/lib/cjs/locale/zh-CN.js +1 -1
  20. package/lib/cjs/locale/zh-HK.js +1 -1
  21. package/lib/cjs/locale/zh-TW.js +1 -1
  22. package/lib/es/facade.js +1 -1
  23. package/lib/es/index.js +1 -1
  24. package/lib/es/locale/ar-SA.js +1 -1
  25. package/lib/es/locale/ca-ES.js +1 -1
  26. package/lib/es/locale/de-DE.js +1 -1
  27. package/lib/es/locale/en-US.js +1 -1
  28. package/lib/es/locale/es-ES.js +1 -1
  29. package/lib/es/locale/fa-IR.js +1 -1
  30. package/lib/es/locale/fr-FR.js +1 -1
  31. package/lib/es/locale/id-ID.js +1 -1
  32. package/lib/es/locale/it-IT.js +1 -1
  33. package/lib/es/locale/ja-JP.js +1 -1
  34. package/lib/es/locale/ko-KR.js +1 -1
  35. package/lib/es/locale/pl-PL.js +1 -1
  36. package/lib/es/locale/pt-BR.js +1 -1
  37. package/lib/es/locale/ru-RU.js +1 -1
  38. package/lib/es/locale/sk-SK.js +1 -1
  39. package/lib/es/locale/vi-VN.js +1 -1
  40. package/lib/es/locale/zh-CN.js +1 -1
  41. package/lib/es/locale/zh-HK.js +1 -1
  42. package/lib/es/locale/zh-TW.js +1 -1
  43. package/lib/facade.js +1 -1
  44. package/lib/index.js +1 -1
  45. package/lib/locale/ar-SA.js +1 -1
  46. package/lib/locale/ca-ES.js +1 -1
  47. package/lib/locale/de-DE.js +1 -1
  48. package/lib/locale/en-US.js +1 -1
  49. package/lib/locale/es-ES.js +1 -1
  50. package/lib/locale/fa-IR.js +1 -1
  51. package/lib/locale/fr-FR.js +1 -1
  52. package/lib/locale/id-ID.js +1 -1
  53. package/lib/locale/it-IT.js +1 -1
  54. package/lib/locale/ja-JP.js +1 -1
  55. package/lib/locale/ko-KR.js +1 -1
  56. package/lib/locale/pl-PL.js +1 -1
  57. package/lib/locale/pt-BR.js +1 -1
  58. package/lib/locale/ru-RU.js +1 -1
  59. package/lib/locale/sk-SK.js +1 -1
  60. package/lib/locale/vi-VN.js +1 -1
  61. package/lib/locale/zh-CN.js +1 -1
  62. package/lib/locale/zh-HK.js +1 -1
  63. package/lib/locale/zh-TW.js +1 -1
  64. package/lib/types/commands/commands/batch-create-base-records.command.d.ts +2 -0
  65. package/lib/types/commands/commands/batch-delete-base-records.command.d.ts +2 -0
  66. package/lib/types/commands/commands/create-base-child-record.command.d.ts +15 -0
  67. package/lib/types/commands/commands/create-base-record.command.d.ts +2 -0
  68. package/lib/types/commands/commands/delete-base-record.command.d.ts +2 -0
  69. package/lib/types/commands/commands/duplicate-base-record.command.d.ts +2 -0
  70. package/lib/types/commands/commands/move-base-hierarchy-record.command.d.ts +17 -0
  71. package/lib/types/commands/commands/set-base-range-values.command.d.ts +8 -1
  72. package/lib/types/commands/commands/set-base-record-values.command.d.ts +2 -0
  73. package/lib/types/commands/commands/set-base-table-hierarchy-field.command.d.ts +9 -0
  74. package/lib/types/commands/commands/update-base-cell.command.d.ts +2 -0
  75. package/lib/types/commands/commands/update-base-view-conditional-color-rules.command.d.ts +32 -0
  76. package/lib/types/commands/commands/validate-base-formula.command.d.ts +8 -0
  77. package/lib/types/commands/mutations/apply-base-json1.mutation.d.ts +4 -0
  78. package/lib/types/commands/utils/base-record-creation.d.ts +18 -0
  79. package/lib/types/commands/utils/base-validation.d.ts +6 -1
  80. package/lib/types/controllers/base-formula-calculation.controller.d.ts +1 -0
  81. package/lib/types/facade/f-base.d.ts +24 -9
  82. package/lib/types/facade/f-enum.d.ts +208 -3
  83. package/lib/types/facade/f-event.d.ts +102 -0
  84. package/lib/types/facade/f-field.d.ts +6 -6
  85. package/lib/types/facade/f-range.d.ts +4 -1
  86. package/lib/types/facade/f-record.d.ts +325 -1
  87. package/lib/types/facade/f-table.d.ts +293 -16
  88. package/lib/types/facade/f-univer.d.ts +2 -0
  89. package/lib/types/facade/f-view.d.ts +179 -3
  90. package/lib/types/facade/field-key.d.ts +3 -1
  91. package/lib/types/fields/date-format.d.ts +20 -1
  92. package/lib/types/fields/field-core-plugin.d.ts +7 -4
  93. package/lib/types/fields/field-stat.d.ts +10 -0
  94. package/lib/types/fields/progress-format.d.ts +1 -0
  95. package/lib/types/index.d.ts +25 -2
  96. package/lib/types/locale/en-US.d.ts +1 -1
  97. package/lib/types/models/base-hierarchy.d.ts +108 -0
  98. package/lib/types/models/base-table-name.d.ts +8 -0
  99. package/lib/types/models/record-link.d.ts +14 -0
  100. package/lib/types/ot/json1-op-applier.d.ts +3 -1
  101. package/lib/types/ot/json1-op-factory.d.ts +1 -1
  102. package/lib/types/ot/json1-util.d.ts +2 -2
  103. package/lib/types/services/base-command.service.d.ts +1 -0
  104. package/lib/types/services/base-formula.service.d.ts +2 -23
  105. package/lib/types/services/base-projection.service.d.ts +4 -0
  106. package/lib/umd/facade.js +1 -1
  107. package/lib/umd/index.js +1 -1
  108. package/lib/umd/locale/ar-SA.js +1 -1
  109. package/lib/umd/locale/ca-ES.js +1 -1
  110. package/lib/umd/locale/de-DE.js +1 -1
  111. package/lib/umd/locale/en-US.js +1 -1
  112. package/lib/umd/locale/es-ES.js +1 -1
  113. package/lib/umd/locale/fa-IR.js +1 -1
  114. package/lib/umd/locale/fr-FR.js +1 -1
  115. package/lib/umd/locale/id-ID.js +1 -1
  116. package/lib/umd/locale/it-IT.js +1 -1
  117. package/lib/umd/locale/ja-JP.js +1 -1
  118. package/lib/umd/locale/ko-KR.js +1 -1
  119. package/lib/umd/locale/pl-PL.js +1 -1
  120. package/lib/umd/locale/pt-BR.js +1 -1
  121. package/lib/umd/locale/ru-RU.js +1 -1
  122. package/lib/umd/locale/sk-SK.js +1 -1
  123. package/lib/umd/locale/vi-VN.js +1 -1
  124. package/lib/umd/locale/zh-CN.js +1 -1
  125. package/lib/umd/locale/zh-HK.js +1 -1
  126. package/lib/umd/locale/zh-TW.js +1 -1
  127. package/package.json +7 -7
@@ -1,8 +1,9 @@
1
1
  import type { IBaseTableSearchRequest, IBaseTableSearchResult } from '@univerjs-pro/bases';
2
2
  import type { IHostExternalReference } from '@univerjs-pro/engine-formula';
3
- import type { BaseCellValue, BaseDataModel, BaseFilterOperator, FieldId, IFieldSnapshot, Injector, IRecordSnapshot, ISortConfig, ITableSnapshot, IViewSnapshot } from '@univerjs/core';
3
+ import type { BaseCellValue, BaseDataModel, BaseFilterOperator, FieldId, IFieldSnapshot, Injector, IRecordLinkFieldConfig, IRecordSnapshot, ISortConfig, ITableSnapshot, IViewSnapshot } from '@univerjs/core';
4
4
  import type { FBase } from './f-base';
5
- import { BaseFieldType, BaseFilterConjunction, BaseViewType } from '@univerjs/core';
5
+ import { IBaseProjectionService } from '@univerjs-pro/bases';
6
+ import { BaseFieldType, BaseFilterConjunction, BaseViewType, ICommandService } from '@univerjs/core';
6
7
  import { FBaseTableField } from './f-field';
7
8
  import { FBaseTableRange } from './f-range';
8
9
  import { FBaseTableRecord } from './f-record';
@@ -39,7 +40,10 @@ export interface IQueryRecordsResult {
39
40
  }
40
41
  export interface IBaseTableSchemaSnapshot {
41
42
  id: string;
43
+ /** Worksheet-compatible display name. Do not use it as a structured-reference identifier. */
42
44
  name: string;
45
+ /** Stable table identifier for Excel structured references. */
46
+ formulaName: string;
43
47
  primaryFieldId: string;
44
48
  fieldIds: string[];
45
49
  viewIds: string[];
@@ -62,6 +66,23 @@ export interface IBaseAddFormulaFieldOptions extends IBaseAddFieldOptions {
62
66
  */
63
67
  externalReferences: readonly IHostExternalReference[];
64
68
  }
69
+ export interface IBaseAddRecordLinkFieldOptions {
70
+ /** Optional insertion index in the source table's field order. */
71
+ index?: number;
72
+ /**
73
+ * Required RecordLink field definition.
74
+ *
75
+ * `config.targetTableId` must identify another table in this Base and
76
+ * `config.multiple` selects single-link or multi-link behavior.
77
+ * `config.displayFieldId` selects the one target field used as the visible label;
78
+ * it defaults to the target table's primary field. `config.pickerFieldIds` adds
79
+ * ordered secondary context to the picker only. All configured field IDs must
80
+ * reference existing, non-system fields in the target table.
81
+ */
82
+ field: Partial<Omit<IFieldSnapshot, 'id' | 'type' | 'config'>> & {
83
+ config: IRecordLinkFieldConfig;
84
+ };
85
+ }
65
86
  /**
66
87
  * Facade API object bound to a Base table.
67
88
  *
@@ -154,7 +175,9 @@ export declare class FBaseTable {
154
175
  private readonly _base;
155
176
  private readonly _tableId;
156
177
  private readonly _injector;
157
- constructor(_base: FBase, _tableId: string, _injector: Injector);
178
+ private readonly _commandService;
179
+ private readonly _projectionService;
180
+ constructor(_base: FBase, _tableId: string, _injector: Injector, _commandService: ICommandService, _projectionService: IBaseProjectionService);
158
181
  /**
159
182
  * Get the table's parent Base data model.
160
183
  * @returns {BaseDataModel} The Base data model.
@@ -189,8 +212,100 @@ export declare class FBaseTable {
189
212
  */
190
213
  getId(): string;
191
214
  /**
192
- * Get the table name.
193
- * @returns {string} The table name.
215
+ * Get the field id used by this table's record hierarchy.
216
+ *
217
+ * Record hierarchy is a table-level capability. Every Grid or Kanban view of
218
+ * the table observes the same Parent relationship; a view does not enable or
219
+ * own hierarchy separately.
220
+ *
221
+ * Before the first hierarchy write, this method returns a deterministic virtual
222
+ * field id. Passing that id to `addRecord()`, `addRecords()`,
223
+ * `FBaseTableRecord.addChild()`, or `FBaseTableRecord.setParent()` creates the
224
+ * same-table, single-value RecordLink field and applies the record change in one
225
+ * command and one Undo entry.
226
+ * Therefore, `getFieldById(getHierarchyFieldId())` may return `null` until the
227
+ * first successful write.
228
+ *
229
+ * @returns {string} The materialized Parent field id, or the deterministic id
230
+ * that the first hierarchy write will materialize.
231
+ * @example Create the first child without creating a field manually
232
+ * ```ts
233
+ * const base = univerAPI.getActiveBase();
234
+ * if (!base) throw new Error('No active Base.');
235
+ *
236
+ * const table = base.getTableById('tasks');
237
+ * if (!table) throw new Error('Tasks table not found.');
238
+ *
239
+ * const parent = table.getRecordById('task-1');
240
+ * if (!parent) throw new Error('Parent record not found.');
241
+ *
242
+ * const parentFieldId = table.getHierarchyFieldId();
243
+ * console.log(table.getFieldById(parentFieldId)); // null before the first write
244
+ *
245
+ * const child = parent.addChild(parentFieldId, {
246
+ * [table.getPrimaryFieldId()]: 'Write integration tests',
247
+ * });
248
+ *
249
+ * console.log(table.getFieldById(parentFieldId)?.getId()); // parentFieldId
250
+ * console.log(child.getParent(parentFieldId)?.getId()); // 'task-1'
251
+ * ```
252
+ */
253
+ getHierarchyFieldId(): string;
254
+ /**
255
+ * Select or clear the table's materialized Parent field.
256
+ *
257
+ * The selected field must already exist and must be a RecordLink that targets
258
+ * this table with `multiple: false`. The command assigns the Parent semantic
259
+ * role to that field and removes the role from the previous Parent field.
260
+ * Stored RecordLink values are not rewritten.
261
+ *
262
+ * Passing `null` clears the semantic role and flattens the effective hierarchy.
263
+ * It does not delete the former field or its values. Because hierarchy is a
264
+ * default table capability, `getHierarchyFieldId()` will then return a new
265
+ * deterministic virtual id for a future hierarchy write.
266
+ *
267
+ * Grid and Kanban views consume the table-level relationship automatically;
268
+ * there is no per-view activation API.
269
+ *
270
+ * @param {string | null} fieldId An eligible RecordLink field id, or `null` to
271
+ * clear the currently materialized Parent role.
272
+ * @returns {boolean} `true` when the command succeeds. The change is undoable.
273
+ * @throws {BaseHierarchyError} With
274
+ * `univerAPI.Enum.BaseHierarchyErrorCode.InvalidField` when `fieldId` is not an
275
+ * existing same-table, single-value RecordLink field.
276
+ * @example Use an existing RecordLink field as Parent
277
+ * ```ts
278
+ * import { BaseHierarchyError } from '@univerjs-pro/bases';
279
+ *
280
+ * const base = univerAPI.getActiveBase();
281
+ * if (!base) throw new Error('No active Base.');
282
+ *
283
+ * const table = base.getTableById('tasks');
284
+ * if (!table) throw new Error('Tasks table not found.');
285
+ *
286
+ * try {
287
+ * table.setHierarchyField('parent-record-link');
288
+ * } catch (error) {
289
+ * if (
290
+ * error instanceof BaseHierarchyError &&
291
+ * error.code === univerAPI.Enum.BaseHierarchyErrorCode.InvalidField
292
+ * ) {
293
+ * console.error('The Parent field must link to one record in the same table.');
294
+ * } else {
295
+ * throw error;
296
+ * }
297
+ * }
298
+ *
299
+ * // Later, flatten the table without deleting the RecordLink field or its values.
300
+ * table.setHierarchyField(null);
301
+ * ```
302
+ */
303
+ setHierarchyField(fieldId: string | null): boolean;
304
+ /**
305
+ * Get the human-readable table display name.
306
+ *
307
+ * Do not use this value in formulas. Use `getFormulaName()` for structured references.
308
+ * @returns {string} The human-readable table display name.
194
309
  * @example
195
310
  * ```ts
196
311
  * const fBase = univerAPI.getActiveBase();
@@ -200,8 +315,40 @@ export declare class FBaseTable {
200
315
  */
201
316
  getName(): string;
202
317
  /**
203
- * Rename this table.
204
- * @param {string} name The new table name.
318
+ * Get the stable canonical OOXML-compatible table name used in structured formula references.
319
+ * The name is allocated when the table is created or a historical snapshot is migrated;
320
+ * later display-name changes do not change formula identity. Always use the returned name; `table` is not
321
+ * a reserved alias unless it is the actual identifier returned by this method.
322
+ *
323
+ * Base Formula fields use Excel structured-reference scopes. Use
324
+ * `Table[[#This Row],[Column]]` (or `Table[@[Column]]`) for one value from the
325
+ * formula record's row. Use `Table[[#Data],[Column]]` (or `Table[Column]`) only
326
+ * when the formula intentionally consumes the complete data column. For another
327
+ * Base table, call that table's `getFormulaName()` instead of typing its display
328
+ * name or relying on a placeholder.
329
+ * @returns {string} The canonical normalized formula table name.
330
+ * @example
331
+ * ```ts
332
+ * const fBase = univerAPI.getActiveBase();
333
+ * const table = fBase.getTableById('table-1');
334
+ * const tableName = table.getFormulaName();
335
+ * const currentAmount = `=${tableName}[[#This Row],[Amount]]`;
336
+ * const allAmounts = `=SUM(${tableName}[[#Data],[Amount]])`;
337
+ * ```
338
+ */
339
+ getFormulaName(): string;
340
+ /**
341
+ * Rename this table's human-readable display label. The name becomes an Excel
342
+ * worksheet name during export, so it must contain 1-31 characters, not start or
343
+ * end with an apostrophe, not contain `: \\ / ? * [ ]`, and be unique within the
344
+ * Base (case-insensitive).
345
+ *
346
+ * Its stable formula name does not change. Existing and new structured references
347
+ * must continue to use the value returned by `getFormulaName()`.
348
+ * @param {string} displayName The new human-readable table display name.
349
+ * @returns {boolean} Whether the rename command succeeded.
350
+ * @throws {Error} If `displayName` violates the Excel worksheet-name rules. The error includes
351
+ * both the complete contract and the specific reason, so callers and agents can correct it.
205
352
  * @example
206
353
  * ```ts
207
354
  * const fBase = univerAPI.getActiveBase();
@@ -210,7 +357,7 @@ export declare class FBaseTable {
210
357
  * console.log(success);
211
358
  * ```
212
359
  */
213
- setName(name: string): boolean;
360
+ setName(displayName: string): boolean;
214
361
  /**
215
362
  * Search records in this table.
216
363
  *
@@ -298,7 +445,7 @@ export declare class FBaseTable {
298
445
  /**
299
446
  * Add a field to this table.
300
447
  * @param {string} name The field display name.
301
- * @param {BaseFieldType} type The field type, such as `text`, `number`, `singleSelect`, or `formula`.
448
+ * @param {BaseFieldType} type The field type, such as `text`, `number`, `singleSelect`, `recordLink`, or `formula`.
302
449
  * @param {object} [options] Optional field parameters.
303
450
  * @param {number} [options.index] Optional insertion index in the table field order.
304
451
  * @param {Partial<Omit<IFieldSnapshot, 'id'>>} [options.field] Optional partial field snapshot to merge into the new field.
@@ -335,6 +482,41 @@ export declare class FBaseTable {
335
482
  * console.log(field);
336
483
  * ```
337
484
  *
485
+ * @example RecordLink field
486
+ * ```ts
487
+ * const fBase = univerAPI.getActiveBase();
488
+ * const tasks = fBase.getTableById('tasks-table');
489
+ * const projects = fBase.getTableById('projects-table');
490
+ * if (!tasks || !projects) {
491
+ * throw new Error('Expected the Tasks and Projects tables.');
492
+ * }
493
+ * const ownerField = projects.getFieldByName('Owner');
494
+ *
495
+ * const relatedProjects = tasks.addField(
496
+ * 'Related projects',
497
+ * univerAPI.Enum.BaseFieldType.RecordLink,
498
+ * {
499
+ * field: {
500
+ * config: {
501
+ * targetTableId: projects.getId(),
502
+ * multiple: true,
503
+ * // One field supplies the visible chip label. Omit this to use
504
+ * // the target table's primary field.
505
+ * displayFieldId: projects.getPrimaryFieldId(),
506
+ * // These fields appear below the label in the picker only.
507
+ * pickerFieldIds: ownerField ? [ownerField.getId()] : [],
508
+ * },
509
+ * },
510
+ * }
511
+ * );
512
+ *
513
+ * const task = tasks.getRecordById('task-1');
514
+ * const project = projects.getRecordById('project-1');
515
+ * if (task && project) {
516
+ * task.addLinkedRecord(relatedProjects.getId(), project.getId());
517
+ * }
518
+ * ```
519
+ *
338
520
  * @example Formula field
339
521
  * ```ts
340
522
  * const fBase = univerAPI.getActiveBase();
@@ -361,13 +543,14 @@ export declare class FBaseTable {
361
543
  * }
362
544
  * }
363
545
  * );
546
+ * const formulaTableName = fBaseTable.getFormulaName();
364
547
  * const total = fBaseTable.addField(
365
548
  * 'Total',
366
549
  * univerAPI.Enum.BaseFieldType.Formula,
367
550
  * {
368
551
  * field: {
369
552
  * config: {
370
- * formula: '=SUM([Amount],[Tax])'
553
+ * formula: `=SUM(${formulaTableName}[[#This Row],[Amount]],${formulaTableName}[[#This Row],[Tax]])`
371
554
  * }
372
555
  * },
373
556
  * externalReferences: [], // Required; this formula only uses fields in the Host Base.
@@ -383,6 +566,7 @@ export declare class FBaseTable {
383
566
  * unitId: 'pricing-base',
384
567
  * formulaQualifier: 'Pricing',
385
568
  * };
569
+ * const formulaTableName = fBaseTable.getFormulaName();
386
570
  * const taxAmount = univerAPI.getFormula().buildReference({
387
571
  * hostUnitId: fBase.getId(),
388
572
  * unit: pricingBase,
@@ -398,7 +582,7 @@ export declare class FBaseTable {
398
582
  * {
399
583
  * field: {
400
584
  * config: {
401
- * formula: `=SUM([Amount])+SUM(${taxAmount})`,
585
+ * formula: `=SUM(${formulaTableName}[[#This Row],[Amount]])+SUM(${taxAmount})`,
402
586
  * numberFormat: {
403
587
  * type: 'currency',
404
588
  * pattern: '"$"#,##0.00'
@@ -416,8 +600,10 @@ export declare class FBaseTable {
416
600
  * ```
417
601
  */
418
602
  addField(name: string, type: BaseFieldType.Formula, options: IBaseAddFormulaFieldOptions): FBaseTableField;
419
- addField(name: string, type: Exclude<BaseFieldType, BaseFieldType.Formula>, options?: IBaseAddFieldOptions): FBaseTableField;
420
- addField(name: string, type: BaseFieldType, options: IBaseAddFieldOptions | IBaseAddFormulaFieldOptions): FBaseTableField;
603
+ addField(name: string, type: BaseFieldType.RecordLink, options: IBaseAddRecordLinkFieldOptions): FBaseTableField;
604
+ addField(name: string, type: Exclude<BaseFieldType, BaseFieldType.Formula | BaseFieldType.RecordLink>, options?: IBaseAddFieldOptions): FBaseTableField;
605
+ addField(name: string, type: BaseFieldType, options: IBaseAddFieldOptions | IBaseAddFormulaFieldOptions | IBaseAddRecordLinkFieldOptions): FBaseTableField;
606
+ private _prepareFormulaFieldReferences;
421
607
  /**
422
608
  * Get all records in this table, optionally filtered, sorted, and paginated.
423
609
  * @returns {FBaseTableRecord[]} An array of record facades ordered by the table record order.
@@ -470,11 +656,35 @@ export declare class FBaseTable {
470
656
  */
471
657
  queryRecords(options?: IListRecordOptions): IQueryRecordsResult;
472
658
  /**
473
- * Add a record to this table.
659
+ * Add a record to this table synchronously.
660
+ *
661
+ * This method is also the low-level, data-oriented way to create a child record:
662
+ * use the id returned by `getHierarchyFieldId()` as a value key and use the
663
+ * parent's record id as its value. The call does not require `await`. If the
664
+ * table has not created its canonical Parent field yet, the command creates the
665
+ * same-table, single-value RecordLink field, the record, and the Parent edge as
666
+ * one JSON1 operation and one Undo entry. No observer can see an intermediate
667
+ * field-only or record-only state.
668
+ *
669
+ * Parent values use the same canonical storage format as a single-value
670
+ * RecordLink: pass one record id string. Hierarchy validation rejects a missing
671
+ * parent, self-parent, cycle, or hierarchy deeper than five levels before any
672
+ * mutation is committed. `parent.addChild()` remains available as a semantic
673
+ * convenience when code already has the parent record Facade.
674
+ *
675
+ * The virtual Parent field has no display name until the first write. Therefore,
676
+ * address it by id and keep the default `BaseFieldKeyEnum.Id`; other ordinary
677
+ * fields may still be resolved by name in a separate call.
678
+ *
474
679
  * @param {Record<FieldId, BaseCellValue>} values The record field values, keyed by field id or name.
475
680
  * @param {BaseFieldKeyEnum} [fieldKey] Optional field key type for the values. Defaults to `id`.
476
681
  * @param {Partial<Omit<IRecordSnapshot, 'id'>>} [record] Optional partial record snapshot to merge into the new record.
477
- * @returns {FBaseTableRecord} The new record facade.
682
+ * @returns {FBaseTableRecord} The new record Facade immediately after the
683
+ * synchronous command succeeds.
684
+ * @throws {BaseHierarchyError} If an initial Parent value is missing, cyclic,
685
+ * self-referential, or would exceed five levels.
686
+ * @throws {Error} If another record or field value is invalid, or the atomic
687
+ * command cannot be applied.
478
688
  *
479
689
  * @example
480
690
  * ```ts
@@ -522,16 +732,64 @@ export declare class FBaseTable {
522
732
  * const record = fBaseTable.addRecord(values);
523
733
  * console.log(record);
524
734
  * ```
735
+ *
736
+ * @example Create the first child with the generic record API (no `await`)
737
+ * ```ts
738
+ * const base = univerAPI.getActiveBase();
739
+ * if (!base) throw new Error('No active Base.');
740
+ *
741
+ * const table = base.getTableById('tasks');
742
+ * if (!table) throw new Error('Tasks table not found.');
743
+ *
744
+ * const parent = table.getRecordById('task-1');
745
+ * if (!parent) throw new Error('Parent record not found.');
746
+ *
747
+ * const parentFieldId = table.getHierarchyFieldId();
748
+ * const child = table.addRecord({
749
+ * [table.getPrimaryFieldId()]: 'Write integration tests',
750
+ * [parentFieldId]: parent.getId(),
751
+ * });
752
+ *
753
+ * console.log(child.getParent(parentFieldId)?.getId()); // 'task-1'
754
+ * console.log(parent.getChildren(parentFieldId).map((item) => item.getId()));
755
+ * ```
756
+ *
757
+ * @example Add a record with a public URL attachment
758
+ * ```ts
759
+ * const fBase = univerAPI.getActiveBase();
760
+ * const fBaseTable = fBase.getTableById('table-1');
761
+ * const imageField = fBaseTable.getFieldByName('Product image');
762
+ * const record = fBaseTable.addRecord({
763
+ * [fBaseTable.getPrimaryFieldId()]: 'Red Nail Polish',
764
+ * [imageField.getId()]: [{
765
+ * id: 'product-image',
766
+ * name: 'product.webp',
767
+ * source: 'https://example.com/product.webp',
768
+ * sourceType: univerAPI.Enum.ImageSourceType.URL,
769
+ * mimeType: 'image/webp'
770
+ * }]
771
+ * });
772
+ * console.log(record);
773
+ * ```
525
774
  */
526
775
  addRecord(values: Record<FieldId, BaseCellValue>, fieldKey?: BaseFieldKeyEnum, record?: Partial<Omit<IRecordSnapshot, 'id'>>): FBaseTableRecord;
527
776
  /**
528
777
  * Add multiple records to this table.
778
+ *
779
+ * Each initial Parent value is validated before the batch is applied. The first
780
+ * batch may use the virtual id returned by `getHierarchyFieldId()`; the Parent
781
+ * field is then materialized once in the same atomic command.
782
+ * Every accepted Parent edge is included in one atomic hierarchy event with
783
+ * `Facade` source, and Undo removes the records and those edges together.
784
+ *
529
785
  * @param {Array<{
530
786
  * values: Record<FieldId, BaseCellValue>;
531
787
  * fieldKey?: BaseFieldKeyEnum;
532
788
  * record?: Partial<Omit<IRecordSnapshot, 'id'>>;
533
789
  * }>} records An array of record values, optional field key types, and optional partial record snapshots.
534
- * @returns {FBaseTableRecord[]} An array of created record facades.
790
+ * @returns {FBaseTableRecord[]} The created record Facades immediately after
791
+ * the synchronous command succeeds.
792
+ * @throws {BaseHierarchyError} If any initial Parent value is invalid.
535
793
  * @example
536
794
  * ```ts
537
795
  * const fBase = univerAPI.getActiveBase();
@@ -542,6 +800,25 @@ export declare class FBaseTable {
542
800
  * ]);
543
801
  * console.log(records);
544
802
  * ```
803
+ * @example Create sibling records below one parent in a single Undo entry
804
+ * ```ts
805
+ * const base = univerAPI.getActiveBase();
806
+ * if (!base) throw new Error('No active Base.');
807
+ *
808
+ * const table = base.getTableById('tasks');
809
+ * if (!table) throw new Error('Tasks table not found.');
810
+ *
811
+ * const parent = table.getRecordById('task-1');
812
+ * if (!parent) throw new Error('Parent record not found.');
813
+ *
814
+ * const parentFieldId = table.getHierarchyFieldId();
815
+ * const children = table.addRecords([
816
+ * { values: { [table.getPrimaryFieldId()]: 'Child A', [parentFieldId]: parent.getId() } },
817
+ * { values: { [table.getPrimaryFieldId()]: 'Child B', [parentFieldId]: parent.getId() } },
818
+ * ]);
819
+ *
820
+ * console.log(children.map((child) => child.getId()));
821
+ * ```
545
822
  */
546
823
  addRecords(records: Array<{
547
824
  values: Record<FieldId, BaseCellValue>;
@@ -58,6 +58,8 @@ export declare class FUniverBaseMixin extends FUniver implements IFUniverBaseMix
58
58
  getBases(): FBase[];
59
59
  private _initBaseAfterEvents;
60
60
  private _initBaseBeforeEvents;
61
+ private _getHierarchyEventContext;
62
+ private _fireBeforeHierarchyChange;
61
63
  }
62
64
  declare module '@univerjs/core/facade' {
63
65
  interface FUniver extends IFUniverBaseMixin {
@@ -1,7 +1,8 @@
1
- import type { BaseViewProjection, IFilterConfig, IGroupConfig, Injector, ISortConfig, IViewFieldSetting, IViewSnapshot, ViewSpecificConfig } from '@univerjs/core';
1
+ import type { BaseViewProjection, IBaseConditionalColorRule, IFilterConfig, IGroupConfig, Injector, ISortConfig, IViewFieldSetting, IViewSnapshot, ViewSpecificConfig } from '@univerjs/core';
2
2
  import type { FBase } from './f-base';
3
3
  import type { FBaseTable } from './f-table';
4
- import { BaseViewType } from '@univerjs/core';
4
+ import { IBaseProjectionService } from '@univerjs-pro/bases';
5
+ import { BaseViewType, ICommandService } from '@univerjs/core';
5
6
  import { FBaseTableField } from './f-field';
6
7
  /**
7
8
  * Facade API object bound to a Base view.
@@ -44,7 +45,9 @@ export declare class FBaseTableView {
44
45
  private readonly _table;
45
46
  private readonly _viewId;
46
47
  private readonly _injector;
47
- constructor(_base: FBase, _table: FBaseTable, _viewId: string, _injector: Injector);
48
+ private readonly _commandService;
49
+ private readonly _projectionService;
50
+ constructor(_base: FBase, _table: FBaseTable, _viewId: string, _injector: Injector, _commandService: ICommandService, _projectionService: IBaseProjectionService);
48
51
  /**
49
52
  * Get the view id.
50
53
  * @returns {string} The view id.
@@ -139,6 +142,179 @@ export declare class FBaseTableView {
139
142
  * ```
140
143
  */
141
144
  updateConfig(patch: Partial<ViewSpecificConfig>): boolean;
145
+ /**
146
+ * Get this view's conditional coloring rules in priority order.
147
+ *
148
+ * Rules at the beginning of the array have higher priority. The returned
149
+ * array is a deep copy, so changing it does not mutate the Base. Call
150
+ * {@link setConditionalColorRules} to persist edits or reorder rules.
151
+ *
152
+ * @returns {IBaseConditionalColorRule[]} A copy of the persisted rules. Returns `[]` when no rules are configured.
153
+ * @example Read rules created by either the UI or the Facade
154
+ * ```ts
155
+ * const base = univerAPI.getActiveBase();
156
+ * if (!base) throw new Error('Open a Base before running this example.');
157
+ *
158
+ * const table = base.insertTable(`Conditional color read ${Date.now()}`, {
159
+ * primaryFieldName: 'Task'
160
+ * });
161
+ * const title = table.getPrimaryField();
162
+ * const view = table.getViewByName('Grid');
163
+ * if (!view) throw new Error('The default Grid view was not created.');
164
+ * view.setConditionalColorRules([{
165
+ * id: 'blocked-task',
166
+ * color: '#fde9e9',
167
+ * target: univerAPI.Enum.BaseConditionalColorTarget.CELL,
168
+ * fieldId: title.getId(),
169
+ * operator: univerAPI.Enum.BaseConditionalColorOperator.CONTAINS,
170
+ * operand: 'blocked'
171
+ * }]);
172
+ *
173
+ * const rules = view.getConditionalColorRules();
174
+ * console.log(rules.map((rule, priority) => ({ priority, ...rule })));
175
+ * ```
176
+ */
177
+ getConditionalColorRules(): IBaseConditionalColorRule[];
178
+ /**
179
+ * Replace all conditional coloring rules for this view.
180
+ *
181
+ * The array order is the rule priority: index `0` is evaluated first.
182
+ * Passing `[]` clears conditional coloring. Every rule id must be unique,
183
+ * and every `fieldId` must reference a public field in this table. Invalid
184
+ * targets, field/operator combinations, date modes, CSS colors, ids, or
185
+ * fields throw an actionable error before any mutation is executed. For a
186
+ * `COLUMN` target, the field's entire column is painted unconditionally;
187
+ * `operator`, `operand`, and `dateMode` are retained but ignored.
188
+ *
189
+ * Prefer this typed method and `univerAPI.Enum.BaseConditional*` values to
190
+ * writing `getConfig().conditionalColoring` through {@link updateConfig};
191
+ * the command validates the complete replacement before persisting it.
192
+ *
193
+ * @param {IBaseConditionalColorRule[]} rules Complete replacement rules in descending priority order.
194
+ * @returns {boolean} `true` when the update command succeeds; otherwise `false`.
195
+ * @example Create a runnable risk table and color high-risk rows
196
+ * ```ts
197
+ * const base = univerAPI.getActiveBase();
198
+ * if (!base) throw new Error('Open a Base before running this example.');
199
+ *
200
+ * const table = base.insertTable(`Risk tracker ${Date.now()}`, {
201
+ * primaryFieldName: 'Risk item'
202
+ * });
203
+ * const level = table.addField(
204
+ * 'Risk level',
205
+ * univerAPI.Enum.BaseFieldType.Number
206
+ * );
207
+ * table.addRecords([
208
+ * { values: { [table.getPrimaryFieldId()]: 'Stock shortage', [level.getId()]: 90 } },
209
+ * { values: { [table.getPrimaryFieldId()]: 'Delivery delay', [level.getId()]: 60 } }
210
+ * ]);
211
+ *
212
+ * const view = table.getViewByName('Grid');
213
+ * if (!view) throw new Error('The default Grid view was not created.');
214
+ * const success = view.setConditionalColorRules([{
215
+ * id: 'high-risk',
216
+ * color: '#fde9e9',
217
+ * target: univerAPI.Enum.BaseConditionalColorTarget.ROW,
218
+ * fieldId: level.getId(),
219
+ * operator: univerAPI.Enum.BaseConditionalColorOperator.GREATER_THAN,
220
+ * operand: 80
221
+ * }]);
222
+ *
223
+ * console.log({ success, rules: view.getConditionalColorRules() });
224
+ * ```
225
+ */
226
+ setConditionalColorRules(rules: IBaseConditionalColorRule[]): boolean;
227
+ /**
228
+ * Append one conditional coloring rule at the lowest priority.
229
+ *
230
+ * Use {@link setConditionalColorRules} when inserting at a specific priority
231
+ * or replacing an existing rule. Duplicate ids and invalid rule values throw
232
+ * before the Base is mutated.
233
+ *
234
+ * @param {IBaseConditionalColorRule} rule Rule to append after all existing rules.
235
+ * @returns {boolean} `true` when the update command succeeds; otherwise `false`.
236
+ * @example Add a low-priority warning rule
237
+ * ```ts
238
+ * const base = univerAPI.getActiveBase();
239
+ * if (!base) throw new Error('Open a Base before running this example.');
240
+ * const table = base.insertTable(`Conditional color add ${Date.now()}`, {
241
+ * primaryFieldName: 'Risk item'
242
+ * });
243
+ * const level = table.addField('Risk level', univerAPI.Enum.BaseFieldType.Number);
244
+ * const view = table.getViewByName('Grid');
245
+ * if (!view) throw new Error('The default Grid view was not created.');
246
+ *
247
+ * const success = view.addConditionalColorRule({
248
+ * id: 'medium-risk',
249
+ * color: '#fff7df',
250
+ * target: univerAPI.Enum.BaseConditionalColorTarget.CELL,
251
+ * fieldId: level.getId(),
252
+ * operator: univerAPI.Enum.BaseConditionalColorOperator.GREATER_THAN,
253
+ * operand: 50
254
+ * });
255
+ * console.log({ success, rules: view.getConditionalColorRules() });
256
+ * ```
257
+ */
258
+ addConditionalColorRule(rule: IBaseConditionalColorRule): boolean;
259
+ /**
260
+ * Delete one conditional coloring rule by its stable id.
261
+ *
262
+ * Deleting the last rule clears the persisted conditional coloring config.
263
+ * A missing id is a no-op and returns `false`.
264
+ *
265
+ * @param {string} ruleId Id of the rule to delete.
266
+ * @returns {boolean} `true` when the rule existed and the update command succeeds; otherwise `false`.
267
+ * @example Delete a rule and verify the persisted result
268
+ * ```ts
269
+ * const base = univerAPI.getActiveBase();
270
+ * if (!base) throw new Error('Open a Base before running this example.');
271
+ * const table = base.insertTable(`Conditional color delete ${Date.now()}`, {
272
+ * primaryFieldName: 'Task'
273
+ * });
274
+ * const title = table.getPrimaryField();
275
+ * const view = table.getViewByName('Grid');
276
+ * if (!view) throw new Error('The default Grid view was not created.');
277
+ * view.addConditionalColorRule({
278
+ * id: 'medium-risk',
279
+ * color: '#fff7df',
280
+ * target: univerAPI.Enum.BaseConditionalColorTarget.CELL,
281
+ * fieldId: title.getId(),
282
+ * operator: univerAPI.Enum.BaseConditionalColorOperator.CONTAINS,
283
+ * operand: 'delay'
284
+ * });
285
+ *
286
+ * const deleted = view.deleteConditionalColorRule('medium-risk');
287
+ * console.log({ deleted, rules: view.getConditionalColorRules() });
288
+ * ```
289
+ */
290
+ deleteConditionalColorRule(ruleId: string): boolean;
291
+ /**
292
+ * Clear every conditional coloring rule from this view.
293
+ *
294
+ * @returns {boolean} `true` when rules existed and the clear command succeeds; otherwise `false`.
295
+ * @example Clear conditional coloring without changing filters, sorts, or other view config
296
+ * ```ts
297
+ * const base = univerAPI.getActiveBase();
298
+ * if (!base) throw new Error('Open a Base before running this example.');
299
+ * const table = base.insertTable(`Conditional color clear ${Date.now()}`, {
300
+ * primaryFieldName: 'Task'
301
+ * });
302
+ * const title = table.getPrimaryField();
303
+ * const view = table.getViewByName('Grid');
304
+ * if (!view) throw new Error('The default Grid view was not created.');
305
+ * view.addConditionalColorRule({
306
+ * id: 'temporary-rule',
307
+ * color: '#eef3ff',
308
+ * target: univerAPI.Enum.BaseConditionalColorTarget.CELL,
309
+ * fieldId: title.getId(),
310
+ * operator: univerAPI.Enum.BaseConditionalColorOperator.IS_NOT_EMPTY
311
+ * });
312
+ *
313
+ * const cleared = view.clearConditionalColorRules();
314
+ * console.log({ cleared, rules: view.getConditionalColorRules() });
315
+ * ```
316
+ */
317
+ clearConditionalColorRules(): boolean;
142
318
  /**
143
319
  * Get the view filter.
144
320
  * @returns {IFilterConfig | null} The view filter, or `null` if no filter is set.
@@ -1,8 +1,10 @@
1
- import type { BaseCellValue, ITableSnapshot } from '@univerjs/core';
1
+ import type { BaseCellValue, IFieldSnapshot, ITableSnapshot } from '@univerjs/core';
2
2
  export declare enum BaseFieldKeyEnum {
3
3
  Id = "id",
4
4
  Name = "name",
5
5
  Auto = "auto"
6
6
  }
7
+ export declare function isPublicBaseField(field: IFieldSnapshot | undefined): field is IFieldSnapshot;
7
8
  export declare function resolveBaseFieldValues(table: ITableSnapshot, values: Record<string, BaseCellValue> | undefined, fieldKey?: BaseFieldKeyEnum): Record<string, BaseCellValue>;
9
+ export declare function normalizeBaseFieldValueForWrite(field: IFieldSnapshot | undefined, value: BaseCellValue): BaseCellValue;
8
10
  export declare function resolveBaseFieldKey(table: ITableSnapshot, key: string, fieldKey: BaseFieldKeyEnum): string;