@omnigrid/resize-plugin 0.1.1 → 0.1.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.
Files changed (3) hide show
  1. package/dist/index.d.ts +106 -19
  2. package/dist/index.js +136 -32
  3. package/package.json +1 -1
package/dist/index.d.ts CHANGED
@@ -1,12 +1,24 @@
1
1
  import type { GridApi, GridPlugin } from "@omnigrid/core";
2
+ /**
3
+ * Live drag-guide state for the currently resizing column.
4
+ */
2
5
  export interface ColumnResizeGuide {
6
+ /** ID of the column being resized. */
3
7
  columnId: string;
8
+ /** Current pointer x-coordinate, in viewport pixels. */
4
9
  x: number;
10
+ /** Whether the guide is currently visible. */
5
11
  visible: boolean;
6
12
  }
13
+ /**
14
+ * Configuration for the column resize plugin.
15
+ */
7
16
  export interface ColumnResizePluginOptions<T> {
17
+ /** Called with the column id and clamped width each time the width changes during a drag. */
8
18
  onResize?: (columnId: string, width: number) => void;
19
+ /** Called when a drag begins, with the column id and its starting width. */
9
20
  onResizeStart?: (columnId: string, width: number) => void;
21
+ /** Called when a drag completes, with the column id and its final width. */
10
22
  onResizeEnd?: (columnId: string, width: number) => void;
11
23
  }
12
24
  /**
@@ -20,49 +32,118 @@ export interface ColumnResizePluginOptions<T> {
20
32
  export declare class ColumnResizePlugin<T> implements GridPlugin<T> {
21
33
  readonly name = "@omnigrid/column-resize-plugin";
22
34
  private api?;
35
+ private readonly wrappedRenderers;
23
36
  private activeResize?;
24
37
  private guide;
25
38
  private readonly onResize?;
26
39
  private readonly onResizeStart?;
27
40
  private readonly onResizeEnd?;
41
+ /** Set while an actual drag occurred during the current resize session. */
42
+ private resizeDragged;
43
+ /** Suppress the click synthesized right after a completed drag-resize. */
44
+ private suppressNextClick;
28
45
  constructor(options?: ColumnResizePluginOptions<T>);
29
46
  register(api: GridApi<T>): () => void;
30
- /** Returns the current width of a leaf column. */
47
+ /**
48
+ * Returns the current width of a leaf column.
49
+ *
50
+ * @api
51
+ */
31
52
  getColumnWidth(columnId: string): number | undefined;
32
- private canResizeColumn;
33
- /** Alias for `getColumnWidth`. */
34
- getWidth(columnId: string): number | undefined;
35
- /** Sets a column width and clamps it to the configured min/max constraints. */
53
+ /**
54
+ * Sets a column width and clamps it to the configured min/max constraints.
55
+ *
56
+ * @api
57
+ */
36
58
  setColumnWidth(columnId: string, width: number): number;
37
- /** Alias for `setColumnWidth`. */
59
+ /**
60
+ * Alias for `setColumnWidth`.
61
+ *
62
+ * @api
63
+ */
38
64
  setWidth(columnId: string, width: number): number;
39
- /** Changes the width by a delta in pixels. */
65
+ /**
66
+ * Changes the width by a delta in pixels.
67
+ *
68
+ * @api
69
+ */
40
70
  resizeColumn(columnId: string, delta: number): number;
41
- /** Alias for `resizeColumn`. */
71
+ /**
72
+ * Alias for `resizeColumn`.
73
+ *
74
+ * @api
75
+ */
42
76
  resize(columnId: string, delta: number): number;
43
- /** Starts an interactive resize using the pointer x-coordinate. */
77
+ /**
78
+ * Starts an interactive resize using the pointer x-coordinate.
79
+ *
80
+ * @api
81
+ */
44
82
  beginResize(columnId: string, clientX: number): number;
45
- /** Adapter-friendly alias for pointer-down events. */
83
+ /**
84
+ * Adapter-friendly alias for pointer-down events.
85
+ *
86
+ * @api
87
+ */
46
88
  handlePointerDown(columnId: string, clientX: number): number;
47
- /** Updates the current resize using the new pointer x-coordinate. */
89
+ /**
90
+ * Updates the current resize using the new pointer x-coordinate.
91
+ *
92
+ * @api
93
+ */
48
94
  resizeTo(clientX: number): number;
