@macrostrat/data-sheet 4.11.2 → 4.11.3

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.
@@ -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 { 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","toasterAtom","tableActionsAtom","toaster","actionRequiresEditable","runActionWrapper","singleFocusedCell"],"mappings":";;;;;;;;AAqBA,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,QAAMC,aAAY,OAAO,aAAa,CAAA;AACtC,SAAOA,WAAU,SAAS;AAC5B,CAAC;AAEM,MAAM,mBAAmBF,MAAAA;AAAAA,EAC9B;AAAA,EACA,CAAC,KAAK,QAAQ;AACZ,UAAM,QAAQ,IAAIG,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,aAAa,GAAU,OAAO,QAAQ;AAClD,cAAI,eAAe,SAAS;AAC1B,gBACG,KAAK,MAAM;AAAA,YAAC,CAAC,EACb,MAAM,CAAC,QAAQ;AACd,sBAAQ,MAAM,GAAG;AACjB,0BAAY,SAAS,GAAG;AAAA,YAC1B,CAAC;AAAA,UACL;AAAA,QACF,SAAS,KAAK;AACZ,sBAAY,SAAS,GAAG;AAAA,QAC1B;AAAA,MACF;AAAA,IACF;AAEA,aAAS,YAAYC,UAAS,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AAc;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;;;;"}
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 type DataSheetStoreMain,\n singleFocusedCell,\n ctx,\n} from \"../provider\";\nimport { atom } from \"jotai\";\nimport { runActionWrapper, type TableAction } 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\n// Actions are passed in: tableActionsAtom is synced in an effect, so it lags a render.\nexport const tableHotkeysAtom = atom<null, [TableAction[]], HotkeyConfig[]>(\n null,\n (get, set, tableActions) => {\n const store = get(storeAPIAtom);\n const toaster = get(toasterAtom);\n const editable = get(isEditableAtom);\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","toasterAtom","toaster","actionRequiresEditable","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,QAAMC,aAAY,OAAO,aAAa,CAAA;AACtC,SAAOA,WA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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,6 +1,7 @@
1
1
  import { KeyboardEvent } from 'react';
2
+ import { TableAction } from '../actions';
2
3
  import { HotkeyConfig } from '@blueprintjs/core';
