@4399ywkf/editor 0.15.0 → 0.16.0
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/dist/{DocView-4MOGGX4Z.js → DocView-7QBKHBU4.js} +4 -4
- package/dist/{DocView-4MOGGX4Z.js.map → DocView-7QBKHBU4.js.map} +1 -1
- package/dist/base-model.d.ts +28 -396
- package/dist/base-model.js +1 -1
- package/dist/base.d.ts +70 -2
- package/dist/base.js +11443 -10234
- package/dist/base.js.map +1 -1
- package/dist/{chunk-3KW24N3T.js → chunk-F4LWFKIA.js} +678 -3
- package/dist/chunk-F4LWFKIA.js.map +1 -0
- package/dist/{chunk-3BRBH7MU.js → chunk-S57WFYWY.js} +417 -236
- package/dist/chunk-S57WFYWY.js.map +1 -0
- package/dist/{chunk-KZEURCGO.js → chunk-X2JCKZP2.js} +5 -4
- package/dist/chunk-X2JCKZP2.js.map +1 -0
- package/dist/doc-standalone.js +677 -1
- package/dist/doc-standalone.js.map +1 -1
- package/dist/doc.js +3 -3
- package/dist/form-DqUOPVKI.d.ts +475 -0
- package/dist/index.js +1 -1
- package/dist/style.css +2 -2
- package/package.json +1 -1
- package/dist/chunk-3BRBH7MU.js.map +0 -1
- package/dist/chunk-3KW24N3T.js.map +0 -1
- package/dist/chunk-KZEURCGO.js.map +0 -1
|
@@ -46,6 +46,7 @@ var COMPUTED_TYPES = /* @__PURE__ */ new Set([
|
|
|
46
46
|
FieldType.AutoNumber
|
|
47
47
|
]);
|
|
48
48
|
var isComputedFieldType = (type) => COMPUTED_TYPES.has(type);
|
|
49
|
+
var isKanbanGroupFieldType = (type) => type === FieldType.SingleSelect || type === FieldType.User;
|
|
49
50
|
function fieldUiType(field) {
|
|
50
51
|
return field.type === FieldType.Number && field.uiType ? field.uiType : FIELD_UI_TYPE[field.type];
|
|
51
52
|
}
|
|
@@ -625,6 +626,8 @@ var tableTypes = (doc, tableId) => [
|
|
|
625
626
|
var isBaseInitialized = (doc) => baseRoot(doc).get("schemaVersion") !== void 0;
|
|
626
627
|
|
|
627
628
|
// src/base-model/read.ts
|
|
629
|
+
var BASE_VIEW_TYPES = ["grid", "kanban", "form"];
|
|
630
|
+
var isBaseViewType = (value) => typeof value === "string" && BASE_VIEW_TYPES.includes(value);
|
|
628
631
|
var unique = (ids) => [...new Set(ids)];
|
|
629
632
|
function readTables(doc) {
|
|
630
633
|
const tables = tablesMap(doc);
|
|
@@ -685,10 +688,18 @@ function readViewMap(doc, tableId, viewId, view) {
|
|
|
685
688
|
]).filter((id) => fields.has(id));
|
|
686
689
|
const colInfos = view.get("colInfos")?.toJSON() ?? {};
|
|
687
690
|
const level = Number(view.get("rowHeightLevel"));
|
|
691
|
+
const type = view.get("type");
|
|
692
|
+
const fieldRef = (key, accepts) => {
|
|
693
|
+
const id = view.get(key);
|
|
694
|
+
if (typeof id !== "string") return null;
|
|
695
|
+
const map = fields.get(id);
|
|
696
|
+
const field = map ? readFieldMap(id, map) : null;
|
|
697
|
+
return field && accepts(field.type) ? id : null;
|
|
698
|
+
};
|
|
688
699
|
return {
|
|
689
700
|
id: viewId,
|
|
690
701
|
name: String(view.get("name") ?? ""),
|
|
691
|
-
type: "grid",
|
|
702
|
+
type: isBaseViewType(type) ? type : "grid",
|
|
692
703
|
fieldIds,
|
|
693
704
|
colInfos: Object.fromEntries(
|
|
694
705
|
Object.entries(colInfos).filter(([id]) => fields.has(id))
|
|
@@ -697,7 +708,12 @@ function readViewMap(doc, tableId, viewId, view) {
|
|
|
697
708
|
rowHeightLevel: level >= 1 && level <= 4 ? level : 1,
|
|
698
709
|
filter: view.get("filter") ?? null,
|
|
699
710
|
sort: view.get("sort") ?? [],
|
|
700
|
-
group: view.get("group") ?? []
|
|
711
|
+
group: view.get("group") ?? [],
|
|
712
|
+
groupFieldId: fieldRef("groupFieldId", isKanbanGroupFieldType),
|
|
713
|
+
coverFieldId: fieldRef("coverFieldId", (type2) => type2 === FieldType.Attachment),
|
|
714
|
+
showFieldName: view.get("showFieldName") === true,
|
|
715
|
+
formTitle: String(view.get("formTitle") ?? ""),
|
|
716
|
+
formDescription: String(view.get("formDescription") ?? "")
|
|
701
717
|
};
|
|
702
718
|
}
|
|
703
719
|
function readViews(doc, tableId) {
|
|
@@ -746,6 +762,11 @@ function setBaseActor(doc, actor) {
|
|
|
746
762
|
if (actor) actors.set(doc, { ...actor });
|
|
747
763
|
else actors.delete(doc);
|
|
748
764
|
}
|
|
765
|
+
var DEFAULT_VIEW_NAMES = {
|
|
766
|
+
grid: "表格",
|
|
767
|
+
kanban: "看板",
|
|
768
|
+
form: "表单"
|
|
769
|
+
};
|
|
749
770
|
function inTransaction(doc, origin, fn) {
|
|
750
771
|
let result;
|
|
751
772
|
doc.transact(() => {
|
|
@@ -791,12 +812,12 @@ function writeField(doc, tableId, input) {
|
|
|
791
812
|
return id;
|
|
792
813
|
}
|
|
793
814
|
var storedUiType = (type, uiType) => type === FieldType.Number && uiType && uiType !== "Number" ? uiType : void 0;
|
|
794
|
-
function writeView(doc, tableId, name, fieldIds, index) {
|
|
815
|
+
function writeView(doc, tableId, name, fieldIds, index, type = "grid") {
|
|
795
816
|
const id = newBaseId("vew");
|
|
796
817
|
const view = new Y.Map();
|
|
797
818
|
viewsMap(doc, tableId).set(id, view);
|
|
798
819
|
view.set("name", name);
|
|
799
|
-
view.set("type",
|
|
820
|
+
view.set("type", type);
|
|
800
821
|
const fields = new Y.Array();
|
|
801
822
|
view.set("fields", fields);
|
|
802
823
|
fields.push(fieldIds);
|
|
@@ -1284,13 +1305,59 @@ function moveRecords(doc, tableId, recordIds, index, origin) {
|
|
|
1284
1305
|
});
|
|
1285
1306
|
});
|
|
1286
1307
|
}
|
|
1287
|
-
function
|
|
1288
|
-
|
|
1308
|
+
function writeViewExtras(view, source) {
|
|
1309
|
+
if (source.groupFieldId) view.set("groupFieldId", source.groupFieldId);
|
|
1310
|
+
if (source.coverFieldId) view.set("coverFieldId", source.coverFieldId);
|
|
1311
|
+
if (source.showFieldName) view.set("showFieldName", true);
|
|
1312
|
+
if (source.formTitle) view.set("formTitle", source.formTitle);
|
|
1313
|
+
if (source.formDescription) view.set("formDescription", source.formDescription);
|
|
1314
|
+
}
|
|
1315
|
+
function addView(doc, tableId, input, origin) {
|
|
1316
|
+
const { name, type = "grid" } = typeof input === "string" ? { name: input } : input ?? {};
|
|
1317
|
+
const fields = readFields(doc, tableId);
|
|
1289
1318
|
const viewName = name?.trim() || uniqueName(
|
|
1290
1319
|
readViews(doc, tableId).map((view) => view.name),
|
|
1291
|
-
|
|
1320
|
+
DEFAULT_VIEW_NAMES[type]
|
|
1292
1321
|
);
|
|
1293
|
-
return inTransaction(doc, origin, () =>
|
|
1322
|
+
return inTransaction(doc, origin, () => {
|
|
1323
|
+
const id = writeView(
|
|
1324
|
+
doc,
|
|
1325
|
+
tableId,
|
|
1326
|
+
viewName,
|
|
1327
|
+
fields.map((field) => field.id),
|
|
1328
|
+
void 0,
|
|
1329
|
+
type
|
|
1330
|
+
);
|
|
1331
|
+
const view = viewsMap(doc, tableId).get(id);
|
|
1332
|
+
const colInfos = view.get("colInfos");
|
|
1333
|
+
if (type === "kanban") {
|
|
1334
|
+
const group = fields.find((field) => field.type === FieldType.SingleSelect) ?? fields.find((field) => field.type === FieldType.User);
|
|
1335
|
+
if (group) {
|
|
1336
|
+
view.set("groupFieldId", group.id);
|
|
1337
|
+
colInfos.set(group.id, { hidden: true });
|
|
1338
|
+
}
|
|
1339
|
+
} else if (type === "form") {
|
|
1340
|
+
for (const field of fields) {
|
|
1341
|
+
if (isComputedFieldType(field.type)) colInfos.set(field.id, { hidden: true });
|
|
1342
|
+
}
|
|
1343
|
+
view.set("formTitle", readTable(doc, tableId)?.name ?? "");
|
|
1344
|
+
}
|
|
1345
|
+
return id;
|
|
1346
|
+
});
|
|
1347
|
+
}
|
|
1348
|
+
function setKanbanGroupField(doc, tableId, viewId, fieldId, origin) {
|
|
1349
|
+
const view = viewsMap(doc, tableId).get(viewId);
|
|
1350
|
+
if (!view) throw new BaseModelError(`视图不存在:${viewId}`);
|
|
1351
|
+
const map = fieldsMap(doc, tableId).get(fieldId);
|
|
1352
|
+
const field = map ? readFieldMap(fieldId, map) : null;
|
|
1353
|
+
if (!field || !isKanbanGroupFieldType(field.type)) {
|
|
1354
|
+
throw new BaseModelError("只能按单选或人员字段分组");
|
|
1355
|
+
}
|
|
1356
|
+
const colInfos = view.get("colInfos");
|
|
1357
|
+
inTransaction(doc, origin, () => {
|
|
1358
|
+
view.set("groupFieldId", fieldId);
|
|
1359
|
+
colInfos?.set(fieldId, { ...colInfos.get(fieldId), hidden: true });
|
|
1360
|
+
});
|
|
1294
1361
|
}
|
|
1295
1362
|
function deleteView(doc, tableId, viewId, origin) {
|
|
1296
1363
|
const views = viewsMap(doc, tableId);
|
|
@@ -1316,7 +1383,8 @@ function duplicateView(doc, tableId, viewId, origin) {
|
|
|
1316
1383
|
source.name
|
|
1317
1384
|
),
|
|
1318
1385
|
source.fieldIds,
|
|
1319
|
-
at === -1 ? void 0 : at + 1
|
|
1386
|
+
at === -1 ? void 0 : at + 1,
|
|
1387
|
+
source.type
|
|
1320
1388
|
);
|
|
1321
1389
|
const view = viewsMap(doc, tableId).get(id);
|
|
1322
1390
|
const colInfos = view.get("colInfos");
|
|
@@ -1326,6 +1394,7 @@ function duplicateView(doc, tableId, viewId, origin) {
|
|
|
1326
1394
|
if (source.filter) view.set("filter", source.filter);
|
|
1327
1395
|
if (source.sort.length) view.set("sort", source.sort);
|
|
1328
1396
|
if (source.group.length) view.set("group", source.group);
|
|
1397
|
+
writeViewExtras(view, source);
|
|
1329
1398
|
return id;
|
|
1330
1399
|
});
|
|
1331
1400
|
}
|
|
@@ -1360,236 +1429,52 @@ function createBaseUndoManager(doc, tableId, origin) {
|
|
|
1360
1429
|
captureTimeout: 500
|
|
1361
1430
|
});
|
|
1362
1431
|
}
|
|
1363
|
-
var META_KEYS = [
|
|
1364
|
-
["createdTime", RECORD_META.createdTime],
|
|
1365
|
-
["createdUser", RECORD_META.createdUser],
|
|
1366
|
-
["modifiedTime", RECORD_META.modifiedTime],
|
|
1367
|
-
["modifiedUser", RECORD_META.modifiedUser],
|
|
1368
|
-
["autoNumber", RECORD_META.autoNumber]
|
|
1369
|
-
];
|
|
1370
|
-
function splitRecord(json) {
|
|
1371
|
-
const fields = {};
|
|
1372
|
-
for (const [key, value] of Object.entries(json)) if (!isRecordMetaKey(key)) fields[key] = value;
|
|
1373
|
-
const meta = {};
|
|
1374
|
-
for (const [name, key] of META_KEYS) {
|
|
1375
|
-
if (json[key] !== void 0) meta[name] = json[key];
|
|
1376
|
-
}
|
|
1377
|
-
return Object.keys(meta).length ? { fields, meta } : { fields };
|
|
1378
|
-
}
|
|
1379
|
-
function toSnapshot(doc) {
|
|
1380
|
-
return {
|
|
1381
|
-
schemaVersion: Number(baseRoot(doc).get("schemaVersion") ?? SCHEMA_VERSION),
|
|
1382
|
-
tables: readTables(doc).map((table) => {
|
|
1383
|
-
const records = recordsMap(doc, table.id);
|
|
1384
|
-
const ranks = ranksMap(doc, table.id);
|
|
1385
|
-
return {
|
|
1386
|
-
...table,
|
|
1387
|
-
fields: readFields(doc, table.id).map((field) => ({
|
|
1388
|
-
...field,
|
|
1389
|
-
uiType: fieldUiType(field)
|
|
1390
|
-
})),
|
|
1391
|
-
views: readViews(doc, table.id),
|
|
1392
|
-
records: orderedRecordIds(doc, table.id).map((id) => ({
|
|
1393
|
-
id,
|
|
1394
|
-
rank: ranks.get(id) ?? "",
|
|
1395
|
-
...splitRecord(records.get(id)?.toJSON() ?? {})
|
|
1396
|
-
}))
|
|
1397
|
-
};
|
|
1398
|
-
})
|
|
1399
|
-
};
|
|
1400
|
-
}
|
|
1401
|
-
function loadSnapshot(doc, snapshot, origin) {
|
|
1402
|
-
if (isBaseInitialized(doc)) throw new BaseModelError("文档已初始化,不能再载入快照");
|
|
1403
|
-
doc.transact(() => {
|
|
1404
|
-
baseRoot(doc).set("schemaVersion", snapshot.schemaVersion);
|
|
1405
|
-
for (const table of snapshot.tables) {
|
|
1406
|
-
const tableMap = new Y.Map();
|
|
1407
|
-
tablesMap(doc).set(table.id, tableMap);
|
|
1408
|
-
tableMap.set("name", table.name);
|
|
1409
|
-
tableMap.set("primaryFieldId", table.primaryFieldId);
|
|
1410
|
-
tableOrderArray(doc).push([table.id]);
|
|
1411
|
-
for (const field of table.fields) {
|
|
1412
|
-
const fieldMap = new Y.Map();
|
|
1413
|
-
fieldsMap(doc, table.id).set(field.id, fieldMap);
|
|
1414
|
-
fieldMap.set("name", field.name);
|
|
1415
|
-
fieldMap.set("type", field.type);
|
|
1416
|
-
if (field.type === FieldType.Number && isNumberUiType(field.uiType) && field.uiType !== "Number") {
|
|
1417
|
-
fieldMap.set("uiType", field.uiType);
|
|
1418
|
-
}
|
|
1419
|
-
fieldMap.set("property", field.property);
|
|
1420
|
-
if (field.description) fieldMap.set("description", field.description);
|
|
1421
|
-
}
|
|
1422
|
-
for (const view of table.views) {
|
|
1423
|
-
const viewMap = new Y.Map();
|
|
1424
|
-
viewsMap(doc, table.id).set(view.id, viewMap);
|
|
1425
|
-
viewMap.set("name", view.name);
|
|
1426
|
-
viewMap.set("type", view.type);
|
|
1427
|
-
const fields = new Y.Array();
|
|
1428
|
-
viewMap.set("fields", fields);
|
|
1429
|
-
fields.push(view.fieldIds);
|
|
1430
|
-
const colInfos = new Y.Map();
|
|
1431
|
-
viewMap.set("colInfos", colInfos);
|
|
1432
|
-
for (const [fieldId, info] of Object.entries(view.colInfos)) colInfos.set(fieldId, info);
|
|
1433
|
-
viewMap.set("frozenColCount", view.frozenColCount);
|
|
1434
|
-
viewMap.set("rowHeightLevel", view.rowHeightLevel);
|
|
1435
|
-
if (view.filter) viewMap.set("filter", view.filter);
|
|
1436
|
-
if (view.sort.length) viewMap.set("sort", view.sort);
|
|
1437
|
-
if (view.group.length) viewMap.set("group", view.group);
|
|
1438
|
-
viewOrderArray(doc, table.id).push([view.id]);
|
|
1439
|
-
}
|
|
1440
|
-
const records = recordsMap(doc, table.id);
|
|
1441
|
-
const ranks = ranksMap(doc, table.id);
|
|
1442
|
-
for (const record of table.records) {
|
|
1443
|
-
const recordMap = new Y.Map();
|
|
1444
|
-
records.set(record.id, recordMap);
|
|
1445
|
-
for (const [fieldId, value] of Object.entries(record.fields)) {
|
|
1446
|
-
if (value !== null && value !== void 0) recordMap.set(fieldId, value);
|
|
1447
|
-
}
|
|
1448
|
-
for (const [name, key] of META_KEYS) {
|
|
1449
|
-
const value = record.meta?.[name];
|
|
1450
|
-
if (value !== null && value !== void 0) recordMap.set(key, value);
|
|
1451
|
-
}
|
|
1452
|
-
if (record.rank) ranks.set(record.id, record.rank);
|
|
1453
|
-
}
|
|
1454
|
-
}
|
|
1455
|
-
}, origin);
|
|
1456
|
-
}
|
|
1457
|
-
function toPlainText(doc) {
|
|
1458
|
-
return readTables(doc).map((table) => {
|
|
1459
|
-
const fields = readFields(doc, table.id);
|
|
1460
|
-
const records = recordsMap(doc, table.id);
|
|
1461
|
-
const lines = orderedRecordIds(doc, table.id).map((id) => {
|
|
1462
|
-
const record = records.get(id);
|
|
1463
|
-
return fields.map(
|
|
1464
|
-
(field) => cellValueToText(
|
|
1465
|
-
field,
|
|
1466
|
-
readFieldValue(field, (key) => record?.get(key))
|
|
1467
|
-
)
|
|
1468
|
-
).join(" ");
|
|
1469
|
-
});
|
|
1470
|
-
return [table.name, fields.map((field) => field.name).join(" "), ...lines].join("\n");
|
|
1471
|
-
}).join("\n\n");
|
|
1472
|
-
}
|
|
1473
1432
|
|
|
1474
|
-
// src/base-model/
|
|
1475
|
-
var
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
average: "平均值",
|
|
1486
|
-
max: "最大值",
|
|
1487
|
-
min: "最小值",
|
|
1488
|
-
earliest: "最早时间",
|
|
1489
|
-
latest: "最晚时间",
|
|
1490
|
-
dateRangeOfDays: "时间范围(日)",
|
|
1491
|
-
dateRangeOfMonths: "时间范围(月)",
|
|
1492
|
-
checked: "已勾选",
|
|
1493
|
-
unchecked: "未勾选",
|
|
1494
|
-
percentChecked: "已勾选占比",
|
|
1495
|
-
percentUnchecked: "未勾选占比"
|
|
1496
|
-
};
|
|
1497
|
-
var GENERAL = [
|
|
1498
|
-
"none",
|
|
1499
|
-
"countAll",
|
|
1500
|
-
"empty",
|
|
1501
|
-
"filled",
|
|
1502
|
-
"unique",
|
|
1503
|
-
"percentEmpty",
|
|
1504
|
-
"percentFilled",
|
|
1505
|
-
"percentUnique"
|
|
1506
|
-
];
|
|
1507
|
-
function statTypesFor(field) {
|
|
1508
|
-
switch (field.type) {
|
|
1509
|
-
case FieldType.Number:
|
|
1510
|
-
case FieldType.AutoNumber:
|
|
1511
|
-
return [...GENERAL, "sum", "average", "max", "min"];
|
|
1512
|
-
case FieldType.DateTime:
|
|
1513
|
-
case FieldType.CreatedTime:
|
|
1514
|
-
case FieldType.ModifiedTime:
|
|
1515
|
-
return [...GENERAL, "earliest", "latest", "dateRangeOfDays", "dateRangeOfMonths"];
|
|
1516
|
-
case FieldType.Checkbox:
|
|
1517
|
-
return ["none", "countAll", "checked", "unchecked", "percentChecked", "percentUnchecked"];
|
|
1518
|
-
default:
|
|
1519
|
-
return GENERAL;
|
|
1520
|
-
}
|
|
1433
|
+
// src/base-model/form.ts
|
|
1434
|
+
var isFormFieldType = (type) => !isComputedFieldType(type);
|
|
1435
|
+
function formQuestions(view, fields, primaryFieldId) {
|
|
1436
|
+
const byId = new Map(fields.map((field) => [field.id, field]));
|
|
1437
|
+
return view.fieldIds.flatMap((id) => {
|
|
1438
|
+
const field = byId.get(id);
|
|
1439
|
+
if (!field || !isFormFieldType(field.type)) return [];
|
|
1440
|
+
const info = view.colInfos[id] ?? {};
|
|
1441
|
+
if (info.hidden && id !== primaryFieldId) return [];
|
|
1442
|
+
return [{ field, required: Boolean(info.required), description: info.description ?? "" }];
|
|
1443
|
+
});
|
|
1521
1444
|
}
|
|
1522
|
-
function
|
|
1523
|
-
|
|
1524
|
-
|
|
1445
|
+
function answerOf(field, input) {
|
|
1446
|
+
const value = normalizeCellValue(field, input);
|
|
1447
|
+
if (field.type !== FieldType.Text || value === null) return value;
|
|
1448
|
+
return value.some((segment) => segment.text.trim()) ? value : null;
|
|
1525
1449
|
}
|
|
1526
|
-
var
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
const total = values.length;
|
|
1532
|
-
if (stat === "countAll") return `${total} 条记录`;
|
|
1533
|
-
const filled = values.filter((value) => !isEmptyCellValue(value));
|
|
1534
|
-
const label = STAT_LABELS[stat];
|
|
1535
|
-
switch (stat) {
|
|
1536
|
-
case "empty":
|
|
1537
|
-
return `${label} ${total - filled.length}`;
|
|
1538
|
-
case "filled":
|
|
1539
|
-
return `${label} ${filled.length}`;
|
|
1540
|
-
case "percentEmpty":
|
|
1541
|
-
return `${label} ${percent(total - filled.length, total)}`;
|
|
1542
|
-
case "percentFilled":
|
|
1543
|
-
return `${label} ${percent(filled.length, total)}`;
|
|
1544
|
-
case "unique":
|
|
1545
|
-
case "percentUnique": {
|
|
1546
|
-
const unique2 = new Set(filled.map((value) => cellValueToText(field, value, timeZone))).size;
|
|
1547
|
-
return `${label} ${stat === "unique" ? unique2 : percent(unique2, total)}`;
|
|
1548
|
-
}
|
|
1549
|
-
case "checked":
|
|
1550
|
-
case "unchecked":
|
|
1551
|
-
case "percentChecked":
|
|
1552
|
-
case "percentUnchecked": {
|
|
1553
|
-
const checked = values.filter((value) => value === true).length;
|
|
1554
|
-
const count = stat === "checked" || stat === "percentChecked" ? checked : total - checked;
|
|
1555
|
-
return `${label} ${stat.startsWith("percent") ? percent(count, total) : count}`;
|
|
1556
|
-
}
|
|
1450
|
+
var FormSubmitError = class extends BaseModelError {
|
|
1451
|
+
missing;
|
|
1452
|
+
constructor(missing) {
|
|
1453
|
+
super("有必填项未填写");
|
|
1454
|
+
this.missing = missing;
|
|
1557
1455
|
}
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
return `${label} ${cellValueToText(field, Math.max(...numbers), timeZone)}`;
|
|
1576
|
-
case "dateRangeOfDays": {
|
|
1577
|
-
const from = startOfDayIn(Math.min(...numbers), timeZone);
|
|
1578
|
-
const to = startOfDayIn(Math.max(...numbers), timeZone);
|
|
1579
|
-
let days = Math.round((to - from) / DAY);
|
|
1580
|
-
while (addDaysIn(from, days, timeZone) > to) days--;
|
|
1581
|
-
return `时间范围 ${days} 天`;
|
|
1582
|
-
}
|
|
1583
|
-
case "dateRangeOfMonths": {
|
|
1584
|
-
const from = Math.min(...numbers);
|
|
1585
|
-
const to = Math.max(...numbers);
|
|
1586
|
-
let months = 0;
|
|
1587
|
-
while (startOfMonthIn(from, months + 1, timeZone) <= startOfMonthIn(to, 0, timeZone)) months++;
|
|
1588
|
-
return `时间范围 ${months} 月`;
|
|
1456
|
+
};
|
|
1457
|
+
function submitForm(doc, tableId, viewId, values, origin) {
|
|
1458
|
+
const table = readTable(doc, tableId);
|
|
1459
|
+
const view = readView(doc, tableId, viewId);
|
|
1460
|
+
if (!table || !view || view.type !== "form") throw new BaseModelError("表单不存在");
|
|
1461
|
+
const row = {};
|
|
1462
|
+
const missing = [];
|
|
1463
|
+
for (const { field, required } of formQuestions(
|
|
1464
|
+
view,
|
|
1465
|
+
readFields(doc, tableId),
|
|
1466
|
+
table.primaryFieldId
|
|
1467
|
+
)) {
|
|
1468
|
+
const value = answerOf(field, values[field.id]);
|
|
1469
|
+
if (value === null) {
|
|
1470
|
+
if (required) missing.push(field.id);
|
|
1471
|
+
} else {
|
|
1472
|
+
row[field.id] = value;
|
|
1589
1473
|
}
|
|
1590
|
-
default:
|
|
1591
|
-
return "";
|
|
1592
1474
|
}
|
|
1475
|
+
if (missing.length) throw new FormSubmitError(missing);
|
|
1476
|
+
const [recordId] = addRecords(doc, tableId, [row], {}, origin);
|
|
1477
|
+
return recordId;
|
|
1593
1478
|
}
|
|
1594
1479
|
|
|
1595
1480
|
// src/base-model/view.ts
|
|
@@ -1828,6 +1713,302 @@ function computeView(input) {
|
|
|
1828
1713
|
return { recordIds, groups, total: sorted.length };
|
|
1829
1714
|
}
|
|
1830
1715
|
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1716
|
+
// src/base-model/kanban.ts
|
|
1717
|
+
var usersOf = (value) => Array.isArray(value) ? value.filter(
|
|
1718
|
+
(user) => typeof user === "object" && user !== null && typeof user.id === "string"
|
|
1719
|
+
) : [];
|
|
1720
|
+
function kanbanColumnKey(field, value) {
|
|
1721
|
+
if (isEmptyCellValue(value)) return "";
|
|
1722
|
+
if (field.type === FieldType.SingleSelect) {
|
|
1723
|
+
return typeof value === "string" && field.property.options?.some((option) => option.id === value) ? value : "";
|
|
1724
|
+
}
|
|
1725
|
+
return usersOf(value).map((user) => user.id).join(",");
|
|
1726
|
+
}
|
|
1727
|
+
function computeKanban(input) {
|
|
1728
|
+
const { field } = input;
|
|
1729
|
+
if (!isKanbanGroupFieldType(field.type)) throw new BaseModelError("只能按单选或人员字段分组");
|
|
1730
|
+
const { recordIds } = computeView({
|
|
1731
|
+
fields: input.fields,
|
|
1732
|
+
recordIds: input.recordIds,
|
|
1733
|
+
getValue: input.getValue,
|
|
1734
|
+
filter: input.filter,
|
|
1735
|
+
sort: input.sort,
|
|
1736
|
+
timeZone: input.timeZone,
|
|
1737
|
+
now: input.now
|
|
1738
|
+
});
|
|
1739
|
+
const buckets = /* @__PURE__ */ new Map();
|
|
1740
|
+
for (const id of recordIds) {
|
|
1741
|
+
const value = input.getValue(id, field.id);
|
|
1742
|
+
const key = kanbanColumnKey(field, value);
|
|
1743
|
+
const bucket = buckets.get(key);
|
|
1744
|
+
if (bucket) bucket.ids.push(id);
|
|
1745
|
+
else buckets.set(key, { users: usersOf(value), ids: [id] });
|
|
1746
|
+
}
|
|
1747
|
+
const columns = field.type === FieldType.SingleSelect ? (field.property.options ?? []).map((option) => ({
|
|
1748
|
+
key: option.id,
|
|
1749
|
+
value: option.id,
|
|
1750
|
+
option,
|
|
1751
|
+
recordIds: buckets.get(option.id)?.ids ?? []
|
|
1752
|
+
})) : [...buckets.entries()].filter(([key]) => key !== "").map(([key, bucket]) => ({
|
|
1753
|
+
key,
|
|
1754
|
+
value: bucket.users,
|
|
1755
|
+
users: bucket.users,
|
|
1756
|
+
recordIds: bucket.ids
|
|
1757
|
+
}));
|
|
1758
|
+
const unclassified = buckets.get("");
|
|
1759
|
+
if (unclassified?.ids.length) {
|
|
1760
|
+
columns.unshift({ key: "", value: null, recordIds: unclassified.ids });
|
|
1761
|
+
}
|
|
1762
|
+
return columns;
|
|
1763
|
+
}
|
|
1764
|
+
function moveKanbanRecord(doc, tableId, groupFieldId, move, origin) {
|
|
1765
|
+
doc.transact(() => {
|
|
1766
|
+
setCells(
|
|
1767
|
+
doc,
|
|
1768
|
+
tableId,
|
|
1769
|
+
[{ recordId: move.recordId, fieldId: groupFieldId, value: move.value }],
|
|
1770
|
+
origin
|
|
1771
|
+
);
|
|
1772
|
+
const anchor = move.beforeId ?? move.afterId;
|
|
1773
|
+
if (!anchor || anchor === move.recordId) return;
|
|
1774
|
+
const at = orderedRecordIds(doc, tableId).indexOf(anchor);
|
|
1775
|
+
if (at === -1) return;
|
|
1776
|
+
moveRecords(doc, tableId, [move.recordId], move.beforeId ? at : at + 1, origin);
|
|
1777
|
+
}, origin);
|
|
1778
|
+
}
|
|
1779
|
+
var META_KEYS = [
|
|
1780
|
+
["createdTime", RECORD_META.createdTime],
|
|
1781
|
+
["createdUser", RECORD_META.createdUser],
|
|
1782
|
+
["modifiedTime", RECORD_META.modifiedTime],
|
|
1783
|
+
["modifiedUser", RECORD_META.modifiedUser],
|
|
1784
|
+
["autoNumber", RECORD_META.autoNumber]
|
|
1785
|
+
];
|
|
1786
|
+
function splitRecord(json) {
|
|
1787
|
+
const fields = {};
|
|
1788
|
+
for (const [key, value] of Object.entries(json)) if (!isRecordMetaKey(key)) fields[key] = value;
|
|
1789
|
+
const meta = {};
|
|
1790
|
+
for (const [name, key] of META_KEYS) {
|
|
1791
|
+
if (json[key] !== void 0) meta[name] = json[key];
|
|
1792
|
+
}
|
|
1793
|
+
return Object.keys(meta).length ? { fields, meta } : { fields };
|
|
1794
|
+
}
|
|
1795
|
+
function toSnapshot(doc) {
|
|
1796
|
+
return {
|
|
1797
|
+
schemaVersion: Number(baseRoot(doc).get("schemaVersion") ?? SCHEMA_VERSION),
|
|
1798
|
+
tables: readTables(doc).map((table) => {
|
|
1799
|
+
const records = recordsMap(doc, table.id);
|
|
1800
|
+
const ranks = ranksMap(doc, table.id);
|
|
1801
|
+
return {
|
|
1802
|
+
...table,
|
|
1803
|
+
fields: readFields(doc, table.id).map((field) => ({
|
|
1804
|
+
...field,
|
|
1805
|
+
uiType: fieldUiType(field)
|
|
1806
|
+
})),
|
|
1807
|
+
views: readViews(doc, table.id),
|
|
1808
|
+
records: orderedRecordIds(doc, table.id).map((id) => ({
|
|
1809
|
+
id,
|
|
1810
|
+
rank: ranks.get(id) ?? "",
|
|
1811
|
+
...splitRecord(records.get(id)?.toJSON() ?? {})
|
|
1812
|
+
}))
|
|
1813
|
+
};
|
|
1814
|
+
})
|
|
1815
|
+
};
|
|
1816
|
+
}
|
|
1817
|
+
function loadSnapshot(doc, snapshot, origin) {
|
|
1818
|
+
if (isBaseInitialized(doc)) throw new BaseModelError("文档已初始化,不能再载入快照");
|
|
1819
|
+
doc.transact(() => {
|
|
1820
|
+
baseRoot(doc).set("schemaVersion", snapshot.schemaVersion);
|
|
1821
|
+
for (const table of snapshot.tables) {
|
|
1822
|
+
const tableMap = new Y.Map();
|
|
1823
|
+
tablesMap(doc).set(table.id, tableMap);
|
|
1824
|
+
tableMap.set("name", table.name);
|
|
1825
|
+
tableMap.set("primaryFieldId", table.primaryFieldId);
|
|
1826
|
+
tableOrderArray(doc).push([table.id]);
|
|
1827
|
+
for (const field of table.fields) {
|
|
1828
|
+
const fieldMap = new Y.Map();
|
|
1829
|
+
fieldsMap(doc, table.id).set(field.id, fieldMap);
|
|
1830
|
+
fieldMap.set("name", field.name);
|
|
1831
|
+
fieldMap.set("type", field.type);
|
|
1832
|
+
if (field.type === FieldType.Number && isNumberUiType(field.uiType) && field.uiType !== "Number") {
|
|
1833
|
+
fieldMap.set("uiType", field.uiType);
|
|
1834
|
+
}
|
|
1835
|
+
fieldMap.set("property", field.property);
|
|
1836
|
+
if (field.description) fieldMap.set("description", field.description);
|
|
1837
|
+
}
|
|
1838
|
+
for (const view of table.views) {
|
|
1839
|
+
const viewMap = new Y.Map();
|
|
1840
|
+
viewsMap(doc, table.id).set(view.id, viewMap);
|
|
1841
|
+
viewMap.set("name", view.name);
|
|
1842
|
+
viewMap.set("type", view.type);
|
|
1843
|
+
const fields = new Y.Array();
|
|
1844
|
+
viewMap.set("fields", fields);
|
|
1845
|
+
fields.push(view.fieldIds);
|
|
1846
|
+
const colInfos = new Y.Map();
|
|
1847
|
+
viewMap.set("colInfos", colInfos);
|
|
1848
|
+
for (const [fieldId, info] of Object.entries(view.colInfos)) colInfos.set(fieldId, info);
|
|
1849
|
+
viewMap.set("frozenColCount", view.frozenColCount);
|
|
1850
|
+
viewMap.set("rowHeightLevel", view.rowHeightLevel);
|
|
1851
|
+
if (view.filter) viewMap.set("filter", view.filter);
|
|
1852
|
+
if (view.sort.length) viewMap.set("sort", view.sort);
|
|
1853
|
+
if (view.group.length) viewMap.set("group", view.group);
|
|
1854
|
+
writeViewExtras(viewMap, view);
|
|
1855
|
+
viewOrderArray(doc, table.id).push([view.id]);
|
|
1856
|
+
}
|
|
1857
|
+
const records = recordsMap(doc, table.id);
|
|
1858
|
+
const ranks = ranksMap(doc, table.id);
|
|
1859
|
+
for (const record of table.records) {
|
|
1860
|
+
const recordMap = new Y.Map();
|
|
1861
|
+
records.set(record.id, recordMap);
|
|
1862
|
+
for (const [fieldId, value] of Object.entries(record.fields)) {
|
|
1863
|
+
if (value !== null && value !== void 0) recordMap.set(fieldId, value);
|
|
1864
|
+
}
|
|
1865
|
+
for (const [name, key] of META_KEYS) {
|
|
1866
|
+
const value = record.meta?.[name];
|
|
1867
|
+
if (value !== null && value !== void 0) recordMap.set(key, value);
|
|
1868
|
+
}
|
|
1869
|
+
if (record.rank) ranks.set(record.id, record.rank);
|
|
1870
|
+
}
|
|
1871
|
+
}
|
|
1872
|
+
}, origin);
|
|
1873
|
+
}
|
|
1874
|
+
function toPlainText(doc) {
|
|
1875
|
+
return readTables(doc).map((table) => {
|
|
1876
|
+
const fields = readFields(doc, table.id);
|
|
1877
|
+
const records = recordsMap(doc, table.id);
|
|
1878
|
+
const lines = orderedRecordIds(doc, table.id).map((id) => {
|
|
1879
|
+
const record = records.get(id);
|
|
1880
|
+
return fields.map(
|
|
1881
|
+
(field) => cellValueToText(
|
|
1882
|
+
field,
|
|
1883
|
+
readFieldValue(field, (key) => record?.get(key))
|
|
1884
|
+
)
|
|
1885
|
+
).join(" ");
|
|
1886
|
+
});
|
|
1887
|
+
return [table.name, fields.map((field) => field.name).join(" "), ...lines].join("\n");
|
|
1888
|
+
}).join("\n\n");
|
|
1889
|
+
}
|
|
1890
|
+
|
|
1891
|
+
// src/base-model/stats.ts
|
|
1892
|
+
var STAT_LABELS = {
|
|
1893
|
+
none: "不展示",
|
|
1894
|
+
countAll: "记录总数",
|
|
1895
|
+
empty: "未填写",
|
|
1896
|
+
filled: "已填写",
|
|
1897
|
+
unique: "唯一值",
|
|
1898
|
+
percentEmpty: "未填写占比",
|
|
1899
|
+
percentFilled: "已填写占比",
|
|
1900
|
+
percentUnique: "唯一值占比",
|
|
1901
|
+
sum: "求和",
|
|
1902
|
+
average: "平均值",
|
|
1903
|
+
max: "最大值",
|
|
1904
|
+
min: "最小值",
|
|
1905
|
+
earliest: "最早时间",
|
|
1906
|
+
latest: "最晚时间",
|
|
1907
|
+
dateRangeOfDays: "时间范围(日)",
|
|
1908
|
+
dateRangeOfMonths: "时间范围(月)",
|
|
1909
|
+
checked: "已勾选",
|
|
1910
|
+
unchecked: "未勾选",
|
|
1911
|
+
percentChecked: "已勾选占比",
|
|
1912
|
+
percentUnchecked: "未勾选占比"
|
|
1913
|
+
};
|
|
1914
|
+
var GENERAL = [
|
|
1915
|
+
"none",
|
|
1916
|
+
"countAll",
|
|
1917
|
+
"empty",
|
|
1918
|
+
"filled",
|
|
1919
|
+
"unique",
|
|
1920
|
+
"percentEmpty",
|
|
1921
|
+
"percentFilled",
|
|
1922
|
+
"percentUnique"
|
|
1923
|
+
];
|
|
1924
|
+
function statTypesFor(field) {
|
|
1925
|
+
switch (field.type) {
|
|
1926
|
+
case FieldType.Number:
|
|
1927
|
+
case FieldType.AutoNumber:
|
|
1928
|
+
return [...GENERAL, "sum", "average", "max", "min"];
|
|
1929
|
+
case FieldType.DateTime:
|
|
1930
|
+
case FieldType.CreatedTime:
|
|
1931
|
+
case FieldType.ModifiedTime:
|
|
1932
|
+
return [...GENERAL, "earliest", "latest", "dateRangeOfDays", "dateRangeOfMonths"];
|
|
1933
|
+
case FieldType.Checkbox:
|
|
1934
|
+
return ["none", "countAll", "checked", "unchecked", "percentChecked", "percentUnchecked"];
|
|
1935
|
+
default:
|
|
1936
|
+
return GENERAL;
|
|
1937
|
+
}
|
|
1938
|
+
}
|
|
1939
|
+
function resolveStat(field, primaryFieldId, stat) {
|
|
1940
|
+
if (stat && statTypesFor(field).includes(stat)) return stat;
|
|
1941
|
+
return field.id === primaryFieldId ? "countAll" : "none";
|
|
1942
|
+
}
|
|
1943
|
+
var percent = (part, total) => `${total ? Math.round(part / total * 1e4) / 100 : 0}%`;
|
|
1944
|
+
var DAY = 864e5;
|
|
1945
|
+
var formatNumber2 = (field, value) => field.type === FieldType.Number ? formatNumberField(field, value) : String(value);
|
|
1946
|
+
function statText(field, values, stat, timeZone = DEFAULT_TIME_ZONE) {
|
|
1947
|
+
if (stat === "none") return "";
|
|
1948
|
+
const total = values.length;
|
|
1949
|
+
if (stat === "countAll") return `${total} 条记录`;
|
|
1950
|
+
const filled = values.filter((value) => !isEmptyCellValue(value));
|
|
1951
|
+
const label = STAT_LABELS[stat];
|
|
1952
|
+
switch (stat) {
|
|
1953
|
+
case "empty":
|
|
1954
|
+
return `${label} ${total - filled.length}`;
|
|
1955
|
+
case "filled":
|
|
1956
|
+
return `${label} ${filled.length}`;
|
|
1957
|
+
case "percentEmpty":
|
|
1958
|
+
return `${label} ${percent(total - filled.length, total)}`;
|
|
1959
|
+
case "percentFilled":
|
|
1960
|
+
return `${label} ${percent(filled.length, total)}`;
|
|
1961
|
+
case "unique":
|
|
1962
|
+
case "percentUnique": {
|
|
1963
|
+
const unique2 = new Set(filled.map((value) => cellValueToText(field, value, timeZone))).size;
|
|
1964
|
+
return `${label} ${stat === "unique" ? unique2 : percent(unique2, total)}`;
|
|
1965
|
+
}
|
|
1966
|
+
case "checked":
|
|
1967
|
+
case "unchecked":
|
|
1968
|
+
case "percentChecked":
|
|
1969
|
+
case "percentUnchecked": {
|
|
1970
|
+
const checked = values.filter((value) => value === true).length;
|
|
1971
|
+
const count = stat === "checked" || stat === "percentChecked" ? checked : total - checked;
|
|
1972
|
+
return `${label} ${stat.startsWith("percent") ? percent(count, total) : count}`;
|
|
1973
|
+
}
|
|
1974
|
+
}
|
|
1975
|
+
const numbers = filled.filter((value) => typeof value === "number");
|
|
1976
|
+
if (!numbers.length) return `${label} -`;
|
|
1977
|
+
switch (stat) {
|
|
1978
|
+
case "sum":
|
|
1979
|
+
return `${label} ${formatNumber2(
|
|
1980
|
+
field,
|
|
1981
|
+
numbers.reduce((sum, value) => sum + value, 0)
|
|
1982
|
+
)}`;
|
|
1983
|
+
case "average":
|
|
1984
|
+
return `${label} ${formatNumber2(field, numbers.reduce((sum, value) => sum + value, 0) / numbers.length)}`;
|
|
1985
|
+
case "max":
|
|
1986
|
+
return `${label} ${formatNumber2(field, Math.max(...numbers))}`;
|
|
1987
|
+
case "min":
|
|
1988
|
+
return `${label} ${formatNumber2(field, Math.min(...numbers))}`;
|
|
1989
|
+
case "earliest":
|
|
1990
|
+
return `${label} ${cellValueToText(field, Math.min(...numbers), timeZone)}`;
|
|
1991
|
+
case "latest":
|
|
1992
|
+
return `${label} ${cellValueToText(field, Math.max(...numbers), timeZone)}`;
|
|
1993
|
+
case "dateRangeOfDays": {
|
|
1994
|
+
const from = startOfDayIn(Math.min(...numbers), timeZone);
|
|
1995
|
+
const to = startOfDayIn(Math.max(...numbers), timeZone);
|
|
1996
|
+
let days = Math.round((to - from) / DAY);
|
|
1997
|
+
while (addDaysIn(from, days, timeZone) > to) days--;
|
|
1998
|
+
return `时间范围 ${days} 天`;
|
|
1999
|
+
}
|
|
2000
|
+
case "dateRangeOfMonths": {
|
|
2001
|
+
const from = Math.min(...numbers);
|
|
2002
|
+
const to = Math.max(...numbers);
|
|
2003
|
+
let months = 0;
|
|
2004
|
+
while (startOfMonthIn(from, months + 1, timeZone) <= startOfMonthIn(to, 0, timeZone)) months++;
|
|
2005
|
+
return `时间范围 ${months} 月`;
|
|
2006
|
+
}
|
|
2007
|
+
default:
|
|
2008
|
+
return "";
|
|
2009
|
+
}
|
|
2010
|
+
}
|
|
2011
|
+
|
|
2012
|
+
export { BASE_VIEW_TYPES, BaseModelError, CURRENCY_SYMBOLS, DEFAULT_RECORD_COUNT, DEFAULT_TABLE_FIELDS, DEFAULT_TIME_ZONE, DEFAULT_VIEW_NAMES, FIELD_UI_TYPE, FieldType, FormSubmitError, MAX_RECORDS_PER_TABLE, NUMBER_UI_TYPES, RECORD_META, RankError, SCHEMA_VERSION, STAT_LABELS, addDaysIn, addField, addRecords, addTable, addView, baseRoot, cellValueToText, compareCellValues, compareRank, computeKanban, computeView, copyName, createBaseUndoManager, dateFilterRange, defaultFieldProperty, deleteField, deleteRecords, deleteTable, deleteView, duplicateField, duplicateTable, duplicateView, fieldUiType, fieldValueKey, fieldsMap, filterKind, filterOperatorsFor, formQuestions, formatNumberField, fromWallTime, initBase, isBaseInitialized, isBaseViewType, isComputedFieldType, isEmptyCellValue, isFieldType, isFormFieldType, isKanbanGroupFieldType, isNumberUiType, isRecordMetaKey, kanbanColumnKey, loadSnapshot, moveKanbanRecord, moveRecords, moveTable, moveViewField, newBaseId, normalizeCellValue, orderedRecordIds, rankBetween, ranksBetween, ranksMap, readCellValue, readFieldMap, readFieldValue, readFields, readTable, readTables, readView, readViewMap, readViews, recordsMap, renameTable, repairAutoNumbers, resolveStat, setBaseActor, setCells, setColumnInfo, setKanbanGroupField, startOfDayIn, startOfMonthIn, statText, statTypesFor, submitForm, tableKeys, tableOrderArray, tableTypes, tablesMap, toPlainText, toSnapshot, toWallTime, uniqueName, updateField, updateView, viewOrderArray, viewsMap, weekdayIn, writeViewExtras };
|
|
2013
|
+
//# sourceMappingURL=chunk-S57WFYWY.js.map
|
|
2014
|
+
//# sourceMappingURL=chunk-S57WFYWY.js.map
|