@linzjs/step-ag-grid 35.1.0 → 35.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. package/dist/index.css +17 -0
  2. package/dist/src/components/Grid.d.ts +3 -1
  3. package/dist/src/components/GridRangeSelectContextMenu.d.ts +12 -1
  4. package/dist/src/components/gridFilter/GridFilterCopyButton.d.ts +9 -0
  5. package/dist/src/components/gridFilter/index.d.ts +1 -0
  6. package/dist/src/components/gridHook/copyGridCells.d.ts +19 -0
  7. package/dist/src/components/gridHook/copyGridCells.test.d.ts +1 -0
  8. package/dist/src/components/gridHook/useGridCopy.d.ts +5 -2
  9. package/dist/src/components/gridHook/useGridCopySettings.d.ts +2 -0
  10. package/dist/src/contexts/GridContext.d.ts +35 -0
  11. package/dist/src/contexts/GridContextProvider.copy.test.d.ts +1 -0
  12. package/dist/step-ag-grid.cjs +431 -231
  13. package/dist/step-ag-grid.cjs.map +1 -1
  14. package/dist/step-ag-grid.esm.js +432 -233
  15. package/dist/step-ag-grid.esm.js.map +1 -1
  16. package/package.json +1 -1
  17. package/src/components/Grid.tsx +15 -0
  18. package/src/components/GridRangeSelectContextMenu.tsx +54 -51
  19. package/src/components/gridFilter/GridFilterCopyButton.tsx +93 -0
  20. package/src/components/gridFilter/index.ts +1 -0
  21. package/src/components/gridHook/copyGridCells.test.ts +142 -0
  22. package/src/components/gridHook/copyGridCells.ts +225 -0
  23. package/src/components/gridHook/useGridCopy.ts +88 -211
  24. package/src/components/gridHook/useGridCopySettings.ts +21 -6
  25. package/src/contexts/GridContext.tsx +44 -0
  26. package/src/contexts/GridContextProvider.copy.test.tsx +158 -0
  27. package/src/contexts/GridContextProvider.tsx +129 -2
  28. package/src/stories/grid/GridCopy.stories.tsx +35 -20
  29. package/src/stories/grid/GridCopyEditable.stories.tsx +131 -0
  30. package/src/stories/grid/GridCopyMultiple.stories.tsx +119 -0
  31. package/src/styles/GridFilterCopyButton.scss +22 -0
  32. package/src/styles/index.scss +1 -0