3
- export declare const tableHotkeysAtom: import('jotai').WritableAtom<null, [], HotkeyConfig[]> & {
4
+ export declare const tableHotkeysAtom: import('jotai').WritableAtom<null, [TableAction<any, null>[]], HotkeyConfig[]> & {
4
5
  init: null;
5
6
  };
6
7
  export declare const editorKeyHandlerAtom: import('jotai').Atom<(e: KeyboardEvent) => void>;
@@ -1,9 +1,9 @@
1
1
  import { atom } from "jotai";
2
2
  import { actionRequiresEditable } from "../actions/selection.js";
3
3
  import { toasterAtom } from "../notifications.js";
4
- import { storeAPIAtom, tableActionsAtom, storeAtom } from "../provider/core.js";
5
4
  import { runActionWrapper } from "../actions/context.js";
6
5
  import { singleFocusedCell } from "../provider/zustand-store.js";
6
+ import { storeAPIAtom, storeAtom } from "../provider/core.js";
7
7
  const isEditableAtom = atom((get) => {
8
8
  const store = get(storeAtom);
9
9
  return store?.editable ?? false;
@@ -15,11 +15,10 @@ atom((get) => {
15
15
  });
16
16
  const tableHotkeysAtom = atom(
17
17
  null,
18
- (get, set) => {
18
+ (get, set, tableActions) => {
19
19
  const store = get(storeAPIAtom);
20
20
  const toaster = get(toasterAtom);
21
21
  const editable = get(isEditableAtom);
22
- const tableActions = get(tableActionsAtom);
23
22
  if (store == null) return [];
24
23
  const keyHandler = (actionRunner) => {
25
24
  return (e) => {
@@ -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 { 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.stop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AI,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
+ {"version":3,"file":"key-handling.js","sources":["../../src/utils/key-handling.ts"],"sourcesContent":["import type { KeyboardEvent } from \"react\";\nimport {\n storeAPIAtom,\n storeAtom,\n type DataSheetStoreMain,\n singleFocusedCell,\n ctx,\n} from \"../provider\";\nimport { atom } from \"jotai\";\nimport { runActionWrapper, type TableAction } 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\n// Actions are passed in: tableActionsAtom is synced in an effect, so it lags a render.\nexport const tableHotkeysAtom = atom<null, [TableAction[]], HotkeyConfig[]>(\n null,\n (get, set, tableActions) => {\n const store = get(storeAPIAtom);\n const toaster = get(toasterAtom);\n const editable = get(isEditableAtom);\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":";;;;;;AAoBA,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;AAGM,MAAM,mBAAmB;AAAA,EAC9B;AAAA,EACA,CAAC,KAAK,KAAK,iBAAiB;AAC1B,UAAM,QAAQ,IAAI,YAAY;AAC9B,UAAM,UAAU,IAAI,WAAW;AAC/B,UAAM,WAAW,IAAI,cAAc;AAEnC,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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AACpB,MAAE,gBAAA;AAAA,EAEJ,OAAO;AAEL,MAAE,eAAA;AACF,WAAO,KAAA;AAIP,WAAO,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.11.2",
3
+ "version": "4.11.3",
4
4
  "description": "Scalable data sheet with optional editing capabilities",
5
5
  "repository": {
6
6
  "type": "git",
@@ -62,7 +62,7 @@
62
62
  },
63
63
  "devDependencies": {
64
64
  "@macrostrat/web-components-bundler": "1.0.1",
65
- "@storybook/react-vite": "10.5.10"
65
+ "@storybook/react-vite": "10.6.7"
66
66
  },
67
67
  "publishConfig": {
68
68
  "access": "public"
package/src/data-sheet.ts CHANGED
@@ -209,7 +209,7 @@ export function DataSheetRenderer<T>({
209
209
 
210
210
  // This is quite hacky!
211
211
  const hotkeysConfig = useMemo(() => {
212
- return getHotkeys();
212
+ return getHotkeys(_actions);
213
213
  }, [_actions]);
214
214
 
215
215
  const { handleKeyDown, handleKeyUp } = useHotkeys(hotkeysConfig);
@@ -84,6 +84,11 @@
84
84
  color: var(--pz-text-subtle-color, var(--secondary-color)) !important
85
85
  font-style: italic
86
86
 
87
+ // Locked columns in an editable sheet: dimmed, not italic, so they read apart
88
+ // from derived ones.
89
+ .readonly-cell
90
+ color: var(--pz-text-subtle-color, var(--secondary-color)) !important
91
+
87
92
  .value-viewer-cell
88
93
  :global(.bp6-popover-target)
89
94
  width: 100%
@@ -174,8 +174,13 @@ export function basicCellRenderer<T>(
174
174
  col.cellDetail != null ? (col.detailPresentation ?? "popover") : null;
175
175
 
176
176
  // Derived values are drawn dimmed, so a reader can tell a record from a
177
- // restatement of one. Applied on every path below.
178
- const derivedClass = col.derived ? "derived-cell" : undefined;
177
+ // restatement of one. Locked columns in an editable sheet are dimmed too.
178
+ // Applied on every path below.
179
+ const locked = tableIsEditable && col.editable === false && !col.derived;
180
+ const valueClass = classNames({
181
+ "derived-cell": col.derived,
182
+ "readonly-cell": locked,
183
+ });
179
184
 
180
185
  // Fast path — any non-focused cell whose *unfocused* appearance is just the
181
186
  // value viewer (a plain Blueprint `Cell`). That's everything except a custom
@@ -196,7 +201,7 @@ export function basicCellRenderer<T>(
196
201
  loading,
197
202
  //value,
198
203
  style,
199
- className: derivedClass,
204
+ className: valueClass,
200
205
  interactive: false,
201
206
  //disabled: tableIsEditable && !editable,
202
207
  },
@@ -277,7 +282,7 @@ export function basicCellRenderer<T>(
277
282
  loading,
278
283
  value,
279
284
  style,
280
- className: derivedClass,
285
+ className: valueClass,
281
286
  disabled: tableIsEditable && !editable,
282
287
  interactive: false,
283
288
  ...cellComponentProps,
@@ -412,7 +417,7 @@ export function basicCellRenderer<T>(
412
417
  if (_dataEditor != null) {
413
418
  cellContents = _dataEditor;
414
419
  }
415
- const className = classNames("value-viewer-cell", derivedClass);
420
+ const className = classNames("value-viewer-cell", valueClass);
416
421
 
417
422
  return h(
418
423
  _Cell,
@@ -9,7 +9,12 @@
9
9
  import h from "@macrostrat/hyper";
10
10
  import { ColumnHeaderCell, RegionCardinality } from "@blueprintjs/table";
11
11
  import { Icon, Menu, MenuItem } from "@blueprintjs/core";
12
- import { ctx, tableActionsAtom, type ColumnSpec } from "../provider";
12
+ import {
13
+ ctx,
14
+ tableActionsAtom,
15
+ useSelector,
16
+ type ColumnSpec,
17
+ } from "../provider";
13
18
  import type {
14
19
  PostgrestColumnFilter,
15
20
  ColumnSortEntry,
@@ -92,6 +97,9 @@ export function renderColumnHeaderCell({
92
97
  * exposed as `data-column-key` so consumers can target a specific header from
93
98
  * CSS (e.g. to tint a column) without a bespoke styling prop. */
94
99
  function ColumnHeaderName({ col, hasSortActive, hasFilterActive, activeSort }) {
100
+ // Only meaningful when the rest of the sheet takes edits.
101
+ const tableEditable = useSelector((s) => s.editable);
102
+ const locked = tableEditable && col.editable === false && !col.derived;
95
103
  return h(
96
104
  "div",
97
105
  {
@@ -120,6 +128,12 @@ function ColumnHeaderName({ col, hasSortActive, hasFilterActive, activeSort }) {
120
128
  title: "Derived — computed from other values, not editable",
121
129
  style: { marginLeft: "4px", flexShrink: 0, opacity: 0.6 },
122
130
  }),
131
+ h.if(locked)(Icon, {
132
+ icon: "lock",
133
+ size: 12,
134
+ title: "Not editable",
135
+ style: { marginLeft: "4px", flexShrink: 0, opacity: 0.6 },
136
+ }),
123
137
  h.if(hasSortActive || hasFilterActive)(
124
138
  "span",
125
139
  {
@@ -2,13 +2,12 @@ import type { KeyboardEvent } from "react";
2
2
  import {
3
3
  storeAPIAtom,
4
4
  storeAtom,
5
- tableActionsAtom,
6
5
  type DataSheetStoreMain,
7
6
  singleFocusedCell,
8
7
  ctx,
9
8
  } from "../provider";
10
9
  import { atom } from "jotai";
11
- import { runActionWrapper } from "../actions";
10
+ import { runActionWrapper, type TableAction } from "../actions";
12
11
  import { actionRequiresEditable } from "../actions/selection.ts";
13
12
  import { HotkeyConfig } from "@blueprintjs/core";
14
13
  import { toasterAtom } from "../notifications.ts";
@@ -30,16 +29,14 @@ const hasSelectionAtom = atom((get) => {
30
29
  return selection.length > 0;
31
30
  });
32
31
 
33
- export const tableHotkeysAtom = atom<null, [], HotkeyConfig[]>(
32
+ // Actions are passed in: tableActionsAtom is synced in an effect, so it lags a render.
33
+ export const tableHotkeysAtom = atom<null, [TableAction[]], HotkeyConfig[]>(
34
34
  null,
35
- (get, set) => {
35
+ (get, set, tableActions) => {
36
36
  const store = get(storeAPIAtom);
37
37
  const toaster = get(toasterAtom);
38
38
  const editable = get(isEditableAtom);
39
39
 
40
- /** Set up hotkeys for table actions */
41
- const tableActions = get(tableActionsAtom);
42
-
43
40
  if (store == null) return [];
44
41
  const keyHandler = (
45
42
  actionRunner: HotkeyActionRunner,