@viewkitjs/views 0.1.0 → 0.2.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/CHANGELOG.md +196 -1
- package/README.md +90 -41
- package/dist/atoms/Row.js +1 -1
- package/dist/atoms/RowMenu.d.ts +1 -1
- package/dist/atoms/RowMenu.js +15 -4
- package/dist/atoms/types.d.ts +4 -4
- package/dist/contract.d.ts +37 -7
- package/dist/contract.js +24 -5
- package/dist/display/format.d.ts +16 -1
- package/dist/display/format.js +10 -1
- package/dist/display/index.d.ts +1 -1
- package/dist/display/index.js +1 -1
- package/dist/display/link.js +5 -3
- package/dist/display/location.d.ts +4 -3
- package/dist/display/location.js +12 -5
- package/dist/display/options.d.ts +1 -1
- package/dist/display/percent.js +10 -3
- package/dist/display/phone.d.ts +2 -1
- package/dist/display/phone.js +8 -1
- package/dist/display/relation.js +1 -1
- package/dist/editors/ChipsEditor.js +1 -1
- package/dist/editors/LocationEditor.js +16 -17
- package/dist/editors/NameEditor.js +5 -4
- package/dist/editors/NumberEditor.js +2 -2
- package/dist/editors/RangeEditor.js +2 -2
- package/dist/editors/ValueListEditor.d.ts +2 -2
- package/dist/editors/ValueListEditor.js +8 -8
- package/dist/editors/edit-cores.d.ts +1 -1
- package/dist/fields/binding.d.ts +1 -1
- package/dist/fields/currency.d.ts +0 -10
- package/dist/fields/currency.js +0 -14
- package/dist/fields/money.d.ts +1 -0
- package/dist/fields/money.js +1 -0
- package/dist/filters/filter-model.d.ts +0 -1
- package/dist/filters/registry.d.ts +3 -36
- package/dist/filters/registry.js +0 -38
- package/dist/grid/chrome/GridAddColumn.d.ts +11 -0
- package/dist/grid/chrome/GridAddColumn.js +29 -0
- package/dist/grid/chrome/GridAggregateMenu.d.ts +31 -0
- package/dist/grid/chrome/GridAggregateMenu.js +27 -0
- package/dist/grid/chrome/GridCellEditors.d.ts +64 -0
- package/dist/grid/chrome/GridCellEditors.js +55 -0
- package/dist/grid/chrome/GridCellOutline.d.ts +70 -0
- package/dist/grid/chrome/GridCellOutline.js +21 -0
- package/dist/grid/chrome/GridChipsEditor.d.ts +28 -0
- package/dist/grid/chrome/GridChipsEditor.js +40 -0
- package/dist/grid/chrome/GridColumnColorSection.d.ts +11 -0
- package/dist/grid/chrome/GridColumnColorSection.js +40 -0
- package/dist/grid/chrome/GridColumnDateSection.d.ts +23 -0
- package/dist/grid/chrome/GridColumnDateSection.js +23 -0
- package/dist/grid/chrome/GridColumnDurationSection.d.ts +19 -0
- package/dist/grid/chrome/GridColumnDurationSection.js +24 -0
- package/dist/grid/chrome/GridColumnMenu.d.ts +101 -0
- package/dist/grid/chrome/GridColumnMenu.js +99 -0
- package/dist/grid/chrome/GridColumnNumberSection.d.ts +25 -0
- package/dist/grid/chrome/GridColumnNumberSection.js +27 -0
- package/dist/grid/chrome/GridColumnPercentSection.d.ts +15 -0
- package/dist/grid/chrome/GridColumnPercentSection.js +16 -0
- package/dist/grid/chrome/GridColumnSortSection.d.ts +18 -0
- package/dist/grid/chrome/GridColumnSortSection.js +16 -0
- package/dist/grid/chrome/GridDateEditor.d.ts +22 -0
- package/dist/grid/chrome/GridDateEditor.js +31 -0
- package/dist/grid/chrome/GridFlyout.d.ts +44 -0
- package/dist/grid/chrome/GridFlyout.js +80 -0
- package/dist/grid/chrome/GridFooter.d.ts +23 -0
- package/dist/grid/chrome/GridFooter.js +81 -0
- package/dist/grid/chrome/GridGroupMenu.d.ts +37 -0
- package/dist/grid/chrome/GridGroupMenu.js +57 -0
- package/dist/grid/chrome/GridHeader.d.ts +61 -0
- package/dist/grid/chrome/GridHeader.js +37 -0
- package/dist/grid/chrome/GridLongTextEditor.d.ts +36 -0
- package/dist/grid/chrome/GridLongTextEditor.js +127 -0
- package/dist/grid/chrome/GridMenuRow.d.ts +15 -0
- package/dist/grid/chrome/GridMenuRow.js +15 -0
- package/dist/grid/chrome/GridNameEditor.d.ts +19 -0
- package/dist/grid/chrome/GridNameEditor.js +36 -0
- package/dist/grid/chrome/GridNumberEditor.d.ts +35 -0
- package/dist/grid/chrome/GridNumberEditor.js +25 -0
- package/dist/grid/chrome/GridRangeEditor.d.ts +20 -0
- package/dist/grid/chrome/GridRangeEditor.js +25 -0
- package/dist/grid/chrome/GridRelationEditor.d.ts +21 -0
- package/dist/grid/chrome/GridRelationEditor.js +30 -0
- package/dist/grid/chrome/GridResizeHandle.d.ts +19 -0
- package/dist/grid/chrome/GridResizeHandle.js +19 -0
- package/dist/grid/chrome/GridRow.d.ts +25 -0
- package/dist/grid/chrome/GridRow.js +136 -0
- package/dist/grid/chrome/GridRowActions.d.ts +32 -0
- package/dist/grid/chrome/GridRowActions.js +25 -0
- package/dist/grid/chrome/GridRowCells.d.ts +4 -0
- package/dist/grid/chrome/GridRowCells.js +133 -0
- package/dist/grid/chrome/GridRowGlyph.d.ts +12 -0
- package/dist/grid/chrome/GridRowGlyph.js +13 -0
- package/dist/grid/chrome/GridSelectEditor.d.ts +18 -0
- package/dist/grid/chrome/GridSelectEditor.js +49 -0
- package/dist/grid/chrome/GridStructure.d.ts +48 -0
- package/dist/grid/chrome/GridStructure.js +45 -0
- package/dist/grid/chrome/GridTable.d.ts +4 -0
- package/dist/grid/chrome/GridTable.js +125 -0
- package/dist/grid/chrome/GridTableSurface.d.ts +15 -0
- package/dist/grid/chrome/GridTableSurface.js +6 -0
- package/dist/grid/chrome/GridTextEditor.d.ts +11 -0
- package/dist/grid/chrome/GridTextEditor.js +40 -0
- package/dist/grid/chrome/GridValueListEditor.d.ts +18 -0
- package/dist/grid/chrome/GridValueListEditor.js +36 -0
- package/dist/grid/chrome/StuckMarker.d.ts +16 -0
- package/dist/grid/chrome/StuckMarker.js +34 -0
- package/dist/grid/chrome/cell-clipboard.d.ts +9 -0
- package/dist/grid/chrome/cell-clipboard.js +28 -0
- package/dist/grid/chrome/cells.d.ts +21 -0
- package/dist/grid/chrome/cells.js +20 -0
- package/dist/grid/chrome/checks.d.ts +35 -0
- package/dist/grid/chrome/checks.js +22 -0
- package/dist/grid/chrome/column-glyph.d.ts +14 -0
- package/dist/grid/chrome/column-glyph.js +44 -0
- package/dist/grid/chrome/editors.d.ts +9 -0
- package/dist/grid/chrome/editors.js +10 -0
- package/dist/grid/chrome/grid-cell-classes.d.ts +59 -0
- package/dist/grid/chrome/grid-cell-classes.js +30 -0
- package/dist/grid/chrome/grid-menu-search.d.ts +7 -0
- package/dist/grid/chrome/grid-menu-search.js +7 -0
- package/dist/grid/chrome/grid-row-props.d.ts +131 -0
- package/dist/grid/chrome/grid-row-props.js +1 -0
- package/dist/grid/chrome/grid-styles.d.ts +37 -0
- package/dist/grid/chrome/grid-styles.js +8 -0
- package/dist/grid/chrome/grid-table-props.d.ts +173 -0
- package/dist/grid/chrome/grid-table-props.js +1 -0
- package/dist/grid/chrome/useGridBatchApi.d.ts +34 -0
- package/dist/grid/chrome/useGridBatchApi.js +35 -0
- package/dist/grid/chrome/useGridColumnLayout.d.ts +30 -0
- package/dist/grid/chrome/useGridColumnLayout.js +206 -0
- package/dist/grid/chrome/useGridHeaderDrag.d.ts +20 -0
- package/dist/grid/chrome/useGridHeaderDrag.js +37 -0
- package/dist/grid/chrome/useGridKeyboard.d.ts +47 -0
- package/dist/grid/chrome/useGridKeyboard.js +274 -0
- package/dist/grid/chrome/useGridPendingReconciliation.d.ts +12 -0
- package/dist/grid/chrome/useGridPendingReconciliation.js +32 -0
- package/dist/grid/chrome/useGridRailReveal.d.ts +57 -0
- package/dist/grid/chrome/useGridRailReveal.js +67 -0
- package/dist/grid/chrome/useGridRangeActions.d.ts +49 -0
- package/dist/grid/chrome/useGridRangeActions.js +188 -0
- package/dist/grid/chrome/useGridRangeGestures.d.ts +32 -0
- package/dist/grid/chrome/useGridRangeGestures.js +190 -0
- package/dist/grid/chrome/useGridRowContextMenu.d.ts +35 -0
- package/dist/grid/chrome/useGridRowContextMenu.js +127 -0
- package/dist/grid/chrome/useGridScaleStyles.d.ts +1 -0
- package/dist/grid/chrome/useGridScaleStyles.js +29 -0
- package/dist/grid/chrome/useGridScrollSync.d.ts +38 -0
- package/dist/grid/chrome/useGridScrollSync.js +176 -0
- package/dist/grid/chrome/useGridTableInteractionModel.d.ts +55 -0
- package/dist/grid/chrome/useGridTableInteractionModel.js +197 -0
- package/dist/grid/chrome/useGridTableSelection.d.ts +29 -0
- package/dist/grid/chrome/useGridTableSelection.js +55 -0
- package/dist/grid/chrome/useHeaderDragReorder.d.ts +21 -0
- package/dist/grid/chrome/useHeaderDragReorder.js +216 -0
- package/dist/grid/core/clipboard-model.d.ts +81 -0
- package/dist/grid/core/clipboard-model.js +291 -0
- package/dist/grid/core/column-values.d.ts +27 -0
- package/dist/grid/core/column-values.js +76 -0
- package/dist/grid/core/commit-model.d.ts +34 -0
- package/dist/grid/core/commit-model.js +3 -0
- package/dist/grid/core/coordinates.d.ts +48 -0
- package/dist/grid/core/coordinates.js +27 -0
- package/dist/grid/core/edit-values.d.ts +9 -0
- package/dist/grid/core/edit-values.js +1 -0
- package/dist/grid/core/editing-controller.d.ts +59 -0
- package/dist/grid/core/editing-controller.js +240 -0
- package/dist/grid/core/editing-model.d.ts +78 -0
- package/dist/grid/core/editing-model.js +136 -0
- package/dist/grid/core/grid-scale.d.ts +14 -0
- package/dist/grid/core/grid-scale.js +8 -0
- package/dist/grid/core/nav.d.ts +94 -0
- package/dist/grid/core/nav.js +92 -0
- package/dist/grid/core/pending-reconciliation.d.ts +50 -0
- package/dist/grid/core/pending-reconciliation.js +49 -0
- package/dist/grid/core/range-model.d.ts +25 -0
- package/dist/grid/core/range-model.js +59 -0
- package/dist/grid/core/spreadsheet-operations.d.ts +85 -0
- package/dist/grid/core/spreadsheet-operations.js +253 -0
- package/dist/grid/core/useGridEditing.d.ts +43 -0
- package/dist/grid/core/useGridEditing.js +90 -0
- package/dist/grid/core/useGridRange.d.ts +20 -0
- package/dist/grid/core/useGridRange.js +24 -0
- package/dist/grid/core/value-equality.d.ts +32 -0
- package/dist/grid/core/value-equality.js +34 -0
- package/dist/grid/index.d.ts +18 -0
- package/dist/grid/index.js +5 -0
- package/dist/grid/useGridTablePrefs.d.ts +50 -0
- package/dist/grid/useGridTablePrefs.js +101 -0
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/internal/viewport.d.ts +5 -0
- package/dist/internal/viewport.js +29 -0
- package/dist/model/column-capabilities.d.ts +2 -2
- package/dist/model/column-state.d.ts +1 -1
- package/dist/model/density.js +3 -3
- package/dist/model/filter-capabilities.d.ts +1 -1
- package/dist/model/filter-capabilities.js +1 -1
- package/dist/model/index.d.ts +4 -6
- package/dist/model/index.js +0 -1
- package/dist/model/preferences.d.ts +7 -3
- package/dist/provider/ViewsProvider.d.ts +1 -1
- package/dist/provider/ViewsProvider.js +5 -1
- package/dist/provider/components.d.ts +16 -8
- package/dist/provider/context.d.ts +3 -2
- package/dist/provider/menu-groups.d.ts +8 -0
- package/dist/provider/menu-groups.js +38 -0
- package/dist/provider/ports.d.ts +22 -0
- package/dist/provider/ports.js +1 -0
- package/dist/react.d.ts +1 -1
- package/docs/contract.md +128 -56
- package/docs/getting-started.md +250 -0
- package/docs/guides/atoms.md +245 -0
- package/docs/guides/cells.md +302 -0
- package/docs/guides/columns.md +412 -0
- package/docs/guides/display.md +203 -0
- package/docs/guides/editors.md +404 -0
- package/docs/guides/ports.md +246 -0
- package/docs/guides/selection.md +270 -0
- package/docs/guides/the-grid.md +677 -0
- package/docs/guides/the-provider.md +446 -0
- package/docs/index.md +50 -0
- package/docs/install.md +9 -47
- package/docs/reference/cells.md +28 -0
- package/docs/reference/display.md +42 -0
- package/docs/reference/editors-chips.md +6 -0
- package/docs/reference/editors-date.md +6 -0
- package/docs/reference/editors-location.md +10 -0
- package/docs/reference/editors-name.md +6 -0
- package/docs/reference/editors-number.md +11 -0
- package/docs/reference/editors-open-values.md +9 -0
- package/docs/reference/editors-range.md +6 -0
- package/docs/reference/editors-relation.md +9 -0
- package/docs/reference/editors-select.md +8 -0
- package/docs/reference/editors-session.md +9 -0
- package/docs/reference/editors-value-list.md +6 -0
- package/docs/reference/editors.md +5 -0
- package/docs/reference/grid.md +39 -0
- package/docs/reference/react.md +86 -0
- package/docs/reference/selection.md +33 -0
- package/docs/reference/views.md +601 -0
- package/package.json +7 -3
- package/skills/views/SKILL.md +20 -5
- package/dist/filters/types.d.ts +0 -27
- package/dist/filters/types.js +0 -1
- package/dist/model/filter-pushdown.d.ts +0 -106
- package/dist/model/filter-pushdown.js +0 -147
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useMemo } from "react";
|
|
3
|
+
import { useCheckDragSelect } from "../../selection/useCheckDragSelect.js";
|
|
4
|
+
import { useEscapeToClear } from "../../selection/useListSelectionShortcuts.js";
|
|
5
|
+
import { useListSelection } from "../../selection/useListSelection.js";
|
|
6
|
+
import { gridRowIdFromKey } from "../core/coordinates.js";
|
|
7
|
+
export function getVisibleGridRowIds({ groups, grouped, isCollapsed, getRowId, }) {
|
|
8
|
+
const ids = [];
|
|
9
|
+
for (const group of groups) {
|
|
10
|
+
if (grouped && (isCollapsed?.(group.key) ?? false))
|
|
11
|
+
continue;
|
|
12
|
+
for (const row of group.items)
|
|
13
|
+
ids.push(getRowId(row));
|
|
14
|
+
}
|
|
15
|
+
return ids;
|
|
16
|
+
}
|
|
17
|
+
export function useGridTableSelection({ rootRef, groups, grouped, isCollapsed, getRowId, selection, }) {
|
|
18
|
+
const selectable = !!selection;
|
|
19
|
+
const derivedOrderedIds = useMemo(() => groups.flatMap((group) => group.items).map(getRowId), [groups, getRowId]);
|
|
20
|
+
const orderedIds = selection?.orderedIds ?? derivedOrderedIds;
|
|
21
|
+
const selectionState = useListSelection({
|
|
22
|
+
orderedIds,
|
|
23
|
+
retention: selection?.retention,
|
|
24
|
+
resetKey: selection?.resetKey,
|
|
25
|
+
});
|
|
26
|
+
useEscapeToClear(selectionState.count > 0, selectionState.clear);
|
|
27
|
+
const selecting = selectable && selectionState.count > 0;
|
|
28
|
+
const visibleRowIds = useMemo(() => getVisibleGridRowIds({ groups, grouped, isCollapsed, getRowId }), [groups, grouped, isCollapsed, getRowId]);
|
|
29
|
+
const checkDrag = useCheckDragSelect({
|
|
30
|
+
enabled: selectable,
|
|
31
|
+
rootRef,
|
|
32
|
+
rowIds: visibleRowIds,
|
|
33
|
+
rowIdOf: gridRowIdFromKey,
|
|
34
|
+
isSelected: selectionState.isSelected,
|
|
35
|
+
setRowSelected: selectionState.toggle,
|
|
36
|
+
});
|
|
37
|
+
const groupSelection = (items) => {
|
|
38
|
+
const ids = items.map(getRowId);
|
|
39
|
+
const selectedCount = ids.filter((id) => selectionState.isSelected(id)).length;
|
|
40
|
+
return {
|
|
41
|
+
allSelected: selectedCount > 0 && selectedCount === ids.length,
|
|
42
|
+
someSelected: selectedCount > 0 && selectedCount < ids.length,
|
|
43
|
+
onToggle: () => selectionState.toggleMany(ids),
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
return {
|
|
47
|
+
selectable,
|
|
48
|
+
selecting,
|
|
49
|
+
selectionState,
|
|
50
|
+
derivedOrderedIds,
|
|
51
|
+
visibleRowIds,
|
|
52
|
+
checkDrag,
|
|
53
|
+
groupSelection,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type React from "react";
|
|
2
|
+
export declare function useHeaderDragReorder({ enabled, ids, scrollXRef, getScroller, canDrag, onReorder, }: {
|
|
3
|
+
enabled: boolean;
|
|
4
|
+
/** Visible column ids, in current order. */
|
|
5
|
+
ids: string[];
|
|
6
|
+
/** The scroll sync's current scrollLeft (GridTable's scrollXRef). */
|
|
7
|
+
scrollXRef: React.MutableRefObject<number>;
|
|
8
|
+
/** Any one synced scroller — autoscroll target (the sync fans it out). */
|
|
9
|
+
getScroller: () => HTMLElement | undefined;
|
|
10
|
+
/** Optional ownership gate for columns that remain fixed in declaration order. */
|
|
11
|
+
canDrag?: (id: string) => boolean;
|
|
12
|
+
/** Commit: the moved id + the full visible order after the move. */
|
|
13
|
+
onReorder: (movedId: string, nextVisibleIds: string[]) => void;
|
|
14
|
+
}): {
|
|
15
|
+
cellStyle: (id: string, index: number) => React.CSSProperties | undefined;
|
|
16
|
+
onCellPointerDown: (id: string, index: number) => (e: React.PointerEvent<HTMLElement>) => void;
|
|
17
|
+
onCellPointerMove: (e: React.PointerEvent<HTMLElement>) => void;
|
|
18
|
+
onCellPointerUp: (e: React.PointerEvent<HTMLElement>) => void;
|
|
19
|
+
onCellPointerCancel: () => void;
|
|
20
|
+
onCellClickCapture: (e: React.MouseEvent<HTMLElement>) => void;
|
|
21
|
+
};
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
3
|
+
import { GRID_GROUND } from "./grid-styles.js";
|
|
4
|
+
const SETTLE_MS = 150;
|
|
5
|
+
const EASE = "cubic-bezier(0.2, 0.7, 0.3, 1)";
|
|
6
|
+
const EDGE_PX = 56;
|
|
7
|
+
const EDGE_MAX_SPEED = 16;
|
|
8
|
+
export function useHeaderDragReorder({ enabled, ids, scrollXRef, getScroller, canDrag, onReorder, }) {
|
|
9
|
+
const dragRef = useRef(null);
|
|
10
|
+
const armRef = useRef(null);
|
|
11
|
+
const movedRef = useRef(false);
|
|
12
|
+
const [drag, setDrag] = useState(null);
|
|
13
|
+
const idsRef = useRef(ids);
|
|
14
|
+
const onReorderRef = useRef(onReorder);
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
idsRef.current = ids;
|
|
17
|
+
onReorderRef.current = onReorder;
|
|
18
|
+
});
|
|
19
|
+
const finish = useCallback((commit) => {
|
|
20
|
+
const d = dragRef.current;
|
|
21
|
+
if (!d || d.settling)
|
|
22
|
+
return;
|
|
23
|
+
d.settling = true;
|
|
24
|
+
if (d.raf != null)
|
|
25
|
+
cancelAnimationFrame(d.raf);
|
|
26
|
+
d.raf = null;
|
|
27
|
+
const ins = commit ? d.ins : d.from;
|
|
28
|
+
let dxFinal = 0;
|
|
29
|
+
if (ins > d.from) {
|
|
30
|
+
for (let j = d.from + 1; j <= ins; j++)
|
|
31
|
+
dxFinal += d.geom[j].width;
|
|
32
|
+
}
|
|
33
|
+
else if (ins < d.from) {
|
|
34
|
+
for (let j = ins; j < d.from; j++)
|
|
35
|
+
dxFinal -= d.geom[j].width;
|
|
36
|
+
}
|
|
37
|
+
if (!commit && dragRef.current) {
|
|
38
|
+
setDrag({ id: d.id, from: d.from, ins: d.from });
|
|
39
|
+
}
|
|
40
|
+
d.el.style.transition = `transform ${SETTLE_MS}ms ${EASE}`;
|
|
41
|
+
d.el.style.setProperty("--vk-drag-shift", `${dxFinal}px`);
|
|
42
|
+
window.setTimeout(() => {
|
|
43
|
+
d.el.style.transition = "";
|
|
44
|
+
d.el.style.removeProperty("--vk-drag-shift");
|
|
45
|
+
d.el.style.willChange = "";
|
|
46
|
+
d.el.classList.remove("z-20", GRID_GROUND, "shadow-md");
|
|
47
|
+
document.body.style.cursor = "";
|
|
48
|
+
document.body.style.userSelect = "";
|
|
49
|
+
dragRef.current = null;
|
|
50
|
+
if (commit && ins !== d.from) {
|
|
51
|
+
const next = [...idsRef.current];
|
|
52
|
+
next.splice(d.from, 1);
|
|
53
|
+
next.splice(ins, 0, d.id);
|
|
54
|
+
onReorderRef.current(d.id, next);
|
|
55
|
+
}
|
|
56
|
+
setDrag(null);
|
|
57
|
+
}, SETTLE_MS + 20);
|
|
58
|
+
}, []);
|
|
59
|
+
const tickRef = useRef(() => { });
|
|
60
|
+
const tick = useCallback(() => {
|
|
61
|
+
const d = dragRef.current;
|
|
62
|
+
if (!d || d.settling)
|
|
63
|
+
return;
|
|
64
|
+
const scroller = getScroller();
|
|
65
|
+
if (scroller) {
|
|
66
|
+
const w = window.innerWidth;
|
|
67
|
+
let v = 0;
|
|
68
|
+
if (d.lastClientX < EDGE_PX) {
|
|
69
|
+
v = -Math.ceil(((EDGE_PX - d.lastClientX) / EDGE_PX) * EDGE_MAX_SPEED);
|
|
70
|
+
}
|
|
71
|
+
else if (d.lastClientX > w - EDGE_PX) {
|
|
72
|
+
v = Math.ceil(((d.lastClientX - (w - EDGE_PX)) / EDGE_PX) * EDGE_MAX_SPEED);
|
|
73
|
+
}
|
|
74
|
+
if (v !== 0)
|
|
75
|
+
scroller.scrollLeft += v;
|
|
76
|
+
}
|
|
77
|
+
const dx = d.lastClientX - d.startX + (scrollXRef.current - d.startScrollX);
|
|
78
|
+
d.el.style.setProperty("--vk-drag-shift", `${dx}px`);
|
|
79
|
+
const x = d.geom[d.from].center + dx;
|
|
80
|
+
let ins = d.from;
|
|
81
|
+
for (let j = 0; j < d.geom.length; j++) {
|
|
82
|
+
if (j === d.from)
|
|
83
|
+
continue;
|
|
84
|
+
if (j < d.from && x < d.geom[j].center)
|
|
85
|
+
ins = Math.min(ins, j);
|
|
86
|
+
if (j > d.from && x > d.geom[j].center)
|
|
87
|
+
ins = Math.max(ins, j);
|
|
88
|
+
}
|
|
89
|
+
if (ins !== d.ins) {
|
|
90
|
+
d.ins = ins;
|
|
91
|
+
setDrag({ id: d.id, from: d.from, ins });
|
|
92
|
+
}
|
|
93
|
+
d.raf = requestAnimationFrame(() => tickRef.current());
|
|
94
|
+
}, [getScroller, scrollXRef]);
|
|
95
|
+
useEffect(() => {
|
|
96
|
+
tickRef.current = tick;
|
|
97
|
+
}, [tick]);
|
|
98
|
+
const onCellPointerDown = useCallback((id, index) => (e) => {
|
|
99
|
+
if (!enabled || e.button !== 0 || e.pointerType !== "mouse")
|
|
100
|
+
return;
|
|
101
|
+
if (idsRef.current.length < 2)
|
|
102
|
+
return;
|
|
103
|
+
if (canDrag && !canDrag(id))
|
|
104
|
+
return;
|
|
105
|
+
movedRef.current = false;
|
|
106
|
+
armRef.current = {
|
|
107
|
+
id,
|
|
108
|
+
index,
|
|
109
|
+
el: e.currentTarget,
|
|
110
|
+
startX: e.clientX,
|
|
111
|
+
startY: e.clientY,
|
|
112
|
+
};
|
|
113
|
+
}, [canDrag, enabled]);
|
|
114
|
+
const onCellPointerMove = useCallback((e) => {
|
|
115
|
+
const d = dragRef.current;
|
|
116
|
+
if (d && e.pointerId === d.pointerId) {
|
|
117
|
+
d.lastClientX = e.clientX;
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
const a = armRef.current;
|
|
121
|
+
if (!a || d)
|
|
122
|
+
return;
|
|
123
|
+
if (!(e.buttons & 1)) {
|
|
124
|
+
armRef.current = null;
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
if (Math.abs(e.clientX - a.startX) <= 4)
|
|
128
|
+
return;
|
|
129
|
+
const el = a.el;
|
|
130
|
+
const strip = el.parentElement;
|
|
131
|
+
if (!strip)
|
|
132
|
+
return;
|
|
133
|
+
const byId = new Map();
|
|
134
|
+
for (const c of Array.from(strip.querySelectorAll("[data-headcell]"))) {
|
|
135
|
+
byId.set(c.dataset.headcell ?? "", c);
|
|
136
|
+
}
|
|
137
|
+
armRef.current = null;
|
|
138
|
+
movedRef.current = true;
|
|
139
|
+
const geom = idsRef.current.map((cid) => {
|
|
140
|
+
const c = byId.get(cid);
|
|
141
|
+
const left = c?.offsetLeft ?? 0;
|
|
142
|
+
const width = c?.offsetWidth ?? 0;
|
|
143
|
+
return { id: cid, left, width, center: left + width / 2 };
|
|
144
|
+
});
|
|
145
|
+
el.setPointerCapture(e.pointerId);
|
|
146
|
+
el.style.willChange = "transform";
|
|
147
|
+
el.classList.add("z-20", GRID_GROUND, "shadow-md");
|
|
148
|
+
document.body.style.cursor = "grabbing";
|
|
149
|
+
document.body.style.userSelect = "none";
|
|
150
|
+
dragRef.current = {
|
|
151
|
+
id: a.id,
|
|
152
|
+
from: a.index,
|
|
153
|
+
ins: a.index,
|
|
154
|
+
el,
|
|
155
|
+
pointerId: e.pointerId,
|
|
156
|
+
startX: a.startX,
|
|
157
|
+
startScrollX: scrollXRef.current,
|
|
158
|
+
geom,
|
|
159
|
+
lastClientX: e.clientX,
|
|
160
|
+
raf: null,
|
|
161
|
+
settling: false,
|
|
162
|
+
};
|
|
163
|
+
setDrag({ id: a.id, from: a.index, ins: a.index });
|
|
164
|
+
dragRef.current.raf = requestAnimationFrame(() => tickRef.current());
|
|
165
|
+
}, [scrollXRef]);
|
|
166
|
+
const onCellPointerUp = useCallback((e) => {
|
|
167
|
+
armRef.current = null;
|
|
168
|
+
const d = dragRef.current;
|
|
169
|
+
if (d && e.pointerId === d.pointerId)
|
|
170
|
+
finish(true);
|
|
171
|
+
}, [finish]);
|
|
172
|
+
const onCellPointerCancel = useCallback(() => {
|
|
173
|
+
armRef.current = null;
|
|
174
|
+
finish(false);
|
|
175
|
+
}, [finish]);
|
|
176
|
+
const onCellClickCapture = useCallback((e) => {
|
|
177
|
+
if (!movedRef.current)
|
|
178
|
+
return;
|
|
179
|
+
movedRef.current = false;
|
|
180
|
+
e.preventDefault();
|
|
181
|
+
e.stopPropagation();
|
|
182
|
+
}, []);
|
|
183
|
+
useEffect(() => {
|
|
184
|
+
if (!drag)
|
|
185
|
+
return;
|
|
186
|
+
const onKey = (e) => {
|
|
187
|
+
if (e.key === "Escape")
|
|
188
|
+
finish(false);
|
|
189
|
+
};
|
|
190
|
+
window.addEventListener("keydown", onKey);
|
|
191
|
+
return () => window.removeEventListener("keydown", onKey);
|
|
192
|
+
}, [drag, finish]);
|
|
193
|
+
const cellStyle = useCallback((id, index) => {
|
|
194
|
+
const d = dragRef.current;
|
|
195
|
+
if (!drag || !d || id === drag.id)
|
|
196
|
+
return undefined;
|
|
197
|
+
const w = d.geom[drag.from]?.width ?? 0;
|
|
198
|
+
let shift = 0;
|
|
199
|
+
if (drag.from < index && index <= drag.ins)
|
|
200
|
+
shift = -w;
|
|
201
|
+
else if (drag.ins <= index && index < drag.from)
|
|
202
|
+
shift = w;
|
|
203
|
+
return {
|
|
204
|
+
["--vk-drag-shift"]: `${shift}px`,
|
|
205
|
+
transition: `transform ${SETTLE_MS}ms ${EASE}`,
|
|
206
|
+
};
|
|
207
|
+
}, [drag]);
|
|
208
|
+
return {
|
|
209
|
+
cellStyle,
|
|
210
|
+
onCellPointerDown,
|
|
211
|
+
onCellPointerMove,
|
|
212
|
+
onCellPointerUp,
|
|
213
|
+
onCellPointerCancel,
|
|
214
|
+
onCellClickCapture,
|
|
215
|
+
};
|
|
216
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { type FieldConventions } from "@viewkitjs/fields";
|
|
2
|
+
import type { ListColumn, ListColumnType } from "../../model/column-schema.js";
|
|
3
|
+
import { type ViewsMessageResolver } from "../../contract.js";
|
|
4
|
+
export type GridClipboardCell = {
|
|
5
|
+
text: string;
|
|
6
|
+
value: unknown;
|
|
7
|
+
type?: ListColumnType;
|
|
8
|
+
unit?: string;
|
|
9
|
+
};
|
|
10
|
+
export type GridCellClipboard = GridClipboardCell & {
|
|
11
|
+
/** Range values in visible row/column order. */
|
|
12
|
+
matrix?: GridClipboardCell[][];
|
|
13
|
+
};
|
|
14
|
+
export type GridPasteSource = {
|
|
15
|
+
text: string;
|
|
16
|
+
value?: unknown;
|
|
17
|
+
type?: ListColumnType;
|
|
18
|
+
unit?: string;
|
|
19
|
+
};
|
|
20
|
+
type GridPasteResult = {
|
|
21
|
+
ok: true;
|
|
22
|
+
next: unknown;
|
|
23
|
+
overlay: boolean;
|
|
24
|
+
} | {
|
|
25
|
+
ok: false;
|
|
26
|
+
reason: string;
|
|
27
|
+
};
|
|
28
|
+
/** Pending value → raw edit accessor → display accessor. */
|
|
29
|
+
export declare function cellEditBaseline<Row>(col: ListColumn<Row>, row: Row, pendingByCol?: Readonly<Record<string, unknown>>): unknown;
|
|
30
|
+
export declare function cellCopyText<Row>(col: ListColumn<Row>, baseline: unknown): string;
|
|
31
|
+
/** Parse external plain text as a spreadsheet matrix. */
|
|
32
|
+
export declare function parseTsvText(text: string): {
|
|
33
|
+
text: string;
|
|
34
|
+
}[][];
|
|
35
|
+
/** Current value converted to the host commit payload shape. */
|
|
36
|
+
export declare function cellPrevPayload<Row>(col: ListColumn<Row>, baseline: unknown, unit?: string): unknown;
|
|
37
|
+
/** The undo value of a write that empties a cell (a cut, Delete, Clear, a range
|
|
38
|
+
* clear), in the commit payload's shape: what the cell holds, as the host's
|
|
39
|
+
* handler takes it back. */
|
|
40
|
+
export declare function cellClearPrev<Row>(col: ListColumn<Row>, row: Row, pendingByCol?: Readonly<Record<string, unknown>>): unknown;
|
|
41
|
+
/**
|
|
42
|
+
* The unit a pasted or filled figure lands in. A money cell copied from a record
|
|
43
|
+
* in one currency never lands in a record that holds another: the copied amount
|
|
44
|
+
* means nothing in the target's currency, and carrying the currency over would
|
|
45
|
+
* re-denominate the record. So it is refused, and the currency is carried only
|
|
46
|
+
* into a record that has none of its own (`holdsCurrency` false). A unit the
|
|
47
|
+
* column doesn't offer is not carried; the target keeps its own. A paste and a
|
|
48
|
+
* fill both land through here.
|
|
49
|
+
*/
|
|
50
|
+
export declare function pasteUnit(sourceUnit: string | undefined, currentUnit: string | undefined, allowed: readonly string[] | undefined, holdsCurrency: boolean): {
|
|
51
|
+
unit: string | undefined;
|
|
52
|
+
} | {
|
|
53
|
+
refuse: string;
|
|
54
|
+
};
|
|
55
|
+
/** Whether a row holds a currency of its own. A money column reads it off the
|
|
56
|
+
* rates — a null map is the database's answer that the row has none — never off
|
|
57
|
+
* `editUnit`, which a host may fill with a default. Any other column: yes. */
|
|
58
|
+
export declare function rowHoldsCurrency<Row>(col: ListColumn<Row>, row: Row): boolean;
|
|
59
|
+
/**
|
|
60
|
+
* Whether a paste or a fill into this column paints its value at once, as an overlay over the cell. Only a value the
|
|
61
|
+
* refetch compares equal to (`looselyEqualValue`) may: a text, an option's key, a yes/no, a list of strings. A figure
|
|
62
|
+
* commits `{ value, unit }`, a date a day or a minute string the row stores in its own shape, a relation an id against
|
|
63
|
+
* a resolved object, a range an object, a list of several values its items: none reads equal to what the row holds,
|
|
64
|
+
* so an overlay would draw the payload ("[object Object]") and the settled write would read as changed elsewhere.
|
|
65
|
+
* Those skip it, as the editors do, and the refetch repaints the cell.
|
|
66
|
+
*/
|
|
67
|
+
export declare function gridPasteOverlay<Row>(col: ListColumn<Row>): boolean;
|
|
68
|
+
/** Coerce one clipboard value into the target column's commit payload. The conventions and the
|
|
69
|
+
* messages are the caller's — the app's, from the provider: a paste reads a number and a date the
|
|
70
|
+
* way the person writes them, and refuses in their words. */
|
|
71
|
+
export declare function coercePaste<Row>(col: ListColumn<Row>, source: GridPasteSource, opts: {
|
|
72
|
+
conventions: FieldConventions;
|
|
73
|
+
messages?: ViewsMessageResolver;
|
|
74
|
+
currentUnit?: string;
|
|
75
|
+
/** Whether the target row holds a currency of its own (`rowHoldsCurrency`). Absent: it does. */
|
|
76
|
+
holdsCurrency?: boolean;
|
|
77
|
+
values?: readonly string[];
|
|
78
|
+
}): GridPasteResult;
|
|
79
|
+
/** Plain TSV form used for the system clipboard. */
|
|
80
|
+
export declare function serializeClipboardMatrix(matrix: readonly (readonly GridClipboardCell[])[]): string;
|
|
81
|
+
export {};
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import { actorLabel, formatLocalMinute, isActorValue, isBreakdown, isRangeValue, isValueItem, listValues, locationText, nameText, parseNumber, rangeText, relationIdOf, relationKey, } from "@viewkitjs/fields";
|
|
2
|
+
import { phoneText } from "@viewkitjs/fields/phone";
|
|
3
|
+
import { fromDayKey, msToDurationUnit, parseDateInput, parseDurationInput } from "@viewkitjs/fields/query";
|
|
4
|
+
import { GRID_TYPE_RULES, gridPasteKind } from "../../model/column-types.js";
|
|
5
|
+
import { resolveViewsMessage } from "../../contract.js";
|
|
6
|
+
import { gridNormalizeWrite } from "../../model/column-capabilities.js";
|
|
7
|
+
import { prettifyOptionValue } from "../../model/column-state.js";
|
|
8
|
+
import { fieldTypeOf, temporalKindOf } from "../../model/field-type-of.js";
|
|
9
|
+
export function cellEditBaseline(col, row, pendingByCol) {
|
|
10
|
+
return pendingByCol && col.id in pendingByCol
|
|
11
|
+
? pendingByCol[col.id]
|
|
12
|
+
: (col.editAccessor ?? col.accessor)?.(row);
|
|
13
|
+
}
|
|
14
|
+
export function cellCopyText(col, baseline) {
|
|
15
|
+
if (baseline == null)
|
|
16
|
+
return "";
|
|
17
|
+
if (Array.isArray(baseline))
|
|
18
|
+
return (baseline.some(isValueItem) ? listValues(baseline) : baseline)
|
|
19
|
+
.map((entry) => cellValueText(col, entry))
|
|
20
|
+
.join(", ");
|
|
21
|
+
return cellValueText(col, baseline);
|
|
22
|
+
}
|
|
23
|
+
function cellValueText(col, value) {
|
|
24
|
+
if (value == null)
|
|
25
|
+
return "";
|
|
26
|
+
if (isValueItem(value))
|
|
27
|
+
return cellValueText(col, value.value);
|
|
28
|
+
if (fieldTypeOf(col) === "location")
|
|
29
|
+
return locationText(value);
|
|
30
|
+
if (fieldTypeOf(col) === "name")
|
|
31
|
+
return nameText(value);
|
|
32
|
+
if (fieldTypeOf(col) === "phone") {
|
|
33
|
+
const text = phoneText(value);
|
|
34
|
+
if (text)
|
|
35
|
+
return text;
|
|
36
|
+
}
|
|
37
|
+
if (isRangeValue(value)) {
|
|
38
|
+
const field = col.field?.type === "range" ? col.field : { key: "", label: "", type: "range", of: "number" };
|
|
39
|
+
return rangeText(field, value);
|
|
40
|
+
}
|
|
41
|
+
if (isActorValue(value))
|
|
42
|
+
return actorLabel(value);
|
|
43
|
+
if (isBreakdown(value))
|
|
44
|
+
return value.headline == null ? "" : String(value.headline);
|
|
45
|
+
if (typeof value === "string" && (col.type === "select" || col.type === "status" || col.type === "multiselect")) {
|
|
46
|
+
return optionLabelOf(col, value);
|
|
47
|
+
}
|
|
48
|
+
const relation = value;
|
|
49
|
+
if (relationKey(value) != null)
|
|
50
|
+
return String(relation.label);
|
|
51
|
+
const ref = typeof value === "object" ? relationIdOf(value) : null;
|
|
52
|
+
if (ref)
|
|
53
|
+
return ref;
|
|
54
|
+
return String(value);
|
|
55
|
+
}
|
|
56
|
+
function optionLabelOf(col, value) {
|
|
57
|
+
const option = col.options?.find((candidate) => candidate.value === value);
|
|
58
|
+
if (!option)
|
|
59
|
+
return value;
|
|
60
|
+
return option.label ?? prettifyOptionValue(value);
|
|
61
|
+
}
|
|
62
|
+
function matchOption(col, text, value, offered) {
|
|
63
|
+
const keys = offered ?? (col.options ?? []).filter((option) => !option.archived).map((option) => option.value);
|
|
64
|
+
const labelled = (col.options ?? [])
|
|
65
|
+
.filter((option) => keys.includes(option.value))
|
|
66
|
+
.map((option) => ({ key: option.value, label: optionLabelOf(col, option.value) }));
|
|
67
|
+
const one = (found) => found.length === 0 ? undefined : found.length === 1 ? { key: found[0] } : { ambiguous: true };
|
|
68
|
+
if (typeof value === "string" && keys.includes(value))
|
|
69
|
+
return { key: value };
|
|
70
|
+
const folded = text.toLowerCase();
|
|
71
|
+
return (one(labelled.filter((option) => option.label === text).map((option) => option.key)) ??
|
|
72
|
+
one(keys.filter((key) => key === text)) ??
|
|
73
|
+
one(labelled.filter((option) => option.label.toLowerCase() === folded).map((option) => option.key)) ??
|
|
74
|
+
one(keys.filter((key) => key.toLowerCase() === folded)));
|
|
75
|
+
}
|
|
76
|
+
export function parseTsvText(text) {
|
|
77
|
+
const normalized = text.replace(/\r/g, "");
|
|
78
|
+
if (normalized === "")
|
|
79
|
+
return [];
|
|
80
|
+
const lines = normalized.split("\n");
|
|
81
|
+
if (lines.length > 1 && lines[lines.length - 1] === "")
|
|
82
|
+
lines.pop();
|
|
83
|
+
return lines.map((line) => line.split("\t").map((cell) => ({ text: cell })));
|
|
84
|
+
}
|
|
85
|
+
function toLocalMinuteString(date) {
|
|
86
|
+
return formatLocalMinute(date);
|
|
87
|
+
}
|
|
88
|
+
export function cellPrevPayload(col, baseline, unit) {
|
|
89
|
+
const type = fieldTypeOf(col);
|
|
90
|
+
if (type === "number" || type === "percent" || type === "currency") {
|
|
91
|
+
if (baseline == null || baseline === "")
|
|
92
|
+
return null;
|
|
93
|
+
const parsed = typeof baseline === "number" ? baseline : Number(baseline ?? 0);
|
|
94
|
+
const payload = {
|
|
95
|
+
value: Number.isFinite(parsed) ? parsed : 0,
|
|
96
|
+
unit,
|
|
97
|
+
};
|
|
98
|
+
return payload;
|
|
99
|
+
}
|
|
100
|
+
if (col.type === "date" || col.type === "datetime") {
|
|
101
|
+
if (temporalKindOf(col) === "date") {
|
|
102
|
+
return typeof baseline === "string" && fromDayKey(baseline) ? baseline : undefined;
|
|
103
|
+
}
|
|
104
|
+
const date = new Date(String(baseline ?? ""));
|
|
105
|
+
return Number.isNaN(date.getTime()) ? undefined : toLocalMinuteString(date);
|
|
106
|
+
}
|
|
107
|
+
return baseline;
|
|
108
|
+
}
|
|
109
|
+
export function cellClearPrev(col, row, pendingByCol) {
|
|
110
|
+
return cellPrevPayload(col, cellEditBaseline(col, row, pendingByCol), col.editUnit?.value(row));
|
|
111
|
+
}
|
|
112
|
+
export function pasteUnit(sourceUnit, currentUnit, allowed, holdsCurrency) {
|
|
113
|
+
const compatible = sourceUnit != null && (!allowed || allowed.includes(sourceUnit));
|
|
114
|
+
if (!compatible)
|
|
115
|
+
return { unit: currentUnit };
|
|
116
|
+
if (holdsCurrency && currentUnit != null && sourceUnit !== currentUnit)
|
|
117
|
+
return { refuse: currentUnit };
|
|
118
|
+
return { unit: sourceUnit };
|
|
119
|
+
}
|
|
120
|
+
export function rowHoldsCurrency(col, row) {
|
|
121
|
+
return col.money ? col.money.rates(row) !== null : true;
|
|
122
|
+
}
|
|
123
|
+
export function gridPasteOverlay(col) {
|
|
124
|
+
const kind = gridPasteKind(col);
|
|
125
|
+
if (kind === "text" || kind === "option" || kind === "boolean")
|
|
126
|
+
return true;
|
|
127
|
+
if (kind === "list")
|
|
128
|
+
return !col.multiple;
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
export function coercePaste(col, source, opts) {
|
|
132
|
+
const messages = opts.messages ?? resolveViewsMessage;
|
|
133
|
+
const write = { messages };
|
|
134
|
+
const empty = { ok: false, reason: messages({ key: "views.grid.paste-empty" }) };
|
|
135
|
+
const refused = { ok: false, reason: messages({ key: "views.grid.paste-refused" }) };
|
|
136
|
+
const overlay = gridPasteOverlay(col);
|
|
137
|
+
const kind = gridPasteKind(col);
|
|
138
|
+
if (kind == null)
|
|
139
|
+
return refused;
|
|
140
|
+
switch (kind) {
|
|
141
|
+
case "text": {
|
|
142
|
+
const next = source.text.trim();
|
|
143
|
+
if (next === "" && !col.clearable)
|
|
144
|
+
return empty;
|
|
145
|
+
const vetted = gridNormalizeWrite(col, next, opts.conventions, write);
|
|
146
|
+
if (!vetted.ok)
|
|
147
|
+
return { ok: false, reason: vetted.reason };
|
|
148
|
+
return { ok: true, next: vetted.value, overlay };
|
|
149
|
+
}
|
|
150
|
+
case "option": {
|
|
151
|
+
const value = source.text.trim();
|
|
152
|
+
if (!value)
|
|
153
|
+
return empty;
|
|
154
|
+
const match = matchOption(col, value, source.value, opts.values);
|
|
155
|
+
if (!match)
|
|
156
|
+
return { ok: false, reason: messages({ key: "views.grid.paste-not-option", values: { value } }) };
|
|
157
|
+
if ("ambiguous" in match) {
|
|
158
|
+
return { ok: false, reason: messages({ key: "views.grid.paste-ambiguous-option", values: { value } }) };
|
|
159
|
+
}
|
|
160
|
+
return { ok: true, next: match.key, overlay };
|
|
161
|
+
}
|
|
162
|
+
case "number": {
|
|
163
|
+
const duration = col.format?.style === "duration";
|
|
164
|
+
const readDuration = (text) => {
|
|
165
|
+
const ms = parseDurationInput(text, opts.conventions);
|
|
166
|
+
return ms == null ? null : msToDurationUnit(ms, col.format?.unit ?? "ms");
|
|
167
|
+
};
|
|
168
|
+
const value = typeof source.value === "number" &&
|
|
169
|
+
source.type != null &&
|
|
170
|
+
GRID_TYPE_RULES[source.type]?.paste === "number"
|
|
171
|
+
? source.value
|
|
172
|
+
: duration
|
|
173
|
+
? readDuration(source.text)
|
|
174
|
+
:
|
|
175
|
+
parseNumber(source.text, opts.conventions);
|
|
176
|
+
if (value == null) {
|
|
177
|
+
return {
|
|
178
|
+
ok: false,
|
|
179
|
+
reason: messages({ key: duration ? "views.grid.paste-not-duration" : "views.grid.paste-not-number" }),
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
const unit = pasteUnit(source.unit, opts.currentUnit, col.editUnit?.options, fieldTypeOf(col) === "currency" && (opts.holdsCurrency ?? true));
|
|
183
|
+
if ("refuse" in unit) {
|
|
184
|
+
return { ok: false, reason: messages({ key: "views.grid.paste-other-currency", values: { currency: unit.refuse } }) };
|
|
185
|
+
}
|
|
186
|
+
const next = { value, unit: unit.unit };
|
|
187
|
+
return { ok: true, next, overlay };
|
|
188
|
+
}
|
|
189
|
+
case "date": {
|
|
190
|
+
if (temporalKindOf(col) === "date") {
|
|
191
|
+
const day = parseDateInput(source.text);
|
|
192
|
+
return day
|
|
193
|
+
? { ok: true, next: day, overlay }
|
|
194
|
+
: { ok: false, reason: messages({ key: "views.grid.paste-not-date" }) };
|
|
195
|
+
}
|
|
196
|
+
const time = Date.parse(source.text.trim());
|
|
197
|
+
if (Number.isNaN(time))
|
|
198
|
+
return { ok: false, reason: messages({ key: "views.grid.paste-not-date" }) };
|
|
199
|
+
return { ok: true, next: toLocalMinuteString(new Date(time)), overlay };
|
|
200
|
+
}
|
|
201
|
+
case "boolean": {
|
|
202
|
+
const raw = source.type === "boolean" ? source.value : source.text;
|
|
203
|
+
const res = gridNormalizeWrite(col, raw, opts.conventions, write);
|
|
204
|
+
if (!res.ok)
|
|
205
|
+
return { ok: false, reason: res.reason };
|
|
206
|
+
return { ok: true, next: res.value, overlay };
|
|
207
|
+
}
|
|
208
|
+
case "relation": {
|
|
209
|
+
const text = source.text.trim();
|
|
210
|
+
if (text === "") {
|
|
211
|
+
if (!col.clearable)
|
|
212
|
+
return empty;
|
|
213
|
+
return { ok: true, next: null, overlay };
|
|
214
|
+
}
|
|
215
|
+
if (col.relation?.source?.kind === "remote") {
|
|
216
|
+
const id = relationKey(source.value);
|
|
217
|
+
return id
|
|
218
|
+
? { ok: true, next: id, overlay }
|
|
219
|
+
: { ok: false, reason: messages({ key: "views.grid.paste-pick", values: { value: text } }) };
|
|
220
|
+
}
|
|
221
|
+
const candidates = col.relation?.options ?? [];
|
|
222
|
+
const byId = relationKey(source.value);
|
|
223
|
+
const exact = byId ? candidates.find((option) => option.id === byId) : undefined;
|
|
224
|
+
const matched = exact ?? candidates.find((option) => option.label.toLowerCase() === text.toLowerCase());
|
|
225
|
+
if (!matched)
|
|
226
|
+
return { ok: false, reason: messages({ key: "views.grid.paste-not-linkable", values: { value: text } }) };
|
|
227
|
+
return { ok: true, next: matched.id, overlay };
|
|
228
|
+
}
|
|
229
|
+
case "list": {
|
|
230
|
+
const pasted = Array.isArray(source.value)
|
|
231
|
+
? source.value
|
|
232
|
+
: col.options?.length
|
|
233
|
+
? source.text.split(",").map((raw, _index, entries) => {
|
|
234
|
+
const entry = raw.trim();
|
|
235
|
+
const value = entries.length === 1 ? source.value : undefined;
|
|
236
|
+
const match = matchOption(col, entry, value, opts.values);
|
|
237
|
+
return match && "key" in match ? match.key : entry;
|
|
238
|
+
})
|
|
239
|
+
: source.text;
|
|
240
|
+
const vetted = gridNormalizeWrite(col, pasted, opts.conventions, write);
|
|
241
|
+
if (!vetted.ok)
|
|
242
|
+
return { ok: false, reason: vetted.reason };
|
|
243
|
+
const values = (Array.isArray(vetted.value) ? vetted.value : []);
|
|
244
|
+
if (values.length === 0 && !col.clearable)
|
|
245
|
+
return empty;
|
|
246
|
+
return { ok: true, next: vetted.value, overlay };
|
|
247
|
+
}
|
|
248
|
+
case "range": {
|
|
249
|
+
if (isRangeValue(source.value))
|
|
250
|
+
return { ok: true, next: source.value, overlay };
|
|
251
|
+
const text = source.text.trim();
|
|
252
|
+
if (text === "") {
|
|
253
|
+
if (!col.clearable)
|
|
254
|
+
return empty;
|
|
255
|
+
return { ok: true, next: { low: null, high: null }, overlay };
|
|
256
|
+
}
|
|
257
|
+
const range = parseRangeText(text, opts.conventions);
|
|
258
|
+
if (!range)
|
|
259
|
+
return { ok: false, reason: messages({ key: "views.grid.paste-not-range" }) };
|
|
260
|
+
return { ok: true, next: range, overlay };
|
|
261
|
+
}
|
|
262
|
+
case "none":
|
|
263
|
+
return refused;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
function parseRangeText(text, cv) {
|
|
267
|
+
const num = (part) => parseNumber(part, cv);
|
|
268
|
+
const upTo = /^up\s+to\s+(.+)$/i.exec(text);
|
|
269
|
+
if (upTo) {
|
|
270
|
+
const high = num(upTo[1]);
|
|
271
|
+
return high == null ? null : { low: null, high };
|
|
272
|
+
}
|
|
273
|
+
const andUp = /^(.+?)\s*\+$/.exec(text);
|
|
274
|
+
if (andUp) {
|
|
275
|
+
const low = num(andUp[1]);
|
|
276
|
+
return low == null ? null : { low, high: null };
|
|
277
|
+
}
|
|
278
|
+
const pair = /^(.+?)\s*(?:–|—|\s+to\s+|(?<=\d)(?:-|\s+-\s+)(?=\d))\s*(.+)$/i.exec(text);
|
|
279
|
+
if (pair) {
|
|
280
|
+
const low = num(pair[1]);
|
|
281
|
+
const high = num(pair[2]);
|
|
282
|
+
return low == null || high == null ? null : { low, high };
|
|
283
|
+
}
|
|
284
|
+
const one = num(text);
|
|
285
|
+
return one == null ? null : { low: one, high: one };
|
|
286
|
+
}
|
|
287
|
+
export function serializeClipboardMatrix(matrix) {
|
|
288
|
+
return matrix
|
|
289
|
+
.map((row) => row.map((cell) => cell.text.replace(/[\t\n\r]+/g, " ")).join("\t"))
|
|
290
|
+
.join("\n");
|
|
291
|
+
}
|