@@ -62387,6 +62387,13 @@ const GridContext = React14.createContext({
62387
62387
  isExternalFilterPresent: NoContext,
62388
62388
  doesExternalFilterPass: NoContext,
62389
62389
  downloadCsv: NoContext,
62390
+ copyToClipboard: NoContext,
62391
+ getCopyScope: () => "all",
62392
+ getSelectedDataForCopy: NoContext,
62393
+ getCopyData: NoContext,
62394
+ setRangeCopy: NoContext,
62395
+ copyEnabled: false,
62396
+ setCopyEnabled: NoContext,
62390
62397
  onBulkEditingComplete: NoContext,
62391
62398
  setOnBulkEditingComplete: NoContext,
62392
62399
  showNoRowsOverlay: NoContext
@@ -65032,13 +65039,21 @@ const gridCopyOptions = {
65032
65039
  json: { text: "Json", icon: "ic_file_attached_outline", developer: true }
65033
65040
  };
65034
65041
  const CopyOptionsStorageKey = "stepAgGrid_defaultCopy";
65042
+ const isCopyOptionsKey = (key) => Object.keys(gridCopyOptions).includes(key);
65043
+ const getDefaultCopyType = () => {
65044
+ const defaultCopy = window.localStorage.getItem(CopyOptionsStorageKey) ?? "";
65045
+ return isCopyOptionsKey(defaultCopy) ? defaultCopy : "html";
65046
+ };
65035
65047
  const useGridCopySettings = () => {
65036
- const [copyType, _setCopyType] = React14.useState(() => {
65037
- const defaultCopy = window.localStorage.getItem(CopyOptionsStorageKey) ?? "";
65038
- return Object.keys(gridCopyOptions).includes(defaultCopy) ? defaultCopy : "html";
65039
- });
65048
+ const [copyType, _setCopyType] = React14.useState(getDefaultCopyType);
65049
+ const listenerRef = React14.useRef(void 0);
65050
+ React14.useEffect(() => {
65051
+ const listener = (value) => isCopyOptionsKey(value) && _setCopyType(value);
65052
+ listenerRef.current = listener;
65053
+ return subscribeStorageKey(CopyOptionsStorageKey, listener);
65054
+ }, []);
65040
65055
  const setCopyType = React14.useCallback((key) => {
65041
- window.localStorage.setItem(CopyOptionsStorageKey, key);
65056
+ publishStorageKey(window.localStorage, CopyOptionsStorageKey, key, listenerRef.current);
65042
65057
  _setCopyType(key);
65043
65058
  }, []);
65044
65059
  return {
@@ -65047,59 +65062,231 @@ const useGridCopySettings = () => {
65047
65062
  };
65048
65063
  };
65049
65064
 
65065
+ const GridCopyOptionsMenuItems = ({
65066
+ onCopy,
65067
+ copyType,
65068
+ setCopyType,
65069
+ developer: showDeveloper = false,
65070
+ header = "Copy as"
65071
+ }) => /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
65072
+ /* @__PURE__ */ jsxRuntime.jsx(MenuHeader, { children: header }),
65073
+ typedEntries(gridCopyOptions).map(([key, { icon, text, developer }]) => {
65074
+ return (!developer || showDeveloper) && /* @__PURE__ */ jsxRuntime.jsx(MenuItem, { onClick: () => onCopy(key), children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "copyMenuMenuItem", children: [
65075
+ /* @__PURE__ */ jsxRuntime.jsx(lui.LuiIcon, { name: icon, alt: text, size: "md" }),
65076
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "copyMenuMenuItem__text", children: text })
65077
+ ] }) }, key);
65078
+ }),
65079
+ /* @__PURE__ */ jsxRuntime.jsx(MenuDivider, {}),
65080
+ /* @__PURE__ */ jsxRuntime.jsx(MenuHeader, { children: "Set copy default" }),
65081
+ typedEntries(gridCopyOptions).map(([key, { text, developer }]) => {
65082
+ return !developer && /* @__PURE__ */ jsxRuntime.jsx(MenuItem, { onClick: () => setCopyType(key), children: /* @__PURE__ */ jsxRuntime.jsxs(
65083
+ "div",
65084
+ {
65085
+ className: clsx("copyMenuMenuItem", copyType === key ? "" : "copyMenuMenuItem__buttonDefault--hidden"),
65086
+ children: [
65087
+ /* @__PURE__ */ jsxRuntime.jsx(lui.LuiIcon, { name: "ic_tick", alt: "Default", size: "sm" }),
65088
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "copyMenuMenuItem__text", children: text })
65089
+ ]
65090
+ }
65091
+ ) }, "default_" + key);
65092
+ })
65093
+ ] });
65050
65094
  const GridRangeSelectContextMenu = ({
65051
65095
  event,
65052
65096
  context
65053
65097
  }) => {
65054
65098
  const developerContextMenu = !!event?.event?.ctrlKey && !!event?.event?.shiftKey;
65055
- const onCopy = React14.useMemo(() => context?.onCopy, [context?.onCopy]);
65056
- const onClick = React14.useCallback(
65057
- (type) => {
65058
- onCopy?.(type);
65059
- },
65060
- [onCopy]
65061
- );
65062
- return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
65063
- /* @__PURE__ */ jsxRuntime.jsx(MenuHeader, { children: "Copy as" }),
65064
- typedEntries(gridCopyOptions).map(([key, { icon, text, developer }]) => {
65065
- return (!developer || developerContextMenu) && /* @__PURE__ */ jsxRuntime.jsx(MenuItem, { onClick: () => void onClick(key), children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "copyMenuMenuItem", children: [
65066
- /* @__PURE__ */ jsxRuntime.jsx(lui.LuiIcon, { name: icon, alt: text, size: "md" }),
65067
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "copyMenuMenuItem__text", children: text })
65068
- ] }) }, key);
65069
- }),
65070
- /* @__PURE__ */ jsxRuntime.jsx(MenuDivider, {}),
65071
- /* @__PURE__ */ jsxRuntime.jsx(MenuHeader, { children: "Set copy default" }),
65072
- typedEntries(gridCopyOptions).map(([key, { text, developer }]) => {
65073
- return !developer && /* @__PURE__ */ jsxRuntime.jsx(
65074
- MenuItem,
65075
- {
65076
- onClick: (e) => {
65077
- context?.setCopyType(key);
65078
- e.keepOpen = true;
65079
- },
65080
- children: /* @__PURE__ */ jsxRuntime.jsxs(
65081
- "div",
65082
- {
65083
- className: clsx(
65084
- "copyMenuMenuItem",
65085
- context?.copyType === key ? "" : "copyMenuMenuItem__buttonDefault--hidden"
65086
- ),
65087
- children: [
65088
- /* @__PURE__ */ jsxRuntime.jsx(lui.LuiIcon, { name: "ic_tick", alt: "CSV", size: "sm" }),
65089
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "copyMenuMenuItem__text", children: text })
65090
- ]
65091
- }
65092
- )
65093
- },
65094
- "default_" + key
65095
- );
65096
- })
65097
- ] });
65099
+ return context ? /* @__PURE__ */ jsxRuntime.jsx(GridCopyOptionsMenuItems, { ...context, developer: developerContextMenu }) : null;
65098
65100
  };
65099
65101
 
65100
65102
  const agGridSelectRowColId = "ag-Grid-SelectionColumn";
65101
65103
 