49
- /** Adapter-friendly alias for pointer-move events. */
95
+ /**
96
+ * Adapter-friendly alias for pointer-move events.
97
+ *
98
+ * @api
99
+ */
50
100
  handlePointerMove(clientX: number): number;
51
- /** Updates the current resize using an already-calculated delta. */
101
+ /**
102
+ * Updates the current resize using an already-calculated delta.
103
+ *
104
+ * @api
105
+ */
52
106
  resizeBy(delta: number): number;
53
- /** Completes the active pointer drag and hides the resize guide. */
107
+ /**
108
+ * Completes the active pointer drag and hides the resize guide.
109
+ *
110
+ * @api
111
+ */
54
112
  endResize(): number;
55
- /** Adapter-friendly alias for double-click auto-fit. */
113
+ /**
114
+ * Adapter-friendly alias for double-click auto-fit.
115
+ *
116
+ * @api
117
+ */
56
118
  handleDoubleClick(columnId: string): number;
57
- /** Calculates the best-fit width from header text and visible row content. */
119
+ /**
120
+ * Calculates the best-fit width from header text and visible row content.
121
+ *
122
+ * @api
123
+ */
58
124
  autoSizeColumn(columnId: string): number;
59
- /** Alias for `autoSizeColumn`. */
125
+ /**
126
+ * Alias for `autoSizeColumn`.
127
+ *
128
+ * @api
129
+ */
60
130
  autoFitColumn(columnId: string): number;
61
- /** Returns the current drag guide state for integrations. */
131
+ /**
132
+ * Returns the current drag guide state for integrations.
133
+ *
134
+ * @api
135
+ */
62
136
  getResizeGuide(): ColumnResizeGuide;
63
- /** Resets the current drag guide state. */
137
+ /**
138
+ * Resets the current drag guide state.
139
+ *
140
+ * @api
141
+ */
64
142
  clearGuide(): void;
143
+ private canResizeColumn;
65
144
  private attachResizeHandles;
145
+ /** Restores the original header renderers that were wrapped with resize handles. */
146
+ private detachResizeHandles;
66
147
  private createResizeHandleNode;
67
148
  private bindPointerHandlers;
68
149
  private findLeafColumn;
@@ -71,5 +152,11 @@ export declare class ColumnResizePlugin<T> implements GridPlugin<T> {
71
152
  private resolveCellValue;
72
153
  private updateColumns;
73
154
  }
155
+ /**
156
+ * {@link ColumnResizePlugin} with the `Column` prefix stripped for ergonomic construction.
157
+ */
74
158
  export declare const ResizePlugin: typeof ColumnResizePlugin;
159
+ /**
160
+ * Alias of {@link ColumnResizePluginOptions} with the `Column` prefix stripped for ergonomic construction.
161
+ */
75
162
  export type ResizePluginOptions<T> = ColumnResizePluginOptions<T>;
