@yudaocode/univer-bases 1.6.218 → 1.6.219
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.
- package/README.md +8 -1
- package/lib/cjs/facade/index.js +49 -0
- package/lib/cjs/facade/index.js.map +1 -1
- package/lib/cjs/index.js +2293 -131
- package/lib/cjs/index.js.map +1 -1
- package/lib/es/facade/index.js +56 -0
- package/lib/es/facade/index.js.map +1 -1
- package/lib/es/index.js +2224 -86
- package/lib/es/index.js.map +1 -1
- package/lib/types/facade/index.d.ts +19 -1
- package/lib/types/index.d.ts +316 -7
- package/package.json +1 -1
package/lib/cjs/index.js
CHANGED
|
@@ -39,46 +39,59 @@ __export(src_exports, {
|
|
|
39
39
|
ApplyBaseTablesStateMutation: () => ApplyBaseTablesStateMutation,
|
|
40
40
|
BASE_ROW_COLOR_PALETTE: () => BASE_ROW_COLOR_PALETTE,
|
|
41
41
|
BASE_SELECT_PALETTE: () => BASE_SELECT_PALETTE,
|
|
42
|
+
BaseAutomationService: () => BaseAutomationService,
|
|
42
43
|
BaseIoService: () => BaseIoService,
|
|
43
44
|
BaseSelectionService: () => BaseSelectionService,
|
|
44
45
|
BaseUnitModel: () => BaseUnitModel,
|
|
45
46
|
BatchInsertBaseRecordsCommand: () => BatchInsertBaseRecordsCommand,
|
|
46
47
|
COMPUTED_BASE_FIELD_TYPES: () => COMPUTED_BASE_FIELD_TYPES,
|
|
48
|
+
DeleteBaseAutomationRuleCommand: () => DeleteBaseAutomationRuleCommand,
|
|
47
49
|
DeleteBaseDashboardWidgetCommand: () => DeleteBaseDashboardWidgetCommand,
|
|
48
50
|
DeleteBaseFieldCommand: () => DeleteBaseFieldCommand,
|
|
49
51
|
DeleteBaseRecordsCommand: () => DeleteBaseRecordsCommand,
|
|
50
52
|
DeleteBaseTableCommand: () => DeleteBaseTableCommand,
|
|
51
53
|
DeleteBaseViewCommand: () => DeleteBaseViewCommand,
|
|
54
|
+
IBaseAutomationService: () => IBaseAutomationService,
|
|
52
55
|
IBaseIoService: () => IBaseIoService,
|
|
53
56
|
IBaseSelectionService: () => IBaseSelectionService,
|
|
57
|
+
MAX_AUTOMATION_RECURSION_DEPTH: () => MAX_AUTOMATION_RECURSION_DEPTH,
|
|
54
58
|
ReorderBaseFieldsCommand: () => ReorderBaseFieldsCommand,
|
|
55
59
|
ReplaceBaseSnapshotCommand: () => ReplaceBaseSnapshotCommand,
|
|
56
60
|
ReplaceBaseSnapshotMutation: () => ReplaceBaseSnapshotMutation,
|
|
61
|
+
SaveBaseAutomationRuleCommand: () => SaveBaseAutomationRuleCommand,
|
|
57
62
|
SetActiveBaseTableCommand: () => SetActiveBaseTableCommand,
|
|
58
63
|
SetActiveBaseViewCommand: () => SetActiveBaseViewCommand,
|
|
59
64
|
SetBaseActiveNavigationMutation: () => SetBaseActiveNavigationMutation,
|
|
60
65
|
SetBaseWorkspaceModeCommand: () => SetBaseWorkspaceModeCommand,
|
|
61
66
|
SubmitBaseFormResponseCommand: () => SubmitBaseFormResponseCommand,
|
|
67
|
+
ToggleBaseAutomationRuleCommand: () => ToggleBaseAutomationRuleCommand,
|
|
68
|
+
TriggerBaseAutomationCommand: () => TriggerBaseAutomationCommand,
|
|
62
69
|
UNIVER_BASES_PLUGIN_NAME: () => UNIVER_BASES_PLUGIN_NAME,
|
|
63
70
|
UNIVER_BASE_SCHEMA: () => UNIVER_BASE_SCHEMA,
|
|
64
71
|
UNIVER_BASE_SCHEMA_VERSION: () => UNIVER_BASE_SCHEMA_VERSION,
|
|
65
72
|
UniverBasesPlugin: () => UniverBasesPlugin,
|
|
66
73
|
UpdateBaseDashboardWidgetCommand: () => UpdateBaseDashboardWidgetCommand,
|
|
74
|
+
UpdateBaseDetailLayoutCommand: () => UpdateBaseDetailLayoutCommand,
|
|
67
75
|
UpdateBaseFieldCommand: () => UpdateBaseFieldCommand,
|
|
68
76
|
UpdateBaseRecordCommand: () => UpdateBaseRecordCommand,
|
|
69
77
|
UpdateBaseTableCommand: () => UpdateBaseTableCommand,
|
|
70
78
|
UpdateBaseViewCommand: () => UpdateBaseViewCommand,
|
|
71
79
|
VERSION: () => VERSION,
|
|
80
|
+
applyBaseRowReadScope: () => applyBaseRowReadScope,
|
|
72
81
|
buildBaseTableFromGridRows: () => buildBaseTableFromGridRows,
|
|
73
82
|
buildLinearBarcodeBars: () => buildLinearBarcodeBars,
|
|
74
83
|
coerceImportedCellValue: () => coerceImportedCellValue,
|
|
75
84
|
computeBaseDashboardWidget: () => computeBaseDashboardWidget,
|
|
76
85
|
computeBaseGanttTasks: () => computeBaseGanttTasks,
|
|
86
|
+
computeBaseHierarchicalList: () => computeBaseHierarchicalList,
|
|
77
87
|
computeFieldSummary: () => computeFieldSummary,
|
|
78
88
|
createDefaultBaseUnitData: () => createDefaultBaseUnitData,
|
|
79
89
|
createSampleCrmBaseData: () => createSampleCrmBaseData,
|
|
80
90
|
createSampleProjectBaseData: () => createSampleProjectBaseData,
|
|
91
|
+
doesAutomationTriggerMatch: () => doesAutomationTriggerMatch,
|
|
92
|
+
evaluateAutomationConditions: () => evaluateAutomationConditions,
|
|
81
93
|
evaluateBaseFormula: () => evaluateBaseFormula,
|
|
94
|
+
executeBaseAutomations: () => executeBaseAutomations,
|
|
82
95
|
exportBaseTableToCsv: () => exportBaseTableToCsv,
|
|
83
96
|
exportBaseToXlsxBytes: () => exportBaseToXlsxBytes,
|
|
84
97
|
formatBaseCellValue: () => formatBaseCellValue,
|
|
@@ -86,16 +99,27 @@ __export(src_exports, {
|
|
|
86
99
|
importCsvToBaseTable: () => importCsvToBaseTable,
|
|
87
100
|
importXlsxBytesToBase: () => importXlsxBytesToBase,
|
|
88
101
|
inferFieldFromColumnSamples: () => inferFieldFromColumnSamples,
|
|
102
|
+
interpolateAutomationCellValue: () => interpolateAutomationCellValue,
|
|
103
|
+
interpolateAutomationString: () => interpolateAutomationString,
|
|
89
104
|
isCellValueEmpty: () => isCellValueEmpty,
|
|
90
105
|
isComputedBaseField: () => isComputedBaseField,
|
|
106
|
+
isFormFieldRequired: () => isFormFieldRequired,
|
|
107
|
+
isFormFieldVisible: () => isFormFieldVisible,
|
|
91
108
|
matchesBaseFilterRule: () => matchesBaseFilterRule,
|
|
109
|
+
mergeMultipleSourcesIntoBaseTable: () => mergeMultipleSourcesIntoBaseTable,
|
|
110
|
+
normalizeAutomationSteps: () => normalizeAutomationSteps,
|
|
92
111
|
normalizeLinkIds: () => normalizeLinkIds,
|
|
93
112
|
parseDelimitedText: () => parseDelimitedText,
|
|
94
113
|
queryBaseTableView: () => queryBaseTableView,
|
|
95
114
|
resolveBaseCellValue: () => resolveBaseCellValue,
|
|
115
|
+
resolveBaseDetailLayout: () => resolveBaseDetailLayout,
|
|
96
116
|
resolveLinkedRecordItems: () => resolveLinkedRecordItems,
|
|
97
117
|
roundToPrecision: () => roundToPrecision,
|
|
118
|
+
runBaseAutomationsInPlaceSync: () => runBaseAutomationsInPlaceSync,
|
|
98
119
|
serializeDelimitedText: () => serializeDelimitedText,
|
|
120
|
+
syncBaseTables: () => syncBaseTables,
|
|
121
|
+
syncOneWayBaseTable: () => syncOneWayBaseTable,
|
|
122
|
+
syncTwoWayBaseTables: () => syncTwoWayBaseTables,
|
|
99
123
|
toNumericValue: () => toNumericValue
|
|
100
124
|
});
|
|
101
125
|
module.exports = __toCommonJS(src_exports);
|
|
@@ -1577,11 +1601,1874 @@ function computeBaseGanttTasks(unitData, tableId, viewId) {
|
|
|
1577
1601
|
totalDays
|
|
1578
1602
|
};
|
|
1579
1603
|
}
|
|
1604
|
+
function computeBaseHierarchicalList(unitData, tableId, viewId, options) {
|
|
1605
|
+
const query = queryBaseTableView(unitData, tableId, viewId, options?.overrides);
|
|
1606
|
+
const { table, view, rows, allFields } = query;
|
|
1607
|
+
const selfLinkFields = allFields.filter(
|
|
1608
|
+
(f) => f.type === "link" && f.config?.linkedTableId === table.id
|
|
1609
|
+
);
|
|
1610
|
+
const explicitParentId = options?.parentFieldId ?? view.listParentFieldId;
|
|
1611
|
+
const preferredSelfLink = (explicitParentId ? selfLinkFields.find((f) => f.id === explicitParentId) : void 0) ?? selfLinkFields.find(
|
|
1612
|
+
(f) => f.name.includes("\u7236") || f.name.includes("\u4E0A\u7EA7") || f.name.toLowerCase().includes("parent")
|
|
1613
|
+
) ?? selfLinkFields[0] ?? null;
|
|
1614
|
+
const primaryField = table.fields[table.primaryFieldId] ?? allFields[0];
|
|
1615
|
+
const collapsedSet = new Set(
|
|
1616
|
+
options?.collapsedRecordIds ?? view.listCollapsedRecordIds ?? []
|
|
1617
|
+
);
|
|
1618
|
+
const rowById = /* @__PURE__ */ new Map();
|
|
1619
|
+
for (const row of rows) {
|
|
1620
|
+
rowById.set(row.recordId, row);
|
|
1621
|
+
}
|
|
1622
|
+
const resolvedParentMap = /* @__PURE__ */ new Map();
|
|
1623
|
+
const cycleFlags = /* @__PURE__ */ new Map();
|
|
1624
|
+
for (const row of rows) {
|
|
1625
|
+
if (!preferredSelfLink) {
|
|
1626
|
+
resolvedParentMap.set(row.recordId, null);
|
|
1627
|
+
cycleFlags.set(row.recordId, false);
|
|
1628
|
+
continue;
|
|
1629
|
+
}
|
|
1630
|
+
const rawLinks = normalizeLinkIds(row.values[preferredSelfLink.id]);
|
|
1631
|
+
const selfRef = rawLinks.includes(row.recordId);
|
|
1632
|
+
const candParentId = rawLinks.find((id) => id !== row.recordId && rowById.has(id)) ?? null;
|
|
1633
|
+
if (!candParentId) {
|
|
1634
|
+
resolvedParentMap.set(row.recordId, null);
|
|
1635
|
+
cycleFlags.set(row.recordId, selfRef);
|
|
1636
|
+
continue;
|
|
1637
|
+
}
|
|
1638
|
+
const visited = /* @__PURE__ */ new Set([row.recordId]);
|
|
1639
|
+
let cur = candParentId;
|
|
1640
|
+
let hasCycle = false;
|
|
1641
|
+
while (cur) {
|
|
1642
|
+
if (visited.has(cur)) {
|
|
1643
|
+
hasCycle = true;
|
|
1644
|
+
break;
|
|
1645
|
+
}
|
|
1646
|
+
visited.add(cur);
|
|
1647
|
+
cur = resolvedParentMap.get(cur) ?? null;
|
|
1648
|
+
}
|
|
1649
|
+
if (hasCycle) {
|
|
1650
|
+
resolvedParentMap.set(row.recordId, null);
|
|
1651
|
+
cycleFlags.set(row.recordId, true);
|
|
1652
|
+
} else {
|
|
1653
|
+
resolvedParentMap.set(row.recordId, candParentId);
|
|
1654
|
+
cycleFlags.set(row.recordId, selfRef);
|
|
1655
|
+
}
|
|
1656
|
+
}
|
|
1657
|
+
const nodeById = /* @__PURE__ */ new Map();
|
|
1658
|
+
for (const row of rows) {
|
|
1659
|
+
const title = (primaryField ? row.displayValues[primaryField.id] : "") || row.recordId;
|
|
1660
|
+
nodeById.set(row.recordId, {
|
|
1661
|
+
recordId: row.recordId,
|
|
1662
|
+
parentId: resolvedParentMap.get(row.recordId) ?? null,
|
|
1663
|
+
depth: 0,
|
|
1664
|
+
title,
|
|
1665
|
+
row,
|
|
1666
|
+
collapsed: collapsedSet.has(row.recordId),
|
|
1667
|
+
cycleDetected: Boolean(cycleFlags.get(row.recordId)),
|
|
1668
|
+
children: [],
|
|
1669
|
+
descendantCount: 0
|
|
1670
|
+
});
|
|
1671
|
+
}
|
|
1672
|
+
const roots = [];
|
|
1673
|
+
for (const row of rows) {
|
|
1674
|
+
const node = nodeById.get(row.recordId);
|
|
1675
|
+
if (node.parentId && nodeById.has(node.parentId)) {
|
|
1676
|
+
nodeById.get(node.parentId).children.push(node);
|
|
1677
|
+
} else {
|
|
1678
|
+
node.parentId = null;
|
|
1679
|
+
roots.push(node);
|
|
1680
|
+
}
|
|
1681
|
+
}
|
|
1682
|
+
let maxDepth = 0;
|
|
1683
|
+
const flattenedVisibleNodes = [];
|
|
1684
|
+
const finalizeTree = (node, depth, ancestorCollapsed, guard2) => {
|
|
1685
|
+
if (guard2.has(node.recordId)) return 0;
|
|
1686
|
+
guard2.add(node.recordId);
|
|
1687
|
+
node.depth = depth;
|
|
1688
|
+
if (depth > maxDepth) maxDepth = depth;
|
|
1689
|
+
if (!ancestorCollapsed) {
|
|
1690
|
+
flattenedVisibleNodes.push(node);
|
|
1691
|
+
}
|
|
1692
|
+
let desc = 0;
|
|
1693
|
+
const hideKids = ancestorCollapsed || node.collapsed;
|
|
1694
|
+
for (const child of node.children) {
|
|
1695
|
+
desc += 1 + finalizeTree(child, depth + 1, hideKids, guard2);
|
|
1696
|
+
}
|
|
1697
|
+
node.descendantCount = desc;
|
|
1698
|
+
return desc;
|
|
1699
|
+
};
|
|
1700
|
+
const guard = /* @__PURE__ */ new Set();
|
|
1701
|
+
for (const root of roots) {
|
|
1702
|
+
finalizeTree(root, 0, false, guard);
|
|
1703
|
+
}
|
|
1704
|
+
return {
|
|
1705
|
+
table,
|
|
1706
|
+
view,
|
|
1707
|
+
parentField: preferredSelfLink,
|
|
1708
|
+
roots,
|
|
1709
|
+
flattenedVisibleNodes,
|
|
1710
|
+
totalNodeCount: nodeById.size,
|
|
1711
|
+
maxDepth
|
|
1712
|
+
};
|
|
1713
|
+
}
|
|
1714
|
+
function isFormFieldVisible(item, values, table, unitData, formConfig) {
|
|
1715
|
+
if (!item.visible) return false;
|
|
1716
|
+
const extraRule = formConfig?.fieldRules?.[item.fieldId];
|
|
1717
|
+
const rules = item.visibleWhen ?? extraRule?.visibleWhen ?? [];
|
|
1718
|
+
if (rules.length === 0) return true;
|
|
1719
|
+
const conjunction = item.visibleWhenConjunction ?? extraRule?.visibleWhenConjunction ?? "and";
|
|
1720
|
+
const draftRecord = {
|
|
1721
|
+
id: "__form_draft__",
|
|
1722
|
+
createdTime: "",
|
|
1723
|
+
lastModifiedTime: "",
|
|
1724
|
+
cells: values
|
|
1725
|
+
};
|
|
1726
|
+
const outcomes = rules.map((rule) => {
|
|
1727
|
+
const refField = table.fields[rule.fieldId];
|
|
1728
|
+
if (!refField) return true;
|
|
1729
|
+
const rawVal = resolveBaseCellValue(unitData, table.id, draftRecord, refField);
|
|
1730
|
+
const dispVal = formatBaseCellValue(unitData, table.id, refField, rawVal);
|
|
1731
|
+
return matchesBaseFilterRule(rule, refField, rawVal, dispVal);
|
|
1732
|
+
});
|
|
1733
|
+
return conjunction === "or" ? outcomes.some(Boolean) : outcomes.every(Boolean);
|
|
1734
|
+
}
|
|
1735
|
+
function isFormFieldRequired(item, values, table, unitData, formConfig) {
|
|
1736
|
+
if (!isFormFieldVisible(item, values, table, unitData, formConfig)) {
|
|
1737
|
+
return false;
|
|
1738
|
+
}
|
|
1739
|
+
const extraRule = formConfig?.fieldRules?.[item.fieldId];
|
|
1740
|
+
const condReq = Boolean(item.conditionalRequired ?? extraRule?.conditionalRequired);
|
|
1741
|
+
return Boolean(item.required || condReq);
|
|
1742
|
+
}
|
|
1743
|
+
function resolveBaseDetailLayout(table, view, explicitLayout) {
|
|
1744
|
+
const layout = explicitLayout ?? view?.detailLayout ?? table.detailLayout;
|
|
1745
|
+
const columns = layout?.columns === 2 ? 2 : 1;
|
|
1746
|
+
const orderedFields = table.fieldOrder.map((fid) => table.fields[fid]).filter((f) => Boolean(f));
|
|
1747
|
+
if (!layout?.sections || layout.sections.length === 0) {
|
|
1748
|
+
return {
|
|
1749
|
+
columns,
|
|
1750
|
+
sections: [
|
|
1751
|
+
{
|
|
1752
|
+
id: "sec_default",
|
|
1753
|
+
title: "\u6838\u5FC3\u5B57\u6BB5\u4FE1\u606F",
|
|
1754
|
+
collapsed: false,
|
|
1755
|
+
fields: orderedFields
|
|
1756
|
+
}
|
|
1757
|
+
]
|
|
1758
|
+
};
|
|
1759
|
+
}
|
|
1760
|
+
const assigned = /* @__PURE__ */ new Set();
|
|
1761
|
+
const sections = layout.sections.map((sec) => {
|
|
1762
|
+
const secFields = [];
|
|
1763
|
+
for (const fid of sec.fieldIds) {
|
|
1764
|
+
const f = table.fields[fid];
|
|
1765
|
+
if (f && !assigned.has(f.id)) {
|
|
1766
|
+
assigned.add(f.id);
|
|
1767
|
+
secFields.push(f);
|
|
1768
|
+
}
|
|
1769
|
+
}
|
|
1770
|
+
return {
|
|
1771
|
+
id: sec.id,
|
|
1772
|
+
title: sec.title || "\u672A\u547D\u540D\u5206\u7EC4",
|
|
1773
|
+
collapsed: Boolean(sec.collapsed),
|
|
1774
|
+
fields: secFields
|
|
1775
|
+
};
|
|
1776
|
+
});
|
|
1777
|
+
const remaining = orderedFields.filter((f) => !assigned.has(f.id));
|
|
1778
|
+
if (remaining.length > 0) {
|
|
1779
|
+
sections.push({
|
|
1780
|
+
id: "sec_remaining",
|
|
1781
|
+
title: "\u5176\u4ED6\u5B57\u6BB5",
|
|
1782
|
+
collapsed: false,
|
|
1783
|
+
fields: remaining
|
|
1784
|
+
});
|
|
1785
|
+
}
|
|
1786
|
+
return { columns, sections };
|
|
1787
|
+
}
|
|
1788
|
+
|
|
1789
|
+
// src/engine/base-automation-engine.ts
|
|
1790
|
+
var import_core = require("@univerjs/core");
|
|
1791
|
+
var MAX_AUTOMATION_RECURSION_DEPTH = 3;
|
|
1792
|
+
var IBaseAutomationService = (0, import_core.createIdentifier)(
|
|
1793
|
+
"univer.bases.automation.service"
|
|
1794
|
+
);
|
|
1795
|
+
var BaseAutomationService = class extends import_core.Disposable {
|
|
1796
|
+
setWebhookExecutor(executor) {
|
|
1797
|
+
this._webhookExecutor = executor;
|
|
1798
|
+
}
|
|
1799
|
+
getWebhookExecutor() {
|
|
1800
|
+
return this._webhookExecutor;
|
|
1801
|
+
}
|
|
1802
|
+
};
|
|
1803
|
+
function nowTimestamp() {
|
|
1804
|
+
return (/* @__PURE__ */ new Date()).toISOString().replace("T", " ").slice(0, 19);
|
|
1805
|
+
}
|
|
1806
|
+
function syncLinkInTables(tables, sourceTableId, sourceRecordId, linkField, prevLinkedIds, nextLinkedIds, timestamp) {
|
|
1807
|
+
const targetTableId = linkField.config?.linkedTableId;
|
|
1808
|
+
const symFieldId = linkField.config?.symmetricFieldId;
|
|
1809
|
+
if (!targetTableId || !symFieldId) return;
|
|
1810
|
+
const targetTable = tables[targetTableId];
|
|
1811
|
+
const sourceTable = tables[sourceTableId];
|
|
1812
|
+
if (!targetTable || !sourceTable) return;
|
|
1813
|
+
const symField = targetTable.fields[symFieldId];
|
|
1814
|
+
if (!symField || symField.type !== "link") return;
|
|
1815
|
+
const nextSet = new Set(nextLinkedIds);
|
|
1816
|
+
for (const oldTargetId of prevLinkedIds) {
|
|
1817
|
+
if (nextSet.has(oldTargetId)) continue;
|
|
1818
|
+
const targetRec = targetTable.records[oldTargetId];
|
|
1819
|
+
if (!targetRec) continue;
|
|
1820
|
+
const curBackIds = normalizeLinkIds(targetRec.cells[symFieldId]);
|
|
1821
|
+
const filtered = curBackIds.filter((id) => id !== sourceRecordId);
|
|
1822
|
+
if (filtered.length !== curBackIds.length) {
|
|
1823
|
+
targetRec.cells[symFieldId] = filtered;
|
|
1824
|
+
targetRec.lastModifiedTime = timestamp;
|
|
1825
|
+
}
|
|
1826
|
+
}
|
|
1827
|
+
for (const newTargetId of nextLinkedIds) {
|
|
1828
|
+
const targetRec = targetTable.records[newTargetId];
|
|
1829
|
+
if (!targetRec) continue;
|
|
1830
|
+
const curBackIds = normalizeLinkIds(targetRec.cells[symFieldId]);
|
|
1831
|
+
if (symField.config?.allowMultiple === false) {
|
|
1832
|
+
for (const otherSourceId of curBackIds) {
|
|
1833
|
+
if (otherSourceId === sourceRecordId) continue;
|
|
1834
|
+
const otherSourceRec = sourceTable.records[otherSourceId];
|
|
1835
|
+
if (otherSourceRec) {
|
|
1836
|
+
otherSourceRec.cells[linkField.id] = normalizeLinkIds(
|
|
1837
|
+
otherSourceRec.cells[linkField.id]
|
|
1838
|
+
).filter((id) => id !== newTargetId);
|
|
1839
|
+
otherSourceRec.lastModifiedTime = timestamp;
|
|
1840
|
+
}
|
|
1841
|
+
}
|
|
1842
|
+
targetRec.cells[symFieldId] = [sourceRecordId];
|
|
1843
|
+
targetRec.lastModifiedTime = timestamp;
|
|
1844
|
+
} else if (!curBackIds.includes(sourceRecordId)) {
|
|
1845
|
+
targetRec.cells[symFieldId] = [...curBackIds, sourceRecordId];
|
|
1846
|
+
targetRec.lastModifiedTime = timestamp;
|
|
1847
|
+
}
|
|
1848
|
+
}
|
|
1849
|
+
}
|
|
1850
|
+
function findFieldByIdOrName(table, key) {
|
|
1851
|
+
return table.fields[key] ?? Object.values(table.fields).find((f) => f.name === key);
|
|
1852
|
+
}
|
|
1853
|
+
function evaluateAutomationConditions(conditions, conjunction, unitData, table, record) {
|
|
1854
|
+
if (!conditions || conditions.length === 0) return true;
|
|
1855
|
+
if (!record) return false;
|
|
1856
|
+
const mode = conjunction ?? "and";
|
|
1857
|
+
const outcomes = conditions.map((rule) => {
|
|
1858
|
+
const field = findFieldByIdOrName(table, rule.fieldId);
|
|
1859
|
+
if (!field) return true;
|
|
1860
|
+
const rawVal = resolveBaseCellValue(unitData, table.id, record, field);
|
|
1861
|
+
const dispVal = formatBaseCellValue(unitData, table.id, field, rawVal);
|
|
1862
|
+
return matchesBaseFilterRule(rule, field, rawVal, dispVal);
|
|
1863
|
+
});
|
|
1864
|
+
return mode === "or" ? outcomes.some(Boolean) : outcomes.every(Boolean);
|
|
1865
|
+
}
|
|
1866
|
+
function resolveWebhookPath(payload, path) {
|
|
1867
|
+
if (!payload || typeof payload !== "object") return "";
|
|
1868
|
+
const parts = path.split(".").filter(Boolean);
|
|
1869
|
+
let cur = payload;
|
|
1870
|
+
for (const p of parts) {
|
|
1871
|
+
if (cur && typeof cur === "object" && p in cur) {
|
|
1872
|
+
cur = cur[p];
|
|
1873
|
+
} else {
|
|
1874
|
+
return "";
|
|
1875
|
+
}
|
|
1876
|
+
}
|
|
1877
|
+
return cur;
|
|
1878
|
+
}
|
|
1879
|
+
function interpolateAutomationString(template, unitData, table, record, webhookPayload) {
|
|
1880
|
+
if (!template) return "";
|
|
1881
|
+
if (template.startsWith("=") && record) {
|
|
1882
|
+
const evaluated = evaluateBaseFormula(template.slice(1).trim(), record, table, unitData);
|
|
1883
|
+
return evaluated === null || evaluated === void 0 ? "" : String(evaluated);
|
|
1884
|
+
}
|
|
1885
|
+
return template.replace(/\{([^{}]+)\}/g, (_match, rawToken) => {
|
|
1886
|
+
const token = rawToken.trim();
|
|
1887
|
+
if (token.startsWith("webhook.")) {
|
|
1888
|
+
const wVal = resolveWebhookPath(webhookPayload, token.slice("webhook.".length));
|
|
1889
|
+
return wVal === null || wVal === void 0 ? "" : String(wVal);
|
|
1890
|
+
}
|
|
1891
|
+
if (!record) return "";
|
|
1892
|
+
if (token === "recordId" || token === "_id") return record.id;
|
|
1893
|
+
const field = findFieldByIdOrName(table, token);
|
|
1894
|
+
if (!field) return "";
|
|
1895
|
+
const val = resolveBaseCellValue(unitData, table.id, record, field);
|
|
1896
|
+
return formatBaseCellValue(unitData, table.id, field, val);
|
|
1897
|
+
});
|
|
1898
|
+
}
|
|
1899
|
+
function interpolateAutomationCellValue(rawTemplate, targetField, unitData, sourceTable, sourceRecord, webhookPayload) {
|
|
1900
|
+
if (typeof rawTemplate !== "string") {
|
|
1901
|
+
return import_core.Tools.deepClone(rawTemplate);
|
|
1902
|
+
}
|
|
1903
|
+
if (rawTemplate.startsWith("=") && sourceRecord) {
|
|
1904
|
+
const res = evaluateBaseFormula(rawTemplate.slice(1).trim(), sourceRecord, sourceTable, unitData);
|
|
1905
|
+
if (targetField && (targetField.type === "number" || targetField.type === "currency" || targetField.type === "percent" || targetField.type === "rating")) {
|
|
1906
|
+
return toNumericValue(res) ?? 0;
|
|
1907
|
+
}
|
|
1908
|
+
if (targetField?.type === "checkbox") {
|
|
1909
|
+
return Boolean(res);
|
|
1910
|
+
}
|
|
1911
|
+
return res;
|
|
1912
|
+
}
|
|
1913
|
+
const exactMatch = /^\{([^{}]+)\}$/.exec(rawTemplate.trim());
|
|
1914
|
+
if (exactMatch && sourceRecord) {
|
|
1915
|
+
const token = exactMatch[1].trim();
|
|
1916
|
+
if (token.startsWith("webhook.")) {
|
|
1917
|
+
const wVal = resolveWebhookPath(webhookPayload, token.slice("webhook.".length));
|
|
1918
|
+
return wVal ?? "";
|
|
1919
|
+
}
|
|
1920
|
+
if (token === "recordId" || token === "_id") {
|
|
1921
|
+
return targetField?.type === "link" ? [sourceRecord.id] : sourceRecord.id;
|
|
1922
|
+
}
|
|
1923
|
+
const srcField = findFieldByIdOrName(sourceTable, token);
|
|
1924
|
+
if (srcField) {
|
|
1925
|
+
const resolved = resolveBaseCellValue(unitData, sourceTable.id, sourceRecord, srcField);
|
|
1926
|
+
if (targetField && targetField.type === srcField.type) {
|
|
1927
|
+
return import_core.Tools.deepClone(resolved);
|
|
1928
|
+
}
|
|
1929
|
+
if (targetField && (targetField.type === "number" || targetField.type === "currency" || targetField.type === "percent" || targetField.type === "rating")) {
|
|
1930
|
+
const num = toNumericValue(resolved);
|
|
1931
|
+
if (num !== null) return num;
|
|
1932
|
+
}
|
|
1933
|
+
if (targetField?.type === "checkbox") {
|
|
1934
|
+
return Boolean(resolved);
|
|
1935
|
+
}
|
|
1936
|
+
return formatBaseCellValue(unitData, sourceTable.id, srcField, resolved);
|
|
1937
|
+
}
|
|
1938
|
+
}
|
|
1939
|
+
const interpolated = interpolateAutomationString(
|
|
1940
|
+
rawTemplate,
|
|
1941
|
+
unitData,
|
|
1942
|
+
sourceTable,
|
|
1943
|
+
sourceRecord,
|
|
1944
|
+
webhookPayload
|
|
1945
|
+
);
|
|
1946
|
+
if (targetField && (targetField.type === "number" || targetField.type === "currency" || targetField.type === "percent" || targetField.type === "rating")) {
|
|
1947
|
+
const n = toNumericValue(interpolated);
|
|
1948
|
+
if (n !== null) return n;
|
|
1949
|
+
}
|
|
1950
|
+
if (targetField?.type === "checkbox") {
|
|
1951
|
+
const lower = interpolated.trim().toLowerCase();
|
|
1952
|
+
return lower === "true" || lower === "1" || lower === "\u662F";
|
|
1953
|
+
}
|
|
1954
|
+
return interpolated;
|
|
1955
|
+
}
|
|
1956
|
+
function interpolateStructuredPayload(payload, unitData, table, record, webhookPayload) {
|
|
1957
|
+
if (typeof payload === "string") {
|
|
1958
|
+
const str = interpolateAutomationString(payload, unitData, table, record, webhookPayload);
|
|
1959
|
+
const trimmed = str.trim();
|
|
1960
|
+
if (trimmed.startsWith("{") && trimmed.endsWith("}") || trimmed.startsWith("[") && trimmed.endsWith("]")) {
|
|
1961
|
+
try {
|
|
1962
|
+
return JSON.parse(trimmed);
|
|
1963
|
+
} catch {
|
|
1964
|
+
return str;
|
|
1965
|
+
}
|
|
1966
|
+
}
|
|
1967
|
+
return str;
|
|
1968
|
+
}
|
|
1969
|
+
if (Array.isArray(payload)) {
|
|
1970
|
+
return payload.map(
|
|
1971
|
+
(item) => interpolateStructuredPayload(item, unitData, table, record, webhookPayload)
|
|
1972
|
+
);
|
|
1973
|
+
}
|
|
1974
|
+
if (payload && typeof payload === "object") {
|
|
1975
|
+
const out = {};
|
|
1976
|
+
for (const [k, v] of Object.entries(payload)) {
|
|
1977
|
+
out[k] = interpolateStructuredPayload(v, unitData, table, record, webhookPayload);
|
|
1978
|
+
}
|
|
1979
|
+
return out;
|
|
1980
|
+
}
|
|
1981
|
+
return payload;
|
|
1982
|
+
}
|
|
1983
|
+
function normalizeAutomationSteps(rule) {
|
|
1984
|
+
if (rule.steps && rule.steps.length > 0) {
|
|
1985
|
+
return rule.steps;
|
|
1986
|
+
}
|
|
1987
|
+
if (!rule.action) return [];
|
|
1988
|
+
if (rule.action === "updateRecord") {
|
|
1989
|
+
const fieldUpdates = rule.actionConfig?.fieldUpdates ?? rule.actionConfig?.cells ?? {};
|
|
1990
|
+
return [
|
|
1991
|
+
{
|
|
1992
|
+
id: `${rule.id}_legacy_update`,
|
|
1993
|
+
type: "updateRecord",
|
|
1994
|
+
fieldUpdates
|
|
1995
|
+
}
|
|
1996
|
+
];
|
|
1997
|
+
}
|
|
1998
|
+
if (rule.action === "webhook" || rule.action === "httpWebhook") {
|
|
1999
|
+
return [
|
|
2000
|
+
{
|
|
2001
|
+
id: `${rule.id}_legacy_webhook`,
|
|
2002
|
+
type: "httpWebhook",
|
|
2003
|
+
webhookUrl: String(rule.actionConfig?.url ?? "https://webhook.local/automations"),
|
|
2004
|
+
webhookMethod: rule.actionConfig?.method ?? "POST",
|
|
2005
|
+
webhookBodyTemplate: rule.actionConfig?.body
|
|
2006
|
+
}
|
|
2007
|
+
];
|
|
2008
|
+
}
|
|
2009
|
+
return [
|
|
2010
|
+
{
|
|
2011
|
+
id: `${rule.id}_legacy_notify`,
|
|
2012
|
+
type: "notify",
|
|
2013
|
+
notifyChannel: String(rule.actionConfig?.channel ?? "\u7CFB\u7EDF\u901A\u77E5"),
|
|
2014
|
+
notifyMessage: String(rule.actionConfig?.message ?? rule.name)
|
|
2015
|
+
}
|
|
2016
|
+
];
|
|
2017
|
+
}
|
|
2018
|
+
function cellsEqual(a, b) {
|
|
2019
|
+
if (a === b) return true;
|
|
2020
|
+
if ((a === null || a === void 0 || a === "") && (b === null || b === void 0 || b === "")) {
|
|
2021
|
+
return true;
|
|
2022
|
+
}
|
|
2023
|
+
return JSON.stringify(a) === JSON.stringify(b);
|
|
2024
|
+
}
|
|
2025
|
+
function doesAutomationTriggerMatch(rule, event, unitData, table, record) {
|
|
2026
|
+
if (!rule.enabled && event.ruleId !== rule.id) {
|
|
2027
|
+
return { triggerMatched: false, conditionsMatched: false };
|
|
2028
|
+
}
|
|
2029
|
+
if (rule.tableId !== event.tableId) {
|
|
2030
|
+
return { triggerMatched: false, conditionsMatched: false };
|
|
2031
|
+
}
|
|
2032
|
+
if (event.ruleId && event.ruleId !== rule.id) {
|
|
2033
|
+
return { triggerMatched: false, conditionsMatched: false };
|
|
2034
|
+
}
|
|
2035
|
+
if (event.trigger === "manual" && (!event.ruleId || event.ruleId === rule.id)) {
|
|
2036
|
+
const condOk = evaluateAutomationConditions(
|
|
2037
|
+
rule.conditions,
|
|
2038
|
+
rule.conditionConjunction,
|
|
2039
|
+
unitData,
|
|
2040
|
+
table,
|
|
2041
|
+
record
|
|
2042
|
+
);
|
|
2043
|
+
return { triggerMatched: true, conditionsMatched: condOk };
|
|
2044
|
+
}
|
|
2045
|
+
let triggerMatched = false;
|
|
2046
|
+
switch (rule.trigger) {
|
|
2047
|
+
case "recordCreated":
|
|
2048
|
+
triggerMatched = event.trigger === "recordCreated";
|
|
2049
|
+
break;
|
|
2050
|
+
case "recordUpdated":
|
|
2051
|
+
triggerMatched = event.trigger === "recordUpdated";
|
|
2052
|
+
break;
|
|
2053
|
+
case "fieldChanged": {
|
|
2054
|
+
if (event.trigger === "recordUpdated" || event.trigger === "fieldChanged" || event.trigger === "recordCreated") {
|
|
2055
|
+
const watchedIds = [
|
|
2056
|
+
...rule.triggerFieldIds ?? [],
|
|
2057
|
+
...rule.triggerFieldId ? [rule.triggerFieldId] : []
|
|
2058
|
+
];
|
|
2059
|
+
if (watchedIds.length === 0) {
|
|
2060
|
+
triggerMatched = (event.changedFieldIds?.length ?? 0) > 0 || event.trigger === "recordCreated";
|
|
2061
|
+
} else {
|
|
2062
|
+
const changedSet = new Set(event.changedFieldIds ?? []);
|
|
2063
|
+
triggerMatched = watchedIds.some((fid) => {
|
|
2064
|
+
const resolvedField = findFieldByIdOrName(table, fid);
|
|
2065
|
+
const actualId = resolvedField?.id ?? fid;
|
|
2066
|
+
if (!changedSet.has(actualId) && !changedSet.has(fid)) {
|
|
2067
|
+
if (event.trigger === "recordCreated" && record) {
|
|
2068
|
+
return record.cells[actualId] !== void 0 && record.cells[actualId] !== "";
|
|
2069
|
+
}
|
|
2070
|
+
return false;
|
|
2071
|
+
}
|
|
2072
|
+
if (event.previousCells && record) {
|
|
2073
|
+
return !cellsEqual(event.previousCells[actualId], record.cells[actualId]);
|
|
2074
|
+
}
|
|
2075
|
+
return true;
|
|
2076
|
+
});
|
|
2077
|
+
}
|
|
2078
|
+
}
|
|
2079
|
+
break;
|
|
2080
|
+
}
|
|
2081
|
+
case "conditionMatched": {
|
|
2082
|
+
if (event.trigger === "conditionMatched" || event.trigger === "recordCreated" || event.trigger === "recordUpdated") {
|
|
2083
|
+
const nowMatched = evaluateAutomationConditions(
|
|
2084
|
+
rule.conditions,
|
|
2085
|
+
rule.conditionConjunction,
|
|
2086
|
+
unitData,
|
|
2087
|
+
table,
|
|
2088
|
+
record
|
|
2089
|
+
);
|
|
2090
|
+
if (event.previousCells && record && event.trigger === "recordUpdated" && rule.conditions?.length) {
|
|
2091
|
+
const prevRec = {
|
|
2092
|
+
...record,
|
|
2093
|
+
cells: event.previousCells
|
|
2094
|
+
};
|
|
2095
|
+
const prevMatched = evaluateAutomationConditions(
|
|
2096
|
+
rule.conditions,
|
|
2097
|
+
rule.conditionConjunction,
|
|
2098
|
+
unitData,
|
|
2099
|
+
table,
|
|
2100
|
+
prevRec
|
|
2101
|
+
);
|
|
2102
|
+
if (prevMatched && nowMatched) {
|
|
2103
|
+
return { triggerMatched: false, conditionsMatched: false };
|
|
2104
|
+
}
|
|
2105
|
+
}
|
|
2106
|
+
return { triggerMatched: true, conditionsMatched: nowMatched };
|
|
2107
|
+
}
|
|
2108
|
+
break;
|
|
2109
|
+
}
|
|
2110
|
+
case "fieldMatches": {
|
|
2111
|
+
if ((event.trigger === "recordCreated" || event.trigger === "recordUpdated" || event.trigger === "fieldChanged") && record) {
|
|
2112
|
+
if (!rule.triggerFieldId) {
|
|
2113
|
+
triggerMatched = true;
|
|
2114
|
+
} else {
|
|
2115
|
+
const f = findFieldByIdOrName(table, rule.triggerFieldId);
|
|
2116
|
+
if (f) {
|
|
2117
|
+
if (event.changedFieldIds && event.changedFieldIds.length > 0 && !event.changedFieldIds.includes(f.id)) {
|
|
2118
|
+
triggerMatched = false;
|
|
2119
|
+
} else {
|
|
2120
|
+
const rawVal = resolveBaseCellValue(unitData, table.id, record, f);
|
|
2121
|
+
const dispVal = formatBaseCellValue(unitData, table.id, f, rawVal);
|
|
2122
|
+
triggerMatched = matchesBaseFilterRule(
|
|
2123
|
+
{
|
|
2124
|
+
id: "legacy_match",
|
|
2125
|
+
fieldId: f.id,
|
|
2126
|
+
operator: "is",
|
|
2127
|
+
value: rule.triggerValue
|
|
2128
|
+
},
|
|
2129
|
+
f,
|
|
2130
|
+
rawVal,
|
|
2131
|
+
dispVal
|
|
2132
|
+
);
|
|
2133
|
+
}
|
|
2134
|
+
}
|
|
2135
|
+
}
|
|
2136
|
+
}
|
|
2137
|
+
break;
|
|
2138
|
+
}
|
|
2139
|
+
case "scheduled":
|
|
2140
|
+
triggerMatched = event.trigger === "scheduled";
|
|
2141
|
+
break;
|
|
2142
|
+
case "webhookReceived":
|
|
2143
|
+
triggerMatched = event.trigger === "webhookReceived";
|
|
2144
|
+
break;
|
|
2145
|
+
default:
|
|
2146
|
+
triggerMatched = false;
|
|
2147
|
+
}
|
|
2148
|
+
if (!triggerMatched) {
|
|
2149
|
+
return { triggerMatched: false, conditionsMatched: false };
|
|
2150
|
+
}
|
|
2151
|
+
const conditionsMatched = evaluateAutomationConditions(
|
|
2152
|
+
rule.conditions,
|
|
2153
|
+
rule.conditionConjunction,
|
|
2154
|
+
unitData,
|
|
2155
|
+
table,
|
|
2156
|
+
record
|
|
2157
|
+
);
|
|
2158
|
+
return { triggerMatched: true, conditionsMatched };
|
|
2159
|
+
}
|
|
2160
|
+
function applyCellsUpdateToRecord(workingUnit, table, record, rawUpdates, sourceTable, sourceRecord, webhookPayload, timestamp) {
|
|
2161
|
+
const previousCells = import_core.Tools.deepClone(record.cells);
|
|
2162
|
+
const changedFieldIds = [];
|
|
2163
|
+
for (const [key, rawTemplate] of Object.entries(rawUpdates)) {
|
|
2164
|
+
const targetField = findFieldByIdOrName(table, key);
|
|
2165
|
+
if (!targetField || isComputedBaseField(targetField.type)) continue;
|
|
2166
|
+
const computedVal = interpolateAutomationCellValue(
|
|
2167
|
+
rawTemplate,
|
|
2168
|
+
targetField,
|
|
2169
|
+
workingUnit,
|
|
2170
|
+
sourceTable,
|
|
2171
|
+
sourceRecord,
|
|
2172
|
+
webhookPayload
|
|
2173
|
+
);
|
|
2174
|
+
if (targetField.type === "link") {
|
|
2175
|
+
const prevIds = normalizeLinkIds(record.cells[targetField.id]);
|
|
2176
|
+
const rawNextIds = normalizeLinkIds(computedVal);
|
|
2177
|
+
const nextIds = targetField.config?.allowMultiple === false ? rawNextIds.slice(0, 1) : rawNextIds;
|
|
2178
|
+
if (!cellsEqual(prevIds, nextIds)) {
|
|
2179
|
+
record.cells[targetField.id] = nextIds;
|
|
2180
|
+
syncLinkInTables(
|
|
2181
|
+
workingUnit.tables,
|
|
2182
|
+
table.id,
|
|
2183
|
+
record.id,
|
|
2184
|
+
targetField,
|
|
2185
|
+
prevIds,
|
|
2186
|
+
nextIds,
|
|
2187
|
+
timestamp
|
|
2188
|
+
);
|
|
2189
|
+
changedFieldIds.push(targetField.id);
|
|
2190
|
+
}
|
|
2191
|
+
} else if (!cellsEqual(record.cells[targetField.id], computedVal)) {
|
|
2192
|
+
record.cells[targetField.id] = computedVal;
|
|
2193
|
+
changedFieldIds.push(targetField.id);
|
|
2194
|
+
}
|
|
2195
|
+
}
|
|
2196
|
+
if (changedFieldIds.length > 0) {
|
|
2197
|
+
record.lastModifiedTime = timestamp;
|
|
2198
|
+
}
|
|
2199
|
+
return { changedFieldIds, previousCells };
|
|
2200
|
+
}
|
|
2201
|
+
function buildWebhookRequestForStep(step, rule, workingUnit, currentTable, currentRecord, webhookPayload) {
|
|
2202
|
+
const url = interpolateAutomationString(
|
|
2203
|
+
step.webhookUrl || "https://webhook.local/bases",
|
|
2204
|
+
workingUnit,
|
|
2205
|
+
currentTable,
|
|
2206
|
+
currentRecord,
|
|
2207
|
+
webhookPayload
|
|
2208
|
+
);
|
|
2209
|
+
const method = step.webhookMethod || "POST";
|
|
2210
|
+
let headers;
|
|
2211
|
+
if (step.webhookHeaders) {
|
|
2212
|
+
headers = {};
|
|
2213
|
+
for (const [hk, hv] of Object.entries(step.webhookHeaders)) {
|
|
2214
|
+
headers[hk] = interpolateAutomationString(
|
|
2215
|
+
hv,
|
|
2216
|
+
workingUnit,
|
|
2217
|
+
currentTable,
|
|
2218
|
+
currentRecord,
|
|
2219
|
+
webhookPayload
|
|
2220
|
+
);
|
|
2221
|
+
}
|
|
2222
|
+
}
|
|
2223
|
+
const body = step.webhookBodyTemplate !== void 0 ? interpolateStructuredPayload(
|
|
2224
|
+
step.webhookBodyTemplate,
|
|
2225
|
+
workingUnit,
|
|
2226
|
+
currentTable,
|
|
2227
|
+
currentRecord,
|
|
2228
|
+
webhookPayload
|
|
2229
|
+
) : {
|
|
2230
|
+
ruleId: rule.id,
|
|
2231
|
+
ruleName: rule.name,
|
|
2232
|
+
tableId: currentTable.id,
|
|
2233
|
+
recordId: currentRecord?.id ?? null,
|
|
2234
|
+
cells: currentRecord ? import_core.Tools.deepClone(currentRecord.cells) : {}
|
|
2235
|
+
};
|
|
2236
|
+
return { url, method, headers, body };
|
|
2237
|
+
}
|
|
2238
|
+
function pushAutomationLog(workingUnit, log, maxLogs = 200) {
|
|
2239
|
+
if (!workingUnit.automationLogs) {
|
|
2240
|
+
workingUnit.automationLogs = [];
|
|
2241
|
+
}
|
|
2242
|
+
workingUnit.automationLogs.push(log);
|
|
2243
|
+
if (workingUnit.automationLogs.length > maxLogs) {
|
|
2244
|
+
workingUnit.automationLogs = workingUnit.automationLogs.slice(
|
|
2245
|
+
workingUnit.automationLogs.length - maxLogs
|
|
2246
|
+
);
|
|
2247
|
+
}
|
|
2248
|
+
}
|
|
2249
|
+
function runBaseAutomationsInPlaceSync(workingUnit, initialEvent, options) {
|
|
2250
|
+
const logs = [];
|
|
2251
|
+
const allQueuedWebhooks = [];
|
|
2252
|
+
const executedRuleIds = [];
|
|
2253
|
+
const timestamp = options?.timestamp ?? nowTimestamp();
|
|
2254
|
+
const maxLogs = options?.maxLogs ?? 200;
|
|
2255
|
+
const queue = [
|
|
2256
|
+
{ ...initialEvent, depth: initialEvent.depth ?? 1 }
|
|
2257
|
+
];
|
|
2258
|
+
while (queue.length > 0) {
|
|
2259
|
+
const event = queue.shift();
|
|
2260
|
+
const depth = event.depth ?? 1;
|
|
2261
|
+
const table = workingUnit.tables[event.tableId];
|
|
2262
|
+
if (!table) continue;
|
|
2263
|
+
const record = event.recordId ? table.records[event.recordId] : void 0;
|
|
2264
|
+
const rules = workingUnit.automations ?? [];
|
|
2265
|
+
for (const rule of rules) {
|
|
2266
|
+
const match = doesAutomationTriggerMatch(rule, event, workingUnit, table, record);
|
|
2267
|
+
if (!match.triggerMatched) continue;
|
|
2268
|
+
if (depth > MAX_AUTOMATION_RECURSION_DEPTH) {
|
|
2269
|
+
const skipLog = {
|
|
2270
|
+
id: `alog_${(0, import_core.generateRandomId)(6)}`,
|
|
2271
|
+
ruleId: rule.id,
|
|
2272
|
+
ruleName: rule.name,
|
|
2273
|
+
tableId: table.id,
|
|
2274
|
+
recordId: record?.id,
|
|
2275
|
+
trigger: event.trigger,
|
|
2276
|
+
status: "skipped",
|
|
2277
|
+
executedSteps: 0,
|
|
2278
|
+
timestamp,
|
|
2279
|
+
summary: `\u9012\u5F52\u6DF1\u5EA6\u8D85\u8FC7\u4E0A\u9650 (${MAX_AUTOMATION_RECURSION_DEPTH})\uFF0C\u5DF2\u81EA\u52A8\u62E6\u622A\u9632\u6B62\u6B7B\u5FAA\u73AF`
|
|
2280
|
+
};
|
|
2281
|
+
logs.push(skipLog);
|
|
2282
|
+
pushAutomationLog(workingUnit, skipLog, maxLogs);
|
|
2283
|
+
continue;
|
|
2284
|
+
}
|
|
2285
|
+
if (!match.conditionsMatched) {
|
|
2286
|
+
const skipLog = {
|
|
2287
|
+
id: `alog_${(0, import_core.generateRandomId)(6)}`,
|
|
2288
|
+
ruleId: rule.id,
|
|
2289
|
+
ruleName: rule.name,
|
|
2290
|
+
tableId: table.id,
|
|
2291
|
+
recordId: record?.id,
|
|
2292
|
+
trigger: event.trigger,
|
|
2293
|
+
status: "skipped",
|
|
2294
|
+
executedSteps: 0,
|
|
2295
|
+
timestamp,
|
|
2296
|
+
summary: "\u89E6\u53D1\u5668\u5DF2\u547D\u4E2D\uFF0C\u4F46\u672A\u6EE1\u8DB3\u524D\u7F6E\u8FC7\u6EE4\u6761\u4EF6\uFF0C\u8DF3\u8FC7\u6267\u884C"
|
|
2297
|
+
};
|
|
2298
|
+
logs.push(skipLog);
|
|
2299
|
+
pushAutomationLog(workingUnit, skipLog, maxLogs);
|
|
2300
|
+
continue;
|
|
2301
|
+
}
|
|
2302
|
+
const steps = normalizeAutomationSteps(rule);
|
|
2303
|
+
let executedSteps = 0;
|
|
2304
|
+
const summaries = [];
|
|
2305
|
+
const ruleQueuedWebhooks = [];
|
|
2306
|
+
const cascades = [];
|
|
2307
|
+
let errorSummary = null;
|
|
2308
|
+
const execStepList = (stepList, ctxTable, ctxRecord) => {
|
|
2309
|
+
for (const step of stepList) {
|
|
2310
|
+
if (errorSummary) return;
|
|
2311
|
+
switch (step.type) {
|
|
2312
|
+
case "updateRecord": {
|
|
2313
|
+
if (!ctxRecord || !step.fieldUpdates) break;
|
|
2314
|
+
executedSteps += 1;
|
|
2315
|
+
const { changedFieldIds, previousCells } = applyCellsUpdateToRecord(
|
|
2316
|
+
workingUnit,
|
|
2317
|
+
ctxTable,
|
|
2318
|
+
ctxRecord,
|
|
2319
|
+
step.fieldUpdates,
|
|
2320
|
+
ctxTable,
|
|
2321
|
+
ctxRecord,
|
|
2322
|
+
event.webhookPayload,
|
|
2323
|
+
timestamp
|
|
2324
|
+
);
|
|
2325
|
+
summaries.push(`\u4FEE\u6539\u8BB0\u5F55 (${changedFieldIds.length} \u4E2A\u5B57\u6BB5)`);
|
|
2326
|
+
if (changedFieldIds.length > 0) {
|
|
2327
|
+
cascades.push({
|
|
2328
|
+
event: {
|
|
2329
|
+
trigger: "recordUpdated",
|
|
2330
|
+
tableId: ctxTable.id,
|
|
2331
|
+
recordId: ctxRecord.id,
|
|
2332
|
+
changedFieldIds,
|
|
2333
|
+
previousCells,
|
|
2334
|
+
depth: depth + 1
|
|
2335
|
+
}
|
|
2336
|
+
});
|
|
2337
|
+
}
|
|
2338
|
+
break;
|
|
2339
|
+
}
|
|
2340
|
+
case "createLinkedRecord": {
|
|
2341
|
+
const targetTableId = step.targetTableId;
|
|
2342
|
+
const targetTable = targetTableId ? workingUnit.tables[targetTableId] : void 0;
|
|
2343
|
+
if (!targetTable) {
|
|
2344
|
+
errorSummary = `\u76EE\u6807\u5173\u8054\u8868\u4E0D\u5B58\u5728: ${targetTableId ?? "\u672A\u6307\u5B9A"}`;
|
|
2345
|
+
return;
|
|
2346
|
+
}
|
|
2347
|
+
executedSteps += 1;
|
|
2348
|
+
const newRecId = `rec_auto_${(0, import_core.generateRandomId)(6)}`;
|
|
2349
|
+
const autoNum = targetTable.nextAutoNumber ?? targetTable.recordOrder.length + 1;
|
|
2350
|
+
targetTable.nextAutoNumber = autoNum + 1;
|
|
2351
|
+
const newRec = {
|
|
2352
|
+
id: newRecId,
|
|
2353
|
+
autoNumber: autoNum,
|
|
2354
|
+
createdTime: timestamp,
|
|
2355
|
+
lastModifiedTime: timestamp,
|
|
2356
|
+
createdBy: "\u81EA\u52A8\u5316\u5F15\u64CE",
|
|
2357
|
+
lastModifiedBy: "\u81EA\u52A8\u5316\u5F15\u64CE",
|
|
2358
|
+
cells: {}
|
|
2359
|
+
};
|
|
2360
|
+
targetTable.records[newRecId] = newRec;
|
|
2361
|
+
targetTable.recordOrder.push(newRecId);
|
|
2362
|
+
if (step.recordCells) {
|
|
2363
|
+
applyCellsUpdateToRecord(
|
|
2364
|
+
workingUnit,
|
|
2365
|
+
targetTable,
|
|
2366
|
+
newRec,
|
|
2367
|
+
step.recordCells,
|
|
2368
|
+
ctxTable,
|
|
2369
|
+
ctxRecord,
|
|
2370
|
+
event.webhookPayload,
|
|
2371
|
+
timestamp
|
|
2372
|
+
);
|
|
2373
|
+
}
|
|
2374
|
+
if (ctxRecord) {
|
|
2375
|
+
const explicitFieldId = step.linkBackFieldId;
|
|
2376
|
+
const srcLinkField = explicitFieldId ? ctxTable.fields[explicitFieldId] : Object.values(ctxTable.fields).find(
|
|
2377
|
+
(f) => f.type === "link" && f.config?.linkedTableId === targetTable.id
|
|
2378
|
+
);
|
|
2379
|
+
const tgtLinkField = explicitFieldId && targetTable.fields[explicitFieldId] ? targetTable.fields[explicitFieldId] : Object.values(targetTable.fields).find(
|
|
2380
|
+
(f) => f.type === "link" && f.config?.linkedTableId === ctxTable.id
|
|
2381
|
+
);
|
|
2382
|
+
if (srcLinkField && srcLinkField.type === "link" && srcLinkField.config?.linkedTableId === targetTable.id) {
|
|
2383
|
+
const prevIds = normalizeLinkIds(ctxRecord.cells[srcLinkField.id]);
|
|
2384
|
+
const nextIds = srcLinkField.config?.allowMultiple === false ? [newRecId] : Array.from(/* @__PURE__ */ new Set([...prevIds, newRecId]));
|
|
2385
|
+
ctxRecord.cells[srcLinkField.id] = nextIds;
|
|
2386
|
+
ctxRecord.lastModifiedTime = timestamp;
|
|
2387
|
+
syncLinkInTables(
|
|
2388
|
+
workingUnit.tables,
|
|
2389
|
+
ctxTable.id,
|
|
2390
|
+
ctxRecord.id,
|
|
2391
|
+
srcLinkField,
|
|
2392
|
+
prevIds,
|
|
2393
|
+
nextIds,
|
|
2394
|
+
timestamp
|
|
2395
|
+
);
|
|
2396
|
+
} else if (tgtLinkField && tgtLinkField.type === "link" && tgtLinkField.config?.linkedTableId === ctxTable.id) {
|
|
2397
|
+
const prevIds = normalizeLinkIds(newRec.cells[tgtLinkField.id]);
|
|
2398
|
+
const nextIds = tgtLinkField.config?.allowMultiple === false ? [ctxRecord.id] : Array.from(/* @__PURE__ */ new Set([...prevIds, ctxRecord.id]));
|
|
2399
|
+
newRec.cells[tgtLinkField.id] = nextIds;
|
|
2400
|
+
syncLinkInTables(
|
|
2401
|
+
workingUnit.tables,
|
|
2402
|
+
targetTable.id,
|
|
2403
|
+
newRecId,
|
|
2404
|
+
tgtLinkField,
|
|
2405
|
+
prevIds,
|
|
2406
|
+
nextIds,
|
|
2407
|
+
timestamp
|
|
2408
|
+
);
|
|
2409
|
+
}
|
|
2410
|
+
}
|
|
2411
|
+
summaries.push(`\u65B0\u5EFA\u5173\u8054\u8BB0\u5F55 -> ${targetTable.name}(${newRecId})`);
|
|
2412
|
+
cascades.push({
|
|
2413
|
+
event: {
|
|
2414
|
+
trigger: "recordCreated",
|
|
2415
|
+
tableId: targetTable.id,
|
|
2416
|
+
recordId: newRecId,
|
|
2417
|
+
changedFieldIds: Object.keys(newRec.cells),
|
|
2418
|
+
depth: depth + 1
|
|
2419
|
+
}
|
|
2420
|
+
});
|
|
2421
|
+
break;
|
|
2422
|
+
}
|
|
2423
|
+
case "ifElse": {
|
|
2424
|
+
executedSteps += 1;
|
|
2425
|
+
const branchMatch = evaluateAutomationConditions(
|
|
2426
|
+
step.conditions,
|
|
2427
|
+
step.conditionConjunction,
|
|
2428
|
+
workingUnit,
|
|
2429
|
+
ctxTable,
|
|
2430
|
+
ctxRecord
|
|
2431
|
+
);
|
|
2432
|
+
summaries.push(branchMatch ? "If\u6761\u4EF6\u6210\u7ACB -> Then\u5206\u652F" : "If\u6761\u4EF6\u4E0D\u6210\u7ACB -> Else\u5206\u652F");
|
|
2433
|
+
if (branchMatch) {
|
|
2434
|
+
execStepList(step.thenSteps ?? [], ctxTable, ctxRecord);
|
|
2435
|
+
} else {
|
|
2436
|
+
execStepList(step.elseSteps ?? [], ctxTable, ctxRecord);
|
|
2437
|
+
}
|
|
2438
|
+
break;
|
|
2439
|
+
}
|
|
2440
|
+
case "loopRecords": {
|
|
2441
|
+
const loopTbl = workingUnit.tables[step.loopTableId || ctxTable.id];
|
|
2442
|
+
if (!loopTbl) break;
|
|
2443
|
+
executedSteps += 1;
|
|
2444
|
+
const matchedRecords = loopTbl.recordOrder.map((rid) => loopTbl.records[rid]).filter(
|
|
2445
|
+
(r) => Boolean(r) && evaluateAutomationConditions(
|
|
2446
|
+
step.loopConditions,
|
|
2447
|
+
step.loopConditionConjunction,
|
|
2448
|
+
workingUnit,
|
|
2449
|
+
loopTbl,
|
|
2450
|
+
r
|
|
2451
|
+
)
|
|
2452
|
+
);
|
|
2453
|
+
for (const loopRec of matchedRecords) {
|
|
2454
|
+
execStepList(step.loopSteps ?? [], loopTbl, loopRec);
|
|
2455
|
+
}
|
|
2456
|
+
summaries.push(`\u5FAA\u73AF\u5904\u7406 ${matchedRecords.length} \u6761\u8BB0\u5F55`);
|
|
2457
|
+
break;
|
|
2458
|
+
}
|
|
2459
|
+
case "httpWebhook": {
|
|
2460
|
+
executedSteps += 1;
|
|
2461
|
+
const req = buildWebhookRequestForStep(
|
|
2462
|
+
step,
|
|
2463
|
+
rule,
|
|
2464
|
+
workingUnit,
|
|
2465
|
+
ctxTable,
|
|
2466
|
+
ctxRecord,
|
|
2467
|
+
event.webhookPayload
|
|
2468
|
+
);
|
|
2469
|
+
if (options?.webhookExecutor) {
|
|
2470
|
+
try {
|
|
2471
|
+
const resOrPromise = options.webhookExecutor(req);
|
|
2472
|
+
if (resOrPromise && typeof resOrPromise.then === "function") {
|
|
2473
|
+
summaries.push(`Webhook ${req.method} ${req.url} (\u5F02\u6B65\u5DF2\u6D3E\u53D1)`);
|
|
2474
|
+
void resOrPromise.catch(() => {
|
|
2475
|
+
});
|
|
2476
|
+
} else {
|
|
2477
|
+
const syncRes = resOrPromise;
|
|
2478
|
+
if (syncRes.status >= 400) {
|
|
2479
|
+
errorSummary = `Webhook \u54CD\u5E94\u5F02\u5E38 HTTP ${syncRes.status}`;
|
|
2480
|
+
return;
|
|
2481
|
+
}
|
|
2482
|
+
summaries.push(`Webhook ${req.method} ${req.url} -> ${syncRes.status}`);
|
|
2483
|
+
}
|
|
2484
|
+
} catch (err) {
|
|
2485
|
+
errorSummary = err instanceof Error ? err.message : "Webhook \u6267\u884C\u5F02\u5E38";
|
|
2486
|
+
return;
|
|
2487
|
+
}
|
|
2488
|
+
} else {
|
|
2489
|
+
const queued = {
|
|
2490
|
+
id: `whq_${(0, import_core.generateRandomId)(6)}`,
|
|
2491
|
+
ruleId: rule.id,
|
|
2492
|
+
ruleName: rule.name,
|
|
2493
|
+
url: req.url,
|
|
2494
|
+
method: req.method,
|
|
2495
|
+
headers: req.headers,
|
|
2496
|
+
body: req.body,
|
|
2497
|
+
timestamp
|
|
2498
|
+
};
|
|
2499
|
+
ruleQueuedWebhooks.push(queued);
|
|
2500
|
+
allQueuedWebhooks.push(queued);
|
|
2501
|
+
if (!workingUnit.queuedWebhooks) workingUnit.queuedWebhooks = [];
|
|
2502
|
+
workingUnit.queuedWebhooks.push(queued);
|
|
2503
|
+
summaries.push(`Webhook \u5DF2\u5165\u672C\u5730\u79BB\u7EBF\u961F\u5217 (${req.url})`);
|
|
2504
|
+
}
|
|
2505
|
+
break;
|
|
2506
|
+
}
|
|
2507
|
+
case "notify": {
|
|
2508
|
+
executedSteps += 1;
|
|
2509
|
+
const msg = interpolateAutomationString(
|
|
2510
|
+
step.notifyMessage || rule.name,
|
|
2511
|
+
workingUnit,
|
|
2512
|
+
ctxTable,
|
|
2513
|
+
ctxRecord,
|
|
2514
|
+
event.webhookPayload
|
|
2515
|
+
);
|
|
2516
|
+
summaries.push(`\u53D1\u9001\u901A\u77E5 [${step.notifyChannel || "\u7CFB\u7EDF"}]: ${msg}`);
|
|
2517
|
+
break;
|
|
2518
|
+
}
|
|
2519
|
+
}
|
|
2520
|
+
}
|
|
2521
|
+
};
|
|
2522
|
+
execStepList(steps, table, record);
|
|
2523
|
+
executedRuleIds.push(rule.id);
|
|
2524
|
+
const runLog = {
|
|
2525
|
+
id: `alog_${(0, import_core.generateRandomId)(6)}`,
|
|
2526
|
+
ruleId: rule.id,
|
|
2527
|
+
ruleName: rule.name,
|
|
2528
|
+
tableId: table.id,
|
|
2529
|
+
recordId: record?.id,
|
|
2530
|
+
trigger: event.trigger,
|
|
2531
|
+
status: errorSummary ? "error" : "success",
|
|
2532
|
+
executedSteps,
|
|
2533
|
+
timestamp,
|
|
2534
|
+
summary: errorSummary ?? (summaries.join(" -> ") || "\u89C4\u5219\u5DF2\u6267\u884C"),
|
|
2535
|
+
...ruleQueuedWebhooks.length > 0 ? { queuedWebhooks: ruleQueuedWebhooks } : {}
|
|
2536
|
+
};
|
|
2537
|
+
logs.push(runLog);
|
|
2538
|
+
pushAutomationLog(workingUnit, runLog, maxLogs);
|
|
2539
|
+
if (!errorSummary && cascades.length > 0) {
|
|
2540
|
+
for (const c of cascades) {
|
|
2541
|
+
queue.push(c.event);
|
|
2542
|
+
}
|
|
2543
|
+
}
|
|
2544
|
+
}
|
|
2545
|
+
}
|
|
2546
|
+
return {
|
|
2547
|
+
logs,
|
|
2548
|
+
queuedWebhooks: allQueuedWebhooks,
|
|
2549
|
+
executedRuleIds
|
|
2550
|
+
};
|
|
2551
|
+
}
|
|
2552
|
+
async function executeBaseAutomations(unitData, initialEvent, options) {
|
|
2553
|
+
const workingUnit = import_core.Tools.deepClone(unitData);
|
|
2554
|
+
const logs = [];
|
|
2555
|
+
const allQueuedWebhooks = [];
|
|
2556
|
+
const executedRuleIds = [];
|
|
2557
|
+
const timestamp = options?.timestamp ?? nowTimestamp();
|
|
2558
|
+
const maxLogs = options?.maxLogs ?? 200;
|
|
2559
|
+
const queue = [
|
|
2560
|
+
{ ...initialEvent, depth: initialEvent.depth ?? 1 }
|
|
2561
|
+
];
|
|
2562
|
+
while (queue.length > 0) {
|
|
2563
|
+
const event = queue.shift();
|
|
2564
|
+
const depth = event.depth ?? 1;
|
|
2565
|
+
const table = workingUnit.tables[event.tableId];
|
|
2566
|
+
if (!table) continue;
|
|
2567
|
+
const record = event.recordId ? table.records[event.recordId] : void 0;
|
|
2568
|
+
const rules = workingUnit.automations ?? [];
|
|
2569
|
+
for (const rule of rules) {
|
|
2570
|
+
const match = doesAutomationTriggerMatch(rule, event, workingUnit, table, record);
|
|
2571
|
+
if (!match.triggerMatched) continue;
|
|
2572
|
+
if (depth > MAX_AUTOMATION_RECURSION_DEPTH) {
|
|
2573
|
+
const skipLog = {
|
|
2574
|
+
id: `alog_${(0, import_core.generateRandomId)(6)}`,
|
|
2575
|
+
ruleId: rule.id,
|
|
2576
|
+
ruleName: rule.name,
|
|
2577
|
+
tableId: table.id,
|
|
2578
|
+
recordId: record?.id,
|
|
2579
|
+
trigger: event.trigger,
|
|
2580
|
+
status: "skipped",
|
|
2581
|
+
executedSteps: 0,
|
|
2582
|
+
timestamp,
|
|
2583
|
+
summary: `\u9012\u5F52\u6DF1\u5EA6\u8D85\u8FC7\u4E0A\u9650 (${MAX_AUTOMATION_RECURSION_DEPTH})\uFF0C\u5DF2\u81EA\u52A8\u62E6\u622A\u9632\u6B62\u6B7B\u5FAA\u73AF`
|
|
2584
|
+
};
|
|
2585
|
+
logs.push(skipLog);
|
|
2586
|
+
pushAutomationLog(workingUnit, skipLog, maxLogs);
|
|
2587
|
+
continue;
|
|
2588
|
+
}
|
|
2589
|
+
if (!match.conditionsMatched) {
|
|
2590
|
+
const skipLog = {
|
|
2591
|
+
id: `alog_${(0, import_core.generateRandomId)(6)}`,
|
|
2592
|
+
ruleId: rule.id,
|
|
2593
|
+
ruleName: rule.name,
|
|
2594
|
+
tableId: table.id,
|
|
2595
|
+
recordId: record?.id,
|
|
2596
|
+
trigger: event.trigger,
|
|
2597
|
+
status: "skipped",
|
|
2598
|
+
executedSteps: 0,
|
|
2599
|
+
timestamp,
|
|
2600
|
+
summary: "\u89E6\u53D1\u5668\u5DF2\u547D\u4E2D\uFF0C\u4F46\u672A\u6EE1\u8DB3\u524D\u7F6E\u8FC7\u6EE4\u6761\u4EF6\uFF0C\u8DF3\u8FC7\u6267\u884C"
|
|
2601
|
+
};
|
|
2602
|
+
logs.push(skipLog);
|
|
2603
|
+
pushAutomationLog(workingUnit, skipLog, maxLogs);
|
|
2604
|
+
continue;
|
|
2605
|
+
}
|
|
2606
|
+
const steps = normalizeAutomationSteps(rule);
|
|
2607
|
+
let executedSteps = 0;
|
|
2608
|
+
const summaries = [];
|
|
2609
|
+
const ruleQueuedWebhooks = [];
|
|
2610
|
+
const cascades = [];
|
|
2611
|
+
let errorSummary = null;
|
|
2612
|
+
const execStepListAsync = async (stepList, ctxTable, ctxRecord) => {
|
|
2613
|
+
for (const step of stepList) {
|
|
2614
|
+
if (errorSummary) return;
|
|
2615
|
+
switch (step.type) {
|
|
2616
|
+
case "updateRecord": {
|
|
2617
|
+
if (!ctxRecord || !step.fieldUpdates) break;
|
|
2618
|
+
executedSteps += 1;
|
|
2619
|
+
const { changedFieldIds, previousCells } = applyCellsUpdateToRecord(
|
|
2620
|
+
workingUnit,
|
|
2621
|
+
ctxTable,
|
|
2622
|
+
ctxRecord,
|
|
2623
|
+
step.fieldUpdates,
|
|
2624
|
+
ctxTable,
|
|
2625
|
+
ctxRecord,
|
|
2626
|
+
event.webhookPayload,
|
|
2627
|
+
timestamp
|
|
2628
|
+
);
|
|
2629
|
+
summaries.push(`\u4FEE\u6539\u8BB0\u5F55 (${changedFieldIds.length} \u4E2A\u5B57\u6BB5)`);
|
|
2630
|
+
if (changedFieldIds.length > 0) {
|
|
2631
|
+
cascades.push({
|
|
2632
|
+
event: {
|
|
2633
|
+
trigger: "recordUpdated",
|
|
2634
|
+
tableId: ctxTable.id,
|
|
2635
|
+
recordId: ctxRecord.id,
|
|
2636
|
+
changedFieldIds,
|
|
2637
|
+
previousCells,
|
|
2638
|
+
depth: depth + 1
|
|
2639
|
+
}
|
|
2640
|
+
});
|
|
2641
|
+
}
|
|
2642
|
+
break;
|
|
2643
|
+
}
|
|
2644
|
+
case "createLinkedRecord": {
|
|
2645
|
+
const targetTableId = step.targetTableId;
|
|
2646
|
+
const targetTable = targetTableId ? workingUnit.tables[targetTableId] : void 0;
|
|
2647
|
+
if (!targetTable) {
|
|
2648
|
+
errorSummary = `\u76EE\u6807\u5173\u8054\u8868\u4E0D\u5B58\u5728: ${targetTableId ?? "\u672A\u6307\u5B9A"}`;
|
|
2649
|
+
return;
|
|
2650
|
+
}
|
|
2651
|
+
executedSteps += 1;
|
|
2652
|
+
const newRecId = `rec_auto_${(0, import_core.generateRandomId)(6)}`;
|
|
2653
|
+
const autoNum = targetTable.nextAutoNumber ?? targetTable.recordOrder.length + 1;
|
|
2654
|
+
targetTable.nextAutoNumber = autoNum + 1;
|
|
2655
|
+
const newRec = {
|
|
2656
|
+
id: newRecId,
|
|
2657
|
+
autoNumber: autoNum,
|
|
2658
|
+
createdTime: timestamp,
|
|
2659
|
+
lastModifiedTime: timestamp,
|
|
2660
|
+
createdBy: "\u81EA\u52A8\u5316\u5F15\u64CE",
|
|
2661
|
+
lastModifiedBy: "\u81EA\u52A8\u5316\u5F15\u64CE",
|
|
2662
|
+
cells: {}
|
|
2663
|
+
};
|
|
2664
|
+
targetTable.records[newRecId] = newRec;
|
|
2665
|
+
targetTable.recordOrder.push(newRecId);
|
|
2666
|
+
if (step.recordCells) {
|
|
2667
|
+
applyCellsUpdateToRecord(
|
|
2668
|
+
workingUnit,
|
|
2669
|
+
targetTable,
|
|
2670
|
+
newRec,
|
|
2671
|
+
step.recordCells,
|
|
2672
|
+
ctxTable,
|
|
2673
|
+
ctxRecord,
|
|
2674
|
+
event.webhookPayload,
|
|
2675
|
+
timestamp
|
|
2676
|
+
);
|
|
2677
|
+
}
|
|
2678
|
+
if (ctxRecord) {
|
|
2679
|
+
const explicitFieldId = step.linkBackFieldId;
|
|
2680
|
+
const srcLinkField = explicitFieldId ? ctxTable.fields[explicitFieldId] : Object.values(ctxTable.fields).find(
|
|
2681
|
+
(f) => f.type === "link" && f.config?.linkedTableId === targetTable.id
|
|
2682
|
+
);
|
|
2683
|
+
const tgtLinkField = explicitFieldId && targetTable.fields[explicitFieldId] ? targetTable.fields[explicitFieldId] : Object.values(targetTable.fields).find(
|
|
2684
|
+
(f) => f.type === "link" && f.config?.linkedTableId === ctxTable.id
|
|
2685
|
+
);
|
|
2686
|
+
if (srcLinkField && srcLinkField.type === "link" && srcLinkField.config?.linkedTableId === targetTable.id) {
|
|
2687
|
+
const prevIds = normalizeLinkIds(ctxRecord.cells[srcLinkField.id]);
|
|
2688
|
+
const nextIds = srcLinkField.config?.allowMultiple === false ? [newRecId] : Array.from(/* @__PURE__ */ new Set([...prevIds, newRecId]));
|
|
2689
|
+
ctxRecord.cells[srcLinkField.id] = nextIds;
|
|
2690
|
+
ctxRecord.lastModifiedTime = timestamp;
|
|
2691
|
+
syncLinkInTables(
|
|
2692
|
+
workingUnit.tables,
|
|
2693
|
+
ctxTable.id,
|
|
2694
|
+
ctxRecord.id,
|
|
2695
|
+
srcLinkField,
|
|
2696
|
+
prevIds,
|
|
2697
|
+
nextIds,
|
|
2698
|
+
timestamp
|
|
2699
|
+
);
|
|
2700
|
+
} else if (tgtLinkField && tgtLinkField.type === "link" && tgtLinkField.config?.linkedTableId === ctxTable.id) {
|
|
2701
|
+
const prevIds = normalizeLinkIds(newRec.cells[tgtLinkField.id]);
|
|
2702
|
+
const nextIds = tgtLinkField.config?.allowMultiple === false ? [ctxRecord.id] : Array.from(/* @__PURE__ */ new Set([...prevIds, ctxRecord.id]));
|
|
2703
|
+
newRec.cells[tgtLinkField.id] = nextIds;
|
|
2704
|
+
syncLinkInTables(
|
|
2705
|
+
workingUnit.tables,
|
|
2706
|
+
targetTable.id,
|
|
2707
|
+
newRecId,
|
|
2708
|
+
tgtLinkField,
|
|
2709
|
+
prevIds,
|
|
2710
|
+
nextIds,
|
|
2711
|
+
timestamp
|
|
2712
|
+
);
|
|
2713
|
+
}
|
|
2714
|
+
}
|
|
2715
|
+
summaries.push(`\u65B0\u5EFA\u5173\u8054\u8BB0\u5F55 -> ${targetTable.name}(${newRecId})`);
|
|
2716
|
+
cascades.push({
|
|
2717
|
+
event: {
|
|
2718
|
+
trigger: "recordCreated",
|
|
2719
|
+
tableId: targetTable.id,
|
|
2720
|
+
recordId: newRecId,
|
|
2721
|
+
changedFieldIds: Object.keys(newRec.cells),
|
|
2722
|
+
depth: depth + 1
|
|
2723
|
+
}
|
|
2724
|
+
});
|
|
2725
|
+
break;
|
|
2726
|
+
}
|
|
2727
|
+
case "ifElse": {
|
|
2728
|
+
executedSteps += 1;
|
|
2729
|
+
const branchMatch = evaluateAutomationConditions(
|
|
2730
|
+
step.conditions,
|
|
2731
|
+
step.conditionConjunction,
|
|
2732
|
+
workingUnit,
|
|
2733
|
+
ctxTable,
|
|
2734
|
+
ctxRecord
|
|
2735
|
+
);
|
|
2736
|
+
summaries.push(branchMatch ? "If\u6761\u4EF6\u6210\u7ACB -> Then\u5206\u652F" : "If\u6761\u4EF6\u4E0D\u6210\u7ACB -> Else\u5206\u652F");
|
|
2737
|
+
if (branchMatch) {
|
|
2738
|
+
await execStepListAsync(step.thenSteps ?? [], ctxTable, ctxRecord);
|
|
2739
|
+
} else {
|
|
2740
|
+
await execStepListAsync(step.elseSteps ?? [], ctxTable, ctxRecord);
|
|
2741
|
+
}
|
|
2742
|
+
break;
|
|
2743
|
+
}
|
|
2744
|
+
case "loopRecords": {
|
|
2745
|
+
const loopTbl = workingUnit.tables[step.loopTableId || ctxTable.id];
|
|
2746
|
+
if (!loopTbl) break;
|
|
2747
|
+
executedSteps += 1;
|
|
2748
|
+
const matchedRecords = loopTbl.recordOrder.map((rid) => loopTbl.records[rid]).filter(
|
|
2749
|
+
(r) => Boolean(r) && evaluateAutomationConditions(
|
|
2750
|
+
step.loopConditions,
|
|
2751
|
+
step.loopConditionConjunction,
|
|
2752
|
+
workingUnit,
|
|
2753
|
+
loopTbl,
|
|
2754
|
+
r
|
|
2755
|
+
)
|
|
2756
|
+
);
|
|
2757
|
+
for (const loopRec of matchedRecords) {
|
|
2758
|
+
await execStepListAsync(step.loopSteps ?? [], loopTbl, loopRec);
|
|
2759
|
+
}
|
|
2760
|
+
summaries.push(`\u5FAA\u73AF\u5904\u7406 ${matchedRecords.length} \u6761\u8BB0\u5F55`);
|
|
2761
|
+
break;
|
|
2762
|
+
}
|
|
2763
|
+
case "httpWebhook": {
|
|
2764
|
+
executedSteps += 1;
|
|
2765
|
+
const req = buildWebhookRequestForStep(
|
|
2766
|
+
step,
|
|
2767
|
+
rule,
|
|
2768
|
+
workingUnit,
|
|
2769
|
+
ctxTable,
|
|
2770
|
+
ctxRecord,
|
|
2771
|
+
event.webhookPayload
|
|
2772
|
+
);
|
|
2773
|
+
if (options?.webhookExecutor) {
|
|
2774
|
+
try {
|
|
2775
|
+
const res = await options.webhookExecutor(req);
|
|
2776
|
+
if (res.status >= 400) {
|
|
2777
|
+
errorSummary = `Webhook \u54CD\u5E94\u5F02\u5E38 HTTP ${res.status}`;
|
|
2778
|
+
return;
|
|
2779
|
+
}
|
|
2780
|
+
summaries.push(`Webhook ${req.method} ${req.url} -> ${res.status}`);
|
|
2781
|
+
} catch (err) {
|
|
2782
|
+
errorSummary = err instanceof Error ? err.message : "Webhook \u6267\u884C\u5F02\u5E38";
|
|
2783
|
+
return;
|
|
2784
|
+
}
|
|
2785
|
+
} else {
|
|
2786
|
+
const queued = {
|
|
2787
|
+
id: `whq_${(0, import_core.generateRandomId)(6)}`,
|
|
2788
|
+
ruleId: rule.id,
|
|
2789
|
+
ruleName: rule.name,
|
|
2790
|
+
url: req.url,
|
|
2791
|
+
method: req.method,
|
|
2792
|
+
headers: req.headers,
|
|
2793
|
+
body: req.body,
|
|
2794
|
+
timestamp
|
|
2795
|
+
};
|
|
2796
|
+
ruleQueuedWebhooks.push(queued);
|
|
2797
|
+
allQueuedWebhooks.push(queued);
|
|
2798
|
+
if (!workingUnit.queuedWebhooks) workingUnit.queuedWebhooks = [];
|
|
2799
|
+
workingUnit.queuedWebhooks.push(queued);
|
|
2800
|
+
summaries.push(`Webhook \u5DF2\u5165\u672C\u5730\u79BB\u7EBF\u961F\u5217 (${req.url})`);
|
|
2801
|
+
}
|
|
2802
|
+
break;
|
|
2803
|
+
}
|
|
2804
|
+
case "notify": {
|
|
2805
|
+
executedSteps += 1;
|
|
2806
|
+
const msg = interpolateAutomationString(
|
|
2807
|
+
step.notifyMessage || rule.name,
|
|
2808
|
+
workingUnit,
|
|
2809
|
+
ctxTable,
|
|
2810
|
+
ctxRecord,
|
|
2811
|
+
event.webhookPayload
|
|
2812
|
+
);
|
|
2813
|
+
summaries.push(`\u53D1\u9001\u901A\u77E5 [${step.notifyChannel || "\u7CFB\u7EDF"}]: ${msg}`);
|
|
2814
|
+
break;
|
|
2815
|
+
}
|
|
2816
|
+
}
|
|
2817
|
+
}
|
|
2818
|
+
};
|
|
2819
|
+
await execStepListAsync(steps, table, record);
|
|
2820
|
+
executedRuleIds.push(rule.id);
|
|
2821
|
+
const runLog = {
|
|
2822
|
+
id: `alog_${(0, import_core.generateRandomId)(6)}`,
|
|
2823
|
+
ruleId: rule.id,
|
|
2824
|
+
ruleName: rule.name,
|
|
2825
|
+
tableId: table.id,
|
|
2826
|
+
recordId: record?.id,
|
|
2827
|
+
trigger: event.trigger,
|
|
2828
|
+
status: errorSummary ? "error" : "success",
|
|
2829
|
+
executedSteps,
|
|
2830
|
+
timestamp,
|
|
2831
|
+
summary: errorSummary ?? (summaries.join(" -> ") || "\u89C4\u5219\u5DF2\u6267\u884C"),
|
|
2832
|
+
...ruleQueuedWebhooks.length > 0 ? { queuedWebhooks: ruleQueuedWebhooks } : {}
|
|
2833
|
+
};
|
|
2834
|
+
logs.push(runLog);
|
|
2835
|
+
pushAutomationLog(workingUnit, runLog, maxLogs);
|
|
2836
|
+
if (!errorSummary && cascades.length > 0) {
|
|
2837
|
+
for (const c of cascades) {
|
|
2838
|
+
queue.push(c.event);
|
|
2839
|
+
}
|
|
2840
|
+
}
|
|
2841
|
+
}
|
|
2842
|
+
}
|
|
2843
|
+
return {
|
|
2844
|
+
unitData: workingUnit,
|
|
2845
|
+
logs,
|
|
2846
|
+
queuedWebhooks: allQueuedWebhooks,
|
|
2847
|
+
executedRuleIds
|
|
2848
|
+
};
|
|
2849
|
+
}
|
|
2850
|
+
|
|
2851
|
+
// src/engine/base-sync-and-acl.ts
|
|
2852
|
+
var import_core2 = require("@univerjs/core");
|
|
2853
|
+
function recordMatchesOwnerAliases(unitData, table, record, ownerFieldId, aliases) {
|
|
2854
|
+
const field = table.fields[ownerFieldId] ?? Object.values(table.fields).find((f) => f.name === ownerFieldId);
|
|
2855
|
+
if (!field) {
|
|
2856
|
+
return Boolean(record.createdBy && aliases.has(record.createdBy.toLowerCase()));
|
|
2857
|
+
}
|
|
2858
|
+
if (field.type === "createdBy") {
|
|
2859
|
+
return Boolean(record.createdBy && aliases.has(record.createdBy.toLowerCase()));
|
|
2860
|
+
}
|
|
2861
|
+
if (field.type === "lastModifiedBy") {
|
|
2862
|
+
const mod = record.lastModifiedBy ?? record.createdBy;
|
|
2863
|
+
return Boolean(mod && aliases.has(mod.toLowerCase()));
|
|
2864
|
+
}
|
|
2865
|
+
const raw = resolveBaseCellValue(unitData, table.id, record, field);
|
|
2866
|
+
if (Array.isArray(raw)) {
|
|
2867
|
+
return raw.some((item) => {
|
|
2868
|
+
if (typeof item === "string") {
|
|
2869
|
+
return aliases.has(item.toLowerCase());
|
|
2870
|
+
}
|
|
2871
|
+
if (item && typeof item === "object") {
|
|
2872
|
+
const id = "id" in item && item.id ? String(item.id).toLowerCase() : "";
|
|
2873
|
+
const name = "name" in item && item.name ? String(item.name).toLowerCase() : "";
|
|
2874
|
+
const email = "email" in item && item.email ? String(item.email).toLowerCase() : "";
|
|
2875
|
+
return id && aliases.has(id) || name && aliases.has(name) || email && aliases.has(email);
|
|
2876
|
+
}
|
|
2877
|
+
return false;
|
|
2878
|
+
});
|
|
2879
|
+
}
|
|
2880
|
+
if (raw !== null && raw !== void 0) {
|
|
2881
|
+
return aliases.has(String(raw).trim().toLowerCase());
|
|
2882
|
+
}
|
|
2883
|
+
return false;
|
|
2884
|
+
}
|
|
2885
|
+
function evaluateRowAccessRuleOnRecord(rule, unitData, table, record, context, userAliases) {
|
|
2886
|
+
if (rule.allowAllRecords) return true;
|
|
2887
|
+
const checks = [];
|
|
2888
|
+
if (rule.ownerFieldId) {
|
|
2889
|
+
checks.push(recordMatchesOwnerAliases(unitData, table, record, rule.ownerFieldId, userAliases));
|
|
2890
|
+
}
|
|
2891
|
+
if (rule.filters && rule.filters.length > 0) {
|
|
2892
|
+
const filterOutcomes = rule.filters.map((fRule) => {
|
|
2893
|
+
const field = table.fields[fRule.fieldId] ?? Object.values(table.fields).find((f) => f.name === fRule.fieldId);
|
|
2894
|
+
if (!field) return true;
|
|
2895
|
+
const rawVal = resolveBaseCellValue(unitData, table.id, record, field);
|
|
2896
|
+
const dispVal = formatBaseCellValue(unitData, table.id, field, rawVal);
|
|
2897
|
+
const valStr = typeof fRule.value === "string" ? fRule.value.trim() : "";
|
|
2898
|
+
if (valStr === "$currentUser" || valStr === "$userId" || valStr === "{userId}" || valStr === "{currentUser}") {
|
|
2899
|
+
if (field.type === "person" || field.type === "createdBy" || field.type === "lastModifiedBy") {
|
|
2900
|
+
const hit = recordMatchesOwnerAliases(unitData, table, record, field.id, userAliases);
|
|
2901
|
+
return fRule.operator === "isNot" || fRule.operator === "notContains" ? !hit : hit;
|
|
2902
|
+
}
|
|
2903
|
+
return matchesBaseFilterRule(
|
|
2904
|
+
{ ...fRule, value: context.userId },
|
|
2905
|
+
field,
|
|
2906
|
+
rawVal,
|
|
2907
|
+
dispVal
|
|
2908
|
+
);
|
|
2909
|
+
}
|
|
2910
|
+
return matchesBaseFilterRule(fRule, field, rawVal, dispVal);
|
|
2911
|
+
});
|
|
2912
|
+
const conj = rule.filterConjunction ?? "and";
|
|
2913
|
+
checks.push(conj === "or" ? filterOutcomes.some(Boolean) : filterOutcomes.every(Boolean));
|
|
2914
|
+
}
|
|
2915
|
+
if (checks.length === 0) {
|
|
2916
|
+
return true;
|
|
2917
|
+
}
|
|
2918
|
+
const mode = rule.filterConjunction ?? "and";
|
|
2919
|
+
return mode === "or" ? checks.some(Boolean) : checks.every(Boolean);
|
|
2920
|
+
}
|
|
2921
|
+
function applyBaseRowReadScope(unitData, context) {
|
|
2922
|
+
const scoped = import_core2.Tools.deepClone(unitData);
|
|
2923
|
+
const roleSet = new Set((context.roles ?? []).map((r) => r.toLowerCase()));
|
|
2924
|
+
const userAliases = /* @__PURE__ */ new Set();
|
|
2925
|
+
if (context.userId) userAliases.add(context.userId.toLowerCase());
|
|
2926
|
+
if (context.userName) userAliases.add(context.userName.toLowerCase());
|
|
2927
|
+
const matchedCollab = (unitData.collaborators ?? []).find(
|
|
2928
|
+
(c) => c.id === context.userId || c.name === context.userId || context.userName && c.name === context.userName
|
|
2929
|
+
);
|
|
2930
|
+
if (matchedCollab) {
|
|
2931
|
+
userAliases.add(matchedCollab.id.toLowerCase());
|
|
2932
|
+
userAliases.add(matchedCollab.name.toLowerCase());
|
|
2933
|
+
if (matchedCollab.email) userAliases.add(matchedCollab.email.toLowerCase());
|
|
2934
|
+
}
|
|
2935
|
+
for (const tableId of scoped.tableOrder) {
|
|
2936
|
+
const sourceTable = unitData.tables[tableId];
|
|
2937
|
+
const targetTable = scoped.tables[tableId];
|
|
2938
|
+
if (!sourceTable || !targetTable) continue;
|
|
2939
|
+
const tableRules = context.rules.filter(
|
|
2940
|
+
(r) => r.tableId === tableId || r.tableId === "*"
|
|
2941
|
+
);
|
|
2942
|
+
if (tableRules.length === 0) continue;
|
|
2943
|
+
const applicableRules = tableRules.filter((r) => {
|
|
2944
|
+
const hasRoleFilter = Boolean(r.roles && r.roles.length > 0);
|
|
2945
|
+
const hasUserFilter = Boolean(r.userIds && r.userIds.length > 0);
|
|
2946
|
+
if (!hasRoleFilter && !hasUserFilter) return true;
|
|
2947
|
+
const roleHit = hasRoleFilter && r.roles.some((role) => roleSet.has(role.toLowerCase()));
|
|
2948
|
+
const userHit = hasUserFilter && r.userIds.some((uid) => userAliases.has(uid.toLowerCase()) || uid === context.userId);
|
|
2949
|
+
return Boolean(roleHit || userHit);
|
|
2950
|
+
});
|
|
2951
|
+
if (applicableRules.length === 0) {
|
|
2952
|
+
targetTable.recordOrder = [];
|
|
2953
|
+
targetTable.records = {};
|
|
2954
|
+
continue;
|
|
2955
|
+
}
|
|
2956
|
+
const nextRecordOrder = [];
|
|
2957
|
+
const nextRecords = {};
|
|
2958
|
+
const fieldsHiddenEverywhere = /* @__PURE__ */ new Set();
|
|
2959
|
+
if (applicableRules.every((r) => r.maskMode === "hide" && (r.restrictedFieldIds?.length ?? 0) > 0)) {
|
|
2960
|
+
const firstRestricted = applicableRules[0].restrictedFieldIds ?? [];
|
|
2961
|
+
for (const fid of firstRestricted) {
|
|
2962
|
+
if (applicableRules.every((r) => r.restrictedFieldIds?.includes(fid))) {
|
|
2963
|
+
fieldsHiddenEverywhere.add(fid);
|
|
2964
|
+
}
|
|
2965
|
+
}
|
|
2966
|
+
}
|
|
2967
|
+
for (const recId of sourceTable.recordOrder) {
|
|
2968
|
+
const srcRec = sourceTable.records[recId];
|
|
2969
|
+
if (!srcRec) continue;
|
|
2970
|
+
const permittingRules = applicableRules.filter(
|
|
2971
|
+
(rule) => evaluateRowAccessRuleOnRecord(rule, unitData, sourceTable, srcRec, context, userAliases)
|
|
2972
|
+
);
|
|
2973
|
+
if (permittingRules.length === 0) continue;
|
|
2974
|
+
const clonedRec = import_core2.Tools.deepClone(srcRec);
|
|
2975
|
+
const candidateRestricted = /* @__PURE__ */ new Set();
|
|
2976
|
+
for (const pRule of permittingRules) {
|
|
2977
|
+
for (const fid of pRule.restrictedFieldIds ?? []) {
|
|
2978
|
+
const resolvedField = targetTable.fields[fid] ?? Object.values(targetTable.fields).find((f) => f.name === fid);
|
|
2979
|
+
if (resolvedField) candidateRestricted.add(resolvedField.id);
|
|
2980
|
+
}
|
|
2981
|
+
}
|
|
2982
|
+
for (const fid of candidateRestricted) {
|
|
2983
|
+
const restrictedByAll = permittingRules.every(
|
|
2984
|
+
(pRule) => (pRule.restrictedFieldIds ?? []).some(
|
|
2985
|
+
(rf) => rf === fid || targetTable.fields[fid]?.name === rf
|
|
2986
|
+
)
|
|
2987
|
+
);
|
|
2988
|
+
if (!restrictedByAll) continue;
|
|
2989
|
+
const governingRule = permittingRules[0];
|
|
2990
|
+
if (governingRule.maskMode === "hide") {
|
|
2991
|
+
delete clonedRec.cells[fid];
|
|
2992
|
+
fieldsHiddenEverywhere.add(fid);
|
|
2993
|
+
} else {
|
|
2994
|
+
const maskVal = governingRule.maskValue !== void 0 ? import_core2.Tools.deepClone(governingRule.maskValue) : "***";
|
|
2995
|
+
clonedRec.cells[fid] = maskVal;
|
|
2996
|
+
const fld = targetTable.fields[fid];
|
|
2997
|
+
if (fld && isComputedBaseField(fld.type)) {
|
|
2998
|
+
targetTable.fields[fid] = {
|
|
2999
|
+
...fld,
|
|
3000
|
+
type: "text",
|
|
3001
|
+
config: void 0
|
|
3002
|
+
};
|
|
3003
|
+
}
|
|
3004
|
+
}
|
|
3005
|
+
}
|
|
3006
|
+
nextRecordOrder.push(recId);
|
|
3007
|
+
nextRecords[recId] = clonedRec;
|
|
3008
|
+
}
|
|
3009
|
+
for (const hiddenFid of fieldsHiddenEverywhere) {
|
|
3010
|
+
if (targetTable.fields[hiddenFid] && !targetTable.fields[hiddenFid].isPrimary) {
|
|
3011
|
+
targetTable.fields[hiddenFid].hidden = true;
|
|
3012
|
+
}
|
|
3013
|
+
}
|
|
3014
|
+
targetTable.recordOrder = nextRecordOrder;
|
|
3015
|
+
targetTable.records = nextRecords;
|
|
3016
|
+
}
|
|
3017
|
+
for (const tbl of Object.values(scoped.tables)) {
|
|
3018
|
+
const linkFields = Object.values(tbl.fields).filter(
|
|
3019
|
+
(f) => f.type === "link" && f.config?.linkedTableId && scoped.tables[f.config.linkedTableId]
|
|
3020
|
+
);
|
|
3021
|
+
if (linkFields.length === 0) continue;
|
|
3022
|
+
for (const rec of Object.values(tbl.records)) {
|
|
3023
|
+
for (const lf of linkFields) {
|
|
3024
|
+
const targetTbl = scoped.tables[lf.config.linkedTableId];
|
|
3025
|
+
const curIds = normalizeLinkIds(rec.cells[lf.id]);
|
|
3026
|
+
const validIds = curIds.filter((id) => Boolean(targetTbl.records[id]));
|
|
3027
|
+
if (validIds.length !== curIds.length) {
|
|
3028
|
+
rec.cells[lf.id] = validIds;
|
|
3029
|
+
}
|
|
3030
|
+
}
|
|
3031
|
+
}
|
|
3032
|
+
}
|
|
3033
|
+
return scoped;
|
|
3034
|
+
}
|
|
3035
|
+
function nowTimestamp2() {
|
|
3036
|
+
return (/* @__PURE__ */ new Date()).toISOString().replace("T", " ").slice(0, 19);
|
|
3037
|
+
}
|
|
3038
|
+
function resolveField(table, idOrName) {
|
|
3039
|
+
if (!idOrName) return void 0;
|
|
3040
|
+
return table.fields[idOrName] ?? Object.values(table.fields).find((f) => f.name === idOrName);
|
|
3041
|
+
}
|
|
3042
|
+
function buildFieldMappingPairs(sourceTable, targetTable, explicitMap) {
|
|
3043
|
+
const pairs = [];
|
|
3044
|
+
if (explicitMap && Object.keys(explicitMap).length > 0) {
|
|
3045
|
+
for (const [srcKey, tgtKey] of Object.entries(explicitMap)) {
|
|
3046
|
+
const sField = resolveField(sourceTable, srcKey);
|
|
3047
|
+
const tField = resolveField(targetTable, tgtKey);
|
|
3048
|
+
if (sField && tField && !isComputedBaseField(tField.type)) {
|
|
3049
|
+
pairs.push({ sourceField: sField, targetField: tField });
|
|
3050
|
+
}
|
|
3051
|
+
}
|
|
3052
|
+
return pairs;
|
|
3053
|
+
}
|
|
3054
|
+
for (const sFieldId of sourceTable.fieldOrder) {
|
|
3055
|
+
const sField = sourceTable.fields[sFieldId];
|
|
3056
|
+
if (!sField) continue;
|
|
3057
|
+
const tField = Object.values(targetTable.fields).find((tf) => tf.name === sField.name) ?? targetTable.fields[sField.id];
|
|
3058
|
+
if (tField && !isComputedBaseField(tField.type) && tField.type !== "link") {
|
|
3059
|
+
pairs.push({ sourceField: sField, targetField: tField });
|
|
3060
|
+
}
|
|
3061
|
+
}
|
|
3062
|
+
return pairs;
|
|
3063
|
+
}
|
|
3064
|
+
function extractRecordKey(unit, table, record, keyField) {
|
|
3065
|
+
const raw = resolveBaseCellValue(unit, table.id, record, keyField);
|
|
3066
|
+
if (isCellValueEmpty(raw)) return "";
|
|
3067
|
+
if (typeof raw === "string" || typeof raw === "number") {
|
|
3068
|
+
return String(raw).trim();
|
|
3069
|
+
}
|
|
3070
|
+
return formatBaseCellValue(unit, table.id, keyField, raw).trim();
|
|
3071
|
+
}
|
|
3072
|
+
function cellsDeepEqual(a, b) {
|
|
3073
|
+
if (a === b) return true;
|
|
3074
|
+
if ((a === null || a === void 0 || a === "") && (b === null || b === void 0 || b === "")) {
|
|
3075
|
+
return true;
|
|
3076
|
+
}
|
|
3077
|
+
return JSON.stringify(a) === JSON.stringify(b);
|
|
3078
|
+
}
|
|
3079
|
+
function compareModifiedTimestamps(tsA, tsB) {
|
|
3080
|
+
const msA = Date.parse(tsA.replace(" ", "T"));
|
|
3081
|
+
const msB = Date.parse(tsB.replace(" ", "T"));
|
|
3082
|
+
if (!Number.isNaN(msA) && !Number.isNaN(msB) && msA !== msB) {
|
|
3083
|
+
return msA - msB;
|
|
3084
|
+
}
|
|
3085
|
+
return tsA.localeCompare(tsB);
|
|
3086
|
+
}
|
|
3087
|
+
function passesSourceFilters(filters, unit, table, record) {
|
|
3088
|
+
if (!filters || filters.length === 0) return true;
|
|
3089
|
+
return filters.every((rule) => {
|
|
3090
|
+
const f = resolveField(table, rule.fieldId);
|
|
3091
|
+
if (!f) return true;
|
|
3092
|
+
const raw = resolveBaseCellValue(unit, table.id, record, f);
|
|
3093
|
+
const disp = formatBaseCellValue(unit, table.id, f, raw);
|
|
3094
|
+
return matchesBaseFilterRule(rule, f, raw, disp);
|
|
3095
|
+
});
|
|
3096
|
+
}
|
|
3097
|
+
function syncOneWayBaseTable(sourceUnit, sourceTableId, targetUnit, targetTableId, mapping) {
|
|
3098
|
+
const sourceTable = sourceUnit.tables[sourceTableId];
|
|
3099
|
+
if (!sourceTable) {
|
|
3100
|
+
throw new Error(`[UniverBases] Source table "${sourceTableId}" does not exist.`);
|
|
3101
|
+
}
|
|
3102
|
+
const clonedTargetUnit = import_core2.Tools.deepClone(targetUnit);
|
|
3103
|
+
const targetTable = clonedTargetUnit.tables[targetTableId];
|
|
3104
|
+
if (!targetTable) {
|
|
3105
|
+
throw new Error(`[UniverBases] Target table "${targetTableId}" does not exist.`);
|
|
3106
|
+
}
|
|
3107
|
+
const srcKeyField = resolveField(sourceTable, mapping?.sourceKeyField) ?? sourceTable.fields[sourceTable.primaryFieldId] ?? Object.values(sourceTable.fields)[0];
|
|
3108
|
+
const tgtKeyField = resolveField(targetTable, mapping?.targetKeyField) ?? resolveField(targetTable, srcKeyField?.name) ?? targetTable.fields[targetTable.primaryFieldId] ?? Object.values(targetTable.fields)[0];
|
|
3109
|
+
if (!srcKeyField || !tgtKeyField) {
|
|
3110
|
+
throw new Error("[UniverBases] Unable to resolve key fields for one-way table sync.");
|
|
3111
|
+
}
|
|
3112
|
+
const pairs = buildFieldMappingPairs(sourceTable, targetTable, mapping?.fieldMap);
|
|
3113
|
+
if (!pairs.some((p) => p.targetField.id === tgtKeyField.id) && !isComputedBaseField(tgtKeyField.type)) {
|
|
3114
|
+
pairs.unshift({ sourceField: srcKeyField, targetField: tgtKeyField });
|
|
3115
|
+
}
|
|
3116
|
+
const targetByKey = /* @__PURE__ */ new Map();
|
|
3117
|
+
for (const rid of targetTable.recordOrder) {
|
|
3118
|
+
const rec = targetTable.records[rid];
|
|
3119
|
+
if (!rec) continue;
|
|
3120
|
+
const k = extractRecordKey(clonedTargetUnit, targetTable, rec, tgtKeyField);
|
|
3121
|
+
if (k && !targetByKey.has(k)) {
|
|
3122
|
+
targetByKey.set(k, rec);
|
|
3123
|
+
}
|
|
3124
|
+
}
|
|
3125
|
+
const syncTs = mapping?.syncTimestamp ?? nowTimestamp2();
|
|
3126
|
+
let insertedCount = 0;
|
|
3127
|
+
let updatedCount = 0;
|
|
3128
|
+
let unchangedCount = 0;
|
|
3129
|
+
let nextAuto = targetTable.nextAutoNumber ?? targetTable.recordOrder.length + 1;
|
|
3130
|
+
for (const sRecId of sourceTable.recordOrder) {
|
|
3131
|
+
const sRec = sourceTable.records[sRecId];
|
|
3132
|
+
if (!sRec) continue;
|
|
3133
|
+
if (!passesSourceFilters(mapping?.sourceFilters, sourceUnit, sourceTable, sRec)) continue;
|
|
3134
|
+
const key = extractRecordKey(sourceUnit, sourceTable, sRec, srcKeyField);
|
|
3135
|
+
if (!key) continue;
|
|
3136
|
+
const mappedCells = {};
|
|
3137
|
+
for (const { sourceField, targetField } of pairs) {
|
|
3138
|
+
const val = resolveBaseCellValue(sourceUnit, sourceTable.id, sRec, sourceField);
|
|
3139
|
+
mappedCells[targetField.id] = import_core2.Tools.deepClone(val);
|
|
3140
|
+
}
|
|
3141
|
+
if (!isComputedBaseField(tgtKeyField.type) && mappedCells[tgtKeyField.id] === void 0) {
|
|
3142
|
+
mappedCells[tgtKeyField.id] = key;
|
|
3143
|
+
}
|
|
3144
|
+
const existingTarget = targetByKey.get(key);
|
|
3145
|
+
if (!existingTarget) {
|
|
3146
|
+
const newId = `rec_sync_${(0, import_core2.generateRandomId)(6)}`;
|
|
3147
|
+
const newRec = {
|
|
3148
|
+
id: newId,
|
|
3149
|
+
autoNumber: nextAuto++,
|
|
3150
|
+
createdTime: sRec.createdTime || syncTs,
|
|
3151
|
+
lastModifiedTime: sRec.lastModifiedTime || syncTs,
|
|
3152
|
+
createdBy: sRec.createdBy ?? "\u8DE8\u5E93\u540C\u6B65",
|
|
3153
|
+
lastModifiedBy: "\u8DE8\u5E93\u540C\u6B65",
|
|
3154
|
+
cells: mappedCells
|
|
3155
|
+
};
|
|
3156
|
+
targetTable.records[newId] = newRec;
|
|
3157
|
+
targetTable.recordOrder.push(newId);
|
|
3158
|
+
targetByKey.set(key, newRec);
|
|
3159
|
+
insertedCount += 1;
|
|
3160
|
+
} else {
|
|
3161
|
+
let changed = false;
|
|
3162
|
+
for (const [fid, nextVal] of Object.entries(mappedCells)) {
|
|
3163
|
+
if (!cellsDeepEqual(existingTarget.cells[fid], nextVal)) {
|
|
3164
|
+
existingTarget.cells[fid] = nextVal;
|
|
3165
|
+
changed = true;
|
|
3166
|
+
}
|
|
3167
|
+
}
|
|
3168
|
+
if (changed) {
|
|
3169
|
+
existingTarget.lastModifiedTime = syncTs;
|
|
3170
|
+
existingTarget.lastModifiedBy = "\u8DE8\u5E93\u540C\u6B65";
|
|
3171
|
+
updatedCount += 1;
|
|
3172
|
+
} else {
|
|
3173
|
+
unchangedCount += 1;
|
|
3174
|
+
}
|
|
3175
|
+
}
|
|
3176
|
+
}
|
|
3177
|
+
targetTable.nextAutoNumber = nextAuto;
|
|
3178
|
+
clonedTargetUnit.rev = (clonedTargetUnit.rev ?? 1) + 1;
|
|
3179
|
+
return {
|
|
3180
|
+
targetUnit: clonedTargetUnit,
|
|
3181
|
+
insertedCount,
|
|
3182
|
+
updatedCount,
|
|
3183
|
+
unchangedCount,
|
|
3184
|
+
totalTargetRecords: targetTable.recordOrder.length
|
|
3185
|
+
};
|
|
3186
|
+
}
|
|
3187
|
+
function syncTwoWayBaseTables(unitA, tableIdA, unitB, tableIdB, mapping) {
|
|
3188
|
+
const clonedA = import_core2.Tools.deepClone(unitA);
|
|
3189
|
+
const clonedB = import_core2.Tools.deepClone(unitB);
|
|
3190
|
+
const tableA = clonedA.tables[tableIdA];
|
|
3191
|
+
const tableB = clonedB.tables[tableIdB];
|
|
3192
|
+
if (!tableA || !tableB) {
|
|
3193
|
+
throw new Error("[UniverBases] Both tableA and tableB must exist for two-way sync.");
|
|
3194
|
+
}
|
|
3195
|
+
const keyFieldA = resolveField(tableA, mapping?.sourceKeyField) ?? tableA.fields[tableA.primaryFieldId] ?? Object.values(tableA.fields)[0];
|
|
3196
|
+
const keyFieldB = resolveField(tableB, mapping?.targetKeyField) ?? resolveField(tableB, keyFieldA?.name) ?? tableB.fields[tableB.primaryFieldId] ?? Object.values(tableB.fields)[0];
|
|
3197
|
+
if (!keyFieldA || !keyFieldB) {
|
|
3198
|
+
throw new Error("[UniverBases] Unable to resolve key fields for two-way table sync.");
|
|
3199
|
+
}
|
|
3200
|
+
const pairsAtoB = buildFieldMappingPairs(tableA, tableB, mapping?.fieldMap);
|
|
3201
|
+
if (!pairsAtoB.some((p) => p.targetField.id === keyFieldB.id) && !isComputedBaseField(keyFieldB.type)) {
|
|
3202
|
+
pairsAtoB.unshift({ sourceField: keyFieldA, targetField: keyFieldB });
|
|
3203
|
+
}
|
|
3204
|
+
const reverseFieldMap = mapping?.fieldMap ? Object.fromEntries(Object.entries(mapping.fieldMap).map(([k, v]) => [v, k])) : void 0;
|
|
3205
|
+
const pairsBtoA = buildFieldMappingPairs(tableB, tableA, reverseFieldMap);
|
|
3206
|
+
if (!pairsBtoA.some((p) => p.targetField.id === keyFieldA.id) && !isComputedBaseField(keyFieldA.type)) {
|
|
3207
|
+
pairsBtoA.unshift({ sourceField: keyFieldB, targetField: keyFieldA });
|
|
3208
|
+
}
|
|
3209
|
+
const mapA = /* @__PURE__ */ new Map();
|
|
3210
|
+
const orderedKeys = [];
|
|
3211
|
+
for (const rid of tableA.recordOrder) {
|
|
3212
|
+
const rec = tableA.records[rid];
|
|
3213
|
+
if (!rec) continue;
|
|
3214
|
+
const k = extractRecordKey(clonedA, tableA, rec, keyFieldA);
|
|
3215
|
+
if (k && !mapA.has(k)) {
|
|
3216
|
+
mapA.set(k, rec);
|
|
3217
|
+
orderedKeys.push(k);
|
|
3218
|
+
}
|
|
3219
|
+
}
|
|
3220
|
+
const mapB = /* @__PURE__ */ new Map();
|
|
3221
|
+
for (const rid of tableB.recordOrder) {
|
|
3222
|
+
const rec = tableB.records[rid];
|
|
3223
|
+
if (!rec) continue;
|
|
3224
|
+
const k = extractRecordKey(clonedB, tableB, rec, keyFieldB);
|
|
3225
|
+
if (k && !mapB.has(k)) {
|
|
3226
|
+
mapB.set(k, rec);
|
|
3227
|
+
if (!mapA.has(k)) orderedKeys.push(k);
|
|
3228
|
+
}
|
|
3229
|
+
}
|
|
3230
|
+
const strategy = mapping?.conflictStrategy ?? "latestWins";
|
|
3231
|
+
const syncTs = mapping?.syncTimestamp ?? nowTimestamp2();
|
|
3232
|
+
let insertedInA = 0;
|
|
3233
|
+
let updatedInA = 0;
|
|
3234
|
+
let insertedInB = 0;
|
|
3235
|
+
let updatedInB = 0;
|
|
3236
|
+
let conflictsResolved = 0;
|
|
3237
|
+
let nextAutoA = tableA.nextAutoNumber ?? tableA.recordOrder.length + 1;
|
|
3238
|
+
let nextAutoB = tableB.nextAutoNumber ?? tableB.recordOrder.length + 1;
|
|
3239
|
+
for (const key of orderedKeys) {
|
|
3240
|
+
const recA = mapA.get(key);
|
|
3241
|
+
const recB = mapB.get(key);
|
|
3242
|
+
if (recA && !recB) {
|
|
3243
|
+
const cellsB = {};
|
|
3244
|
+
for (const { sourceField, targetField } of pairsAtoB) {
|
|
3245
|
+
cellsB[targetField.id] = import_core2.Tools.deepClone(
|
|
3246
|
+
resolveBaseCellValue(clonedA, tableA.id, recA, sourceField)
|
|
3247
|
+
);
|
|
3248
|
+
}
|
|
3249
|
+
const newIdB = `rec_sync_${(0, import_core2.generateRandomId)(6)}`;
|
|
3250
|
+
tableB.records[newIdB] = {
|
|
3251
|
+
id: newIdB,
|
|
3252
|
+
autoNumber: nextAutoB++,
|
|
3253
|
+
createdTime: recA.createdTime || syncTs,
|
|
3254
|
+
lastModifiedTime: recA.lastModifiedTime || syncTs,
|
|
3255
|
+
createdBy: recA.createdBy ?? "\u53CC\u5411\u540C\u6B65",
|
|
3256
|
+
lastModifiedBy: "\u53CC\u5411\u540C\u6B65",
|
|
3257
|
+
cells: cellsB
|
|
3258
|
+
};
|
|
3259
|
+
tableB.recordOrder.push(newIdB);
|
|
3260
|
+
insertedInB += 1;
|
|
3261
|
+
continue;
|
|
3262
|
+
}
|
|
3263
|
+
if (!recA && recB) {
|
|
3264
|
+
const cellsA = {};
|
|
3265
|
+
for (const { sourceField, targetField } of pairsBtoA) {
|
|
3266
|
+
cellsA[targetField.id] = import_core2.Tools.deepClone(
|
|
3267
|
+
resolveBaseCellValue(clonedB, tableB.id, recB, sourceField)
|
|
3268
|
+
);
|
|
3269
|
+
}
|
|
3270
|
+
const newIdA = `rec_sync_${(0, import_core2.generateRandomId)(6)}`;
|
|
3271
|
+
tableA.records[newIdA] = {
|
|
3272
|
+
id: newIdA,
|
|
3273
|
+
autoNumber: nextAutoA++,
|
|
3274
|
+
createdTime: recB.createdTime || syncTs,
|
|
3275
|
+
lastModifiedTime: recB.lastModifiedTime || syncTs,
|
|
3276
|
+
createdBy: recB.createdBy ?? "\u53CC\u5411\u540C\u6B65",
|
|
3277
|
+
lastModifiedBy: "\u53CC\u5411\u540C\u6B65",
|
|
3278
|
+
cells: cellsA
|
|
3279
|
+
};
|
|
3280
|
+
tableA.recordOrder.push(newIdA);
|
|
3281
|
+
insertedInA += 1;
|
|
3282
|
+
continue;
|
|
3283
|
+
}
|
|
3284
|
+
if (recA && recB) {
|
|
3285
|
+
let hasDiff = false;
|
|
3286
|
+
for (const { sourceField, targetField } of pairsAtoB) {
|
|
3287
|
+
const valA = resolveBaseCellValue(clonedA, tableA.id, recA, sourceField);
|
|
3288
|
+
const valB = resolveBaseCellValue(clonedB, tableB.id, recB, targetField);
|
|
3289
|
+
if (!cellsDeepEqual(valA, valB)) {
|
|
3290
|
+
hasDiff = true;
|
|
3291
|
+
break;
|
|
3292
|
+
}
|
|
3293
|
+
}
|
|
3294
|
+
if (!hasDiff) continue;
|
|
3295
|
+
conflictsResolved += 1;
|
|
3296
|
+
let winner;
|
|
3297
|
+
if (strategy === "sourceWins") {
|
|
3298
|
+
winner = "A";
|
|
3299
|
+
} else if (strategy === "targetWins") {
|
|
3300
|
+
winner = "B";
|
|
3301
|
+
} else {
|
|
3302
|
+
winner = compareModifiedTimestamps(recA.lastModifiedTime, recB.lastModifiedTime) >= 0 ? "A" : "B";
|
|
3303
|
+
}
|
|
3304
|
+
if (winner === "A") {
|
|
3305
|
+
for (const { sourceField, targetField } of pairsAtoB) {
|
|
3306
|
+
if (isComputedBaseField(targetField.type)) continue;
|
|
3307
|
+
recB.cells[targetField.id] = import_core2.Tools.deepClone(
|
|
3308
|
+
resolveBaseCellValue(clonedA, tableA.id, recA, sourceField)
|
|
3309
|
+
);
|
|
3310
|
+
}
|
|
3311
|
+
recB.lastModifiedTime = recA.lastModifiedTime || syncTs;
|
|
3312
|
+
recB.lastModifiedBy = "\u53CC\u5411\u540C\u6B65";
|
|
3313
|
+
updatedInB += 1;
|
|
3314
|
+
} else {
|
|
3315
|
+
for (const { sourceField, targetField } of pairsBtoA) {
|
|
3316
|
+
if (isComputedBaseField(targetField.type)) continue;
|
|
3317
|
+
recA.cells[targetField.id] = import_core2.Tools.deepClone(
|
|
3318
|
+
resolveBaseCellValue(clonedB, tableB.id, recB, sourceField)
|
|
3319
|
+
);
|
|
3320
|
+
}
|
|
3321
|
+
recA.lastModifiedTime = recB.lastModifiedTime || syncTs;
|
|
3322
|
+
recA.lastModifiedBy = "\u53CC\u5411\u540C\u6B65";
|
|
3323
|
+
updatedInA += 1;
|
|
3324
|
+
}
|
|
3325
|
+
}
|
|
3326
|
+
}
|
|
3327
|
+
tableA.nextAutoNumber = nextAutoA;
|
|
3328
|
+
tableB.nextAutoNumber = nextAutoB;
|
|
3329
|
+
clonedA.rev = (clonedA.rev ?? 1) + 1;
|
|
3330
|
+
clonedB.rev = (clonedB.rev ?? 1) + 1;
|
|
3331
|
+
return {
|
|
3332
|
+
unitA: clonedA,
|
|
3333
|
+
unitB: clonedB,
|
|
3334
|
+
insertedInA,
|
|
3335
|
+
updatedInA,
|
|
3336
|
+
insertedInB,
|
|
3337
|
+
updatedInB,
|
|
3338
|
+
conflictsResolved
|
|
3339
|
+
};
|
|
3340
|
+
}
|
|
3341
|
+
function mergeMultipleSourcesIntoBaseTable(sources, targetUnit, targetTableId, keyFieldName, options) {
|
|
3342
|
+
const clonedTargetUnit = import_core2.Tools.deepClone(targetUnit);
|
|
3343
|
+
const targetTable = clonedTargetUnit.tables[targetTableId];
|
|
3344
|
+
if (!targetTable) {
|
|
3345
|
+
throw new Error(`[UniverBases] Target table "${targetTableId}" does not exist.`);
|
|
3346
|
+
}
|
|
3347
|
+
const tgtKeyField = resolveField(targetTable, keyFieldName) ?? targetTable.fields[targetTable.primaryFieldId] ?? Object.values(targetTable.fields)[0];
|
|
3348
|
+
if (!tgtKeyField) {
|
|
3349
|
+
throw new Error(`[UniverBases] Target key field "${keyFieldName}" not found.`);
|
|
3350
|
+
}
|
|
3351
|
+
const hasAnySourceTag = sources.some((s) => Boolean(s.sourceTag));
|
|
3352
|
+
const tagFieldName = options?.sourceTagFieldName || "\u6570\u636E\u6765\u6E90";
|
|
3353
|
+
let sourceTagField = resolveField(targetTable, tagFieldName) ?? resolveField(targetTable, "\u6765\u6E90");
|
|
3354
|
+
if (hasAnySourceTag && !sourceTagField) {
|
|
3355
|
+
const newTagFieldId = "fld_sync_source_tag";
|
|
3356
|
+
sourceTagField = {
|
|
3357
|
+
id: newTagFieldId,
|
|
3358
|
+
name: tagFieldName,
|
|
3359
|
+
type: "text",
|
|
3360
|
+
width: 140
|
|
3361
|
+
};
|
|
3362
|
+
targetTable.fields[newTagFieldId] = sourceTagField;
|
|
3363
|
+
targetTable.fieldOrder.push(newTagFieldId);
|
|
3364
|
+
}
|
|
3365
|
+
const targetByKey = /* @__PURE__ */ new Map();
|
|
3366
|
+
for (const rid of targetTable.recordOrder) {
|
|
3367
|
+
const rec = targetTable.records[rid];
|
|
3368
|
+
if (!rec) continue;
|
|
3369
|
+
const k = extractRecordKey(clonedTargetUnit, targetTable, rec, tgtKeyField);
|
|
3370
|
+
if (k && !targetByKey.has(k)) {
|
|
3371
|
+
targetByKey.set(k, { record: rec, wasInsertedInMerge: false });
|
|
3372
|
+
}
|
|
3373
|
+
}
|
|
3374
|
+
const strategy = options?.conflictStrategy ?? "latestWins";
|
|
3375
|
+
const syncTs = options?.syncTimestamp ?? nowTimestamp2();
|
|
3376
|
+
let insertedCount = 0;
|
|
3377
|
+
let updatedCount = 0;
|
|
3378
|
+
let unchangedCount = 0;
|
|
3379
|
+
let deduplicatedCount = 0;
|
|
3380
|
+
let nextAuto = targetTable.nextAutoNumber ?? targetTable.recordOrder.length + 1;
|
|
3381
|
+
const seenInSources = /* @__PURE__ */ new Set();
|
|
3382
|
+
for (const srcItem of sources) {
|
|
3383
|
+
const srcTable = srcItem.unit.tables[srcItem.tableId];
|
|
3384
|
+
if (!srcTable) continue;
|
|
3385
|
+
const srcKeyField = resolveField(srcTable, keyFieldName) ?? srcTable.fields[srcTable.primaryFieldId] ?? Object.values(srcTable.fields)[0];
|
|
3386
|
+
if (!srcKeyField) continue;
|
|
3387
|
+
const pairs = buildFieldMappingPairs(srcTable, targetTable);
|
|
3388
|
+
if (!pairs.some((p) => p.targetField.id === tgtKeyField.id) && !isComputedBaseField(tgtKeyField.type)) {
|
|
3389
|
+
pairs.unshift({ sourceField: srcKeyField, targetField: tgtKeyField });
|
|
3390
|
+
}
|
|
3391
|
+
for (const sRecId of srcTable.recordOrder) {
|
|
3392
|
+
const sRec = srcTable.records[sRecId];
|
|
3393
|
+
if (!sRec) continue;
|
|
3394
|
+
const key = extractRecordKey(srcItem.unit, srcTable, sRec, srcKeyField);
|
|
3395
|
+
if (!key) continue;
|
|
3396
|
+
if (seenInSources.has(key)) {
|
|
3397
|
+
deduplicatedCount += 1;
|
|
3398
|
+
} else {
|
|
3399
|
+
seenInSources.add(key);
|
|
3400
|
+
}
|
|
3401
|
+
const mappedCells = {};
|
|
3402
|
+
for (const { sourceField, targetField } of pairs) {
|
|
3403
|
+
mappedCells[targetField.id] = import_core2.Tools.deepClone(
|
|
3404
|
+
resolveBaseCellValue(srcItem.unit, srcTable.id, sRec, sourceField)
|
|
3405
|
+
);
|
|
3406
|
+
}
|
|
3407
|
+
if (srcItem.sourceTag && sourceTagField) {
|
|
3408
|
+
mappedCells[sourceTagField.id] = srcItem.sourceTag;
|
|
3409
|
+
}
|
|
3410
|
+
const existing = targetByKey.get(key);
|
|
3411
|
+
if (!existing) {
|
|
3412
|
+
const newId = `rec_merge_${(0, import_core2.generateRandomId)(6)}`;
|
|
3413
|
+
const newRec = {
|
|
3414
|
+
id: newId,
|
|
3415
|
+
autoNumber: nextAuto++,
|
|
3416
|
+
createdTime: sRec.createdTime || syncTs,
|
|
3417
|
+
lastModifiedTime: sRec.lastModifiedTime || syncTs,
|
|
3418
|
+
createdBy: srcItem.sourceTag ?? sRec.createdBy ?? "\u591A\u6E90\u5F52\u96C6",
|
|
3419
|
+
lastModifiedBy: srcItem.sourceTag ?? "\u591A\u6E90\u5F52\u96C6",
|
|
3420
|
+
cells: mappedCells
|
|
3421
|
+
};
|
|
3422
|
+
targetTable.records[newId] = newRec;
|
|
3423
|
+
targetTable.recordOrder.push(newId);
|
|
3424
|
+
targetByKey.set(key, { record: newRec, wasInsertedInMerge: true });
|
|
3425
|
+
insertedCount += 1;
|
|
3426
|
+
} else {
|
|
3427
|
+
const tgtRec = existing.record;
|
|
3428
|
+
const shouldOverwrite = strategy === "lastWins" ? true : strategy === "firstWins" ? false : compareModifiedTimestamps(sRec.lastModifiedTime, tgtRec.lastModifiedTime) >= 0;
|
|
3429
|
+
if (!shouldOverwrite) {
|
|
3430
|
+
unchangedCount += 1;
|
|
3431
|
+
continue;
|
|
3432
|
+
}
|
|
3433
|
+
let changed = false;
|
|
3434
|
+
for (const [fid, val] of Object.entries(mappedCells)) {
|
|
3435
|
+
if (!cellsDeepEqual(tgtRec.cells[fid], val)) {
|
|
3436
|
+
tgtRec.cells[fid] = val;
|
|
3437
|
+
changed = true;
|
|
3438
|
+
}
|
|
3439
|
+
}
|
|
3440
|
+
if (changed) {
|
|
3441
|
+
tgtRec.lastModifiedTime = sRec.lastModifiedTime || syncTs;
|
|
3442
|
+
if (!existing.wasInsertedInMerge) {
|
|
3443
|
+
updatedCount += 1;
|
|
3444
|
+
}
|
|
3445
|
+
} else {
|
|
3446
|
+
unchangedCount += 1;
|
|
3447
|
+
}
|
|
3448
|
+
}
|
|
3449
|
+
}
|
|
3450
|
+
}
|
|
3451
|
+
targetTable.nextAutoNumber = nextAuto;
|
|
3452
|
+
clonedTargetUnit.rev = (clonedTargetUnit.rev ?? 1) + 1;
|
|
3453
|
+
return {
|
|
3454
|
+
targetUnit: clonedTargetUnit,
|
|
3455
|
+
insertedCount,
|
|
3456
|
+
updatedCount,
|
|
3457
|
+
unchangedCount,
|
|
3458
|
+
deduplicatedCount,
|
|
3459
|
+
totalTargetRecords: targetTable.recordOrder.length
|
|
3460
|
+
};
|
|
3461
|
+
}
|
|
3462
|
+
var syncBaseTables = {
|
|
3463
|
+
oneWay: syncOneWayBaseTable,
|
|
3464
|
+
twoWay: syncTwoWayBaseTables,
|
|
3465
|
+
mergeMultipleSources: mergeMultipleSourcesIntoBaseTable
|
|
3466
|
+
};
|
|
1580
3467
|
|
|
1581
3468
|
// src/presets/base-presets.ts
|
|
1582
|
-
var
|
|
3469
|
+
var import_core3 = require("@univerjs/core");
|
|
1583
3470
|
function createSampleProjectBaseData(options) {
|
|
1584
|
-
const unitId = options?.id || `base-${(0,
|
|
3471
|
+
const unitId = options?.id || `base-${(0, import_core3.generateRandomId)(6)}`;
|
|
1585
3472
|
const unitName = options?.name || "\u4F01\u4E1A\u7814\u53D1\u9879\u76EE\u4E0E\u51B2\u523A\u534F\u540C\u5E93";
|
|
1586
3473
|
const projectsTable = {
|
|
1587
3474
|
id: "tbl_projects",
|
|
@@ -2279,6 +4166,7 @@ function createSampleProjectBaseData(options) {
|
|
|
2279
4166
|
},
|
|
2280
4167
|
viewOrder: [
|
|
2281
4168
|
"vw_task_grid",
|
|
4169
|
+
"vw_task_list",
|
|
2282
4170
|
"vw_task_kanban",
|
|
2283
4171
|
"vw_task_calendar",
|
|
2284
4172
|
"vw_task_gallery",
|
|
@@ -2308,6 +4196,12 @@ function createSampleProjectBaseData(options) {
|
|
|
2308
4196
|
fld_task_progress: "avg"
|
|
2309
4197
|
}
|
|
2310
4198
|
},
|
|
4199
|
+
vw_task_list: {
|
|
4200
|
+
id: "vw_task_list",
|
|
4201
|
+
name: "\u5217\u8868\u89C6\u56FE",
|
|
4202
|
+
type: "list",
|
|
4203
|
+
listParentFieldId: "fld_task_deps"
|
|
4204
|
+
},
|
|
2311
4205
|
vw_task_kanban: {
|
|
2312
4206
|
id: "vw_task_kanban",
|
|
2313
4207
|
name: "\u770B\u677F\u89C6\u56FE",
|
|
@@ -2489,7 +4383,7 @@ function createSampleProjectBaseData(options) {
|
|
|
2489
4383
|
};
|
|
2490
4384
|
}
|
|
2491
4385
|
function createSampleCrmBaseData(options) {
|
|
2492
|
-
const unitId = options?.id || `base-crm-${(0,
|
|
4386
|
+
const unitId = options?.id || `base-crm-${(0, import_core3.generateRandomId)(6)}`;
|
|
2493
4387
|
const unitName = options?.name || "\u653F\u4F01\u5BA2\u6237\u4E0E\u5546\u673A\u56DE\u6B3E\u7BA1\u7406";
|
|
2494
4388
|
const accountsTable = {
|
|
2495
4389
|
id: "tbl_accounts",
|
|
@@ -2862,12 +4756,14 @@ function createDefaultBaseUnitData(partial) {
|
|
|
2862
4756
|
...partial?.rev !== void 0 ? { rev: partial.rev } : {},
|
|
2863
4757
|
...partial?.activeTableId && sample.tables[partial.activeTableId] ? { activeTableId: partial.activeTableId } : {},
|
|
2864
4758
|
...partial?.workspaceMode ? { workspaceMode: partial.workspaceMode } : {},
|
|
2865
|
-
...partial?.dashboards ? { dashboards:
|
|
2866
|
-
...partial?.automations ? { automations:
|
|
2867
|
-
...partial?.
|
|
4759
|
+
...partial?.dashboards ? { dashboards: import_core3.Tools.deepClone(partial.dashboards) } : {},
|
|
4760
|
+
...partial?.automations ? { automations: import_core3.Tools.deepClone(partial.automations) } : {},
|
|
4761
|
+
...partial?.automationLogs ? { automationLogs: import_core3.Tools.deepClone(partial.automationLogs) } : {},
|
|
4762
|
+
...partial?.queuedWebhooks ? { queuedWebhooks: import_core3.Tools.deepClone(partial.queuedWebhooks) } : {},
|
|
4763
|
+
...partial?.metadata ? { metadata: import_core3.Tools.deepClone(partial.metadata) } : {}
|
|
2868
4764
|
};
|
|
2869
4765
|
}
|
|
2870
|
-
const tables =
|
|
4766
|
+
const tables = import_core3.Tools.deepClone(partial.tables);
|
|
2871
4767
|
const tableOrder = partial.tableOrder && partial.tableOrder.length > 0 ? partial.tableOrder.filter((id) => Boolean(tables[id])) : Object.keys(tables);
|
|
2872
4768
|
for (const tid of Object.keys(tables)) {
|
|
2873
4769
|
if (!tableOrder.includes(tid)) tableOrder.push(tid);
|
|
@@ -2876,7 +4772,7 @@ function createDefaultBaseUnitData(partial) {
|
|
|
2876
4772
|
if (!tbl.primaryFieldId) tbl.primaryFieldId = tbl.fieldOrder[0] ?? "";
|
|
2877
4773
|
if (!tbl.recordOrder) tbl.recordOrder = Object.keys(tbl.records ?? {});
|
|
2878
4774
|
if (!tbl.views || Object.keys(tbl.views).length === 0) {
|
|
2879
|
-
const vid = `vw_${(0,
|
|
4775
|
+
const vid = `vw_${(0, import_core3.generateRandomId)(6)}`;
|
|
2880
4776
|
tbl.views = { [vid]: { id: vid, name: "\u8868\u683C\u89C6\u56FE", type: "grid" } };
|
|
2881
4777
|
tbl.viewOrder = [vid];
|
|
2882
4778
|
tbl.activeViewId = vid;
|
|
@@ -2896,7 +4792,7 @@ function createDefaultBaseUnitData(partial) {
|
|
|
2896
4792
|
return {
|
|
2897
4793
|
schema: UNIVER_BASE_SCHEMA,
|
|
2898
4794
|
schemaVersion: partial.schemaVersion ?? UNIVER_BASE_SCHEMA_VERSION,
|
|
2899
|
-
id: partial.id || `base-${(0,
|
|
4795
|
+
id: partial.id || `base-${(0, import_core3.generateRandomId)(6)}`,
|
|
2900
4796
|
name: partial.name || "\u672A\u547D\u540D\u591A\u7EF4\u6570\u636E\u8868",
|
|
2901
4797
|
rev: partial.rev ?? 1,
|
|
2902
4798
|
activeTableId,
|
|
@@ -2905,20 +4801,22 @@ function createDefaultBaseUnitData(partial) {
|
|
|
2905
4801
|
workspaceMode: partial.workspaceMode ?? "table",
|
|
2906
4802
|
tableOrder,
|
|
2907
4803
|
tables,
|
|
2908
|
-
dashboards: partial.dashboards ?
|
|
2909
|
-
automations: partial.automations ?
|
|
2910
|
-
|
|
2911
|
-
|
|
4804
|
+
dashboards: partial.dashboards ? import_core3.Tools.deepClone(partial.dashboards) : [],
|
|
4805
|
+
automations: partial.automations ? import_core3.Tools.deepClone(partial.automations) : [],
|
|
4806
|
+
automationLogs: partial.automationLogs ? import_core3.Tools.deepClone(partial.automationLogs) : [],
|
|
4807
|
+
queuedWebhooks: partial.queuedWebhooks ? import_core3.Tools.deepClone(partial.queuedWebhooks) : [],
|
|
4808
|
+
collaborators: partial.collaborators ? import_core3.Tools.deepClone(partial.collaborators) : [],
|
|
4809
|
+
metadata: partial.metadata ? import_core3.Tools.deepClone(partial.metadata) : {}
|
|
2912
4810
|
};
|
|
2913
4811
|
}
|
|
2914
4812
|
|
|
2915
4813
|
// src/models/base-unit-model.ts
|
|
2916
|
-
var
|
|
4814
|
+
var import_core4 = require("@univerjs/core");
|
|
2917
4815
|
var import_rxjs = require("rxjs");
|
|
2918
|
-
var BaseUnitModel = class extends
|
|
4816
|
+
var BaseUnitModel = class extends import_core4.UnitModel {
|
|
2919
4817
|
constructor(snapshot) {
|
|
2920
4818
|
super();
|
|
2921
|
-
this.type =
|
|
4819
|
+
this.type = import_core4.UniverInstanceType.UNIVER_BASE;
|
|
2922
4820
|
this._snapshotChanged$ = new import_rxjs.Subject();
|
|
2923
4821
|
this.snapshotChanged$ = this._snapshotChanged$.asObservable();
|
|
2924
4822
|
this._snapshot = createDefaultBaseUnitData(snapshot);
|
|
@@ -2946,7 +4844,7 @@ var BaseUnitModel = class extends import_core2.UnitModel {
|
|
|
2946
4844
|
return this._snapshot;
|
|
2947
4845
|
}
|
|
2948
4846
|
save() {
|
|
2949
|
-
return
|
|
4847
|
+
return import_core4.Tools.deepClone(this._snapshot);
|
|
2950
4848
|
}
|
|
2951
4849
|
getRev() {
|
|
2952
4850
|
return this._snapshot.rev;
|
|
@@ -3010,16 +4908,25 @@ var BaseUnitModel = class extends import_core2.UnitModel {
|
|
|
3010
4908
|
computeGanttTasks(tableId, viewId) {
|
|
3011
4909
|
return computeBaseGanttTasks(this._snapshot, tableId, viewId);
|
|
3012
4910
|
}
|
|
4911
|
+
computeHierarchicalList(tableId, viewId) {
|
|
4912
|
+
return computeBaseHierarchicalList(this._snapshot, tableId, viewId);
|
|
4913
|
+
}
|
|
3013
4914
|
computeDashboardWidget(widget) {
|
|
3014
4915
|
return computeBaseDashboardWidget(this._snapshot, widget);
|
|
3015
4916
|
}
|
|
4917
|
+
getAutomations() {
|
|
4918
|
+
return this._snapshot.automations ?? [];
|
|
4919
|
+
}
|
|
4920
|
+
getAutomationLogs() {
|
|
4921
|
+
return this._snapshot.automationLogs ?? [];
|
|
4922
|
+
}
|
|
3016
4923
|
// =========================================================================
|
|
3017
4924
|
// Internal synchronous mutators invoked exclusively by IMutation handlers
|
|
3018
4925
|
// =========================================================================
|
|
3019
4926
|
__replaceSnapshot(nextSnapshot) {
|
|
3020
4927
|
const prevId = this._snapshot.id;
|
|
3021
4928
|
const normalized = createDefaultBaseUnitData({
|
|
3022
|
-
...
|
|
4929
|
+
...import_core4.Tools.deepClone(nextSnapshot),
|
|
3023
4930
|
id: nextSnapshot.id || prevId
|
|
3024
4931
|
});
|
|
3025
4932
|
const prevName = this._snapshot.name;
|
|
@@ -3032,7 +4939,7 @@ var BaseUnitModel = class extends import_core2.UnitModel {
|
|
|
3032
4939
|
}
|
|
3033
4940
|
__applyTablesState(params) {
|
|
3034
4941
|
this._snapshot.tableOrder = [...params.tableOrder];
|
|
3035
|
-
this._snapshot.tables =
|
|
4942
|
+
this._snapshot.tables = import_core4.Tools.deepClone(params.tables);
|
|
3036
4943
|
if (params.activeTableId !== void 0) {
|
|
3037
4944
|
this._snapshot.activeTableId = params.activeTableId;
|
|
3038
4945
|
}
|
|
@@ -3042,6 +4949,15 @@ var BaseUnitModel = class extends import_core2.UnitModel {
|
|
|
3042
4949
|
if (params.workspaceMode !== void 0) {
|
|
3043
4950
|
this._snapshot.workspaceMode = params.workspaceMode;
|
|
3044
4951
|
}
|
|
4952
|
+
if (params.automations !== void 0) {
|
|
4953
|
+
this._snapshot.automations = import_core4.Tools.deepClone(params.automations);
|
|
4954
|
+
}
|
|
4955
|
+
if (params.automationLogs !== void 0) {
|
|
4956
|
+
this._snapshot.automationLogs = import_core4.Tools.deepClone(params.automationLogs);
|
|
4957
|
+
}
|
|
4958
|
+
if (params.queuedWebhooks !== void 0) {
|
|
4959
|
+
this._snapshot.queuedWebhooks = import_core4.Tools.deepClone(params.queuedWebhooks);
|
|
4960
|
+
}
|
|
3045
4961
|
this.incrementRev();
|
|
3046
4962
|
this._notifyChange();
|
|
3047
4963
|
}
|
|
@@ -3070,7 +4986,7 @@ var BaseUnitModel = class extends import_core2.UnitModel {
|
|
|
3070
4986
|
this._notifyChange();
|
|
3071
4987
|
}
|
|
3072
4988
|
__applyDashboardsState(params) {
|
|
3073
|
-
this._snapshot.dashboards =
|
|
4989
|
+
this._snapshot.dashboards = import_core4.Tools.deepClone(params.dashboards);
|
|
3074
4990
|
if (params.activeDashboardId !== void 0) {
|
|
3075
4991
|
this._snapshot.activeDashboardId = params.activeDashboardId;
|
|
3076
4992
|
}
|
|
@@ -3086,19 +5002,19 @@ var BaseUnitModel = class extends import_core2.UnitModel {
|
|
|
3086
5002
|
};
|
|
3087
5003
|
|
|
3088
5004
|
// src/commands/mutations/base.mutation.ts
|
|
3089
|
-
var
|
|
5005
|
+
var import_core5 = require("@univerjs/core");
|
|
3090
5006
|
function resolveBaseUnit(instanceService, unitId) {
|
|
3091
5007
|
if (unitId) {
|
|
3092
|
-
return instanceService.getUnit(unitId,
|
|
5008
|
+
return instanceService.getUnit(unitId, import_core5.UniverInstanceType.UNIVER_BASE) ?? null;
|
|
3093
5009
|
}
|
|
3094
|
-
return instanceService.getCurrentUnitOfType(
|
|
5010
|
+
return instanceService.getCurrentUnitOfType(import_core5.UniverInstanceType.UNIVER_BASE) ?? null;
|
|
3095
5011
|
}
|
|
3096
5012
|
var ApplyBaseTablesStateMutation = {
|
|
3097
5013
|
id: "bases.mutation.apply-tables-state",
|
|
3098
|
-
type:
|
|
5014
|
+
type: import_core5.CommandType.MUTATION,
|
|
3099
5015
|
handler: (accessor, params) => {
|
|
3100
5016
|
if (!params) return false;
|
|
3101
|
-
const unit = resolveBaseUnit(accessor.get(
|
|
5017
|
+
const unit = resolveBaseUnit(accessor.get(import_core5.IUniverInstanceService), params.unitId);
|
|
3102
5018
|
if (!unit) return false;
|
|
3103
5019
|
unit.__applyTablesState(params);
|
|
3104
5020
|
return true;
|
|
@@ -3106,10 +5022,10 @@ var ApplyBaseTablesStateMutation = {
|
|
|
3106
5022
|
};
|
|
3107
5023
|
var SetBaseActiveNavigationMutation = {
|
|
3108
5024
|
id: "bases.mutation.set-active-navigation",
|
|
3109
|
-
type:
|
|
5025
|
+
type: import_core5.CommandType.MUTATION,
|
|
3110
5026
|
handler: (accessor, params) => {
|
|
3111
5027
|
if (!params) return false;
|
|
3112
|
-
const unit = resolveBaseUnit(accessor.get(
|
|
5028
|
+
const unit = resolveBaseUnit(accessor.get(import_core5.IUniverInstanceService), params.unitId);
|
|
3113
5029
|
if (!unit) return false;
|
|
3114
5030
|
unit.__setActiveNavigation(params);
|
|
3115
5031
|
return true;
|
|
@@ -3117,10 +5033,10 @@ var SetBaseActiveNavigationMutation = {
|
|
|
3117
5033
|
};
|
|
3118
5034
|
var ApplyBaseDashboardsStateMutation = {
|
|
3119
5035
|
id: "bases.mutation.apply-dashboards-state",
|
|
3120
|
-
type:
|
|
5036
|
+
type: import_core5.CommandType.MUTATION,
|
|
3121
5037
|
handler: (accessor, params) => {
|
|
3122
5038
|
if (!params) return false;
|
|
3123
|
-
const unit = resolveBaseUnit(accessor.get(
|
|
5039
|
+
const unit = resolveBaseUnit(accessor.get(import_core5.IUniverInstanceService), params.unitId);
|
|
3124
5040
|
if (!unit) return false;
|
|
3125
5041
|
unit.__applyDashboardsState(params);
|
|
3126
5042
|
return true;
|
|
@@ -3128,10 +5044,10 @@ var ApplyBaseDashboardsStateMutation = {
|
|
|
3128
5044
|
};
|
|
3129
5045
|
var ReplaceBaseSnapshotMutation = {
|
|
3130
5046
|
id: "bases.mutation.replace-snapshot",
|
|
3131
|
-
type:
|
|
5047
|
+
type: import_core5.CommandType.MUTATION,
|
|
3132
5048
|
handler: (accessor, params) => {
|
|
3133
5049
|
if (!params?.snapshot) return false;
|
|
3134
|
-
const unit = resolveBaseUnit(accessor.get(
|
|
5050
|
+
const unit = resolveBaseUnit(accessor.get(import_core5.IUniverInstanceService), params.unitId);
|
|
3135
5051
|
if (!unit) return false;
|
|
3136
5052
|
unit.__replaceSnapshot(params.snapshot);
|
|
3137
5053
|
return true;
|
|
@@ -3139,30 +5055,73 @@ var ReplaceBaseSnapshotMutation = {
|
|
|
3139
5055
|
};
|
|
3140
5056
|
|
|
3141
5057
|
// src/commands/commands/base.command.ts
|
|
3142
|
-
var
|
|
3143
|
-
function
|
|
5058
|
+
var import_core6 = require("@univerjs/core");
|
|
5059
|
+
function nowTimestamp3() {
|
|
3144
5060
|
return (/* @__PURE__ */ new Date()).toISOString().replace("T", " ").slice(0, 19);
|
|
3145
5061
|
}
|
|
3146
5062
|
function resolveUnitContext(accessor, explicitUnitId) {
|
|
3147
|
-
const instanceService = accessor.get(
|
|
3148
|
-
const unit = explicitUnitId ? instanceService.getUnit(explicitUnitId,
|
|
5063
|
+
const instanceService = accessor.get(import_core6.IUniverInstanceService);
|
|
5064
|
+
const unit = explicitUnitId ? instanceService.getUnit(explicitUnitId, import_core6.UniverInstanceType.UNIVER_BASE) : instanceService.getCurrentUnitOfType(import_core6.UniverInstanceType.UNIVER_BASE);
|
|
3149
5065
|
if (!unit) return null;
|
|
3150
5066
|
return {
|
|
3151
5067
|
unit,
|
|
3152
5068
|
unitId: unit.getUnitId(),
|
|
3153
|
-
commandService: accessor.get(
|
|
3154
|
-
undoRedoService: accessor.get(
|
|
5069
|
+
commandService: accessor.get(import_core6.ICommandService),
|
|
5070
|
+
undoRedoService: accessor.get(import_core6.IUndoRedoService)
|
|
3155
5071
|
};
|
|
3156
5072
|
}
|
|
5073
|
+
var activeCommandAutomationDepth = 0;
|
|
5074
|
+
function resolveWebhookExecutor(accessor, explicit) {
|
|
5075
|
+
if (explicit) return explicit;
|
|
5076
|
+
try {
|
|
5077
|
+
if (accessor.has(IBaseAutomationService)) {
|
|
5078
|
+
return accessor.get(IBaseAutomationService).getWebhookExecutor();
|
|
5079
|
+
}
|
|
5080
|
+
} catch {
|
|
5081
|
+
}
|
|
5082
|
+
return void 0;
|
|
5083
|
+
}
|
|
3157
5084
|
function captureTablesState(unitId, snap) {
|
|
3158
5085
|
return {
|
|
3159
5086
|
unitId,
|
|
3160
5087
|
tableOrder: [...snap.tableOrder],
|
|
3161
|
-
tables:
|
|
5088
|
+
tables: import_core6.Tools.deepClone(snap.tables),
|
|
3162
5089
|
activeTableId: snap.activeTableId,
|
|
3163
5090
|
activeViewId: snap.activeViewId,
|
|
3164
|
-
workspaceMode: snap.workspaceMode ?? "table"
|
|
5091
|
+
workspaceMode: snap.workspaceMode ?? "table",
|
|
5092
|
+
automations: import_core6.Tools.deepClone(snap.automations ?? []),
|
|
5093
|
+
automationLogs: import_core6.Tools.deepClone(snap.automationLogs ?? []),
|
|
5094
|
+
queuedWebhooks: import_core6.Tools.deepClone(snap.queuedWebhooks ?? [])
|
|
5095
|
+
};
|
|
5096
|
+
}
|
|
5097
|
+
function applyAutomationsToTransitionState(snap, after, event, webhookExecutor) {
|
|
5098
|
+
const workingUnit = {
|
|
5099
|
+
...snap,
|
|
5100
|
+
tableOrder: after.tableOrder,
|
|
5101
|
+
tables: after.tables,
|
|
5102
|
+
automations: after.automations ?? [],
|
|
5103
|
+
automationLogs: after.automationLogs ?? [],
|
|
5104
|
+
queuedWebhooks: after.queuedWebhooks ?? []
|
|
3165
5105
|
};
|
|
5106
|
+
activeCommandAutomationDepth += 1;
|
|
5107
|
+
try {
|
|
5108
|
+
const res = runBaseAutomationsInPlaceSync(
|
|
5109
|
+
workingUnit,
|
|
5110
|
+
{
|
|
5111
|
+
...event,
|
|
5112
|
+
depth: event.depth ?? activeCommandAutomationDepth
|
|
5113
|
+
},
|
|
5114
|
+
{ webhookExecutor }
|
|
5115
|
+
);
|
|
5116
|
+
after.tables = workingUnit.tables;
|
|
5117
|
+
after.tableOrder = workingUnit.tableOrder;
|
|
5118
|
+
after.automations = workingUnit.automations;
|
|
5119
|
+
after.automationLogs = workingUnit.automationLogs;
|
|
5120
|
+
after.queuedWebhooks = workingUnit.queuedWebhooks;
|
|
5121
|
+
return res.logs;
|
|
5122
|
+
} finally {
|
|
5123
|
+
activeCommandAutomationDepth -= 1;
|
|
5124
|
+
}
|
|
3166
5125
|
}
|
|
3167
5126
|
function commitTablesTransition(ctx, beforeState, afterState) {
|
|
3168
5127
|
const ok = ctx.commandService.syncExecuteCommand(ApplyBaseTablesStateMutation.id, afterState);
|
|
@@ -3220,15 +5179,15 @@ function syncBidirectionalLinkField(tables, sourceTableId, sourceRecordId, linkF
|
|
|
3220
5179
|
}
|
|
3221
5180
|
}
|
|
3222
5181
|
function buildDefaultFieldConfig(type, config, unitSnap, currentTableId) {
|
|
3223
|
-
if (config) return
|
|
5182
|
+
if (config) return import_core6.Tools.deepClone(config);
|
|
3224
5183
|
switch (type) {
|
|
3225
5184
|
case "singleSelect":
|
|
3226
5185
|
case "multiSelect":
|
|
3227
5186
|
return {
|
|
3228
5187
|
options: [
|
|
3229
|
-
{ id: `opt_${(0,
|
|
3230
|
-
{ id: `opt_${(0,
|
|
3231
|
-
{ id: `opt_${(0,
|
|
5188
|
+
{ id: `opt_${(0, import_core6.generateRandomId)(4)}`, name: "\u9009\u9879\u4E00", color: BASE_SELECT_PALETTE[0] },
|
|
5189
|
+
{ id: `opt_${(0, import_core6.generateRandomId)(4)}`, name: "\u9009\u9879\u4E8C", color: BASE_SELECT_PALETTE[1] },
|
|
5190
|
+
{ id: `opt_${(0, import_core6.generateRandomId)(4)}`, name: "\u9009\u9879\u4E09", color: BASE_SELECT_PALETTE[2] }
|
|
3232
5191
|
]
|
|
3233
5192
|
};
|
|
3234
5193
|
case "currency":
|
|
@@ -3259,19 +5218,19 @@ function buildDefaultFieldConfig(type, config, unitSnap, currentTableId) {
|
|
|
3259
5218
|
}
|
|
3260
5219
|
var AddBaseTableCommand = {
|
|
3261
5220
|
id: "bases.command.add-table",
|
|
3262
|
-
type:
|
|
5221
|
+
type: import_core6.CommandType.COMMAND,
|
|
3263
5222
|
handler: (accessor, params) => {
|
|
3264
5223
|
const ctx = resolveUnitContext(accessor, params?.unitId);
|
|
3265
5224
|
if (!ctx) return false;
|
|
3266
5225
|
const snap = ctx.unit.getSnapshot();
|
|
3267
5226
|
const before = captureTablesState(ctx.unitId, snap);
|
|
3268
5227
|
const after = captureTablesState(ctx.unitId, snap);
|
|
3269
|
-
const tableId = params?.tableId || `tbl_${(0,
|
|
5228
|
+
const tableId = params?.tableId || `tbl_${(0, import_core6.generateRandomId)(6)}`;
|
|
3270
5229
|
if (after.tables[tableId]) return false;
|
|
3271
|
-
const defaultFields = params?.fields && params.fields.length > 0 ?
|
|
3272
|
-
{ id: `fld_${(0,
|
|
5230
|
+
const defaultFields = params?.fields && params.fields.length > 0 ? import_core6.Tools.deepClone(params.fields) : [
|
|
5231
|
+
{ id: `fld_${(0, import_core6.generateRandomId)(5)}`, name: "\u6807\u9898\u540D\u79F0", type: "text", isPrimary: true, width: 220 },
|
|
3273
5232
|
{
|
|
3274
|
-
id: `fld_${(0,
|
|
5233
|
+
id: `fld_${(0, import_core6.generateRandomId)(5)}`,
|
|
3275
5234
|
name: "\u72B6\u6001\u6807\u7B7E",
|
|
3276
5235
|
type: "singleSelect",
|
|
3277
5236
|
width: 130,
|
|
@@ -3283,8 +5242,8 @@ var AddBaseTableCommand = {
|
|
|
3283
5242
|
]
|
|
3284
5243
|
}
|
|
3285
5244
|
},
|
|
3286
|
-
{ id: `fld_${(0,
|
|
3287
|
-
{ id: `fld_${(0,
|
|
5245
|
+
{ id: `fld_${(0, import_core6.generateRandomId)(5)}`, name: "\u8D1F\u8D23\u4EBA", type: "person", width: 130 },
|
|
5246
|
+
{ id: `fld_${(0, import_core6.generateRandomId)(5)}`, name: "\u622A\u6B62\u65E5\u671F", type: "date", width: 130 }
|
|
3288
5247
|
];
|
|
3289
5248
|
if (!defaultFields.some((f) => f.isPrimary) && defaultFields[0]) {
|
|
3290
5249
|
defaultFields[0].isPrimary = true;
|
|
@@ -3292,9 +5251,9 @@ var AddBaseTableCommand = {
|
|
|
3292
5251
|
const fieldsMap = {};
|
|
3293
5252
|
for (const f of defaultFields) fieldsMap[f.id] = f;
|
|
3294
5253
|
const primaryFieldId = defaultFields.find((f) => f.isPrimary)?.id ?? defaultFields[0].id;
|
|
3295
|
-
const gridViewId = `vw_${(0,
|
|
3296
|
-
const now =
|
|
3297
|
-
const initRecId = `rec_${(0,
|
|
5254
|
+
const gridViewId = `vw_${(0, import_core6.generateRandomId)(6)}`;
|
|
5255
|
+
const now = nowTimestamp3();
|
|
5256
|
+
const initRecId = `rec_${(0, import_core6.generateRandomId)(6)}`;
|
|
3298
5257
|
const newTable = {
|
|
3299
5258
|
id: tableId,
|
|
3300
5259
|
name: params?.name?.trim() || `\u6570\u636E\u8868 ${after.tableOrder.length + 1}`,
|
|
@@ -3335,7 +5294,7 @@ var AddBaseTableCommand = {
|
|
|
3335
5294
|
};
|
|
3336
5295
|
var UpdateBaseTableCommand = {
|
|
3337
5296
|
id: "bases.command.update-table",
|
|
3338
|
-
type:
|
|
5297
|
+
type: import_core6.CommandType.COMMAND,
|
|
3339
5298
|
handler: (accessor, params) => {
|
|
3340
5299
|
if (!params?.tableId || !params.patch) return false;
|
|
3341
5300
|
const ctx = resolveUnitContext(accessor, params.unitId);
|
|
@@ -3359,7 +5318,7 @@ var UpdateBaseTableCommand = {
|
|
|
3359
5318
|
};
|
|
3360
5319
|
var DeleteBaseTableCommand = {
|
|
3361
5320
|
id: "bases.command.delete-table",
|
|
3362
|
-
type:
|
|
5321
|
+
type: import_core6.CommandType.COMMAND,
|
|
3363
5322
|
handler: (accessor, params) => {
|
|
3364
5323
|
if (!params?.tableId) return false;
|
|
3365
5324
|
const ctx = resolveUnitContext(accessor, params.unitId);
|
|
@@ -3380,7 +5339,7 @@ var DeleteBaseTableCommand = {
|
|
|
3380
5339
|
};
|
|
3381
5340
|
var SetActiveBaseTableCommand = {
|
|
3382
5341
|
id: "bases.command.set-active-table",
|
|
3383
|
-
type:
|
|
5342
|
+
type: import_core6.CommandType.COMMAND,
|
|
3384
5343
|
handler: (accessor, params) => {
|
|
3385
5344
|
if (!params?.tableId) return false;
|
|
3386
5345
|
const ctx = resolveUnitContext(accessor, params.unitId);
|
|
@@ -3417,7 +5376,7 @@ var SetActiveBaseTableCommand = {
|
|
|
3417
5376
|
};
|
|
3418
5377
|
var SetBaseWorkspaceModeCommand = {
|
|
3419
5378
|
id: "bases.command.set-workspace-mode",
|
|
3420
|
-
type:
|
|
5379
|
+
type: import_core6.CommandType.COMMAND,
|
|
3421
5380
|
handler: (accessor, params) => {
|
|
3422
5381
|
if (!params?.mode) return false;
|
|
3423
5382
|
const ctx = resolveUnitContext(accessor, params.unitId);
|
|
@@ -3451,7 +5410,7 @@ var SetBaseWorkspaceModeCommand = {
|
|
|
3451
5410
|
};
|
|
3452
5411
|
var AddBaseFieldCommand = {
|
|
3453
5412
|
id: "bases.command.add-field",
|
|
3454
|
-
type:
|
|
5413
|
+
type: import_core6.CommandType.COMMAND,
|
|
3455
5414
|
handler: (accessor, params) => {
|
|
3456
5415
|
if (!params?.name?.trim() || !params.type) return false;
|
|
3457
5416
|
const ctx = resolveUnitContext(accessor, params.unitId);
|
|
@@ -3462,7 +5421,7 @@ var AddBaseFieldCommand = {
|
|
|
3462
5421
|
const before = captureTablesState(ctx.unitId, snap);
|
|
3463
5422
|
const after = captureTablesState(ctx.unitId, snap);
|
|
3464
5423
|
const table = after.tables[tableId];
|
|
3465
|
-
const fieldId = params.fieldId || `fld_${(0,
|
|
5424
|
+
const fieldId = params.fieldId || `fld_${(0, import_core6.generateRandomId)(6)}`;
|
|
3466
5425
|
if (table.fields[fieldId]) return false;
|
|
3467
5426
|
const resolvedConfig = buildDefaultFieldConfig(params.type, params.config, snap, tableId);
|
|
3468
5427
|
const newField = {
|
|
@@ -3484,7 +5443,7 @@ var AddBaseFieldCommand = {
|
|
|
3484
5443
|
symmetricFieldId: fieldId
|
|
3485
5444
|
};
|
|
3486
5445
|
} else {
|
|
3487
|
-
const symId = existingSymId || `fld_sym_${(0,
|
|
5446
|
+
const symId = existingSymId || `fld_sym_${(0, import_core6.generateRandomId)(5)}`;
|
|
3488
5447
|
const symName = params.symmetricFieldName?.trim() || `\u5173\u8054${table.name}`;
|
|
3489
5448
|
targetTable.fields[symId] = {
|
|
3490
5449
|
id: symId,
|
|
@@ -3514,7 +5473,7 @@ var AddBaseFieldCommand = {
|
|
|
3514
5473
|
};
|
|
3515
5474
|
var UpdateBaseFieldCommand = {
|
|
3516
5475
|
id: "bases.command.update-field",
|
|
3517
|
-
type:
|
|
5476
|
+
type: import_core6.CommandType.COMMAND,
|
|
3518
5477
|
handler: (accessor, params) => {
|
|
3519
5478
|
if (!params?.fieldId || !params.patch) return false;
|
|
3520
5479
|
const ctx = resolveUnitContext(accessor, params.unitId);
|
|
@@ -3542,7 +5501,7 @@ var UpdateBaseFieldCommand = {
|
|
|
3542
5501
|
if (params.patch.config !== void 0) {
|
|
3543
5502
|
targetField.config = {
|
|
3544
5503
|
...targetField.config ?? {},
|
|
3545
|
-
...
|
|
5504
|
+
...import_core6.Tools.deepClone(params.patch.config)
|
|
3546
5505
|
};
|
|
3547
5506
|
}
|
|
3548
5507
|
return commitTablesTransition(ctx, before, after);
|
|
@@ -3550,7 +5509,7 @@ var UpdateBaseFieldCommand = {
|
|
|
3550
5509
|
};
|
|
3551
5510
|
var DeleteBaseFieldCommand = {
|
|
3552
5511
|
id: "bases.command.delete-field",
|
|
3553
|
-
type:
|
|
5512
|
+
type: import_core6.CommandType.COMMAND,
|
|
3554
5513
|
handler: (accessor, params) => {
|
|
3555
5514
|
if (!params?.fieldId) return false;
|
|
3556
5515
|
const ctx = resolveUnitContext(accessor, params.unitId);
|
|
@@ -3582,7 +5541,7 @@ var DeleteBaseFieldCommand = {
|
|
|
3582
5541
|
};
|
|
3583
5542
|
var ReorderBaseFieldsCommand = {
|
|
3584
5543
|
id: "bases.command.reorder-fields",
|
|
3585
|
-
type:
|
|
5544
|
+
type: import_core6.CommandType.COMMAND,
|
|
3586
5545
|
handler: (accessor, params) => {
|
|
3587
5546
|
if (!params?.fieldOrder?.length) return false;
|
|
3588
5547
|
const ctx = resolveUnitContext(accessor, params.unitId);
|
|
@@ -3603,7 +5562,7 @@ var ReorderBaseFieldsCommand = {
|
|
|
3603
5562
|
};
|
|
3604
5563
|
var AddBaseRecordCommand = {
|
|
3605
5564
|
id: "bases.command.add-record",
|
|
3606
|
-
type:
|
|
5565
|
+
type: import_core6.CommandType.COMMAND,
|
|
3607
5566
|
handler: (accessor, params) => {
|
|
3608
5567
|
const ctx = resolveUnitContext(accessor, params?.unitId);
|
|
3609
5568
|
if (!ctx) return false;
|
|
@@ -3613,9 +5572,9 @@ var AddBaseRecordCommand = {
|
|
|
3613
5572
|
const before = captureTablesState(ctx.unitId, snap);
|
|
3614
5573
|
const after = captureTablesState(ctx.unitId, snap);
|
|
3615
5574
|
const table = after.tables[tableId];
|
|
3616
|
-
const recordId = params?.recordId || `rec_${(0,
|
|
5575
|
+
const recordId = params?.recordId || `rec_${(0, import_core6.generateRandomId)(6)}`;
|
|
3617
5576
|
if (table.records[recordId]) return false;
|
|
3618
|
-
const now =
|
|
5577
|
+
const now = nowTimestamp3();
|
|
3619
5578
|
const autoNumber = table.nextAutoNumber ?? table.recordOrder.length + 1;
|
|
3620
5579
|
table.nextAutoNumber = autoNumber + 1;
|
|
3621
5580
|
const cleanCells = {};
|
|
@@ -3626,7 +5585,7 @@ var AddBaseRecordCommand = {
|
|
|
3626
5585
|
const ids = normalizeLinkIds(val);
|
|
3627
5586
|
cleanCells[fid] = f.config?.allowMultiple === false ? ids.slice(0, 1) : ids;
|
|
3628
5587
|
} else {
|
|
3629
|
-
cleanCells[fid] =
|
|
5588
|
+
cleanCells[fid] = import_core6.Tools.deepClone(val);
|
|
3630
5589
|
}
|
|
3631
5590
|
}
|
|
3632
5591
|
const newRecord = {
|
|
@@ -3660,6 +5619,19 @@ var AddBaseRecordCommand = {
|
|
|
3660
5619
|
}
|
|
3661
5620
|
}
|
|
3662
5621
|
}
|
|
5622
|
+
if ((after.automations?.length ?? 0) > 0) {
|
|
5623
|
+
applyAutomationsToTransitionState(
|
|
5624
|
+
snap,
|
|
5625
|
+
after,
|
|
5626
|
+
{
|
|
5627
|
+
trigger: "recordCreated",
|
|
5628
|
+
tableId,
|
|
5629
|
+
recordId,
|
|
5630
|
+
changedFieldIds: Object.keys(cleanCells)
|
|
5631
|
+
},
|
|
5632
|
+
resolveWebhookExecutor(accessor)
|
|
5633
|
+
);
|
|
5634
|
+
}
|
|
3663
5635
|
const ok = commitTablesTransition(ctx, before, after);
|
|
3664
5636
|
if (ok && params?.onCreated) params.onCreated(recordId);
|
|
3665
5637
|
return ok;
|
|
@@ -3667,7 +5639,7 @@ var AddBaseRecordCommand = {
|
|
|
3667
5639
|
};
|
|
3668
5640
|
var UpdateBaseRecordCommand = {
|
|
3669
5641
|
id: "bases.command.update-record",
|
|
3670
|
-
type:
|
|
5642
|
+
type: import_core6.CommandType.COMMAND,
|
|
3671
5643
|
handler: (accessor, params) => {
|
|
3672
5644
|
if (!params?.recordId || !params.cells) return false;
|
|
3673
5645
|
const ctx = resolveUnitContext(accessor, params.unitId);
|
|
@@ -3680,7 +5652,9 @@ var UpdateBaseRecordCommand = {
|
|
|
3680
5652
|
const after = captureTablesState(ctx.unitId, snap);
|
|
3681
5653
|
const table = after.tables[tableId];
|
|
3682
5654
|
const record = table.records[params.recordId];
|
|
3683
|
-
const
|
|
5655
|
+
const previousCells = import_core6.Tools.deepClone(record.cells);
|
|
5656
|
+
const changedFieldIds = [];
|
|
5657
|
+
const now = nowTimestamp3();
|
|
3684
5658
|
for (const [fid, val] of Object.entries(params.cells)) {
|
|
3685
5659
|
const field = table.fields[fid];
|
|
3686
5660
|
if (!field || isComputedBaseField(field.type)) continue;
|
|
@@ -3689,6 +5663,7 @@ var UpdateBaseRecordCommand = {
|
|
|
3689
5663
|
const rawNextIds = normalizeLinkIds(val);
|
|
3690
5664
|
const nextIds = field.config?.allowMultiple === false ? rawNextIds.slice(0, 1) : rawNextIds;
|
|
3691
5665
|
record.cells[fid] = nextIds;
|
|
5666
|
+
changedFieldIds.push(fid);
|
|
3692
5667
|
syncBidirectionalLinkField(
|
|
3693
5668
|
after.tables,
|
|
3694
5669
|
tableId,
|
|
@@ -3699,19 +5674,34 @@ var UpdateBaseRecordCommand = {
|
|
|
3699
5674
|
now
|
|
3700
5675
|
);
|
|
3701
5676
|
} else {
|
|
3702
|
-
record.cells[fid] =
|
|
5677
|
+
record.cells[fid] = import_core6.Tools.deepClone(val);
|
|
5678
|
+
changedFieldIds.push(fid);
|
|
3703
5679
|
}
|
|
3704
5680
|
}
|
|
3705
5681
|
record.lastModifiedTime = now;
|
|
3706
5682
|
if (params.modifiedBy) {
|
|
3707
5683
|
record.lastModifiedBy = params.modifiedBy;
|
|
3708
5684
|
}
|
|
5685
|
+
if ((after.automations?.length ?? 0) > 0 && changedFieldIds.length > 0) {
|
|
5686
|
+
applyAutomationsToTransitionState(
|
|
5687
|
+
snap,
|
|
5688
|
+
after,
|
|
5689
|
+
{
|
|
5690
|
+
trigger: "recordUpdated",
|
|
5691
|
+
tableId,
|
|
5692
|
+
recordId: params.recordId,
|
|
5693
|
+
changedFieldIds,
|
|
5694
|
+
previousCells
|
|
5695
|
+
},
|
|
5696
|
+
resolveWebhookExecutor(accessor)
|
|
5697
|
+
);
|
|
5698
|
+
}
|
|
3709
5699
|
return commitTablesTransition(ctx, before, after);
|
|
3710
5700
|
}
|
|
3711
5701
|
};
|
|
3712
5702
|
var DeleteBaseRecordsCommand = {
|
|
3713
5703
|
id: "bases.command.delete-records",
|
|
3714
|
-
type:
|
|
5704
|
+
type: import_core6.CommandType.COMMAND,
|
|
3715
5705
|
handler: (accessor, params) => {
|
|
3716
5706
|
if (!params?.recordIds?.length) return false;
|
|
3717
5707
|
const ctx = resolveUnitContext(accessor, params.unitId);
|
|
@@ -3725,7 +5715,7 @@ var DeleteBaseRecordsCommand = {
|
|
|
3725
5715
|
const before = captureTablesState(ctx.unitId, snap);
|
|
3726
5716
|
const after = captureTablesState(ctx.unitId, snap);
|
|
3727
5717
|
const table = after.tables[tableId];
|
|
3728
|
-
const now =
|
|
5718
|
+
const now = nowTimestamp3();
|
|
3729
5719
|
for (const rid of delSet) {
|
|
3730
5720
|
delete table.records[rid];
|
|
3731
5721
|
}
|
|
@@ -3751,7 +5741,7 @@ var DeleteBaseRecordsCommand = {
|
|
|
3751
5741
|
};
|
|
3752
5742
|
var BatchInsertBaseRecordsCommand = {
|
|
3753
5743
|
id: "bases.command.batch-insert-records",
|
|
3754
|
-
type:
|
|
5744
|
+
type: import_core6.CommandType.COMMAND,
|
|
3755
5745
|
handler: (accessor, params) => {
|
|
3756
5746
|
if (!params?.records?.length) return false;
|
|
3757
5747
|
const ctx = resolveUnitContext(accessor, params.unitId);
|
|
@@ -3762,17 +5752,17 @@ var BatchInsertBaseRecordsCommand = {
|
|
|
3762
5752
|
const before = captureTablesState(ctx.unitId, snap);
|
|
3763
5753
|
const after = captureTablesState(ctx.unitId, snap);
|
|
3764
5754
|
const table = after.tables[tableId];
|
|
3765
|
-
const now =
|
|
5755
|
+
const now = nowTimestamp3();
|
|
3766
5756
|
let nextAuto = table.nextAutoNumber ?? table.recordOrder.length + 1;
|
|
3767
5757
|
const createdIds = [];
|
|
3768
5758
|
for (const item of params.records) {
|
|
3769
|
-
const rid = item.id || `rec_${(0,
|
|
5759
|
+
const rid = item.id || `rec_${(0, import_core6.generateRandomId)(6)}`;
|
|
3770
5760
|
if (table.records[rid]) continue;
|
|
3771
5761
|
const cells = {};
|
|
3772
5762
|
for (const [fid, val] of Object.entries(item.cells ?? {})) {
|
|
3773
5763
|
const f = table.fields[fid];
|
|
3774
5764
|
if (f && isComputedBaseField(f.type)) continue;
|
|
3775
|
-
cells[fid] = f?.type === "link" ? normalizeLinkIds(val) :
|
|
5765
|
+
cells[fid] = f?.type === "link" ? normalizeLinkIds(val) : import_core6.Tools.deepClone(val);
|
|
3776
5766
|
}
|
|
3777
5767
|
table.records[rid] = {
|
|
3778
5768
|
id: rid,
|
|
@@ -3803,7 +5793,7 @@ var BatchInsertBaseRecordsCommand = {
|
|
|
3803
5793
|
};
|
|
3804
5794
|
var SubmitBaseFormResponseCommand = {
|
|
3805
5795
|
id: "bases.command.submit-form-response",
|
|
3806
|
-
type:
|
|
5796
|
+
type: import_core6.CommandType.COMMAND,
|
|
3807
5797
|
handler: (accessor, params) => {
|
|
3808
5798
|
if (!params?.values) return false;
|
|
3809
5799
|
const ctx = resolveUnitContext(accessor, params.unitId);
|
|
@@ -3813,9 +5803,15 @@ var SubmitBaseFormResponseCommand = {
|
|
|
3813
5803
|
const table = snap.tables[tableId];
|
|
3814
5804
|
if (!table) return false;
|
|
3815
5805
|
const view = (params.viewId ? table.views[params.viewId] : void 0) ?? Object.values(table.views).find((v) => v.type === "form");
|
|
5806
|
+
const cleanedValues = { ...params.values };
|
|
3816
5807
|
if (view?.formConfig?.fields?.length) {
|
|
3817
5808
|
for (const item of view.formConfig.fields) {
|
|
3818
|
-
|
|
5809
|
+
const visible = isFormFieldVisible(item, params.values, table, snap, view.formConfig);
|
|
5810
|
+
if (!visible) {
|
|
5811
|
+
delete cleanedValues[item.fieldId];
|
|
5812
|
+
continue;
|
|
5813
|
+
}
|
|
5814
|
+
if (!isFormFieldRequired(item, params.values, table, snap, view.formConfig)) continue;
|
|
3819
5815
|
const field = table.fields[item.fieldId];
|
|
3820
5816
|
if (!field || isComputedBaseField(field.type)) continue;
|
|
3821
5817
|
const v = params.values[item.fieldId];
|
|
@@ -3828,7 +5824,7 @@ var SubmitBaseFormResponseCommand = {
|
|
|
3828
5824
|
return ctx.commandService.syncExecuteCommand(AddBaseRecordCommand.id, {
|
|
3829
5825
|
unitId: ctx.unitId,
|
|
3830
5826
|
tableId,
|
|
3831
|
-
cells:
|
|
5827
|
+
cells: cleanedValues,
|
|
3832
5828
|
createdBy: params.submittedBy ?? "\u8868\u5355\u63D0\u62A5\u4EBA",
|
|
3833
5829
|
onCreated: params.onSubmitted
|
|
3834
5830
|
});
|
|
@@ -3836,7 +5832,7 @@ var SubmitBaseFormResponseCommand = {
|
|
|
3836
5832
|
};
|
|
3837
5833
|
var AddBaseViewCommand = {
|
|
3838
5834
|
id: "bases.command.add-view",
|
|
3839
|
-
type:
|
|
5835
|
+
type: import_core6.CommandType.COMMAND,
|
|
3840
5836
|
handler: (accessor, params) => {
|
|
3841
5837
|
if (!params?.type) return false;
|
|
3842
5838
|
const ctx = resolveUnitContext(accessor, params.unitId);
|
|
@@ -3847,10 +5843,11 @@ var AddBaseViewCommand = {
|
|
|
3847
5843
|
const before = captureTablesState(ctx.unitId, snap);
|
|
3848
5844
|
const after = captureTablesState(ctx.unitId, snap);
|
|
3849
5845
|
const table = after.tables[tableId];
|
|
3850
|
-
const viewId = params.viewId || `vw_${(0,
|
|
5846
|
+
const viewId = params.viewId || `vw_${(0, import_core6.generateRandomId)(6)}`;
|
|
3851
5847
|
if (table.views[viewId]) return false;
|
|
3852
5848
|
const typeLabels = {
|
|
3853
5849
|
grid: "\u8868\u683C\u89C6\u56FE",
|
|
5850
|
+
list: "\u5217\u8868\u89C6\u56FE",
|
|
3854
5851
|
kanban: "\u770B\u677F\u89C6\u56FE",
|
|
3855
5852
|
calendar: "\u65E5\u5386\u89C6\u56FE",
|
|
3856
5853
|
gallery: "\u753B\u5ECA\u89C6\u56FE",
|
|
@@ -3861,11 +5858,15 @@ var AddBaseViewCommand = {
|
|
|
3861
5858
|
const allFields = table.fieldOrder.map((fid) => table.fields[fid]).filter(Boolean);
|
|
3862
5859
|
const selectField = allFields.find((f) => f.type === "singleSelect" || f.type === "person");
|
|
3863
5860
|
const dateFields = allFields.filter((f) => f.type === "date");
|
|
5861
|
+
const selfLinkField = allFields.find(
|
|
5862
|
+
(f) => f.type === "link" && f.config?.linkedTableId === tableId
|
|
5863
|
+
);
|
|
3864
5864
|
const newView = {
|
|
3865
|
-
...params.view ?
|
|
5865
|
+
...params.view ? import_core6.Tools.deepClone(params.view) : {},
|
|
3866
5866
|
id: viewId,
|
|
3867
5867
|
name: params.name?.trim() || params.view?.name || typeLabels[params.type],
|
|
3868
5868
|
type: params.type,
|
|
5869
|
+
...params.type === "list" && !params.view?.listParentFieldId && selfLinkField ? { listParentFieldId: selfLinkField.id } : {},
|
|
3869
5870
|
...params.type === "kanban" && !params.view?.kanbanGroupFieldId && selectField ? { kanbanGroupFieldId: selectField.id } : {},
|
|
3870
5871
|
...params.type === "calendar" && !params.view?.calendarDateFieldId && dateFields[0] ? { calendarDateFieldId: dateFields[0].id } : {},
|
|
3871
5872
|
...(params.type === "gantt" || params.type === "timeline") && !params.view?.ganttStartDateFieldId ? {
|
|
@@ -3902,7 +5903,7 @@ var AddBaseViewCommand = {
|
|
|
3902
5903
|
};
|
|
3903
5904
|
var UpdateBaseViewCommand = {
|
|
3904
5905
|
id: "bases.command.update-view",
|
|
3905
|
-
type:
|
|
5906
|
+
type: import_core6.CommandType.COMMAND,
|
|
3906
5907
|
handler: (accessor, params) => {
|
|
3907
5908
|
if (!params?.patch) return false;
|
|
3908
5909
|
const ctx = resolveUnitContext(accessor, params.unitId);
|
|
@@ -3917,7 +5918,7 @@ var UpdateBaseViewCommand = {
|
|
|
3917
5918
|
const after = captureTablesState(ctx.unitId, snap);
|
|
3918
5919
|
after.tables[tableId].views[viewId] = {
|
|
3919
5920
|
...after.tables[tableId].views[viewId],
|
|
3920
|
-
...
|
|
5921
|
+
...import_core6.Tools.deepClone(params.patch),
|
|
3921
5922
|
id: viewId
|
|
3922
5923
|
};
|
|
3923
5924
|
return commitTablesTransition(ctx, before, after);
|
|
@@ -3925,7 +5926,7 @@ var UpdateBaseViewCommand = {
|
|
|
3925
5926
|
};
|
|
3926
5927
|
var DeleteBaseViewCommand = {
|
|
3927
5928
|
id: "bases.command.delete-view",
|
|
3928
|
-
type:
|
|
5929
|
+
type: import_core6.CommandType.COMMAND,
|
|
3929
5930
|
handler: (accessor, params) => {
|
|
3930
5931
|
if (!params?.viewId) return false;
|
|
3931
5932
|
const ctx = resolveUnitContext(accessor, params.unitId);
|
|
@@ -3950,7 +5951,7 @@ var DeleteBaseViewCommand = {
|
|
|
3950
5951
|
};
|
|
3951
5952
|
var SetActiveBaseViewCommand = {
|
|
3952
5953
|
id: "bases.command.set-active-view",
|
|
3953
|
-
type:
|
|
5954
|
+
type: import_core6.CommandType.COMMAND,
|
|
3954
5955
|
handler: (accessor, params) => {
|
|
3955
5956
|
if (!params?.viewId) return false;
|
|
3956
5957
|
const ctx = resolveUnitContext(accessor, params.unitId);
|
|
@@ -3986,10 +5987,10 @@ var SetActiveBaseViewCommand = {
|
|
|
3986
5987
|
}
|
|
3987
5988
|
};
|
|
3988
5989
|
function ensureDashboardsClone(snap) {
|
|
3989
|
-
const cloned =
|
|
5990
|
+
const cloned = import_core6.Tools.deepClone(snap.dashboards ?? []);
|
|
3990
5991
|
if (cloned.length === 0) {
|
|
3991
5992
|
cloned.push({
|
|
3992
|
-
id: `dash_${(0,
|
|
5993
|
+
id: `dash_${(0, import_core6.generateRandomId)(5)}`,
|
|
3993
5994
|
name: "\u6570\u636E\u5168\u666F\u4EEA\u8868\u76D8",
|
|
3994
5995
|
widgets: []
|
|
3995
5996
|
});
|
|
@@ -3998,18 +5999,18 @@ function ensureDashboardsClone(snap) {
|
|
|
3998
5999
|
}
|
|
3999
6000
|
var AddBaseDashboardWidgetCommand = {
|
|
4000
6001
|
id: "bases.command.add-dashboard-widget",
|
|
4001
|
-
type:
|
|
6002
|
+
type: import_core6.CommandType.COMMAND,
|
|
4002
6003
|
handler: (accessor, params) => {
|
|
4003
6004
|
if (!params?.widget) return false;
|
|
4004
6005
|
const ctx = resolveUnitContext(accessor, params.unitId);
|
|
4005
6006
|
if (!ctx) return false;
|
|
4006
6007
|
const snap = ctx.unit.getSnapshot();
|
|
4007
|
-
const beforeDashboards =
|
|
6008
|
+
const beforeDashboards = import_core6.Tools.deepClone(snap.dashboards ?? []);
|
|
4008
6009
|
const afterDashboards = ensureDashboardsClone(snap);
|
|
4009
6010
|
const targetDash = (params.dashboardId ? afterDashboards.find((d) => d.id === params.dashboardId) : void 0) ?? afterDashboards[0];
|
|
4010
|
-
const widgetId = params.widget.id || `wdg_${(0,
|
|
6011
|
+
const widgetId = params.widget.id || `wdg_${(0, import_core6.generateRandomId)(6)}`;
|
|
4011
6012
|
targetDash.widgets.push({
|
|
4012
|
-
...
|
|
6013
|
+
...import_core6.Tools.deepClone(params.widget),
|
|
4013
6014
|
id: widgetId
|
|
4014
6015
|
});
|
|
4015
6016
|
const beforeParams = {
|
|
@@ -4041,14 +6042,14 @@ var AddBaseDashboardWidgetCommand = {
|
|
|
4041
6042
|
};
|
|
4042
6043
|
var UpdateBaseDashboardWidgetCommand = {
|
|
4043
6044
|
id: "bases.command.update-dashboard-widget",
|
|
4044
|
-
type:
|
|
6045
|
+
type: import_core6.CommandType.COMMAND,
|
|
4045
6046
|
handler: (accessor, params) => {
|
|
4046
6047
|
if (!params?.widgetId || !params.patch) return false;
|
|
4047
6048
|
const ctx = resolveUnitContext(accessor, params.unitId);
|
|
4048
6049
|
if (!ctx) return false;
|
|
4049
6050
|
const snap = ctx.unit.getSnapshot();
|
|
4050
|
-
const beforeDashboards =
|
|
4051
|
-
const afterDashboards =
|
|
6051
|
+
const beforeDashboards = import_core6.Tools.deepClone(snap.dashboards ?? []);
|
|
6052
|
+
const afterDashboards = import_core6.Tools.deepClone(snap.dashboards ?? []);
|
|
4052
6053
|
let updated = false;
|
|
4053
6054
|
for (const dash of afterDashboards) {
|
|
4054
6055
|
if (params.dashboardId && dash.id !== params.dashboardId) continue;
|
|
@@ -4056,7 +6057,7 @@ var UpdateBaseDashboardWidgetCommand = {
|
|
|
4056
6057
|
if (idx >= 0) {
|
|
4057
6058
|
dash.widgets[idx] = {
|
|
4058
6059
|
...dash.widgets[idx],
|
|
4059
|
-
...
|
|
6060
|
+
...import_core6.Tools.deepClone(params.patch),
|
|
4060
6061
|
id: params.widgetId
|
|
4061
6062
|
};
|
|
4062
6063
|
updated = true;
|
|
@@ -4090,14 +6091,14 @@ var UpdateBaseDashboardWidgetCommand = {
|
|
|
4090
6091
|
};
|
|
4091
6092
|
var DeleteBaseDashboardWidgetCommand = {
|
|
4092
6093
|
id: "bases.command.delete-dashboard-widget",
|
|
4093
|
-
type:
|
|
6094
|
+
type: import_core6.CommandType.COMMAND,
|
|
4094
6095
|
handler: (accessor, params) => {
|
|
4095
6096
|
if (!params?.widgetId) return false;
|
|
4096
6097
|
const ctx = resolveUnitContext(accessor, params.unitId);
|
|
4097
6098
|
if (!ctx) return false;
|
|
4098
6099
|
const snap = ctx.unit.getSnapshot();
|
|
4099
|
-
const beforeDashboards =
|
|
4100
|
-
const afterDashboards =
|
|
6100
|
+
const beforeDashboards = import_core6.Tools.deepClone(snap.dashboards ?? []);
|
|
6101
|
+
const afterDashboards = import_core6.Tools.deepClone(snap.dashboards ?? []);
|
|
4101
6102
|
let removed = false;
|
|
4102
6103
|
for (const dash of afterDashboards) {
|
|
4103
6104
|
if (params.dashboardId && dash.id !== params.dashboardId) continue;
|
|
@@ -4135,13 +6136,13 @@ var DeleteBaseDashboardWidgetCommand = {
|
|
|
4135
6136
|
};
|
|
4136
6137
|
var ReplaceBaseSnapshotCommand = {
|
|
4137
6138
|
id: "bases.command.replace-snapshot",
|
|
4138
|
-
type:
|
|
6139
|
+
type: import_core6.CommandType.COMMAND,
|
|
4139
6140
|
handler: (accessor, params) => {
|
|
4140
6141
|
if (!params?.snapshot) return false;
|
|
4141
6142
|
const ctx = resolveUnitContext(accessor, params.unitId);
|
|
4142
6143
|
if (!ctx) return false;
|
|
4143
6144
|
const beforeSnapshot = ctx.unit.save();
|
|
4144
|
-
const afterSnapshot =
|
|
6145
|
+
const afterSnapshot = import_core6.Tools.deepClone(params.snapshot);
|
|
4145
6146
|
afterSnapshot.id = ctx.unitId;
|
|
4146
6147
|
const redoParams = { unitId: ctx.unitId, snapshot: afterSnapshot };
|
|
4147
6148
|
const undoParams = { unitId: ctx.unitId, snapshot: beforeSnapshot };
|
|
@@ -4156,9 +6157,126 @@ var ReplaceBaseSnapshotCommand = {
|
|
|
4156
6157
|
return Boolean(ok);
|
|
4157
6158
|
}
|
|
4158
6159
|
};
|
|
6160
|
+
var UpdateBaseDetailLayoutCommand = {
|
|
6161
|
+
id: "bases.command.update-detail-layout",
|
|
6162
|
+
type: import_core6.CommandType.COMMAND,
|
|
6163
|
+
handler: (accessor, params) => {
|
|
6164
|
+
if (!params?.detailLayout) return false;
|
|
6165
|
+
const ctx = resolveUnitContext(accessor, params.unitId);
|
|
6166
|
+
if (!ctx) return false;
|
|
6167
|
+
const snap = ctx.unit.getSnapshot();
|
|
6168
|
+
const tableId = params.tableId || snap.activeTableId;
|
|
6169
|
+
if (!snap.tables[tableId]) return false;
|
|
6170
|
+
const before = captureTablesState(ctx.unitId, snap);
|
|
6171
|
+
const after = captureTablesState(ctx.unitId, snap);
|
|
6172
|
+
const table = after.tables[tableId];
|
|
6173
|
+
if (params.viewId && table.views[params.viewId]) {
|
|
6174
|
+
table.views[params.viewId].detailLayout = import_core6.Tools.deepClone(params.detailLayout);
|
|
6175
|
+
} else {
|
|
6176
|
+
table.detailLayout = import_core6.Tools.deepClone(params.detailLayout);
|
|
6177
|
+
}
|
|
6178
|
+
return commitTablesTransition(ctx, before, after);
|
|
6179
|
+
}
|
|
6180
|
+
};
|
|
6181
|
+
var SaveBaseAutomationRuleCommand = {
|
|
6182
|
+
id: "bases.command.save-automation-rule",
|
|
6183
|
+
type: import_core6.CommandType.COMMAND,
|
|
6184
|
+
handler: (accessor, params) => {
|
|
6185
|
+
if (!params?.rule?.name?.trim() || !params.rule.tableId) return false;
|
|
6186
|
+
const ctx = resolveUnitContext(accessor, params.unitId);
|
|
6187
|
+
if (!ctx) return false;
|
|
6188
|
+
const snap = ctx.unit.getSnapshot();
|
|
6189
|
+
if (!snap.tables[params.rule.tableId]) return false;
|
|
6190
|
+
const before = captureTablesState(ctx.unitId, snap);
|
|
6191
|
+
const after = captureTablesState(ctx.unitId, snap);
|
|
6192
|
+
const list = after.automations ?? [];
|
|
6193
|
+
const ruleId = params.rule.id || `auto_${(0, import_core6.generateRandomId)(6)}`;
|
|
6194
|
+
const normalizedRule = {
|
|
6195
|
+
...import_core6.Tools.deepClone(params.rule),
|
|
6196
|
+
id: ruleId,
|
|
6197
|
+
name: params.rule.name.trim()
|
|
6198
|
+
};
|
|
6199
|
+
const existingIdx = list.findIndex((r) => r.id === ruleId);
|
|
6200
|
+
if (existingIdx >= 0) {
|
|
6201
|
+
list[existingIdx] = normalizedRule;
|
|
6202
|
+
} else {
|
|
6203
|
+
list.push(normalizedRule);
|
|
6204
|
+
}
|
|
6205
|
+
after.automations = list;
|
|
6206
|
+
const ok = commitTablesTransition(ctx, before, after);
|
|
6207
|
+
if (ok && params.onSaved) params.onSaved(ruleId);
|
|
6208
|
+
return ok;
|
|
6209
|
+
}
|
|
6210
|
+
};
|
|
6211
|
+
var DeleteBaseAutomationRuleCommand = {
|
|
6212
|
+
id: "bases.command.delete-automation-rule",
|
|
6213
|
+
type: import_core6.CommandType.COMMAND,
|
|
6214
|
+
handler: (accessor, params) => {
|
|
6215
|
+
if (!params?.ruleId) return false;
|
|
6216
|
+
const ctx = resolveUnitContext(accessor, params.unitId);
|
|
6217
|
+
if (!ctx) return false;
|
|
6218
|
+
const snap = ctx.unit.getSnapshot();
|
|
6219
|
+
const curList = snap.automations ?? [];
|
|
6220
|
+
if (!curList.some((r) => r.id === params.ruleId)) return false;
|
|
6221
|
+
const before = captureTablesState(ctx.unitId, snap);
|
|
6222
|
+
const after = captureTablesState(ctx.unitId, snap);
|
|
6223
|
+
after.automations = (after.automations ?? []).filter((r) => r.id !== params.ruleId);
|
|
6224
|
+
return commitTablesTransition(ctx, before, after);
|
|
6225
|
+
}
|
|
6226
|
+
};
|
|
6227
|
+
var ToggleBaseAutomationRuleCommand = {
|
|
6228
|
+
id: "bases.command.toggle-automation-rule",
|
|
6229
|
+
type: import_core6.CommandType.COMMAND,
|
|
6230
|
+
handler: (accessor, params) => {
|
|
6231
|
+
if (!params?.ruleId) return false;
|
|
6232
|
+
const ctx = resolveUnitContext(accessor, params.unitId);
|
|
6233
|
+
if (!ctx) return false;
|
|
6234
|
+
const snap = ctx.unit.getSnapshot();
|
|
6235
|
+
const curList = snap.automations ?? [];
|
|
6236
|
+
if (!curList.some((r) => r.id === params.ruleId)) return false;
|
|
6237
|
+
const before = captureTablesState(ctx.unitId, snap);
|
|
6238
|
+
const after = captureTablesState(ctx.unitId, snap);
|
|
6239
|
+
after.automations = (after.automations ?? []).map(
|
|
6240
|
+
(r) => r.id === params.ruleId ? { ...r, enabled: params.enabled !== void 0 ? params.enabled : !r.enabled } : r
|
|
6241
|
+
);
|
|
6242
|
+
return commitTablesTransition(ctx, before, after);
|
|
6243
|
+
}
|
|
6244
|
+
};
|
|
6245
|
+
var TriggerBaseAutomationCommand = {
|
|
6246
|
+
id: "bases.command.trigger-automation",
|
|
6247
|
+
type: import_core6.CommandType.COMMAND,
|
|
6248
|
+
handler: (accessor, params) => {
|
|
6249
|
+
const ctx = resolveUnitContext(accessor, params?.unitId);
|
|
6250
|
+
if (!ctx) return false;
|
|
6251
|
+
const snap = ctx.unit.getSnapshot();
|
|
6252
|
+
const targetRule = params?.ruleId ? (snap.automations ?? []).find((r) => r.id === params.ruleId) : void 0;
|
|
6253
|
+
if (params?.ruleId && !targetRule) return false;
|
|
6254
|
+
const tableId = params?.tableId || targetRule?.tableId || snap.activeTableId;
|
|
6255
|
+
const table = snap.tables[tableId];
|
|
6256
|
+
if (!table) return false;
|
|
6257
|
+
const recordId = params?.recordId ?? table.recordOrder[0];
|
|
6258
|
+
const before = captureTablesState(ctx.unitId, snap);
|
|
6259
|
+
const after = captureTablesState(ctx.unitId, snap);
|
|
6260
|
+
const logs = applyAutomationsToTransitionState(
|
|
6261
|
+
snap,
|
|
6262
|
+
after,
|
|
6263
|
+
{
|
|
6264
|
+
trigger: params?.trigger ?? "manual",
|
|
6265
|
+
ruleId: params?.ruleId,
|
|
6266
|
+
tableId,
|
|
6267
|
+
recordId,
|
|
6268
|
+
webhookPayload: params?.webhookPayload
|
|
6269
|
+
},
|
|
6270
|
+
resolveWebhookExecutor(accessor, params?.webhookExecutor)
|
|
6271
|
+
);
|
|
6272
|
+
const ok = commitTablesTransition(ctx, before, after);
|
|
6273
|
+
if (ok && params?.onExecuted) params.onExecuted(logs);
|
|
6274
|
+
return ok;
|
|
6275
|
+
}
|
|
6276
|
+
};
|
|
4159
6277
|
|
|
4160
6278
|
// src/exporter/csv-codec.ts
|
|
4161
|
-
var
|
|
6279
|
+
var import_core7 = require("@univerjs/core");
|
|
4162
6280
|
function parseDelimitedText(text, delimiter = ",") {
|
|
4163
6281
|
const clean = text.startsWith("\uFEFF") ? text.slice(1) : text;
|
|
4164
6282
|
if (!clean.trim()) return [];
|
|
@@ -4309,7 +6427,7 @@ function exportBaseTableToCsv(unitData, tableId, options) {
|
|
|
4309
6427
|
return options?.includeBom ? `\uFEFF${body}` : body;
|
|
4310
6428
|
}
|
|
4311
6429
|
function buildBaseTableFromGridRows(gridRows, options) {
|
|
4312
|
-
const tableId = options?.tableId || `tbl_${(0,
|
|
6430
|
+
const tableId = options?.tableId || `tbl_${(0, import_core7.generateRandomId)(6)}`;
|
|
4313
6431
|
const tableName = options?.tableName?.trim() || "\u5BFC\u5165\u6570\u636E\u8868";
|
|
4314
6432
|
const headerRow = gridRows[0] ?? ["\u6807\u9898"];
|
|
4315
6433
|
const dataRows = gridRows.slice(1).filter((r) => r.some((c) => c.trim() !== ""));
|
|
@@ -4331,7 +6449,7 @@ function buildBaseTableFromGridRows(gridRows, options) {
|
|
|
4331
6449
|
usedNames.add(uniqueName);
|
|
4332
6450
|
const samples = dataRows.map((r) => r[c] ?? "");
|
|
4333
6451
|
const inferred = c === 0 ? { type: "text" } : inferFieldFromColumnSamples(samples);
|
|
4334
|
-
const fid = `fld_${c + 1}_${(0,
|
|
6452
|
+
const fid = `fld_${c + 1}_${(0, import_core7.generateRandomId)(4)}`;
|
|
4335
6453
|
fields[fid] = {
|
|
4336
6454
|
id: fid,
|
|
4337
6455
|
name: uniqueName,
|
|
@@ -4346,7 +6464,7 @@ function buildBaseTableFromGridRows(gridRows, options) {
|
|
|
4346
6464
|
const records = {};
|
|
4347
6465
|
const recordOrder = [];
|
|
4348
6466
|
dataRows.forEach((rowVals, idx) => {
|
|
4349
|
-
const rid = `rec_${idx + 1}_${(0,
|
|
6467
|
+
const rid = `rec_${idx + 1}_${(0, import_core7.generateRandomId)(4)}`;
|
|
4350
6468
|
const cells = {};
|
|
4351
6469
|
fieldOrder.forEach((fid, colIdx) => {
|
|
4352
6470
|
const f = fields[fid];
|
|
@@ -4363,14 +6481,14 @@ function buildBaseTableFromGridRows(gridRows, options) {
|
|
|
4363
6481
|
};
|
|
4364
6482
|
recordOrder.push(rid);
|
|
4365
6483
|
});
|
|
4366
|
-
const gridViewId = `vw_grid_${(0,
|
|
6484
|
+
const gridViewId = `vw_grid_${(0, import_core7.generateRandomId)(4)}`;
|
|
4367
6485
|
const views = {
|
|
4368
6486
|
[gridViewId]: { id: gridViewId, name: "\u8868\u683C\u89C6\u56FE", type: "grid" }
|
|
4369
6487
|
};
|
|
4370
6488
|
const viewOrder = [gridViewId];
|
|
4371
6489
|
const selectField = Object.values(fields).find((f) => f.type === "singleSelect");
|
|
4372
6490
|
if (selectField) {
|
|
4373
|
-
const kanbanViewId = `vw_kanban_${(0,
|
|
6491
|
+
const kanbanViewId = `vw_kanban_${(0, import_core7.generateRandomId)(4)}`;
|
|
4374
6492
|
views[kanbanViewId] = {
|
|
4375
6493
|
id: kanbanViewId,
|
|
4376
6494
|
name: "\u770B\u677F\u89C6\u56FE",
|
|
@@ -4403,7 +6521,7 @@ function importCsvToBaseTable(csvText, options) {
|
|
|
4403
6521
|
}
|
|
4404
6522
|
|
|
4405
6523
|
// src/exporter/xlsx-codec.ts
|
|
4406
|
-
var
|
|
6524
|
+
var import_core8 = require("@univerjs/core");
|
|
4407
6525
|
var UTF8_ENCODER = new TextEncoder();
|
|
4408
6526
|
var UTF8_DECODER = new TextDecoder("utf-8");
|
|
4409
6527
|
var CRC32_TABLE = (() => {
|
|
@@ -5082,7 +7200,7 @@ function importXlsxBytesToBase(bytes, options) {
|
|
|
5082
7200
|
continue;
|
|
5083
7201
|
}
|
|
5084
7202
|
const grid = parseWorksheetGrid(wsXml, sharedStrings, dateStyleIds);
|
|
5085
|
-
const tid = `tbl_${sheetIdx + 1}_${(0,
|
|
7203
|
+
const tid = `tbl_${sheetIdx + 1}_${(0, import_core8.generateRandomId)(4)}`;
|
|
5086
7204
|
const table = buildBaseTableFromGridRows(grid.length ? grid : [["\u6807\u9898"]], {
|
|
5087
7205
|
tableId: tid,
|
|
5088
7206
|
tableName: sheetName
|
|
@@ -5093,7 +7211,7 @@ function importXlsxBytesToBase(bytes, options) {
|
|
|
5093
7211
|
}
|
|
5094
7212
|
if (tableOrder.length === 0) {
|
|
5095
7213
|
const fallbackTbl = buildBaseTableFromGridRows([["\u6807\u9898"]], {
|
|
5096
|
-
tableId: `tbl_1_${(0,
|
|
7214
|
+
tableId: `tbl_1_${(0, import_core8.generateRandomId)(4)}`,
|
|
5097
7215
|
tableName: "Sheet1"
|
|
5098
7216
|
});
|
|
5099
7217
|
tables[fallbackTbl.id] = fallbackTbl;
|
|
@@ -5101,7 +7219,7 @@ function importXlsxBytesToBase(bytes, options) {
|
|
|
5101
7219
|
}
|
|
5102
7220
|
const firstTbl = tables[tableOrder[0]];
|
|
5103
7221
|
return createDefaultBaseUnitData({
|
|
5104
|
-
id: options?.id || `base-${(0,
|
|
7222
|
+
id: options?.id || `base-${(0, import_core8.generateRandomId)(6)}`,
|
|
5105
7223
|
name: options?.name || "\u5BFC\u5165\u591A\u7EF4\u6570\u636E\u7C3F",
|
|
5106
7224
|
activeTableId: firstTbl.id,
|
|
5107
7225
|
activeViewId: firstTbl.activeViewId,
|
|
@@ -5111,10 +7229,10 @@ function importXlsxBytesToBase(bytes, options) {
|
|
|
5111
7229
|
}
|
|
5112
7230
|
|
|
5113
7231
|
// src/services/base-io.service.ts
|
|
5114
|
-
var
|
|
7232
|
+
var import_core9 = require("@univerjs/core");
|
|
5115
7233
|
var import_rxjs2 = require("rxjs");
|
|
5116
|
-
var IBaseIoService = (0,
|
|
5117
|
-
var BaseIoService = class extends
|
|
7234
|
+
var IBaseIoService = (0, import_core9.createIdentifier)("univer.bases.io.service");
|
|
7235
|
+
var BaseIoService = class extends import_core9.Disposable {
|
|
5118
7236
|
constructor() {
|
|
5119
7237
|
super(...arguments);
|
|
5120
7238
|
this._adapter = { name: "builtin-pure-ts" };
|
|
@@ -5155,7 +7273,7 @@ var BaseIoService = class extends import_core7.Disposable {
|
|
|
5155
7273
|
delimiter
|
|
5156
7274
|
});
|
|
5157
7275
|
return createDefaultBaseUnitData({
|
|
5158
|
-
id: options?.id || `base-${(0,
|
|
7276
|
+
id: options?.id || `base-${(0, import_core9.generateRandomId)(6)}`,
|
|
5159
7277
|
name: options?.name || table.name,
|
|
5160
7278
|
activeTableId: table.id,
|
|
5161
7279
|
activeViewId: table.activeViewId,
|
|
@@ -5193,12 +7311,12 @@ var BaseIoService = class extends import_core7.Disposable {
|
|
|
5193
7311
|
};
|
|
5194
7312
|
|
|
5195
7313
|
// src/services/base-selection.service.ts
|
|
5196
|
-
var
|
|
7314
|
+
var import_core10 = require("@univerjs/core");
|
|
5197
7315
|
var import_rxjs3 = require("rxjs");
|
|
5198
|
-
var IBaseSelectionService = (0,
|
|
7316
|
+
var IBaseSelectionService = (0, import_core10.createIdentifier)(
|
|
5199
7317
|
"univer.bases.selection.service"
|
|
5200
7318
|
);
|
|
5201
|
-
var BaseSelectionService = class extends
|
|
7319
|
+
var BaseSelectionService = class extends import_core10.Disposable {
|
|
5202
7320
|
constructor() {
|
|
5203
7321
|
super(...arguments);
|
|
5204
7322
|
this._state$ = new import_rxjs3.BehaviorSubject({
|
|
@@ -5207,6 +7325,7 @@ var BaseSelectionService = class extends import_core8.Disposable {
|
|
|
5207
7325
|
searchQuery: "",
|
|
5208
7326
|
activeModal: null,
|
|
5209
7327
|
linkPickerSession: null,
|
|
7328
|
+
editingAutomationRuleId: null,
|
|
5210
7329
|
lastToastMessage: null
|
|
5211
7330
|
});
|
|
5212
7331
|
this.state$ = this._state$.asObservable();
|
|
@@ -5248,14 +7367,24 @@ var BaseSelectionService = class extends import_core8.Disposable {
|
|
|
5248
7367
|
this._state$.next({
|
|
5249
7368
|
...this.getState(),
|
|
5250
7369
|
activeModal: modal,
|
|
5251
|
-
...modal !== "linkPicker" ? { linkPickerSession: null } : {}
|
|
7370
|
+
...modal !== "linkPicker" ? { linkPickerSession: null } : {},
|
|
7371
|
+
...modal !== "automationBuilder" ? { editingAutomationRuleId: null } : {}
|
|
5252
7372
|
});
|
|
5253
7373
|
}
|
|
5254
7374
|
openLinkPicker(session) {
|
|
5255
7375
|
this._state$.next({
|
|
5256
7376
|
...this.getState(),
|
|
5257
7377
|
activeModal: session ? "linkPicker" : null,
|
|
5258
|
-
linkPickerSession: session
|
|
7378
|
+
linkPickerSession: session,
|
|
7379
|
+
editingAutomationRuleId: null
|
|
7380
|
+
});
|
|
7381
|
+
}
|
|
7382
|
+
openAutomationBuilder(ruleId) {
|
|
7383
|
+
this._state$.next({
|
|
7384
|
+
...this.getState(),
|
|
7385
|
+
activeModal: "automationBuilder",
|
|
7386
|
+
linkPickerSession: null,
|
|
7387
|
+
editingAutomationRuleId: ruleId ?? null
|
|
5259
7388
|
});
|
|
5260
7389
|
}
|
|
5261
7390
|
notifyToast(message) {
|
|
@@ -5288,9 +7417,9 @@ function buildLinearBarcodeBars(value) {
|
|
|
5288
7417
|
}
|
|
5289
7418
|
|
|
5290
7419
|
// src/plugin.ts
|
|
5291
|
-
var
|
|
7420
|
+
var import_core11 = require("@univerjs/core");
|
|
5292
7421
|
var UNIVER_BASES_PLUGIN_NAME = "UNIVER_BASES_PLUGIN";
|
|
5293
|
-
var UniverBasesPlugin = class extends
|
|
7422
|
+
var UniverBasesPlugin = class extends import_core11.Plugin {
|
|
5294
7423
|
constructor(_config = {}, _injector, _univerInstanceService, _commandService) {
|
|
5295
7424
|
super();
|
|
5296
7425
|
this._config = _config;
|
|
@@ -5307,11 +7436,11 @@ var UniverBasesPlugin = class extends import_core9.Plugin {
|
|
|
5307
7436
|
onStarting() {
|
|
5308
7437
|
this.disposeWithMe(
|
|
5309
7438
|
this._univerInstanceService.registerCtorForType(
|
|
5310
|
-
|
|
7439
|
+
import_core11.UniverInstanceType.UNIVER_BASE,
|
|
5311
7440
|
BaseUnitModel
|
|
5312
7441
|
)
|
|
5313
7442
|
);
|
|
5314
|
-
const focusSub = this._univerInstanceService.getCurrentTypeOfUnit$(
|
|
7443
|
+
const focusSub = this._univerInstanceService.getCurrentTypeOfUnit$(import_core11.UniverInstanceType.UNIVER_BASE).subscribe((unit) => {
|
|
5315
7444
|
if (unit && this._univerInstanceService.getFocusedUnit()?.getUnitId() !== unit.getUnitId()) {
|
|
5316
7445
|
this._univerInstanceService.focusUnit(unit.getUnitId());
|
|
5317
7446
|
}
|
|
@@ -5319,12 +7448,16 @@ var UniverBasesPlugin = class extends import_core9.Plugin {
|
|
|
5319
7448
|
this.disposeWithMe({ dispose: () => focusSub.unsubscribe() });
|
|
5320
7449
|
const dependencies = [
|
|
5321
7450
|
[IBaseSelectionService, { useClass: BaseSelectionService }],
|
|
5322
|
-
[IBaseIoService, { useClass: BaseIoService }]
|
|
7451
|
+
[IBaseIoService, { useClass: BaseIoService }],
|
|
7452
|
+
[IBaseAutomationService, { useClass: BaseAutomationService }]
|
|
5323
7453
|
];
|
|
5324
7454
|
dependencies.forEach((dep) => this._injector.add(dep));
|
|
5325
7455
|
if (this._config.io) {
|
|
5326
7456
|
this._injector.get(IBaseIoService).setAdapter(this._config.io);
|
|
5327
7457
|
}
|
|
7458
|
+
if (this._config.webhookExecutor) {
|
|
7459
|
+
this._injector.get(IBaseAutomationService).setWebhookExecutor(this._config.webhookExecutor);
|
|
7460
|
+
}
|
|
5328
7461
|
const commands = [
|
|
5329
7462
|
// Mutations
|
|
5330
7463
|
ApplyBaseTablesStateMutation,
|
|
@@ -5353,7 +7486,12 @@ var UniverBasesPlugin = class extends import_core9.Plugin {
|
|
|
5353
7486
|
AddBaseDashboardWidgetCommand,
|
|
5354
7487
|
UpdateBaseDashboardWidgetCommand,
|
|
5355
7488
|
DeleteBaseDashboardWidgetCommand,
|
|
5356
|
-
ReplaceBaseSnapshotCommand
|
|
7489
|
+
ReplaceBaseSnapshotCommand,
|
|
7490
|
+
UpdateBaseDetailLayoutCommand,
|
|
7491
|
+
SaveBaseAutomationRuleCommand,
|
|
7492
|
+
DeleteBaseAutomationRuleCommand,
|
|
7493
|
+
ToggleBaseAutomationRuleCommand,
|
|
7494
|
+
TriggerBaseAutomationCommand
|
|
5357
7495
|
];
|
|
5358
7496
|
for (const cmd of commands) {
|
|
5359
7497
|
this.disposeWithMe(this._commandService.registerCommand(cmd));
|
|
@@ -5361,13 +7499,13 @@ var UniverBasesPlugin = class extends import_core9.Plugin {
|
|
|
5361
7499
|
}
|
|
5362
7500
|
};
|
|
5363
7501
|
UniverBasesPlugin = __decorateClass([
|
|
5364
|
-
__decorateParam(1, (0,
|
|
5365
|
-
__decorateParam(2,
|
|
5366
|
-
__decorateParam(3,
|
|
7502
|
+
__decorateParam(1, (0, import_core11.Inject)(import_core11.Injector)),
|
|
7503
|
+
__decorateParam(2, import_core11.IUniverInstanceService),
|
|
7504
|
+
__decorateParam(3, import_core11.ICommandService)
|
|
5367
7505
|
], UniverBasesPlugin);
|
|
5368
7506
|
|
|
5369
7507
|
// src/index.ts
|
|
5370
|
-
var VERSION = "1.6.
|
|
7508
|
+
var VERSION = "1.6.219";
|
|
5371
7509
|
// Annotate the CommonJS export names for ESM import in node:
|
|
5372
7510
|
0 && (module.exports = {
|
|
5373
7511
|
ALL_BASE_FIELD_TYPES,
|
|
@@ -5380,46 +7518,59 @@ var VERSION = "1.6.218";
|
|
|
5380
7518
|
ApplyBaseTablesStateMutation,
|
|
5381
7519
|
BASE_ROW_COLOR_PALETTE,
|
|
5382
7520
|
BASE_SELECT_PALETTE,
|
|
7521
|
+
BaseAutomationService,
|
|
5383
7522
|
BaseIoService,
|
|
5384
7523
|
BaseSelectionService,
|
|
5385
7524
|
BaseUnitModel,
|
|
5386
7525
|
BatchInsertBaseRecordsCommand,
|
|
5387
7526
|
COMPUTED_BASE_FIELD_TYPES,
|
|
7527
|
+
DeleteBaseAutomationRuleCommand,
|
|
5388
7528
|
DeleteBaseDashboardWidgetCommand,
|
|
5389
7529
|
DeleteBaseFieldCommand,
|
|
5390
7530
|
DeleteBaseRecordsCommand,
|
|
5391
7531
|
DeleteBaseTableCommand,
|
|
5392
7532
|
DeleteBaseViewCommand,
|
|
7533
|
+
IBaseAutomationService,
|
|
5393
7534
|
IBaseIoService,
|
|
5394
7535
|
IBaseSelectionService,
|
|
7536
|
+
MAX_AUTOMATION_RECURSION_DEPTH,
|
|
5395
7537
|
ReorderBaseFieldsCommand,
|
|
5396
7538
|
ReplaceBaseSnapshotCommand,
|
|
5397
7539
|
ReplaceBaseSnapshotMutation,
|
|
7540
|
+
SaveBaseAutomationRuleCommand,
|
|
5398
7541
|
SetActiveBaseTableCommand,
|
|
5399
7542
|
SetActiveBaseViewCommand,
|
|
5400
7543
|
SetBaseActiveNavigationMutation,
|
|
5401
7544
|
SetBaseWorkspaceModeCommand,
|
|
5402
7545
|
SubmitBaseFormResponseCommand,
|
|
7546
|
+
ToggleBaseAutomationRuleCommand,
|
|
7547
|
+
TriggerBaseAutomationCommand,
|
|
5403
7548
|
UNIVER_BASES_PLUGIN_NAME,
|
|
5404
7549
|
UNIVER_BASE_SCHEMA,
|
|
5405
7550
|
UNIVER_BASE_SCHEMA_VERSION,
|
|
5406
7551
|
UniverBasesPlugin,
|
|
5407
7552
|
UpdateBaseDashboardWidgetCommand,
|
|
7553
|
+
UpdateBaseDetailLayoutCommand,
|
|
5408
7554
|
UpdateBaseFieldCommand,
|
|
5409
7555
|
UpdateBaseRecordCommand,
|
|
5410
7556
|
UpdateBaseTableCommand,
|
|
5411
7557
|
UpdateBaseViewCommand,
|
|
5412
7558
|
VERSION,
|
|
7559
|
+
applyBaseRowReadScope,
|
|
5413
7560
|
buildBaseTableFromGridRows,
|
|
5414
7561
|
buildLinearBarcodeBars,
|
|
5415
7562
|
coerceImportedCellValue,
|
|
5416
7563
|
computeBaseDashboardWidget,
|
|
5417
7564
|
computeBaseGanttTasks,
|
|
7565
|
+
computeBaseHierarchicalList,
|
|
5418
7566
|
computeFieldSummary,
|
|
5419
7567
|
createDefaultBaseUnitData,
|
|
5420
7568
|
createSampleCrmBaseData,
|
|
5421
7569
|
createSampleProjectBaseData,
|
|
7570
|
+
doesAutomationTriggerMatch,
|
|
7571
|
+
evaluateAutomationConditions,
|
|
5422
7572
|
evaluateBaseFormula,
|
|
7573
|
+
executeBaseAutomations,
|
|
5423
7574
|
exportBaseTableToCsv,
|
|
5424
7575
|
exportBaseToXlsxBytes,
|
|
5425
7576
|
formatBaseCellValue,
|
|
@@ -5427,16 +7578,27 @@ var VERSION = "1.6.218";
|
|
|
5427
7578
|
importCsvToBaseTable,
|
|
5428
7579
|
importXlsxBytesToBase,
|
|
5429
7580
|
inferFieldFromColumnSamples,
|
|
7581
|
+
interpolateAutomationCellValue,
|
|
7582
|
+
interpolateAutomationString,
|
|
5430
7583
|
isCellValueEmpty,
|
|
5431
7584
|
isComputedBaseField,
|
|
7585
|
+
isFormFieldRequired,
|
|
7586
|
+
isFormFieldVisible,
|
|
5432
7587
|
matchesBaseFilterRule,
|
|
7588
|
+
mergeMultipleSourcesIntoBaseTable,
|
|
7589
|
+
normalizeAutomationSteps,
|
|
5433
7590
|
normalizeLinkIds,
|
|
5434
7591
|
parseDelimitedText,
|
|
5435
7592
|
queryBaseTableView,
|
|
5436
7593
|
resolveBaseCellValue,
|
|
7594
|
+
resolveBaseDetailLayout,
|
|
5437
7595
|
resolveLinkedRecordItems,
|
|
5438
7596
|
roundToPrecision,
|
|
7597
|
+
runBaseAutomationsInPlaceSync,
|
|
5439
7598
|
serializeDelimitedText,
|
|
7599
|
+
syncBaseTables,
|
|
7600
|
+
syncOneWayBaseTable,
|
|
7601
|
+
syncTwoWayBaseTables,
|
|
5440
7602
|
toNumericValue
|
|
5441
7603
|
});
|
|
5442
7604
|
//# sourceMappingURL=index.js.map
|