65104
+ const isCopyableColId = (colId, getColDef) => colId !== "gridCellFiller" && getColDef(colId)?.headerComponentParams?.exportable !== false;
65105
+ const copyGridCells = ({
65106
+ type,
65107
+ selectedColIds,
65108
+ selectedNodes,
65109
+ selectedRowIds,
65110
+ getColDef,
65111
+ getCellValue,
65112
+ showToast
65113
+ }) => {
65114
+ const json = type === "json";
65115
+ const filteredSelectedColIds = selectedColIds.filter(
65116
+ (colId) => colId === agGridSelectRowColId || isCopyableColId(colId, getColDef)
65117
+ );
65118
+ const formatters = lodashEs.compact(
65119
+ filteredSelectedColIds.map((colKey) => {
65120
+ return (rowNode) => {
65121
+ if (colKey === agGridSelectRowColId) {
65122
+ return selectedRowIds.includes(rowNode.data.id);
65123
+ } else {
65124
+ const v = getCellValue({ rowNode, colKey });
65125
+ if (json && v !== void 0) {
65126
+ return v;
65127
+ }
65128
+ const f = getCellValue({ rowNode, colKey, useFormatter: true });
65129
+ return v == f ? v : f;
65130
+ }
65131
+ };
65132
+ })
65133
+ );
65134
+ const headers = filteredSelectedColIds.map((colId) => {
65135
+ if (colId === agGridSelectRowColId) return type === "json" ? "selected" : "Selected";
65136
+ if (json) {
65137
+ return colId;
65138
+ }
65139
+ return getColDef(colId)?.headerName ?? "?";
65140
+ });
65141
+ const maxCellLength = {};
65142
+ headers.forEach((headerName, i) => {
65143
+ const colId = filteredSelectedColIds[i];
65144
+ maxCellLength[colId] = headerName.length;
65145
+ });
65146
+ const rows = [];
65147
+ if (type === "csv" || type === "html") {
65148
+ rows.push(headers);
65149
+ }
65150
+ selectedNodes.forEach((node) => {
65151
+ const row = [];
65152
+ rows.push(row);
65153
+ formatters.forEach((formatter, i) => {
65154
+ const colId = filteredSelectedColIds[i];
65155
+ let value = formatter(node);
65156
+ if (typeof value === "string") {
65157
+ value = value.replaceAll("\xA0", " ");
65158
+ }
65159
+ if (!json) {
65160
+ if (value === "-" || value === "\u2013" || value == null) {
65161
+ value = "";
65162
+ }
65163
+ value = String(value);
65164
+ if (value === "-" || value === "\u2013") {
65165
+ value = "";
65166
+ }
65167
+ maxCellLength[colId] = Math.max(maxCellLength[colId], value.length);
65168
+ }
65169
+ switch (type) {
65170
+ case "json":
65171
+ value = value === "undefined" || value !== null && typeof value === "object" || typeof value === "string" ? JSON.stringify(value) : value;
65172
+ break;
65173
+ case "markdown":
65174
+ value = encodeMarkdownValue(value);
65175
+ break;
65176
+ case "csv":
65177
+ value = encodeCSVValue(value);
65178
+ break;
65179
+ }
65180
+ row.push(String(value));
65181
+ });
65182
+ });
65183
+ let result = "";
65184
+ let html = `<table style="
65185
+ font-family: 'Open Sans', system-ui, sans-serif;
65186
+ font-size: 10px;
65187
+ line-height: 10px;
65188
+ border: 1px solid #d1d9e0;
65189
+ border-collapse: collapse;
65190
+ ">`;
65191
+ if (json) {
65192
+ result += "[\n";
65193
+ }
65194
+ rows.forEach((row, rowIndex) => {
65195
+ if (json) {
65196
+ result += " { ";
65197
+ }
65198
+ if (rowIndex == 1) {
65199
+ html += '<tbody style="font-weight: 400;">';
65200
+ }
65201
+ if (rowIndex == 0) {
65202
+ html += '<thead style="font-weight: 600;">';
65203
+ }
65204
+ if (rowIndex === 0 || (rowIndex & 1) === 1) {
65205
+ html += "<tr>";
65206
+ } else {
65207
+ html += '<tr style="background-color: #f6f8fa;">';
65208
+ }
65209
+ if (rowIndex === 1 && type === "html") {
65210
+ Object.values(maxCellLength).forEach((maxLength) => {
65211
+ result += "|" + "-".repeat(maxLength + 2);
65212
+ });
65213
+ result += "|\n";
65214
+ }
65215
+ row.forEach((cell, i) => {
65216
+ switch (type) {
65217
+ case "csv":
65218
+ if (i !== 0) {
65219
+ result += ", ";
65220
+ }
65221
+ result += cell;
65222
+ break;
65223
+ case "html":
65224
+ if (i === 0) {
65225
+ result += "|";
65226
+ }
65227
+ const colId = filteredSelectedColIds[i];
65228
+ result += " " + cell.padEnd(maxCellLength[colId], " ") + " ";
65229
+ result += "|";
65230
+ html += rowIndex === 0 ? '<th style="border: 1px solid #d1d9e0; padding:6px 13px; text-align: left;">' : '<td style="border: 1px solid #d1d9e0; padding:6px 13px;">';
65231
+ html += cell;
65232
+ html += rowIndex === 0 ? "</th>" : "</td>";
65233
+ break;
65234
+ case "json":
65235
+ if (i !== 0) {
65236
+ result += ", ";
65237
+ }
65238
+ result += JSON.stringify(headers[i]) + ": " + cell;
65239
+ break;
65240
+ }
65241
+ });
65242
+ html += "</tr>";
65243
+ if (rowIndex == 0) {
65244
+ html += "</thead>";
65245
+ }
65246
+ if (rowIndex == rows.length - 1) {
65247
+ html += "</tbody>";
65248
+ }
65249
+ if (json) {
65250
+ result += rowIndex < rows.length - 1 ? " }," : " }";
65251
+ }
65252
+ result += "\n";
65253
+ });
65254
+ if (json) {
65255
+ result += "]\n";
65256
+ }
65257
+ html += "</table>";
65258
+ showToast({
65259
+ message: `${gridCopyOptions[type].text} copied`,
65260
+ messageType: "toast",
65261
+ messageLevel: "info"
65262
+ });
65263
+ if (type === "html") {
65264
+ const clipboardItem = new ClipboardItem({
65265
+ "text/html": new Blob([html], { type: "text/html" }),
65266
+ "text/plain": new Blob([result], { type: "text/plain" })
65267
+ // fallback
65268
+ });
65269
+ void navigator.clipboard.write([clipboardItem]).catch((err) => console.error("Failed to copy: ", err));
65270
+ } else {
65271
+ void navigator.clipboard.writeText(result).catch((err) => console.error("Failed to copy: ", err));
65272
+ }
65273
+ };
65274
+ const encodeMarkdownValue = (value) => {
65275
+ return value.replaceAll("|", "\\|");
65276
+ };
65277
+ const encodeCSVValue = (value) => {
65278
+ let str = String(value);
65279
+ if (/[",\n]/.test(str)) {
65280
+ str = '"' + str.replace(/"/g, '""') + '"';
65281
+ }
65282
+ return str;
65283
+ };
65284
+
65285
+ let lastActiveGridElement = null;
65286
+ const isEditableElement = (el) => el instanceof Element && (!!el.closest("input, textarea, select") || el.isContentEditable);
65102
65287
  const useGridCopy = ({
65288
+ gridDivRef,
65289
+ gridDocument,
65103
65290
  ranges,
65104
65291
  rangeStartRef,
65105
65292
  rangeEndRef,
@@ -65107,193 +65294,41 @@ const useGridCopy = ({
65107
65294
  cellContextMenu
65108
65295
  }) => {
65109
65296
  const showToast = lui.useShowLUIMessage();
65110
- const { getSelectedRowIds } = useGridContext();
65111
- const { getColDef, getCellValue } = useGridContext();
65297
+ const { getColDef, getCellValue, getSelectedRowIds, setRangeCopy } = useGridContext();
65112
65298
  const { copyType, setCopyType } = useGridCopySettings();
65299
+ const hasRange = React14.useCallback(
65300
+ () => rangeStartRef.current !== null && rangeEndRef.current !== null && hasSelectedMoreThanOneCellRef.current,
65301
+ [rangeStartRef, rangeEndRef, hasSelectedMoreThanOneCellRef]
65302
+ );
65113
65303
  const onCopy = React14.useCallback(
65114
65304
  (type = copyType) => {
65115
65305
  const rangeStart = rangeStartRef.current;
65116
65306
  const rangeEnd = rangeEndRef.current;
65117
- if (rangeStart === null || rangeEnd === null || !hasSelectedMoreThanOneCellRef.current) {
65307
+ if (rangeStart === null || rangeEnd === null || !hasRange()) {
65118
65308
  return;
65119
65309
  }
65120
65310
  if (rangeStart.rowId === rangeEnd.rowId && rangeStart.colId === rangeEnd.colId) {
65121
65311
  type = "html";
65122
65312
  }
65123
- const json = type === "json";
65124
65313
  const { selectedColIds, selectedNodes } = ranges();
65125
- const filteredSelectedColIds = selectedColIds.filter(
65126
- (colId) => colId === agGridSelectRowColId || colId !== "gridCellFiller" && getColDef(colId)?.headerComponentParams?.exportable !== false
65127
- );
65128
- const selectedRowIds = getSelectedRowIds();
65129
- const formatters = lodashEs.compact(
65130
- filteredSelectedColIds.map((colKey) => {
65131
- return (rowNode) => {
65132
- if (colKey === agGridSelectRowColId) {
65133
- return selectedRowIds.includes(rowNode.data.id);
65134
- } else {
65135
- const v = getCellValue({ rowNode, colKey });
65136
- if (json && v !== void 0) {
65137
- return v;
65138
- }
65139
- const f = getCellValue({ rowNode, colKey, useFormatter: true });
65140
- return v == f ? v : f;
65141
- }
65142
- };
65143
- })
65144
- );
65145
- const headers = filteredSelectedColIds.map((colId) => {
65146
- if (colId === agGridSelectRowColId) return type === "json" ? "selected" : "Selected";
65147
- if (json) {
65148
- return colId;
65149
- }
65150
- return getColDef(colId)?.headerName ?? "?";
65151
- });
65152
- const maxCellLength = {};
65153
- headers.forEach((headerName, i) => {
65154
- const colId = filteredSelectedColIds[i];
65155
- maxCellLength[colId] = headerName.length;
65156
- });
65157
- const rows = [];
65158
- if (type === "csv" || type === "html") {
65159
- rows.push(headers);
65160
- }
65161
- selectedNodes.forEach((node) => {
65162
- const row = [];
65163
- rows.push(row);
65164
- formatters.forEach((formatter, i) => {
65165
- const colId = filteredSelectedColIds[i];
65166
- let value = formatter(node);
65167
- if (typeof value === "string") {
65168
- value = value.replaceAll("\xA0", " ");
65169
- }
65170
- if (!json) {
65171
- if (value === "-" || value === "\u2013" || value == null) {
65172
- value = "";
65173
- }
65174
- value = String(value);
65175
- if (value === "-" || value === "\u2013") {
65176
- value = "";
65177
- }
65178
- maxCellLength[colId] = Math.max(maxCellLength[colId], value.length);
65179
- }
65180
- switch (type) {
65181
- case "json":
65182
- value = value === "undefined" || value !== null && typeof value === "object" || typeof value === "string" ? JSON.stringify(value) : value;
65183
- break;
65184
- case "markdown":
65185
- value = encodeMarkdownValue(value);
65186
- break;
65187
- case "csv":
65188
- value = encodeCSVValue(value);
65189
- break;
65190
- }
65191
- row.push(String(value));
65192
- });
65193
- });
65194
- let result = "";
65195
- let html = `<table style="
65196
- font-family: 'Open Sans', system-ui, sans-serif;
65197
- font-size: 10px;
65198
- line-height: 10px;
65199
- border: 1px solid #d1d9e0;
65200
- border-collapse: collapse;
65201
- ">`;
65202
- if (json) {
65203
- result += "[\n";
65204
- }
65205
- rows.forEach((row, rowIndex) => {
65206
- if (json) {
65207
- result += " { ";
65208
- }
65209
- if (rowIndex == 1) {
65210
- html += '<tbody style="font-weight: 400;">';
65211
- }
65212
- if (rowIndex == 0) {
65213
- html += '<thead style="font-weight: 600;">';
65214
- }
65215
- if (rowIndex === 0 || (rowIndex & 1) === 1) {
65216
- html += "<tr>";
65217
- } else {
65218
- html += '<tr style="background-color: #f6f8fa;">';
65219
- }
65220
- if (rowIndex === 1 && type === "html") {
65221
- Object.values(maxCellLength).forEach((maxLength) => {
65222
- result += "|" + "-".repeat(maxLength + 2);
65223
- });
65224
- result += "|\n";
65225
- }
65226
- row.forEach((cell, i) => {
65227
- switch (type) {
65228
- case "csv":
65229
- if (i !== 0) {
65230
- result += ", ";
65231
- }
65232
- result += cell;
65233
- break;
65234
- case "html":
65235
- if (i === 0) {
65236
- result += "|";
65237
- }
65238
- const colId = filteredSelectedColIds[i];
65239
- result += " " + cell.padEnd(maxCellLength[colId], " ") + " ";
65240
- result += "|";
65241
- html += rowIndex === 0 ? '<th style="border: 1px solid #d1d9e0; padding:6px 13px; text-align: left;">' : '<td style="border: 1px solid #d1d9e0; padding:6px 13px;">';
65242
- html += cell;
65243
- html += rowIndex === 0 ? "</th>" : "</td>";
65244
- break;
65245
- case "json":
65246
- if (i !== 0) {
65247
- result += ", ";
65248
- }
65249
- result += JSON.stringify(headers[i]) + ": " + cell;
65250
- break;
65251
- }
65252
- });
65253
- html += "</tr>";
65254
- if (rowIndex == 0) {
65255
- html += "</thead>";
65256
- }
65257
- if (rowIndex == rows.length - 1) {
65258
- html += "</tbody>";
65259
- }
65260
- if (json) {
65261
- result += " }";
65262
- }
65263
- result += "\n";
65264
- });
65265
- if (json) {
65266
- result += "]\n";
65267
- }
65268
- html += "</table>";
65269
- showToast({
65270
- message: `${gridCopyOptions[type].text} copied`,
65271
- messageType: "toast",
65272
- messageLevel: "info"
65314
+ copyGridCells({
65315
+ type,
65316
+ selectedColIds,
65317
+ selectedNodes,
65318
+ selectedRowIds: getSelectedRowIds(),
65319
+ getColDef,
65320
+ getCellValue,
65321
+ showToast
65273
65322
  });
65274
- if (type === "html") {
65275
- const clipboardItem = new ClipboardItem({
65276
- "text/html": new Blob([html], { type: "text/html" }),
65277
- "text/plain": new Blob([result], { type: "text/plain" })
65278
- // fallback
65279
- });
65280
- void navigator.clipboard.write([clipboardItem]).catch((err) => console.error("Failed to copy: ", err));
65281
- } else {
65282
- void navigator.clipboard.writeText(result).catch((err) => console.error("Failed to copy: ", err));
65283
- }
65284
65323
  },
65285
- [
65286
- getCellValue,
65287
- ranges,
65288
- copyType,
65289
- rangeStartRef,
65290
- hasSelectedMoreThanOneCellRef,
65291
- rangeEndRef,
65292
- showToast,
65293
- getColDef,
65294
- getSelectedRowIds
65295
- ]
65324
+ [getCellValue, ranges, copyType, rangeStartRef, hasRange, rangeEndRef, showToast, getColDef, getSelectedRowIds]
65296
65325
  );
65326
+ const onCopyRef = React14.useRef(onCopy);
65327
+ onCopyRef.current = onCopy;
65328
+ React14.useEffect(() => {
65329
+ setRangeCopy({ hasRange, getRange: ranges, copy: (type) => onCopyRef.current(type) });
65330
+ return () => setRangeCopy(void 0);
65331
+ }, [hasRange, ranges, setRangeCopy]);
65297
65332
  const onCopyEvent = React14.useCallback(
65298
65333
  (e) => {
65299
65334
  const rangeStart = rangeStartRef.current;
@@ -65306,6 +65341,31 @@ const useGridCopy = ({
65306
65341
  },
65307
65342
  [onCopy, rangeStartRef, rangeEndRef, hasSelectedMoreThanOneCellRef]
65308
65343
  );
65344
+ React14.useEffect(() => {
65345
+ const gridDiv = gridDivRef.current;
65346
+ const doc = gridDocument ?? gridDiv?.ownerDocument;
65347
+ if (!gridDiv || !doc) return;
65348
+ const setActive = () => {
65349
+ lastActiveGridElement = gridDiv;
65350
+ };
65351
+ const onDocumentCopy = (e) => {
65352
+ if (gridDiv.contains(e.target) || // handled by onCopyEvent
65353
+ lastActiveGridElement !== gridDiv || !hasRange() || isEditableElement(e.target) || !doc.getSelection()?.isCollapsed) {
65354
+ return;
65355
+ }
65356
+ e.preventDefault();
65357
+ onCopyRef.current();
65358
+ };
65359
+ gridDiv.addEventListener("mousedown", setActive, true);
65360
+ gridDiv.addEventListener("focusin", setActive, true);
65361
+ doc.addEventListener("copy", onDocumentCopy);
65362
+ return () => {
65363
+ gridDiv.removeEventListener("mousedown", setActive, true);
65364
+ gridDiv.removeEventListener("focusin", setActive, true);
65365
+ doc.removeEventListener("copy", onDocumentCopy);
65366
+ if (lastActiveGridElement === gridDiv) lastActiveGridElement = null;
65367
+ };
65368
+ }, [gridDivRef, gridDocument, hasRange]);
65309
65369
  const { cellContextMenu: rangeSelectContextMenu, contextMenuComponent: rangeSelectContextMenuComponent } = useGridContextMenu({
65310
65370
  contextMenu: GridRangeSelectContextMenu,
65311
65371
  context: { onCopy, copyType, setCopyType }
@@ -65322,16 +65382,6 @@ const useGridCopy = ({
65322
65382
  rangeSelectContextMenuComponent
65323
65383
  };
65324
65384
  };
65325
- const encodeMarkdownValue = (value) => {
65326
- return value.replaceAll("|", "\\|");
65327
- };
65328
- const encodeCSVValue = (value) => {
65329
- let str = String(value);
65330
- if (/[",\n]/.test(str)) {
65331
- str = '"' + str.replace(/"/g, '""') + '"';
65332
- }
65333
- return str;
65334
- };
65335
65385
 
65336
65386
  const useGridRangeSelection = ({
65337
65387
  enableRangeSelection,
@@ -65753,6 +65803,7 @@ const Grid = ({
65753
65803
  // ─── Selection ────────────────────────────────
65754
65804
  autoSelectFirstRow,
65755
65805
  enableRangeSelection = false,
65806
+ enableCopy = false,
65756
65807
  externalSelectedIds,
65757
65808
  externalSelectedItems,
65758
65809
  selectColumnPinned = "left",
@@ -65783,6 +65834,7 @@ const Grid = ({
65783
65834
  setApis,
65784
65835
  setExternallySelectedItemsAreInSync,
65785
65836
  setOnBulkEditingComplete,
65837
+ setCopyEnabled,
65786
65838
  gridReady,
65787
65839
  gridRenderState,
65788
65840
  externallySelectedItemsAreInSync,
@@ -65877,6 +65929,7 @@ const Grid = ({
65877
65929
  }
65878
65930
  }, [autoSizeColumns, gridRenderState, maxInitialWidth, params, rowData, sizeColumns, sizeColumnsToFit]);
65879
65931
  const lastOwnerDocumentRef = React14.useRef(void 0);
65932
+ const [gridDocument, setGridDocument] = React14.useState();
65880
65933
  const wasVisibleRef = React14.useRef(false);
65881
65934
  useInterval(() => {
65882
65935
  const visible = !!gridDivRef.current?.checkVisibility?.();
@@ -65894,6 +65947,7 @@ const Grid = ({
65894
65947
  const currentDocument = gridDivRef.current?.ownerDocument;
65895
65948
  if (currentDocument !== lastOwnerDocumentRef.current) {
65896
65949
  lastOwnerDocumentRef.current = currentDocument;
65950
+ setGridDocument(currentDocument);
65897
65951
  if (currentDocument) {
65898
65952
  needsAutoSize.current = true;
65899
65953
  }
@@ -66145,12 +66199,18 @@ const Grid = ({
66145
66199
  rangeSortedNodesRef
66146
66200
  });
66147
66201
  const { onCopyEvent, rangeSelectInterceptContextMenu, rangeSelectContextMenuComponent } = useGridCopy({
66202
+ gridDivRef,
66203
+ gridDocument,
66148
66204
  ranges,
66149
66205
  rangeStartRef,
66150
66206
  rangeEndRef,
66151
66207
  hasSelectedMoreThanOneCellRef,
66152
66208
  cellContextMenu
66153
66209
  });
66210
+ React14.useEffect(() => {
66211
+ setCopyEnabled(enableCopy);
66212
+ return () => setCopyEnabled(false);
66213
+ }, [enableCopy, setCopyEnabled]);
66154
66214
  const userSizedColIds = React14.useRef(/* @__PURE__ */ new Map());
66155
66215
  const { saveColumnState: saveColumnStateToStorage } = useGridColumnStateStorage({
66156
66216
  enabled: saveColumnState,
@@ -67183,6 +67243,72 @@ const GridFilterColumnsToggle = ({ saveState = true }) => {
67183
67243
  );
67184
67244
  };
67185
67245
 
67246
+ const copyScopeHeader = {
67247
+ range: "Copy range",
67248
+ selection: "Copy selection",
67249
+ all: "Copy all"
67250
+ };
67251
+ const copyTypeTooltipText = (copyType) => copyType === "html" ? "text / HTML" : gridCopyOptions[copyType].text;
67252
+ const GridFilterCopyMenuItems = ({ developer }) => {
67253
+ const { copyToClipboard, getCopyScope } = useGridContext();
67254
+ const { copyType, setCopyType } = useGridCopySettings();
67255
+ return /* @__PURE__ */ jsxRuntime.jsx(
67256
+ GridCopyOptionsMenuItems,
67257
+ {
67258
+ header: copyScopeHeader[getCopyScope()],
67259
+ onCopy: copyToClipboard,
67260
+ copyType,
67261
+ setCopyType,
67262
+ developer
67263
+ }
67264
+ );
67265
+ };
67266
+ const GridFilterCopyButton = () => {
67267
+ const { copyEnabled, copyToClipboard, getCopyScope } = useGridContext();
67268
+ const { copyType } = useGridCopySettings();
67269
+ const [copyScope, setCopyScope] = React14.useState("all");
67270
+ const updateCopyScope = React14.useCallback(() => setCopyScope(getCopyScope()), [getCopyScope]);
67271
+ const [developerMenu, setDeveloperMenu] = React14.useState(false);
67272
+ if (!copyEnabled) return null;
67273
+ const copyTitle = `${copyScopeHeader[copyScope]} as ${copyTypeTooltipText(copyType)}`;
67274
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "lui-split-button GridFilterCopyButton", onMouseEnter: updateCopyScope, onFocus: updateCopyScope, children: [
67275
+ /* @__PURE__ */ jsxRuntime.jsx(
67276
+ lui.LuiButton,
67277
+ {
67278
+ type: "button",
67279
+ level: "secondary",
67280
+ className: "lui-button-icon-only lui-split-button-primary-btn GridFilterCopyButton-copy",
67281
+ title: copyTitle,
67282
+ buttonProps: { "aria-label": copyTitle },
67283
+ onClick: () => copyToClipboard(copyType),
67284
+ children: /* @__PURE__ */ jsxRuntime.jsx(lui.LuiIcon, { name: "ic_copy", alt: copyTitle, size: "md" })
67285
+ }
67286
+ ),
67287
+ /* @__PURE__ */ jsxRuntime.jsx(
67288
+ Menu,
67289
+ {
67290
+ menuButton: /* @__PURE__ */ jsxRuntime.jsx(
67291
+ "button",
67292
+ {
67293
+ type: "button",
67294
+ className: "popup-toggle-button popup-toggle-button-secondary GridFilterCopyButton-caret",
67295
+ title: "Copy options",
67296
+ "aria-label": "Copy options",
67297
+ onClick: (e) => setDeveloperMenu(e.ctrlKey && e.shiftKey),
67298
+ children: /* @__PURE__ */ jsxRuntime.jsx(lui.LuiIcon, { name: "ic_expand_more", alt: "Copy options", size: "md" })
67299
+ }
67300
+ ),
67301
+ menuClassName: "step-ag-grid-react-menu",
67302
+ className: "GridContextMenu",
67303
+ align: "end",
67304
+ portal: true,
67305
+ unmountOnClose: true,
67306
+ children: /* @__PURE__ */ jsxRuntime.jsx(GridFilterCopyMenuItems, { developer: developerMenu })
67307
+ }
67308
+ )
67309
+ ] });
67310
+ };
67311
+
67186
67312
  const useTimeoutHook = () => {
67187
67313
  const timeout = React14.useRef(void 0);
67188
67314
  const clearTimeouts = () => {
@@ -69326,10 +69452,12 @@ const colStateFlexed = (colState) => !!colState.flex;
69326
69452
  const colStateNotFlexed = (colState) => !colState.flex;
69327
69453
  const GridContextProvider = (props) => {
69328
69454
  const { modifyUpdating, anyUpdating } = React14.useContext(GridUpdatingContext);
69455
+ const showToast = lui.useShowLUIMessage();
69329
69456
  const [gridApi, setGridApi] = React14.useState();
69330
69457
  const [gridReady, setGridReady] = React14.useState(false);
69331
69458
  const [quickFilter, _setQuickFilter] = React14.useState("");
69332
69459
  const [invisibleColumnIds, _setInvisibleColumnIds] = React14.useState();
69460
+ const [copyEnabled, setCopyEnabled] = React14.useState(false);
69333
69461
  const testId = React14.useRef(void 0);
69334
69462
  const hasExternallySelectedItemsRef = React14.useRef(false);
69335
69463
  const enableMultilineBulkEditRef = React14.useRef(false);
@@ -69958,6 +70086,70 @@ const GridContextProvider = (props) => {
69958
70086
  },
69959
70087
  [gridApi]
69960
70088
  );
70089
+ const rangeCopyRef = React14.useRef(void 0);
70090
+ const setRangeCopy = React14.useCallback((rangeCopy) => {
70091
+ rangeCopyRef.current = rangeCopy;
70092
+ }, []);
70093
+ const getCopyRows = React14.useCallback(() => {
70094
+ const nodes = [];
70095
+ if (gridApi && !gridApi.isDestroyed()) {
70096
+ gridApi.forEachNodeAfterFilterAndSort((node) => {
70097
+ if (node.data && !node.group) nodes.push(node);
70098
+ });
70099
+ }
70100
+ const selectedNodes = nodes.filter((node) => node.isSelected());
70101
+ return lodashEs.isEmpty(selectedNodes) ? { scope: "all", nodes } : { scope: "selection", nodes: selectedNodes };
70102
+ }, [gridApi]);
70103
+ const getDisplayedCopyColIds = React14.useCallback(
70104
+ () => gridApi && !gridApi.isDestroyed() ? gridApi.getAllDisplayedColumns().map((col) => col.getColId()).filter((colId) => colId !== agGridSelectRowColId) : [],
70105
+ [gridApi]
70106
+ );
70107
+ const getCopyTarget = React14.useCallback(() => {
70108
+ const rangeCopy = rangeCopyRef.current;
70109
+ const { selectedColIds, selectedNodes } = rangeCopy?.hasRange() ? rangeCopy.getRange() : { selectedColIds: getDisplayedCopyColIds(), selectedNodes: getCopyRows().nodes };
70110
+ return {
70111
+ colIds: selectedColIds.filter((colId) => colId !== agGridSelectRowColId && isCopyableColId(colId, getColDef)),
70112
+ nodes: selectedNodes
70113
+ };
70114
+ }, [getColDef, getCopyRows, getDisplayedCopyColIds]);
70115
+ const getSelectedDataForCopy = React14.useCallback(() => {
70116
+ const { colIds, nodes } = getCopyTarget();
70117
+ const fields = lodashEs.compact(colIds.map((colId) => getColDef(colId)?.field));
70118
+ return nodes.map((node) => {
70119
+ const data = {};
70120
+ fields.forEach((field) => lodashEs.set(data, field, lodashEs.get(node.data, field)));
70121
+ return data;
70122
+ });
70123
+ }, [getColDef, getCopyTarget]);
70124
+ const getCopyData = React14.useCallback(() => {
70125
+ const { colIds, nodes } = getCopyTarget();
70126
+ return nodes.map(
70127
+ (rowNode) => Object.fromEntries(colIds.map((colKey) => [colKey, gridApi?.getCellValue({ rowNode, colKey })]))
70128
+ );
70129
+ }, [getCopyTarget, gridApi]);
70130
+ const getCopyScope = React14.useCallback(
70131
+ () => rangeCopyRef.current?.hasRange() ? "range" : getCopyRows().scope,
70132
+ [getCopyRows]
70133
+ );
70134
+ const copyToClipboard = React14.useCallback(
70135
+ (type = getDefaultCopyType()) => {
70136
+ if (rangeCopyRef.current?.hasRange()) {
70137
+ rangeCopyRef.current.copy(type);
70138
+ return;
70139
+ }
70140
+ if (!gridApi || gridApi.isDestroyed()) return;
70141
+ copyGridCells({
70142
+ type,
70143
+ selectedColIds: getDisplayedCopyColIds(),
70144
+ selectedNodes: getCopyRows().nodes,
70145
+ selectedRowIds: getSelectedRowIds(),
70146
+ getColDef,
70147
+ getCellValue,
70148
+ showToast
70149
+ });
70150
+ },
70151
+ [getCellValue, getColDef, getCopyRows, getDisplayedCopyColIds, getSelectedRowIds, gridApi, showToast]
70152
+ );
69961
70153
  return /* @__PURE__ */ jsxRuntime.jsx(
69962
70154
  GridContext.Provider,
69963
70155
  {
@@ -70002,6 +70194,13 @@ const GridContextProvider = (props) => {
70002
70194
  isExternalFilterPresent,
70003
70195
  doesExternalFilterPass,
70004
70196
  downloadCsv,
70197
+ copyToClipboard,
70198
+ getCopyScope,
70199
+ getSelectedDataForCopy,
70200
+ getCopyData,
70201
+ setRangeCopy,
70202
+ copyEnabled,
70203
+ setCopyEnabled,
70005
70204
  onBulkEditingComplete,
70006
70205
  setOnBulkEditingComplete,
70007
70206
  showNoRowsOverlay
@@ -70240,6 +70439,7 @@ exports.GridEditBoolean = GridEditBoolean;
70240
70439
  exports.GridFilterButtons = GridFilterButtons;
70241
70440
  exports.GridFilterColumnsMultiSelect = GridFilterColumnsMultiSelect;
70242
70441
  exports.GridFilterColumnsToggle = GridFilterColumnsToggle;
70442
+ exports.GridFilterCopyButton = GridFilterCopyButton;
70243
70443
  exports.GridFilterDownloadCsvButton = GridFilterDownloadCsvButton;
70244
70444
  exports.GridFilterHeaderIconButton = GridFilterHeaderIconButton;
70245
70445
  exports.GridFilterQuick = GridFilterQuick;