package/dist/index.js CHANGED
@@ -10,7 +10,12 @@ import { flattenColumns, isColumnGroup } from "@omnigrid/core";
10
10
  export class ColumnResizePlugin {
11
11
  constructor(options = {}) {
12
12
  this.name = "@omnigrid/column-resize-plugin";
13
+ this.wrappedRenderers = new Map();
13
14
  this.guide = { columnId: "", x: 0, visible: false };
15
+ /** Set while an actual drag occurred during the current resize session. */
16
+ this.resizeDragged = false;
17
+ /** Suppress the click synthesized right after a completed drag-resize. */
18
+ this.suppressNextClick = false;
14
19
  this.onResize = options.onResize;
15
20
  this.onResizeStart = options.onResizeStart;
16
21
  this.onResizeEnd = options.onResizeEnd;
@@ -23,10 +28,16 @@ export class ColumnResizePlugin {
23
28
  unregisterPointerHandlers();
24
29
  this.clearGuide();
25
30
  this.activeResize = undefined;
31
+ if (!api.isDestroyed())
32
+ api.setColumns(this.detachResizeHandles(api.getState().columns));
26
33
  this.api = undefined;
27
34
  };
28
35
  }
29
- /** Returns the current width of a leaf column. */
36
+ /**
37
+ * Returns the current width of a leaf column.
38
+ *
39
+ * @api
40
+ */
30
41
  getColumnWidth(columnId) {
31
42
  const column = this.findLeafColumn(columnId);
32
43
  if (!column)
@@ -37,14 +48,11 @@ export class ColumnResizePlugin {
37
48
  return this.api.getColumnWidth(columnId) ?? column.minWidth ?? 40;
38
49
  return 120;
39
50
  }
40
- canResizeColumn(column) {
41
- return !!column && column.resizable !== false;
42
- }
43
- /** Alias for `getColumnWidth`. */
44
- getWidth(columnId) {
45
- return this.getColumnWidth(columnId);
46
- }
47
- /** Sets a column width and clamps it to the configured min/max constraints. */
51
+ /**
52
+ * Sets a column width and clamps it to the configured min/max constraints.
53
+ *
54
+ * @api
55
+ */
48
56
  setColumnWidth(columnId, width) {
49
57
  if (!this.api)
50
58
  return width;
@@ -78,22 +86,38 @@ export class ColumnResizePlugin {
78
86
  this.onResize?.(columnId, nextWidth);
79
87
  return nextWidth;
80
88
  }
81
- /** Alias for `setColumnWidth`. */
89
+ /**
90
+ * Alias for `setColumnWidth`.
91
+ *
92
+ * @api
93
+ */
82
94
  setWidth(columnId, width) {
83
95
  return this.setColumnWidth(columnId, width);
84
96
  }
85
- /** Changes the width by a delta in pixels. */
97
+ /**
98
+ * Changes the width by a delta in pixels.
99
+ *
100
+ * @api
101
+ */
86
102
  resizeColumn(columnId, delta) {
87
103
  const current = this.getColumnWidth(columnId);
88
104
  if (current === undefined)
89
105
  return delta;
90
106
  return this.setColumnWidth(columnId, current + delta);
91
107
  }
92
- /** Alias for `resizeColumn`. */
108
+ /**
109
+ * Alias for `resizeColumn`.
110
+ *
111
+ * @api
112
+ */
93
113
  resize(columnId, delta) {
94
114
  return this.resizeColumn(columnId, delta);
95
115
  }
96
- /** Starts an interactive resize using the pointer x-coordinate. */
116
+ /**
117
+ * Starts an interactive resize using the pointer x-coordinate.
118
+ *
119
+ * @api
120
+ */
97
121
  beginResize(columnId, clientX) {
98
122
  const column = this.findLeafColumn(columnId);
99
123
  if (!this.canResizeColumn(column))
@@ -107,15 +131,25 @@ export class ColumnResizePlugin {
107
131
  startWidth: currentWidth,
108
132
  direction: column?.pinned === "right" ? -1 : 1,
109
133
  };
134
+ this.resizeDragged = false;
135
+ this.suppressNextClick = false;
110
136
  this.guide = { columnId, x: clientX, visible: true };
111
137
  this.onResizeStart?.(columnId, currentWidth);
112
138
  return currentWidth;
113
139
  }
114
- /** Adapter-friendly alias for pointer-down events. */
140
+ /**
141
+ * Adapter-friendly alias for pointer-down events.
142
+ *
143
+ * @api
144
+ */
115
145
  handlePointerDown(columnId, clientX) {
116
146
  return this.beginResize(columnId, clientX);
117
147
  }
118
- /** Updates the current resize using the new pointer x-coordinate. */
148
+ /**
149
+ * Updates the current resize using the new pointer x-coordinate.
150
+ *
151
+ * @api
152
+ */
119
153
  resizeTo(clientX) {
120
154
  const active = this.activeResize;
121
155
  if (!active)
@@ -124,20 +158,33 @@ export class ColumnResizePlugin {
124
158
  this.guide = { columnId: active.columnId, x: clientX, visible: true };
125
159
  return this.resizeBy(delta);
126
160
  }
127
- /** Adapter-friendly alias for pointer-move events. */
161
+ /**
162
+ * Adapter-friendly alias for pointer-move events.
163
+ *
164
+ * @api
165
+ */
128
166
  handlePointerMove(clientX) {
129
167
  return this.resizeTo(clientX);
130
168
  }
131
- /** Updates the current resize using an already-calculated delta. */
169
+ /**
170
+ * Updates the current resize using an already-calculated delta.
171
+ *
172
+ * @api
173
+ */
132
174
  resizeBy(delta) {
133
175
  if (!this.activeResize)
134
176
  return 0;
177
+ this.resizeDragged = true;
135
178
  const { columnId, startWidth } = this.activeResize;
136
179
  const nextWidth = this.setColumnWidth(columnId, startWidth + delta);
137
180
  this.guide = { columnId, x: this.guide.x, visible: true };
138
181
  return nextWidth;
139
182
  }
140
- /** Completes the active pointer drag and hides the resize guide. */
183
+ /**
184
+ * Completes the active pointer drag and hides the resize guide.
185
+ *
186
+ * @api
187
+ */
141
188
  endResize() {
142
189
  const active = this.activeResize;
143
190
  if (!active)
@@ -145,14 +192,24 @@ export class ColumnResizePlugin {
145
192
  const width = this.getColumnWidth(active.columnId) ?? active.startWidth;
146
193
  this.onResizeEnd?.(active.columnId, width);
147
194
  this.clearGuide();
195
+ this.suppressNextClick = this.resizeDragged;
196
+ this.resizeDragged = false;
148
197
  this.activeResize = undefined;
149
198
  return width;
150
199
  }
151
- /** Adapter-friendly alias for double-click auto-fit. */
200
+ /**
201
+ * Adapter-friendly alias for double-click auto-fit.
202
+ *
203
+ * @api
204
+ */
152
205
  handleDoubleClick(columnId) {
153
206
  return this.autoSizeColumn(columnId);
154
207
  }
155
- /** Calculates the best-fit width from header text and visible row content. */
208
+ /**
209
+ * Calculates the best-fit width from header text and visible row content.
210
+ *
211
+ * @api
212
+ */
156
213
  autoSizeColumn(columnId) {
157
214
  const column = this.findLeafColumn(columnId);
158
215
  if (!column || !this.canResizeColumn(column) || !this.api)
@@ -169,18 +226,33 @@ export class ColumnResizePlugin {
169
226
  const maximum = column.maxWidth ?? Number.POSITIVE_INFINITY;
170
227
  return this.setColumnWidth(columnId, Math.min(Math.max(estimatedWidth, minimum), maximum));
171
228
  }
172
- /** Alias for `autoSizeColumn`. */
229
+ /**
230
+ * Alias for `autoSizeColumn`.
231
+ *
232
+ * @api
233
+ */
173
234
  autoFitColumn(columnId) {
174
235
  return this.autoSizeColumn(columnId);
175
236
  }
176
- /** Returns the current drag guide state for integrations. */
237
+ /**
238
+ * Returns the current drag guide state for integrations.
239
+ *
240
+ * @api
241
+ */
177
242
  getResizeGuide() {
178
243
  return { ...this.guide };
179
244
  }
180
- /** Resets the current drag guide state. */
245
+ /**
246
+ * Resets the current drag guide state.
247
+ *
248
+ * @api
249
+ */
181
250
  clearGuide() {
182
251
  this.guide = { columnId: "", x: 0, visible: false };
183
252
  }
253
+ canResizeColumn(column) {
254
+ return !!column && column.resizable !== false;
255
+ }
184
256
  attachResizeHandles(columns) {
185
257
  return columns.map((column) => {
186
258
  if (isColumnGroup(column)) {
@@ -190,13 +262,23 @@ export class ColumnResizePlugin {
190
262
  return column;
191
263
  }
192
264
  const originalHeaderRenderer = column.headerRenderer;
193
- return {
194
- ...column,
195
- headerRenderer: (headerColumn) => {
196
- const resolvedHeader = originalHeaderRenderer ? originalHeaderRenderer(headerColumn) : headerColumn.header ?? headerColumn.id;
197
- return this.createResizeHandleNode(headerColumn, resolvedHeader);
198
- },
265
+ const wrapped = (headerColumn) => {
266
+ const resolvedHeader = originalHeaderRenderer ? originalHeaderRenderer(headerColumn) : headerColumn.header ?? headerColumn.id;
267
+ return this.createResizeHandleNode(headerColumn, resolvedHeader);
199
268
  };
269
+ this.wrappedRenderers.set(wrapped, originalHeaderRenderer);
270
+ return { ...column, headerRenderer: wrapped };
271
+ });
272
+ }
273
+ /** Restores the original header renderers that were wrapped with resize handles. */
274
+ detachResizeHandles(columns) {
275
+ return columns.map((column) => {
276
+ if (isColumnGroup(column)) {
277
+ return { ...column, children: this.detachResizeHandles(column.children) };
278
+ }
279
+ if (!column.headerRenderer || !this.wrappedRenderers.has(column.headerRenderer))
280
+ return column;
281
+ return { ...column, headerRenderer: this.wrappedRenderers.get(column.headerRenderer) };
200
282
  });
201
283
  }
202
284
  createResizeHandleNode(column, content) {
@@ -204,8 +286,8 @@ export class ColumnResizePlugin {
204
286
  ? "omnigrid-column-resize-handle omnigrid-column-resize-handle-left"
205
287
  : "omnigrid-column-resize-handle";
206
288
  const contentStyle = column.pinned === "right"
207
- ? "position:relative; display:flex; align-items:center; width:calc(100% + 12px); margin-left:-12px; padding-left:12px; min-width:0;"
208
- : "position:relative; display:flex; align-items:center; width:calc(100% + 12px); margin-right:-12px; min-width:0;";
289
+ ? "display:flex; align-items:center; width:calc(100% + 12px); margin-left:-12px; padding-left:12px; min-width:0;"
290
+ : "display:flex; align-items:center; width:calc(100% + 12px); margin-right:-12px; min-width:0;";
209
291
  const sortIndicator = column.sortState && this.api
210
292
  ? {
211
293
  type: "node",
@@ -237,7 +319,12 @@ export class ColumnResizePlugin {
237
319
  {
238
320
  type: "node",
239
321
  tag: "span",
240
- attrs: { class: "omnigrid-header-label", style: "overflow:hidden; text-overflow:ellipsis; white-space:nowrap;" },
322
+ attrs: {
323
+ class: "omnigrid-header-label",
324
+ style: column.wrapText
325
+ ? "overflow:hidden; text-overflow:clip; white-space:normal; word-break:break-word;"
326
+ : "overflow:hidden; text-overflow:ellipsis; white-space:nowrap;",
327
+ },
241
328
  children: [content],
242
329
  },
243
330
  ...(sortIndicator
@@ -257,6 +344,9 @@ export class ColumnResizePlugin {
257
344
  class: handleClass,
258
345
  "aria-hidden": true,
259
346
  title: "Resize column",
347
+ style: column.pinned === "right"
348
+ ? "position:absolute; top:0; bottom:0; left:-8px;"
349
+ : "position:absolute; top:0; bottom:0; right:-8px;",
260
350
  },
261
351
  on: {
262
352
  pointerdown: (_context, event) => {
@@ -287,13 +377,24 @@ export class ColumnResizePlugin {
287
377
  return;
288
378
  this.endResize();
289
379
  };
380
+ // Swallow the click synthesized right after a completed drag-resize so it does
381
+ // not bubble up to the column header and trigger sorting. Capture phase stops it
382
+ // before it reaches React's root listener.
383
+ const handleClick = (event) => {
384
+ if (!this.suppressNextClick)
385
+ return;
386
+ this.suppressNextClick = false;
387
+ event.stopPropagation();
388
+ };
290
389
  document.addEventListener("pointermove", handlePointerMove);
291
390
  document.addEventListener("pointerup", handlePointerUp);
292
391
  document.addEventListener("pointercancel", handlePointerUp);
392
+ document.addEventListener("click", handleClick, true);
293
393
  return () => {
294
394
  document.removeEventListener("pointermove", handlePointerMove);
295
395
  document.removeEventListener("pointerup", handlePointerUp);
296
396
  document.removeEventListener("pointercancel", handlePointerUp);
397
+ document.removeEventListener("click", handleClick, true);
297
398
  };
298
399
  }
299
400
  findLeafColumn(columnId) {
@@ -327,4 +428,7 @@ export class ColumnResizePlugin {
327
428
  });
328
429
  }
329
430
  }
431
+ /**
432
+ * {@link ColumnResizePlugin} with the `Column` prefix stripped for ergonomic construction.
433
+ */
330
434
  export const ResizePlugin = ColumnResizePlugin;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@omnigrid/resize-plugin",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "Headless column resize plugin for OmniGrid",
5
5
  "keywords": [
6
6
  "omnigrid",