@macrostrat/data-sheet 4.9.1 → 4.10.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 +61 -31
- package/dist/actions/selection.cjs +5 -1
- package/dist/actions/selection.cjs.map +1 -1
- package/dist/actions/selection.d.ts +7 -1
- package/dist/actions/selection.js +5 -1
- package/dist/actions/selection.js.map +1 -1
- package/dist/components/editor-actions.cjs +1 -1
- package/dist/components/editor-actions.cjs.map +1 -1
- package/dist/components/editor-actions.js +2 -2
- package/dist/components/editor-actions.js.map +1 -1
- package/dist/components/indicators/unified-filters.cjs +3 -0
- package/dist/components/indicators/unified-filters.cjs.map +1 -1
- package/dist/components/indicators/unified-filters.js +3 -0
- package/dist/components/indicators/unified-filters.js.map +1 -1
- package/dist/components/toolbars/actions-toolbar.cjs +5 -2
- package/dist/components/toolbars/actions-toolbar.cjs.map +1 -1
- package/dist/components/toolbars/actions-toolbar.js +6 -3
- package/dist/components/toolbars/actions-toolbar.js.map +1 -1
- package/dist/data-sheet.cjs +4 -0
- package/dist/data-sheet.cjs.map +1 -1
- package/dist/data-sheet.css +38 -22
- package/dist/data-sheet.js +4 -0
- package/dist/data-sheet.js.map +1 -1
- package/dist/index.cjs +15 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +2 -1
- package/dist/main.module.sass.cjs +14 -14
- package/dist/main.module.sass.js +14 -14
- package/dist/provider/column-spec.cjs.map +1 -1
- package/dist/provider/column-spec.d.ts +8 -0
- package/dist/provider/column-spec.js.map +1 -1
- package/dist/provider/types.cjs.map +1 -1
- package/dist/provider/types.d.ts +6 -0
- package/dist/provider/types.js.map +1 -1
- package/dist/provider/zustand-store.cjs +9 -2
- package/dist/provider/zustand-store.cjs.map +1 -1
- package/dist/provider/zustand-store.js +9 -2
- package/dist/provider/zustand-store.js.map +1 -1
- package/dist/renderers/cell-renderer.cjs +9 -2
- package/dist/renderers/cell-renderer.cjs.map +1 -1
- package/dist/renderers/cell-renderer.js +9 -2
- package/dist/renderers/cell-renderer.js.map +1 -1
- package/dist/utils/key-handling.cjs +6 -5
- package/dist/utils/key-handling.cjs.map +1 -1
- package/dist/utils/key-handling.js +2 -1
- package/dist/utils/key-handling.js.map +1 -1
- package/package.json +2 -2
- package/src/actions/selection.ts +11 -2
- package/src/components/editor-actions.ts +5 -2
- package/src/components/indicators/unified-filters.ts +3 -0
- package/src/components/toolbars/actions-toolbar.ts +10 -4
- package/src/data-sheet.ts +6 -0
- package/src/main.module.sass +14 -0
- package/src/provider/column-spec.ts +8 -0
- package/src/provider/types.ts +10 -1
- package/src/provider/zustand-store.ts +9 -2
- package/src/renderers/cell-renderer.ts +12 -0
- package/src/utils/key-handling.ts +2 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"key-handling.cjs","sources":["../../src/utils/key-handling.ts"],"sourcesContent":["import type { KeyboardEvent } from \"react\";\nimport {\n storeAPIAtom,\n storeAtom,\n tableActionsAtom,\n type DataSheetStoreMain,\n singleFocusedCell,\n ctx,\n} from \"../provider\";\nimport { atom } from \"jotai\";\nimport { runActionWrapper } from \"../actions\";\nimport { HotkeyConfig } from \"@blueprintjs/core\";\nimport { toasterAtom } from \"../notifications.ts\";\n\ntype HotkeyActionRunner = (\n e: KeyboardEvent,\n state: DataSheetStoreMain<any>,\n setState: (state: Partial<DataSheetStoreMain<any>>) => void,\n) => void | Promise<void>;\n\nconst isEditableAtom = atom((get) => {\n const store = get(storeAtom);\n return store?.editable ?? false;\n});\n\nconst hasSelectionAtom = atom((get) => {\n const store = get(storeAtom);\n const selection = store?.selection ?? [];\n return selection.length > 0;\n});\n\nexport const tableHotkeysAtom = atom<null, [], HotkeyConfig[]>(\n null,\n (get, set) => {\n const store = get(storeAPIAtom);\n const toaster = get(toasterAtom);\n const editable = get(isEditableAtom);\n\n /** Set up hotkeys for table actions */\n const tableActions = get(tableActionsAtom);\n\n if (store == null) return [];\n const keyHandler = (\n actionRunner: HotkeyActionRunner,\n ): HotkeyConfig[\"onKeyDown\"] => {\n return (e) => {\n // Kind of a ridiculous hack to get the store from the atom.\n const state = store.getState();\n const setState = store.setState;\n try {\n const res = actionRunner(e as any, state, setState);\n if (res instanceof Promise) {\n res\n .then(() => {})\n .catch((err) => {\n console.error(err);\n handleError(toaster, err);\n });\n }\n } catch (err) {\n handleError(toaster, err);\n }\n };\n };\n\n function handleError(toaster, err) {\n toaster.show({\n message: `${err}`,\n intent: \"danger\",\n });\n }\n\n const directionCombos = [\"up\", \"down\", \"left\", \"right\"].map((dir) => {\n return {\n combo: dir,\n group: \"Navigation\",\n label: \"Move focus \" + dir,\n preventDefault: true,\n onKeyDown: keyHandler((e, state) => {\n state.moveFocusedCell(dir as any);\n }),\n };\n });\n\n /** TODO: merge a bit more with the actions config */\n\n const actionsCombos = tableActions\n .map((action) => {\n if (action.hotkey == null) return null;\n return {\n combo: action.hotkey,\n label: action.name,\n group: action.group ?? \"Actions\",\n preventDefault: true,\n disabled: action.requiresEditable !== false && !editable,\n onKeyDown(event: KeyboardEvent<any>): any {\n runActionWrapper(action, get, set, toaster);\n },\n } as any as HotkeyConfig;\n })\n .filter(Boolean) as HotkeyConfig[];\n\n // Backspace/Delete: on a whole-row selection, delete the row(s); otherwise\n // clear the selected cells.\n const clearOrDeleteSelection = keyHandler((e, state) => {\n const sel = state.selection ?? [];\n const isRowSelection =\n sel.length > 0 && sel.every((r) => r.cols == null && r.rows != null);\n if (isRowSelection) {\n state.deleteSelectedRows();\n } else {\n state.clearSelection();\n }\n });\n\n return [\n {\n combo: \"esc\",\n label: \"Exit editing / clear selection\",\n group: \"Selection\",\n allowInInput: true,\n onKeyDown: keyHandler((e, state) => {\n // First Escape closes the focused cell's open surface (editor/panel)\n // while keeping the selection, so arrow keys keep working — in both\n // auto and manual modes. A second Escape (nothing open) clears the\n // selection.\n if (state.cellSurfaceOpen) {\n state.closeCellSurface();\n e.preventDefault();\n e.stopPropagation();\n return;\n }\n if (state.selection.length == 0) {\n // Focus goes back to parent\n return;\n }\n state.setSelection([]);\n e.preventDefault();\n e.stopPropagation();\n }),\n },\n {\n combo: \"backspace\",\n label: \"Clear cells / delete rows\",\n group: \"Selection\",\n allowInInput: true,\n disabled: !editable,\n onKeyDown: clearOrDeleteSelection,\n },\n {\n combo: \"del\",\n label: \"Clear cells / delete rows\",\n group: \"Selection\",\n allowInInput: true,\n disabled: !editable,\n onKeyDown: clearOrDeleteSelection,\n },\n {\n combo: \"tab\",\n label: \"Move focus right (or left with shift)\",\n group: \"Navigation\",\n allowInInput: true,\n onKeyDown: keyHandler((e, state) => {\n state.moveFocusedCell(e.shiftKey ? \"left\" : \"right\");\n }),\n },\n {\n combo: \"enter\",\n label: \"Edit cell / move focus down\",\n group: \"Navigation\",\n allowInInput: true,\n onKeyDown: keyHandler((e, state) => {\n // In navigation mode (no surface open), Enter enters edit mode —\n // opening and focusing the focused cell's surface (Google Sheets\n // style). While a surface is open, Enter advances downward as usual\n // (an inline editor blurs and this moves to the next row).\n if (!state.cellSurfaceOpen) {\n state.armAutoActivate();\n state.openCellSurface();\n e.preventDefault();\n return;\n }\n state.moveFocusedCell(\"down\");\n }),\n },\n {\n combo: \"f2\",\n label: \"Edit / open cell\",\n group: \"Editing\",\n onKeyDown: keyHandler((e, state) => {\n // Enter edit mode from the keyboard: re-arm auto-activation and open\n // the focused cell's surface (which focuses the editor).\n state.armAutoActivate();\n state.openCellSurface();\n e.preventDefault();\n }),\n },\n ...directionCombos,\n ...actionsCombos,\n ];\n },\n);\n\nconst selectionAtom = atom((get) => {\n const store = get(storeAtom);\n return store?.selection ?? [];\n});\n\nconst isSingleCellSelectionAtom = atom((get) => {\n const selection = get(selectionAtom);\n return singleFocusedCell(selection) != null;\n});\n\nexport const editorKeyHandlerAtom = atom((get) => {\n const store = get(storeAPIAtom);\n return (e: KeyboardEvent) => {\n if (e.key === \"Escape\") {\n // Escape exits the inline editor: close the surface (which enters\n // navigation mode in auto), keep the selection, and return focus to the\n // table so arrow keys navigate. Stop propagation so the table's own\n // Escape hotkey doesn't also fire.\n const state = store?.getState();\n state?.closeCellSurface();\n state?.tableElement?.focus();\n e.preventDefault();\n e.stopPropagation();\n return;\n }\n const isSingleCellSelection = get(isSingleCellSelectionAtom);\n editorKeyHandler(e as any, isSingleCellSelection);\n };\n});\n\nfunction editorKeyHandler(\n e: KeyboardEvent<HTMLInputElement>,\n isSingleCellSelection: boolean,\n) {\n /** Get a key handler for inline cell editing */\n\n const target = e.target as HTMLInputElement;\n\n let shouldCaptureEventForInlineEditing = true;\n\n if (e.key == \"Enter\") {\n shouldCaptureEventForInlineEditing = e.shiftKey;\n }\n\n if (e.key == \"Escape\" || e.key == \"Tab\") {\n shouldCaptureEventForInlineEditing = false;\n }\n\n if (e.key == \"Backspace\" || e.key == \"Delete\") {\n /** Clear selection on Backspace or Delete on a multi-cell selection\n * (if shift is held, we allow deletion in a single cell)\n */\n shouldCaptureEventForInlineEditing = isSingleCellSelection && !e.shiftKey;\n }\n\n if (!shouldCaptureEventForInlineEditing) {\n target.blur();\n e.preventDefault();\n return;\n }\n\n const shouldPropagate = shouldPropagateKeystroke(e);\n if (!shouldPropagate) {\n e.stopPropagation();\n //e.preventDefault();\n } else {\n // we blur the editor and dispatch a keydown event on the parent to propagate the event to the table\n e.preventDefault();\n target.blur();\n //console.log(tableElement);\n //tableElement.focus();\n //target.parentNode?.dispatchEvent(e);\n target.parentNode?.dispatchEvent(new KeyboardEvent(\"keydown\", e as any));\n }\n}\n\nfunction shouldPropagateKeystroke(evt: any): boolean {\n if (evt.target.selectionStart == evt.target.selectionEnd) {\n // We don't have anything selected, so we can propagate the event potentially\n // Propagate copy and paste events even if there is no selection, since they might want to copy/paste an entire row/column based on the focused cell.\n if (evt.key === \"c\" || evt.key === \"v\" || evt.key === \"x\") {\n if (evt.ctrlKey || evt.metaKey) {\n return true;\n }\n }\n }\n\n if (evt.key === \"ArrowUp\" || evt.key === \"ArrowDown\") {\n return true;\n }\n\n const target = evt.target;\n if (\n (evt.key === \"ArrowLeft\" ||\n evt.key === \"ArrowUp\" ||\n evt.key === \"ArrowDown\") &&\n target.selectionStart === 0\n ) {\n return true;\n }\n\n if (\n (evt.key === \"ArrowRight\" ||\n evt.key === \"ArrowUp\" ||\n evt.key === \"Tab\" ||\n evt.key === \"Enter\" ||\n evt.key === \"ArrowDown\") &&\n target.selectionStart === evt.target.value.length\n ) {\n return true;\n }\n\n return false;\n}\n\nexport function multiLineTextKeyHandler(evt: any) {\n if (evt.key === \"Enter\") {\n //evt.preventDefault();\n evt.stopPropagation();\n return;\n }\n\n if (evt.key === \"Escape\") {\n evt.preventDefault();\n return;\n }\n\n const shouldPropagate = shouldPropagateKeystroke(evt);\n if (!shouldPropagate) {\n evt.stopPropagation();\n } else {\n evt.target.blur();\n }\n}\n"],"names":["atom","storeAtom","storeAPIAtom","toasterAtom","tableActionsAtom","toaster","runActionWrapper","singleFocusedCell"],"mappings":";;;;;;;AAoBA,MAAM,iBAAiBA,MAAAA,KAAK,CAAC,QAAQ;AACnC,QAAM,QAAQ,IAAIC,cAAS;AAC3B,SAAO,OAAO,YAAY;AAC5B,CAAC;AAEwBD,MAAAA,KAAK,CAAC,QAAQ;AACrC,QAAM,QAAQ,IAAIC,cAAS;AAC3B,QAAM,YAAY,OAAO,aAAa,CAAA;AACtC,SAAO,UAAU,SAAS;AAC5B,CAAC;AAEM,MAAM,mBAAmBD,MAAAA;AAAAA,EAC9B;AAAA,EACA,CAAC,KAAK,QAAQ;AACZ,UAAM,QAAQ,IAAIE,iBAAY;AAC9B,UAAM,UAAU,IAAIC,yBAAW;AAC/B,UAAM,WAAW,IAAI,cAAc;AAGnC,UAAM,eAAe,IAAIC,qBAAgB;AAEzC,QAAI,SAAS,KAAM,QAAO,CAAA;AAC1B,UAAM,aAAa,CACjB,iBAC8B;AAC9B,aAAO,CAAC,MAAM;AAEZ,cAAM,QAAQ,MAAM,SAAA;AACpB,cAAM,WAAW,MAAM;AACvB,YAAI;AACF,gBAAM,MAAM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vent } from \"react\";\nimport {\n storeAPIAtom,\n storeAtom,\n tableActionsAtom,\n type DataSheetStoreMain,\n singleFocusedCell,\n ctx,\n} from \"../provider\";\nimport { atom } from \"jotai\";\nimport { runActionWrapper } from \"../actions\";\nimport { actionRequiresEditable } from \"../actions/selection.ts\";\nimport { HotkeyConfig } from \"@blueprintjs/core\";\nimport { toasterAtom } from \"../notifications.ts\";\n\ntype HotkeyActionRunner = (\n e: KeyboardEvent,\n state: DataSheetStoreMain<any>,\n setState: (state: Partial<DataSheetStoreMain<any>>) => void,\n) => void | Promise<void>;\n\nconst isEditableAtom = atom((get) => {\n const store = get(storeAtom);\n return store?.editable ?? false;\n});\n\nconst hasSelectionAtom = atom((get) => {\n const store = get(storeAtom);\n const selection = store?.selection ?? [];\n return selection.length > 0;\n});\n\nexport const tableHotkeysAtom = atom<null, [], HotkeyConfig[]>(\n null,\n (get, set) => {\n const store = get(storeAPIAtom);\n const toaster = get(toasterAtom);\n const editable = get(isEditableAtom);\n\n /** Set up hotkeys for table actions */\n const tableActions = get(tableActionsAtom);\n\n if (store == null) return [];\n const keyHandler = (\n actionRunner: HotkeyActionRunner,\n ): HotkeyConfig[\"onKeyDown\"] => {\n return (e) => {\n // Kind of a ridiculous hack to get the store from the atom.\n const state = store.getState();\n const setState = store.setState;\n try {\n const res = actionRunner(e as any, state, setState);\n if (res instanceof Promise) {\n res\n .then(() => {})\n .catch((err) => {\n console.error(err);\n handleError(toaster, err);\n });\n }\n } catch (err) {\n handleError(toaster, err);\n }\n };\n };\n\n function handleError(toaster, err) {\n toaster.show({\n message: `${err}`,\n intent: \"danger\",\n });\n }\n\n const directionCombos = [\"up\", \"down\", \"left\", \"right\"].map((dir) => {\n return {\n combo: dir,\n group: \"Navigation\",\n label: \"Move focus \" + dir,\n preventDefault: true,\n onKeyDown: keyHandler((e, state) => {\n state.moveFocusedCell(dir as any);\n }),\n };\n });\n\n /** TODO: merge a bit more with the actions config */\n\n const actionsCombos = tableActions\n .map((action) => {\n if (action.hotkey == null) return null;\n return {\n combo: action.hotkey,\n label: action.name,\n group: action.group ?? \"Actions\",\n preventDefault: true,\n disabled: actionRequiresEditable(action) && !editable,\n onKeyDown(event: KeyboardEvent<any>): any {\n runActionWrapper(action, get, set, toaster);\n },\n } as any as HotkeyConfig;\n })\n .filter(Boolean) as HotkeyConfig[];\n\n // Backspace/Delete: on a whole-row selection, delete the row(s); otherwise\n // clear the selected cells.\n const clearOrDeleteSelection = keyHandler((e, state) => {\n const sel = state.selection ?? [];\n const isRowSelection =\n sel.length > 0 && sel.every((r) => r.cols == null && r.rows != null);\n if (isRowSelection) {\n state.deleteSelectedRows();\n } else {\n state.clearSelection();\n }\n });\n\n return [\n {\n combo: \"esc\",\n label: \"Exit editing / clear selection\",\n group: \"Selection\",\n allowInInput: true,\n onKeyDown: keyHandler((e, state) => {\n // First Escape closes the focused cell's open surface (editor/panel)\n // while keeping the selection, so arrow keys keep working — in both\n // auto and manual modes. A second Escape (nothing open) clears the\n // selection.\n if (state.cellSurfaceOpen) {\n state.closeCellSurface();\n e.preventDefault();\n e.stopPropagation();\n return;\n }\n if (state.selection.length == 0) {\n // Focus goes back to parent\n return;\n }\n state.setSelection([]);\n e.preventDefault();\n e.stopPropagation();\n }),\n },\n {\n combo: \"backspace\",\n label: \"Clear cells / delete rows\",\n group: \"Selection\",\n allowInInput: true,\n disabled: !editable,\n onKeyDown: clearOrDeleteSelection,\n },\n {\n combo: \"del\",\n label: \"Clear cells / delete rows\",\n group: \"Selection\",\n allowInInput: true,\n disabled: !editable,\n onKeyDown: clearOrDeleteSelection,\n },\n {\n combo: \"tab\",\n label: \"Move focus right (or left with shift)\",\n group: \"Navigation\",\n allowInInput: true,\n onKeyDown: keyHandler((e, state) => {\n state.moveFocusedCell(e.shiftKey ? \"left\" : \"right\");\n }),\n },\n {\n combo: \"enter\",\n label: \"Edit cell / move focus down\",\n group: \"Navigation\",\n allowInInput: true,\n onKeyDown: keyHandler((e, state) => {\n // In navigation mode (no surface open), Enter enters edit mode —\n // opening and focusing the focused cell's surface (Google Sheets\n // style). While a surface is open, Enter advances downward as usual\n // (an inline editor blurs and this moves to the next row).\n if (!state.cellSurfaceOpen) {\n state.armAutoActivate();\n state.openCellSurface();\n e.preventDefault();\n return;\n }\n state.moveFocusedCell(\"down\");\n }),\n },\n {\n combo: \"f2\",\n label: \"Edit / open cell\",\n group: \"Editing\",\n onKeyDown: keyHandler((e, state) => {\n // Enter edit mode from the keyboard: re-arm auto-activation and open\n // the focused cell's surface (which focuses the editor).\n state.armAutoActivate();\n state.openCellSurface();\n e.preventDefault();\n }),\n },\n ...directionCombos,\n ...actionsCombos,\n ];\n },\n);\n\nconst selectionAtom = atom((get) => {\n const store = get(storeAtom);\n return store?.selection ?? [];\n});\n\nconst isSingleCellSelectionAtom = atom((get) => {\n const selection = get(selectionAtom);\n return singleFocusedCell(selection) != null;\n});\n\nexport const editorKeyHandlerAtom = atom((get) => {\n const store = get(storeAPIAtom);\n return (e: KeyboardEvent) => {\n if (e.key === \"Escape\") {\n // Escape exits the inline editor: close the surface (which enters\n // navigation mode in auto), keep the selection, and return focus to the\n // table so arrow keys navigate. Stop propagation so the table's own\n // Escape hotkey doesn't also fire.\n const state = store?.getState();\n state?.closeCellSurface();\n state?.tableElement?.focus();\n e.preventDefault();\n e.stopPropagation();\n return;\n }\n const isSingleCellSelection = get(isSingleCellSelectionAtom);\n editorKeyHandler(e as any, isSingleCellSelection);\n };\n});\n\nfunction editorKeyHandler(\n e: KeyboardEvent<HTMLInputElement>,\n isSingleCellSelection: boolean,\n) {\n /** Get a key handler for inline cell editing */\n\n const target = e.target as HTMLInputElement;\n\n let shouldCaptureEventForInlineEditing = true;\n\n if (e.key == \"Enter\") {\n shouldCaptureEventForInlineEditing = e.shiftKey;\n }\n\n if (e.key == \"Escape\" || e.key == \"Tab\") {\n shouldCaptureEventForInlineEditing = false;\n }\n\n if (e.key == \"Backspace\" || e.key == \"Delete\") {\n /** Clear selection on Backspace or Delete on a multi-cell selection\n * (if shift is held, we allow deletion in a single cell)\n */\n shouldCaptureEventForInlineEditing = isSingleCellSelection && !e.shiftKey;\n }\n\n if (!shouldCaptureEventForInlineEditing) {\n target.blur();\n e.preventDefault();\n return;\n }\n\n const shouldPropagate = shouldPropagateKeystroke(e);\n if (!shouldPropagate) {\n e.stopPropagation();\n //e.preventDefault();\n } else {\n // we blur the editor and dispatch a keydown event on the parent to propagate the event to the table\n e.preventDefault();\n target.blur();\n //console.log(tableElement);\n //tableElement.focus();\n //target.parentNode?.dispatchEvent(e);\n target.parentNode?.dispatchEvent(new KeyboardEvent(\"keydown\", e as any));\n }\n}\n\nfunction shouldPropagateKeystroke(evt: any): boolean {\n if (evt.target.selectionStart == evt.target.selectionEnd) {\n // We don't have anything selected, so we can propagate the event potentially\n // Propagate copy and paste events even if there is no selection, since they might want to copy/paste an entire row/column based on the focused cell.\n if (evt.key === \"c\" || evt.key === \"v\" || evt.key === \"x\") {\n if (evt.ctrlKey || evt.metaKey) {\n return true;\n }\n }\n }\n\n if (evt.key === \"ArrowUp\" || evt.key === \"ArrowDown\") {\n return true;\n }\n\n const target = evt.target;\n if (\n (evt.key === \"ArrowLeft\" ||\n evt.key === \"ArrowUp\" ||\n evt.key === \"ArrowDown\") &&\n target.selectionStart === 0\n ) {\n return true;\n }\n\n if (\n (evt.key === \"ArrowRight\" ||\n evt.key === \"ArrowUp\" ||\n evt.key === \"Tab\" ||\n evt.key === \"Enter\" ||\n evt.key === \"ArrowDown\") &&\n target.selectionStart === evt.target.value.length\n ) {\n return true;\n }\n\n return false;\n}\n\nexport function multiLineTextKeyHandler(evt: any) {\n if (evt.key === \"Enter\") {\n //evt.preventDefault();\n evt.stopPropagation();\n return;\n }\n\n if (evt.key === \"Escape\") {\n evt.preventDefault();\n return;\n }\n\n const shouldPropagate = shouldPropagateKeystroke(evt);\n if (!shouldPropagate) {\n evt.stopPropagation();\n } else {\n evt.target.blur();\n }\n}\n"],"names":["atom","storeAtom","selection","storeAPIAtom","toaster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aAAa;AACpD,WAAO;AAAA,EACT;AAEA,QAAM,SAAS,IAAI;AACnB,OACG,IAAI,QAAQ,eACX,IAAI,QAAQ,aACZ,IAAI,QAAQ,gBACd,OAAO,mBAAmB,GAC1B;AACA,WAAO;AAAA,EACT;AAEA,OACG,IAAI,QAAQ,gBACX,IAAI,QAAQ,aACZ,IAAI,QAAQ,SACZ,IAAI,QAAQ,WACZ,IAAI,QAAQ,gBACd,OAAO,mBAAmB,IAAI,OAAO,MAAM,QAC3C;AACA,WAAO;AAAA,EACT;AAEA,SAAO;AACT;AAEO,SAAS,wBAAwB,KAAU;AAChD,MAAI,IAAI,QAAQ,SAAS;AAEvB,QAAI,gBAAA;AACJ;AAAA,EACF;AAEA,MAAI,IAAI,QAAQ,UAAU;AACxB,QAAI,eAAA;AACJ;AAAA,EACF;AAEA,QAAM,kBAAkB,yBAAyB,GAAG;AACpD,MAAI,CAAC,iBAAiB;AACpB,QAAI,gBAAA;AAAA,EACN,OAAO;AACL,QAAI,OAAO,KAAA;AAAA,EACb;AACF;;;;"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { atom } from "jotai";
|
|
2
|
+
import { actionRequiresEditable } from "../actions/selection.js";
|
|
2
3
|
import { toasterAtom } from "../notifications.js";
|
|
3
4
|
import { storeAPIAtom, tableActionsAtom, storeAtom } from "../provider/core.js";
|
|
4
5
|
import { runActionWrapper } from "../actions/context.js";
|
|
@@ -62,7 +63,7 @@ const tableHotkeysAtom = atom(
|
|
|
62
63
|
label: action.name,
|
|
63
64
|
group: action.group ?? "Actions",
|
|
64
65
|
preventDefault: true,
|
|
65
|
-
disabled: action
|
|
66
|
+
disabled: actionRequiresEditable(action) && !editable,
|
|
66
67
|
onKeyDown(event) {
|
|
67
68
|
runActionWrapper(action, get, set, toaster);
|
|
68
69
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"key-handling.js","sources":["../../src/utils/key-handling.ts"],"sourcesContent":["import type { KeyboardEvent } from \"react\";\nimport {\n storeAPIAtom,\n storeAtom,\n tableActionsAtom,\n type DataSheetStoreMain,\n singleFocusedCell,\n ctx,\n} from \"../provider\";\nimport { atom } from \"jotai\";\nimport { runActionWrapper } from \"../actions\";\nimport { HotkeyConfig } from \"@blueprintjs/core\";\nimport { toasterAtom } from \"../notifications.ts\";\n\ntype HotkeyActionRunner = (\n e: KeyboardEvent,\n state: DataSheetStoreMain<any>,\n setState: (state: Partial<DataSheetStoreMain<any>>) => void,\n) => void | Promise<void>;\n\nconst isEditableAtom = atom((get) => {\n const store = get(storeAtom);\n return store?.editable ?? false;\n});\n\nconst hasSelectionAtom = atom((get) => {\n const store = get(storeAtom);\n const selection = store?.selection ?? [];\n return selection.length > 0;\n});\n\nexport const tableHotkeysAtom = atom<null, [], HotkeyConfig[]>(\n null,\n (get, set) => {\n const store = get(storeAPIAtom);\n const toaster = get(toasterAtom);\n const editable = get(isEditableAtom);\n\n /** Set up hotkeys for table actions */\n const tableActions = get(tableActionsAtom);\n\n if (store == null) return [];\n const keyHandler = (\n actionRunner: HotkeyActionRunner,\n ): HotkeyConfig[\"onKeyDown\"] => {\n return (e) => {\n // Kind of a ridiculous hack to get the store from the atom.\n const state = store.getState();\n const setState = store.setState;\n try {\n const res = actionRunner(e as any, state, setState);\n if (res instanceof Promise) {\n res\n .then(() => {})\n .catch((err) => {\n console.error(err);\n handleError(toaster, err);\n });\n }\n } catch (err) {\n handleError(toaster, err);\n }\n };\n };\n\n function handleError(toaster, err) {\n toaster.show({\n message: `${err}`,\n intent: \"danger\",\n });\n }\n\n const directionCombos = [\"up\", \"down\", \"left\", \"right\"].map((dir) => {\n return {\n combo: dir,\n group: \"Navigation\",\n label: \"Move focus \" + dir,\n preventDefault: true,\n onKeyDown: keyHandler((e, state) => {\n state.moveFocusedCell(dir as any);\n }),\n };\n });\n\n /** TODO: merge a bit more with the actions config */\n\n const actionsCombos = tableActions\n .map((action) => {\n if (action.hotkey == null) return null;\n return {\n combo: action.hotkey,\n label: action.name,\n group: action.group ?? \"Actions\",\n preventDefault: true,\n disabled: action.requiresEditable !== false && !editable,\n onKeyDown(event: KeyboardEvent<any>): any {\n runActionWrapper(action, get, set, toaster);\n },\n } as any as HotkeyConfig;\n })\n .filter(Boolean) as HotkeyConfig[];\n\n // Backspace/Delete: on a whole-row selection, delete the row(s); otherwise\n // clear the selected cells.\n const clearOrDeleteSelection = keyHandler((e, state) => {\n const sel = state.selection ?? [];\n const isRowSelection =\n sel.length > 0 && sel.every((r) => r.cols == null && r.rows != null);\n if (isRowSelection) {\n state.deleteSelectedRows();\n } else {\n state.clearSelection();\n }\n });\n\n return [\n {\n combo: \"esc\",\n label: \"Exit editing / clear selection\",\n group: \"Selection\",\n allowInInput: true,\n onKeyDown: keyHandler((e, state) => {\n // First Escape closes the focused cell's open surface (editor/panel)\n // while keeping the selection, so arrow keys keep working — in both\n // auto and manual modes. A second Escape (nothing open) clears the\n // selection.\n if (state.cellSurfaceOpen) {\n state.closeCellSurface();\n e.preventDefault();\n e.stopPropagation();\n return;\n }\n if (state.selection.length == 0) {\n // Focus goes back to parent\n return;\n }\n state.setSelection([]);\n e.preventDefault();\n e.stopPropagation();\n }),\n },\n {\n combo: \"backspace\",\n label: \"Clear cells / delete rows\",\n group: \"Selection\",\n allowInInput: true,\n disabled: !editable,\n onKeyDown: clearOrDeleteSelection,\n },\n {\n combo: \"del\",\n label: \"Clear cells / delete rows\",\n group: \"Selection\",\n allowInInput: true,\n disabled: !editable,\n onKeyDown: clearOrDeleteSelection,\n },\n {\n combo: \"tab\",\n label: \"Move focus right (or left with shift)\",\n group: \"Navigation\",\n allowInInput: true,\n onKeyDown: keyHandler((e, state) => {\n state.moveFocusedCell(e.shiftKey ? \"left\" : \"right\");\n }),\n },\n {\n combo: \"enter\",\n label: \"Edit cell / move focus down\",\n group: \"Navigation\",\n allowInInput: true,\n onKeyDown: keyHandler((e, state) => {\n // In navigation mode (no surface open), Enter enters edit mode —\n // opening and focusing the focused cell's surface (Google Sheets\n // style). While a surface is open, Enter advances downward as usual\n // (an inline editor blurs and this moves to the next row).\n if (!state.cellSurfaceOpen) {\n state.armAutoActivate();\n state.openCellSurface();\n e.preventDefault();\n return;\n }\n state.moveFocusedCell(\"down\");\n }),\n },\n {\n combo: \"f2\",\n label: \"Edit / open cell\",\n group: \"Editing\",\n onKeyDown: keyHandler((e, state) => {\n // Enter edit mode from the keyboard: re-arm auto-activation and open\n // the focused cell's surface (which focuses the editor).\n state.armAutoActivate();\n state.openCellSurface();\n e.preventDefault();\n }),\n },\n ...directionCombos,\n ...actionsCombos,\n ];\n },\n);\n\nconst selectionAtom = atom((get) => {\n const store = get(storeAtom);\n return store?.selection ?? [];\n});\n\nconst isSingleCellSelectionAtom = atom((get) => {\n const selection = get(selectionAtom);\n return singleFocusedCell(selection) != null;\n});\n\nexport const editorKeyHandlerAtom = atom((get) => {\n const store = get(storeAPIAtom);\n return (e: KeyboardEvent) => {\n if (e.key === \"Escape\") {\n // Escape exits the inline editor: close the surface (which enters\n // navigation mode in auto), keep the selection, and return focus to the\n // table so arrow keys navigate. Stop propagation so the table's own\n // Escape hotkey doesn't also fire.\n const state = store?.getState();\n state?.closeCellSurface();\n state?.tableElement?.focus();\n e.preventDefault();\n e.stopPropagation();\n return;\n }\n const isSingleCellSelection = get(isSingleCellSelectionAtom);\n editorKeyHandler(e as any, isSingleCellSelection);\n };\n});\n\nfunction editorKeyHandler(\n e: KeyboardEvent<HTMLInputElement>,\n isSingleCellSelection: boolean,\n) {\n /** Get a key handler for inline cell editing */\n\n const target = e.target as HTMLInputElement;\n\n let shouldCaptureEventForInlineEditing = true;\n\n if (e.key == \"Enter\") {\n shouldCaptureEventForInlineEditing = e.shiftKey;\n }\n\n if (e.key == \"Escape\" || e.key == \"Tab\") {\n shouldCaptureEventForInlineEditing = false;\n }\n\n if (e.key == \"Backspace\" || e.key == \"Delete\") {\n /** Clear selection on Backspace or Delete on a multi-cell selection\n * (if shift is held, we allow deletion in a single cell)\n */\n shouldCaptureEventForInlineEditing = isSingleCellSelection && !e.shiftKey;\n }\n\n if (!shouldCaptureEventForInlineEditing) {\n target.blur();\n e.preventDefault();\n return;\n }\n\n const shouldPropagate = shouldPropagateKeystroke(e);\n if (!shouldPropagate) {\n e.stopPropagation();\n //e.preventDefault();\n } else {\n // we blur the editor and dispatch a keydown event on the parent to propagate the event to the table\n e.preventDefault();\n target.blur();\n //console.log(tableElement);\n //tableElement.focus();\n //target.parentNode?.dispatchEvent(e);\n target.parentNode?.dispatchEvent(new KeyboardEvent(\"keydown\", e as any));\n }\n}\n\nfunction shouldPropagateKeystroke(evt: any): boolean {\n if (evt.target.selectionStart == evt.target.selectionEnd) {\n // We don't have anything selected, so we can propagate the event potentially\n // Propagate copy and paste events even if there is no selection, since they might want to copy/paste an entire row/column based on the focused cell.\n if (evt.key === \"c\" || evt.key === \"v\" || evt.key === \"x\") {\n if (evt.ctrlKey || evt.metaKey) {\n return true;\n }\n }\n }\n\n if (evt.key === \"ArrowUp\" || evt.key === \"ArrowDown\") {\n return true;\n }\n\n const target = evt.target;\n if (\n (evt.key === \"ArrowLeft\" ||\n evt.key === \"ArrowUp\" ||\n evt.key === \"ArrowDown\") &&\n target.selectionStart === 0\n ) {\n return true;\n }\n\n if (\n (evt.key === \"ArrowRight\" ||\n evt.key === \"ArrowUp\" ||\n evt.key === \"Tab\" ||\n evt.key === \"Enter\" ||\n evt.key === \"ArrowDown\") &&\n target.selectionStart === evt.target.value.length\n ) {\n return true;\n }\n\n return false;\n}\n\nexport function multiLineTextKeyHandler(evt: any) {\n if (evt.key === \"Enter\") {\n //evt.preventDefault();\n evt.stopPropagation();\n return;\n }\n\n if (evt.key === \"Escape\") {\n evt.preventDefault();\n return;\n }\n\n const shouldPropagate = shouldPropagateKeystroke(evt);\n if (!shouldPropagate) {\n evt.stopPropagation();\n } else {\n evt.target.blur();\n }\n}\n"],"names":["toaster"],"mappings":";;;;;AAoBA,MAAM,iBAAiB,KAAK,CAAC,QAAQ;AACnC,QAAM,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,QAAQ,SACZ,IAAI,QAAQ,WACZ,IAAI,QAAQ,gBACd,OAAO,mBAAmB,IAAI,OAAO,MAAM,QAC3C;AACA,WAAO;AAAA,EACT;AAEA,SAAO;AACT;AAEO,SAAS,wBAAwB,KAAU;AAChD,MAAI,IAAI,QAAQ,SAAS;AAEvB,QAAI,gBAAA;AACJ;AAAA,EACF;AAEA,MAAI,IAAI,QAAQ,UAAU;AACxB,QAAI,eAAA;AACJ;AAAA,EACF;AAEA,QAAM,kBAAkB,yBAAyB,GAAG;AACpD,MAAI,CAAC,iBAAiB;AACpB,QAAI,gBAAA;AAAA,EACN,OAAO;AACL,QAAI,OAAO,KAAA;AAAA,EACb;AACF;"}
|
|
1
|
+
{"version":3,"file":"key-handling.js","sources":["../../src/utils/key-handling.ts"],"sourcesContent":["import type { KeyboardEvent } from \"react\";\nimport {\n storeAPIAtom,\n storeAtom,\n tableActionsAtom,\n type DataSheetStoreMain,\n singleFocusedCell,\n ctx,\n} from \"../provider\";\nimport { atom } from \"jotai\";\nimport { runActionWrapper } from \"../actions\";\nimport { actionRequiresEditable } from \"../actions/selection.ts\";\nimport { HotkeyConfig } from \"@blueprintjs/core\";\nimport { toasterAtom } from \"../notifications.ts\";\n\ntype HotkeyActionRunner = (\n e: KeyboardEvent,\n state: DataSheetStoreMain<any>,\n setState: (state: Partial<DataSheetStoreMain<any>>) => void,\n) => void | Promise<void>;\n\nconst isEditableAtom = atom((get) => {\n const store = get(storeAtom);\n return store?.editable ?? false;\n});\n\nconst hasSelectionAtom = atom((get) => {\n const store = get(storeAtom);\n const selection = store?.selection ?? [];\n return selection.length > 0;\n});\n\nexport const tableHotkeysAtom = atom<null, [], HotkeyConfig[]>(\n null,\n (get, set) => {\n const store = get(storeAPIAtom);\n const toaster = get(toasterAtom);\n const editable = get(isEditableAtom);\n\n /** Set up hotkeys for table actions */\n const tableActions = get(tableActionsAtom);\n\n if (store == null) return [];\n const keyHandler = (\n actionRunner: HotkeyActionRunner,\n ): HotkeyConfig[\"onKeyDown\"] => {\n return (e) => {\n // Kind of a ridiculous hack to get the store from the atom.\n const state = store.getState();\n const setState = store.setState;\n try {\n const res = actionRunner(e as any, state, setState);\n if (res instanceof Promise) {\n res\n .then(() => {})\n .catch((err) => {\n console.error(err);\n handleError(toaster, err);\n });\n }\n } catch (err) {\n handleError(toaster, err);\n }\n };\n };\n\n function handleError(toaster, err) {\n toaster.show({\n message: `${err}`,\n intent: \"danger\",\n });\n }\n\n const directionCombos = [\"up\", \"down\", \"left\", \"right\"].map((dir) => {\n return {\n combo: dir,\n group: \"Navigation\",\n label: \"Move focus \" + dir,\n preventDefault: true,\n onKeyDown: keyHandler((e, state) => {\n state.moveFocusedCell(dir as any);\n }),\n };\n });\n\n /** TODO: merge a bit more with the actions config */\n\n const actionsCombos = tableActions\n .map((action) => {\n if (action.hotkey == null) return null;\n return {\n combo: action.hotkey,\n label: action.name,\n group: action.group ?? \"Actions\",\n preventDefault: true,\n disabled: actionRequiresEditable(action) && !editable,\n onKeyDown(event: KeyboardEvent<any>): any {\n runActionWrapper(action, get, set, toaster);\n },\n } as any as HotkeyConfig;\n })\n .filter(Boolean) as HotkeyConfig[];\n\n // Backspace/Delete: on a whole-row selection, delete the row(s); otherwise\n // clear the selected cells.\n const clearOrDeleteSelection = keyHandler((e, state) => {\n const sel = state.selection ?? [];\n const isRowSelection =\n sel.length > 0 && sel.every((r) => r.cols == null && r.rows != null);\n if (isRowSelection) {\n state.deleteSelectedRows();\n } else {\n state.clearSelection();\n }\n });\n\n return [\n {\n combo: \"esc\",\n label: \"Exit editing / clear selection\",\n group: \"Selection\",\n allowInInput: true,\n onKeyDown: keyHandler((e, state) => {\n // First Escape closes the focused cell's open surface (editor/panel)\n // while keeping the selection, so arrow keys keep working — in both\n // auto and manual modes. A second Escape (nothing open) clears the\n // selection.\n if (state.cellSurfaceOpen) {\n state.closeCellSurface();\n e.preventDefault();\n e.stopPropagation();\n return;\n }\n if (state.selection.length == 0) {\n // Focus goes back to parent\n return;\n }\n state.setSelection([]);\n e.preventDefault();\n e.stopPropagation();\n }),\n },\n {\n combo: \"backspace\",\n label: \"Clear cells / delete rows\",\n group: \"Selection\",\n allowInInput: true,\n disabled: !editable,\n onKeyDown: clearOrDeleteSelection,\n },\n {\n combo: \"del\",\n label: \"Clear cells / delete rows\",\n group: \"Selection\",\n allowInInput: true,\n disabled: !editable,\n onKeyDown: clearOrDeleteSelection,\n },\n {\n combo: \"tab\",\n label: \"Move focus right (or left with shift)\",\n group: \"Navigation\",\n allowInInput: true,\n onKeyDown: keyHandler((e, state) => {\n state.moveFocusedCell(e.shiftKey ? \"left\" : \"right\");\n }),\n },\n {\n combo: \"enter\",\n label: \"Edit cell / move focus down\",\n group: \"Navigation\",\n allowInInput: true,\n onKeyDown: keyHandler((e, state) => {\n // In navigation mode (no surface open), Enter enters edit mode —\n // opening and focusing the focused cell's surface (Google Sheets\n // style). While a surface is open, Enter advances downward as usual\n // (an inline editor blurs and this moves to the next row).\n if (!state.cellSurfaceOpen) {\n state.armAutoActivate();\n state.openCellSurface();\n e.preventDefault();\n return;\n }\n state.moveFocusedCell(\"down\");\n }),\n },\n {\n combo: \"f2\",\n label: \"Edit / open cell\",\n group: \"Editing\",\n onKeyDown: keyHandler((e, state) => {\n // Enter edit mode from the keyboard: re-arm auto-activation and open\n // the focused cell's surface (which focuses the editor).\n state.armAutoActivate();\n state.openCellSurface();\n e.preventDefault();\n }),\n },\n ...directionCombos,\n ...actionsCombos,\n ];\n },\n);\n\nconst selectionAtom = atom((get) => {\n const store = get(storeAtom);\n return store?.selection ?? [];\n});\n\nconst isSingleCellSelectionAtom = atom((get) => {\n const selection = get(selectionAtom);\n return singleFocusedCell(selection) != null;\n});\n\nexport const editorKeyHandlerAtom = atom((get) => {\n const store = get(storeAPIAtom);\n return (e: KeyboardEvent) => {\n if (e.key === \"Escape\") {\n // Escape exits the inline editor: close the surface (which enters\n // navigation mode in auto), keep the selection, and return focus to the\n // table so arrow keys navigate. Stop propagation so the table's own\n // Escape hotkey doesn't also fire.\n const state = store?.getState();\n state?.closeCellSurface();\n state?.tableElement?.focus();\n e.preventDefault();\n e.stopPropagation();\n return;\n }\n const isSingleCellSelection = get(isSingleCellSelectionAtom);\n editorKeyHandler(e as any, isSingleCellSelection);\n };\n});\n\nfunction editorKeyHandler(\n e: KeyboardEvent<HTMLInputElement>,\n isSingleCellSelection: boolean,\n) {\n /** Get a key handler for inline cell editing */\n\n const target = e.target as HTMLInputElement;\n\n let shouldCaptureEventForInlineEditing = true;\n\n if (e.key == \"Enter\") {\n shouldCaptureEventForInlineEditing = e.shiftKey;\n }\n\n if (e.key == \"Escape\" || e.key == \"Tab\") {\n shouldCaptureEventForInlineEditing = false;\n }\n\n if (e.key == \"Backspace\" || e.key == \"Delete\") {\n /** Clear selection on Backspace or Delete on a multi-cell selection\n * (if shift is held, we allow deletion in a single cell)\n */\n shouldCaptureEventForInlineEditing = isSingleCellSelection && !e.shiftKey;\n }\n\n if (!shouldCaptureEventForInlineEditing) {\n target.blur();\n e.preventDefault();\n return;\n }\n\n const shouldPropagate = shouldPropagateKeystroke(e);\n if (!shouldPropagate) {\n e.stopPropagation();\n //e.preventDefault();\n } else {\n // we blur the editor and dispatch a keydown event on the parent to propagate the event to the table\n e.preventDefault();\n target.blur();\n //console.log(tableElement);\n //tableElement.focus();\n //target.parentNode?.dispatchEvent(e);\n target.parentNode?.dispatchEvent(new KeyboardEvent(\"keydown\", e as any));\n }\n}\n\nfunction shouldPropagateKeystroke(evt: any): boolean {\n if (evt.target.selectionStart == evt.target.selectionEnd) {\n // We don't have anything selected, so we can propagate the event potentially\n // Propagate copy and paste events even if there is no selection, since they might want to copy/paste an entire row/column based on the focused cell.\n if (evt.key === \"c\" || evt.key === \"v\" || evt.key === \"x\") {\n if (evt.ctrlKey || evt.metaKey) {\n return true;\n }\n }\n }\n\n if (evt.key === \"ArrowUp\" || evt.key === \"ArrowDown\") {\n return true;\n }\n\n const target = evt.target;\n if (\n (evt.key === \"ArrowLeft\" ||\n evt.key === \"ArrowUp\" ||\n evt.key === \"ArrowDown\") &&\n target.selectionStart === 0\n ) {\n return true;\n }\n\n if (\n (evt.key === \"ArrowRight\" ||\n evt.key === \"ArrowUp\" ||\n evt.key === \"Tab\" ||\n evt.key === \"Enter\" ||\n evt.key === \"ArrowDown\") &&\n target.selectionStart === evt.target.value.length\n ) {\n return true;\n }\n\n return false;\n}\n\nexport function multiLineTextKeyHandler(evt: any) {\n if (evt.key === \"Enter\") {\n //evt.preventDefault();\n evt.stopPropagation();\n return;\n }\n\n if (evt.key === \"Escape\") {\n evt.preventDefault();\n return;\n }\n\n const shouldPropagate = shouldPropagateKeystroke(evt);\n if (!shouldPropagate) {\n evt.stopPropagation();\n } else {\n evt.target.blur();\n }\n}\n"],"names":["toaster"],"mappings":";;;;;;AAqBA,MAAM,iBAAiB,KAAK,CAAC,QAAQ;AACnC,QAAM,QAAQ,IAAI,SAAS;AAC3B,SAAO,OAAO,YAAY;AAC5B,CAAC;AAEwB,KAAK,CAAC,QAAQ;AACrC,QAAM,QAAQ,IAAI,SAAS;AAC3B,QAAM,YAAY,OAAO,aAAa,CAAA;AACtC,SAAO,UAAU,SAAS;AAC5B,CAAC;AAEM,MAAM,mBAAmB;AAAA,EAC9B;AAAA,EACA,CAAC,KAAK,QAAQ;AACZ,UAAM,QAAQ,IAAI,YAAY;AAC9B,UAAM,UAAU,IAAI,WAAW;AAC/B,UAAM,WAAW,IAAI,cAAc;AAGnC,UAAM,eAAe,IAAI,gBAAgB;AAEzC,QAAI,SAAS,KAAM,QAAO,CAAA;AAC1B,UAAM,aAAa,CACjB,iBAC8B;AAC9B,aAAO,CAAC,MAAM;AAEZ,cAAM,QAAQ,MAAM,SAAA;AACpB,cAAM,WAAW,MAAM;AACvB,YAAI;AACF,gBAAM,MAAM,aAAa,GAAU,OAAO,QAAQ;AAClD,cAAI,eAAe,SAAS;AAC1B,gBACG,KAAK,MAAM;AAAA,YAAC,CAAC,EACb,MAAM,CAAC,QAAQ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,YAAY,cAAc,IAAI,cAAc,WAAW,CAAQ,CAAC;AAAA,EACzE;AACF;AAEA,SAAS,yBAAyB,KAAmB;AACnD,MAAI,IAAI,OAAO,kBAAkB,IAAI,OAAO,cAAc;AAGxD,QAAI,IAAI,QAAQ,OAAO,IAAI,QAAQ,OAAO,IAAI,QAAQ,KAAK;AACzD,UAAI,IAAI,WAAW,IAAI,SAAS;AAC9B,eAAO;AAAA,MACT;AAAA,IACF;AAAA,EACF;AAEA,MAAI,IAAI,QAAQ,aAAa,IAAI,QAAQ,aAAa;AACpD,WAAO;AAAA,EACT;AAEA,QAAM,SAAS,IAAI;AACnB,OACG,IAAI,QAAQ,eACX,IAAI,QAAQ,aACZ,IAAI,QAAQ,gBACd,OAAO,mBAAmB,GAC1B;AACA,WAAO;AAAA,EACT;AAEA,OACG,IAAI,QAAQ,gBACX,IAAI,QAAQ,aACZ,IAAI,QAAQ,SACZ,IAAI,QAAQ,WACZ,IAAI,QAAQ,gBACd,OAAO,mBAAmB,IAAI,OAAO,MAAM,QAC3C;AACA,WAAO;AAAA,EACT;AAEA,SAAO;AACT;AAEO,SAAS,wBAAwB,KAAU;AAChD,MAAI,IAAI,QAAQ,SAAS;AAEvB,QAAI,gBAAA;AACJ;AAAA,EACF;AAEA,MAAI,IAAI,QAAQ,UAAU;AACxB,QAAI,eAAA;AACJ;AAAA,EACF;AAEA,QAAM,kBAAkB,yBAAyB,GAAG;AACpD,MAAI,CAAC,iBAAiB;AACpB,QAAI,gBAAA;AAAA,EACN,OAAO;AACL,QAAI,OAAO,KAAA;AAAA,EACb;AACF;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@macrostrat/data-sheet",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.10.0",
|
|
4
4
|
"description": "Scalable data sheet with optional editing capabilities",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
"@blueprintjs/core": "^6.14.0",
|
|
50
50
|
"@blueprintjs/table": "^6.1.0",
|
|
51
51
|
"@macrostrat/color-utils": "^1.2.2",
|
|
52
|
-
"@macrostrat/data-components": "^1.
|
|
52
|
+
"@macrostrat/data-components": "^1.8.0",
|
|
53
53
|
"@macrostrat/hyper": "^3.1.1",
|
|
54
54
|
"@macrostrat/scoped-store": "^1.1.0",
|
|
55
55
|
"@macrostrat/ui-components": "^5.2.0",
|
package/src/actions/selection.ts
CHANGED
|
@@ -127,14 +127,15 @@ export function computeSelectionShape(regions: Region[]): SelectionShape {
|
|
|
127
127
|
}
|
|
128
128
|
|
|
129
129
|
/** Filter actions to those applicable for the current selection cardinality
|
|
130
|
-
* and edit mode.
|
|
130
|
+
* and edit mode. An action takes edits unless it says otherwise
|
|
131
|
+
* (`requiresEditable` defaults to `true`). */
|
|
131
132
|
export function getApplicableActions<T>(
|
|
132
133
|
actions: TableAction<T>[],
|
|
133
134
|
cardinality: RegionCardinality,
|
|
134
135
|
editable: boolean,
|
|
135
136
|
): TableAction<T>[] {
|
|
136
137
|
return actions.filter((action) => {
|
|
137
|
-
if (action
|
|
138
|
+
if (actionRequiresEditable(action) && !editable) return false;
|
|
138
139
|
return action.targets.includes(cardinality);
|
|
139
140
|
});
|
|
140
141
|
}
|
|
@@ -242,3 +243,11 @@ export function range(arr: number[]) {
|
|
|
242
243
|
const [start, end] = arr;
|
|
243
244
|
return Array.from({ length: end - start + 1 }, (_, i) => i + start);
|
|
244
245
|
}
|
|
246
|
+
|
|
247
|
+
/** Whether an action needs the table to be editable: unless it says it
|
|
248
|
+
* doesn't, as its type documents — and as the hotkeys have always read it. */
|
|
249
|
+
export function actionRequiresEditable(action: {
|
|
250
|
+
requiresEditable?: boolean;
|
|
251
|
+
}): boolean {
|
|
252
|
+
return action.requiresEditable ?? true;
|
|
253
|
+
}
|
|
@@ -27,7 +27,10 @@ import { type ReactNode, useState } from "react";
|
|
|
27
27
|
import { Button, PopoverNext } from "@blueprintjs/core";
|
|
28
28
|
import { type Region, RegionCardinality } from "@blueprintjs/table";
|
|
29
29
|
import type { TableAction, TableActionContext } from "../actions";
|
|
30
|
-
import {
|
|
30
|
+
import {
|
|
31
|
+
actionRequiresEditable,
|
|
32
|
+
computeSelectionShape,
|
|
33
|
+
} from "../actions/selection.ts";
|
|
31
34
|
import type { CellEdit, ColumnSpec } from "../provider";
|
|
32
35
|
import styles from "./row-editor.module.sass";
|
|
33
36
|
|
|
@@ -69,7 +72,7 @@ export function editorActionsFor<T>(
|
|
|
69
72
|
if (action.render != null || action.hotkey != null) return false;
|
|
70
73
|
if (action.targets.includes(RegionCardinality.FULL_TABLE)) return false;
|
|
71
74
|
if (!cardinalities.some((c) => action.targets.includes(c))) return false;
|
|
72
|
-
if (action
|
|
75
|
+
if (actionRequiresEditable(action) && !target.editable) return false;
|
|
73
76
|
return action.appliesTo?.(ctx) ?? true;
|
|
74
77
|
});
|
|
75
78
|
return { actions: applicable, ctx };
|
|
@@ -99,6 +99,7 @@ export function useDataPanelControls(
|
|
|
99
99
|
name: filter.name,
|
|
100
100
|
icon: filter.icon,
|
|
101
101
|
targets: ALL_CARDINALITIES,
|
|
102
|
+
requiresEditable: false,
|
|
102
103
|
render: () => h(InlineFilterControl, { filter }),
|
|
103
104
|
});
|
|
104
105
|
}
|
|
@@ -303,6 +304,7 @@ function useFilterMenuAction(entries: FilterEntry[]): TableAction | null {
|
|
|
303
304
|
icon: "filter",
|
|
304
305
|
description: "Add a filter to the data panel.",
|
|
305
306
|
targets: ALL_CARDINALITIES,
|
|
307
|
+
requiresEditable: false,
|
|
306
308
|
render: (ctx) =>
|
|
307
309
|
h(
|
|
308
310
|
MenuDropdown,
|
|
@@ -341,6 +343,7 @@ function useSortAction(): TableAction | null {
|
|
|
341
343
|
icon: "sort",
|
|
342
344
|
description: "Add a sort to the data panel.",
|
|
343
345
|
targets: ALL_CARDINALITIES,
|
|
346
|
+
requiresEditable: false,
|
|
344
347
|
render: (ctx) =>
|
|
345
348
|
h(MenuDropdown, { content: sortMenu }, [
|
|
346
349
|
h(
|
|
@@ -3,6 +3,7 @@ import { type ReactNode, useCallback, useMemo, useState } from "react";
|
|
|
3
3
|
import h from "./actions-toolbar.module.sass";
|
|
4
4
|
import { ctx, selectionAtom, useSelector } from "../../provider";
|
|
5
5
|
import {
|
|
6
|
+
actionRequiresEditable,
|
|
6
7
|
getApplicableActions,
|
|
7
8
|
getSelectionCardinality,
|
|
8
9
|
mergeColumnActions,
|
|
@@ -58,11 +59,16 @@ export function ActionsToolbar<T>({
|
|
|
58
59
|
);
|
|
59
60
|
|
|
60
61
|
const store = ctx.useStore();
|
|
61
|
-
//
|
|
62
|
-
//
|
|
62
|
+
// The context snapshots the store, so it is rebuilt when what it resolves
|
|
63
|
+
// changes: the selection (through `allActions`), and the rows. Keyed on the
|
|
64
|
+
// selection alone, a selection made together with new rows — an inserted
|
|
65
|
+
// row, selected as it arrives — resolved against the rows before it.
|
|
66
|
+
const data = useSelector((state) => state.data);
|
|
67
|
+
const updatedData = useSelector((state) => state.updatedData);
|
|
68
|
+
const filteredRowIndices = useSelector((state) => state.filteredRowIndices);
|
|
63
69
|
const actionContext = useMemo(() => {
|
|
64
70
|
return buildActionContext(store.get, store.set);
|
|
65
|
-
}, [allActions]);
|
|
71
|
+
}, [allActions, data, updatedData, filteredRowIndices]);
|
|
66
72
|
|
|
67
73
|
// The toolbar is for actions that AREN'T keyboard-accessible: any action
|
|
68
74
|
// with a `hotkey` (copy/cut/paste, etc.) is reachable from the keyboard and
|
|
@@ -94,7 +100,7 @@ export function ActionsToolbar<T>({
|
|
|
94
100
|
a.placement === "end" ||
|
|
95
101
|
((a.targets.includes(RegionCardinality.FULL_TABLE) ||
|
|
96
102
|
a.targets.includes("none" as any)) &&
|
|
97
|
-
a
|
|
103
|
+
actionRequiresEditable(a));
|
|
98
104
|
const contextual = shownActions.filter((a) => !isGlobal(a));
|
|
99
105
|
// Order the built-in global actions least→most impactful, left→right: reset
|
|
100
106
|
// changes, then save. Any other global actions keep their natural order to
|
package/src/data-sheet.ts
CHANGED
|
@@ -330,6 +330,12 @@ export function DataSheetRenderer<T>({
|
|
|
330
330
|
storeState.setState({ tableElement: tableElementRef.current });
|
|
331
331
|
}, [storeState]);
|
|
332
332
|
|
|
333
|
+
// The row height is the density's; cells report it to their renderers
|
|
334
|
+
const densityRowHeight = styleParamsForDensity(density).rowHeight;
|
|
335
|
+
useEffect(() => {
|
|
336
|
+
storeState.setState({ rowHeight: densityRowHeight });
|
|
337
|
+
}, [storeState, densityRowHeight]);
|
|
338
|
+
|
|
333
339
|
useEffect(() => {
|
|
334
340
|
storeState.setState({ onEdit });
|
|
335
341
|
}, [storeState, onEdit]);
|
package/src/main.module.sass
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
@use '@blueprintjs/core/lib/scss/variables' as bp
|
|
2
|
+
|
|
3
|
+
:global(.bp6-dark)
|
|
4
|
+
.data-sheet-container, .data-sheet-holder
|
|
5
|
+
--data-sheet-cell-background-color-internal: var(--data-sheet-cell-background-color, #{bp.$pt-dark-app-elevated-background-color})
|
|
6
|
+
--data-sheet-divider-color-internal: var(--data-sheet-divider-color, #{bp.$pt-dark-divider-black})
|
|
7
|
+
|
|
1
8
|
.data-sheet-container, .data-sheet-holder
|
|
2
9
|
flex: 1
|
|
3
10
|
min-height: 0
|
|
@@ -6,6 +13,8 @@
|
|
|
6
13
|
line-height: var(--data-sheet-row-height, 20px)
|
|
7
14
|
|
|
8
15
|
--data-sheet-cell-padding: 0px 8px
|
|
16
|
+
--data-sheet-divider-color-internal: var(--data-sheet-divider-color, #{bp.$pt-divider-black})
|
|
17
|
+
--data-sheet-cell-background-color-internal: var(--data-sheet-cell-background-color, #{bp.$pt-app-elevated-background-color})
|
|
9
18
|
|
|
10
19
|
:global
|
|
11
20
|
// Tags drawn in a cell (a tag picker as a value renderer, say) are pinned
|
|
@@ -25,6 +34,11 @@
|
|
|
25
34
|
font-size: var(--data-sheet-font-size)
|
|
26
35
|
line-height: var(--data-sheet-row-height)
|
|
27
36
|
|
|
37
|
+
.bp6-table-cell
|
|
38
|
+
box-shadow: inset 0 -1px 0 var(--data-sheet-divider-color-internal), inset -1px 0 0 var(--data-sheet-divider-color-internal)
|
|
39
|
+
.bp6-table-cell-client
|
|
40
|
+
background-color: var(--data-sheet-cell-background-color-internal)
|
|
41
|
+
|
|
28
42
|
.bp6-table-row-name
|
|
29
43
|
color: var(--secondary-color)
|
|
30
44
|
|
|
@@ -65,6 +65,14 @@ export interface CellRenderContext<T = any> {
|
|
|
65
65
|
/** Validation result for this cell, or `null` when valid. Orthogonal to the
|
|
66
66
|
* edit status — a cell can be both edited and invalid. */
|
|
67
67
|
validation: CellValidation | null;
|
|
68
|
+
/** The cell's size in pixels, border and padding included, when it is a
|
|
69
|
+
* grid cell — the column's width and the row's height, which the table
|
|
70
|
+
* knows and a renderer would otherwise have to measure. Absent on surfaces
|
|
71
|
+
* that aren't laid out in the cell (a popover, the row editor). A value
|
|
72
|
+
* renderer that lays itself out to fit — a row of tags — can re-fit when
|
|
73
|
+
* these change instead of observing its own size. */
|
|
74
|
+
width?: number;
|
|
75
|
+
height?: number;
|
|
68
76
|
}
|
|
69
77
|
|
|
70
78
|
/**
|
package/src/provider/types.ts
CHANGED
|
@@ -28,7 +28,14 @@ export type NavDirection = "up" | "down" | "left" | "right";
|
|
|
28
28
|
*/
|
|
29
29
|
export type EditEvent<T = any> =
|
|
30
30
|
| { type: "setCells"; cells: CellEdit[] }
|
|
31
|
-
| {
|
|
31
|
+
| {
|
|
32
|
+
type: "deleteRows";
|
|
33
|
+
rowIndices: number[];
|
|
34
|
+
/** The rows deleted, as they were. Rows the table added are spliced out
|
|
35
|
+
* of its arrays before this is reported, so the indices alone can no
|
|
36
|
+
* longer be looked up. */
|
|
37
|
+
rows?: T[];
|
|
38
|
+
}
|
|
32
39
|
| { type: "restoreRows"; rowIndices: number[] }
|
|
33
40
|
| { type: "addRow"; rowIndex: number; value: Partial<T> }
|
|
34
41
|
| { type: "resetChanges" };
|
|
@@ -250,6 +257,8 @@ export interface DataSheetStoreMain<T> extends DataSheetVals<T> {
|
|
|
250
257
|
tableRef: React.MutableRefObject<Table2> | null;
|
|
251
258
|
columnWidthsIndex: Map<string, number>;
|
|
252
259
|
defaultColumnWidth: number;
|
|
260
|
+
/** The grid's row height in pixels, from its density. */
|
|
261
|
+
rowHeight: number;
|
|
253
262
|
// Editable is not optional on store
|
|
254
263
|
editable: boolean;
|
|
255
264
|
}
|
|
@@ -35,6 +35,7 @@ export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
|
|
|
35
35
|
columnSpec: [],
|
|
36
36
|
deferColumnSpec: false,
|
|
37
37
|
defaultColumnWidth: 150,
|
|
38
|
+
rowHeight: 20,
|
|
38
39
|
editable: false,
|
|
39
40
|
selection: [],
|
|
40
41
|
fillValueBaseCell: null,
|
|
@@ -166,10 +167,12 @@ export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
|
|
|
166
167
|
if (!get().canDeleteRows) return;
|
|
167
168
|
// Remove selected rows from the data and updatedData arrays
|
|
168
169
|
let deletedIndices: number[] = [];
|
|
170
|
+
let deletedRows: T[] = [];
|
|
169
171
|
set((state) => {
|
|
170
|
-
const { selection
|
|
172
|
+
const { selection } = state;
|
|
171
173
|
const rowIndices = getRowIndices(selection);
|
|
172
174
|
deletedIndices = rowIndices;
|
|
175
|
+
deletedRows = rowIndices.map((i) => state.data[i]);
|
|
173
176
|
|
|
174
177
|
// Delete rows from both updatedData and data
|
|
175
178
|
|
|
@@ -219,7 +222,11 @@ export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
|
|
|
219
222
|
};
|
|
220
223
|
});
|
|
221
224
|
if (deletedIndices.length > 0) {
|
|
222
|
-
get().onEdit?.({
|
|
225
|
+
get().onEdit?.({
|
|
226
|
+
type: "deleteRows",
|
|
227
|
+
rowIndices: deletedIndices,
|
|
228
|
+
rows: deletedRows,
|
|
229
|
+
});
|
|
223
230
|
}
|
|
224
231
|
},
|
|
225
232
|
resetChanges(regions?: Region[]) {
|
|
@@ -40,6 +40,8 @@ function cellContentsEqual(
|
|
|
40
40
|
x.isEdited === y.isEdited &&
|
|
41
41
|
x.isDeleted === y.isDeleted &&
|
|
42
42
|
x.status === y.status &&
|
|
43
|
+
x.width === y.width &&
|
|
44
|
+
x.height === y.height &&
|
|
43
45
|
validationEqual(x.validation, y.validation)
|
|
44
46
|
);
|
|
45
47
|
}
|
|
@@ -149,6 +151,13 @@ export function basicCellRenderer<T>(
|
|
|
149
151
|
isDeleted,
|
|
150
152
|
status: statusVal,
|
|
151
153
|
validation,
|
|
154
|
+
// The size the table gives the cell (resized columns included), so a
|
|
155
|
+
// renderer that fits itself to it needn't measure
|
|
156
|
+
width:
|
|
157
|
+
state.columnWidthsIndex?.get(col.key) ??
|
|
158
|
+
col.width ??
|
|
159
|
+
state.defaultColumnWidth,
|
|
160
|
+
height: state.rowHeight,
|
|
152
161
|
};
|
|
153
162
|
|
|
154
163
|
// Only allocate a new style object when a row-status style must be layered on
|
|
@@ -225,6 +234,9 @@ export function basicCellRenderer<T>(
|
|
|
225
234
|
// Build the detail context for the unified `cellDetail` surface.
|
|
226
235
|
const makeDetailCtx = (editableFlag: boolean): CellDetailContext<T> => ({
|
|
227
236
|
...cellContext,
|
|
237
|
+
// A popover isn't laid out in the cell
|
|
238
|
+
width: undefined,
|
|
239
|
+
height: undefined,
|
|
228
240
|
surface: "cell",
|
|
229
241
|
editable: editableFlag,
|
|
230
242
|
onChange(v: any) {
|
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
} from "../provider";
|
|
10
10
|
import { atom } from "jotai";
|
|
11
11
|
import { runActionWrapper } from "../actions";
|
|
12
|
+
import { actionRequiresEditable } from "../actions/selection.ts";
|
|
12
13
|
import { HotkeyConfig } from "@blueprintjs/core";
|
|
13
14
|
import { toasterAtom } from "../notifications.ts";
|
|
14
15
|
|
|
@@ -92,7 +93,7 @@ export const tableHotkeysAtom = atom<null, [], HotkeyConfig[]>(
|
|
|
92
93
|
label: action.name,
|
|
93
94
|
group: action.group ?? "Actions",
|
|
94
95
|
preventDefault: true,
|
|
95
|
-
disabled: action
|
|
96
|
+
disabled: actionRequiresEditable(action) && !editable,
|
|
96
97
|
onKeyDown(event: KeyboardEvent<any>): any {
|
|
97
98
|
runActionWrapper(action, get, set, toaster);
|
|
98
99
|
},
|