@omnigrid/resize-plugin 0.1.1 → 0.1.2

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 +102 -19
  2. package/dist/index.js +105 -29
  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,6 +32,7 @@ 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?;
@@ -27,42 +40,106 @@ export declare class ColumnResizePlugin<T> implements GridPlugin<T> {
27
40
  private readonly onResizeEnd?;
28
41
  constructor(options?: ColumnResizePluginOptions<T>);
29
42
  register(api: GridApi<T>): () => void;
30
- /** Returns the current width of a leaf column. */
43
+ /**
44
+ * Returns the current width of a leaf column.
45
+ *
46
+ * @api
47
+ */
31
48
  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. */
49
+ /**
50
+ * Sets a column width and clamps it to the configured min/max constraints.
51
+ *
52
+ * @api
53
+ */
36
54
  setColumnWidth(columnId: string, width: number): number;
37
- /** Alias for `setColumnWidth`. */
55
+ /**
56
+ * Alias for `setColumnWidth`.
57
+ *
58
+ * @api
59
+ */
38
60
  setWidth(columnId: string, width: number): number;
39
- /** Changes the width by a delta in pixels. */
61
+ /**
62
+ * Changes the width by a delta in pixels.
63
+ *
64
+ * @api
65
+ */
40
66
  resizeColumn(columnId: string, delta: number): number;
41
- /** Alias for `resizeColumn`. */
67
+ /**
68
+ * Alias for `resizeColumn`.
69
+ *
70
+ * @api
71
+ */
42
72
  resize(columnId: string, delta: number): number;
43
- /** Starts an interactive resize using the pointer x-coordinate. */
73
+ /**
74
+ * Starts an interactive resize using the pointer x-coordinate.
75
+ *
76
+ * @api
77
+ */
44
78
  beginResize(columnId: string, clientX: number): number;
45
- /** Adapter-friendly alias for pointer-down events. */
79
+ /**
80
+ * Adapter-friendly alias for pointer-down events.
81
+ *
82
+ * @api
83
+ */
46
84
  handlePointerDown(columnId: string, clientX: number): number;
47
- /** Updates the current resize using the new pointer x-coordinate. */
85
+ /**
86
+ * Updates the current resize using the new pointer x-coordinate.
87
+ *
88
+ * @api
89
+ */
48
90
  resizeTo(clientX: number): number;
49
- /** Adapter-friendly alias for pointer-move events. */
91
+ /**
92
+ * Adapter-friendly alias for pointer-move events.
93
+ *
94
+ * @api
95
+ */
50
96
  handlePointerMove(clientX: number): number;
51
- /** Updates the current resize using an already-calculated delta. */
97
+ /**
98
+ * Updates the current resize using an already-calculated delta.
99
+ *
100
+ * @api
101
+ */
52
102
  resizeBy(delta: number): number;
53
- /** Completes the active pointer drag and hides the resize guide. */
103
+ /**
104
+ * Completes the active pointer drag and hides the resize guide.
105
+ *
106
+ * @api
107
+ */
54
108
  endResize(): number;
55
- /** Adapter-friendly alias for double-click auto-fit. */
109
+ /**
110
+ * Adapter-friendly alias for double-click auto-fit.
111
+ *
112
+ * @api
113
+ */
56
114
  handleDoubleClick(columnId: string): number;
57
- /** Calculates the best-fit width from header text and visible row content. */
115
+ /**
116
+ * Calculates the best-fit width from header text and visible row content.
117
+ *
118
+ * @api
119
+ */
58
120
  autoSizeColumn(columnId: string): number;
59
- /** Alias for `autoSizeColumn`. */
121
+ /**
122
+ * Alias for `autoSizeColumn`.
123
+ *
124
+ * @api
125
+ */
60
126
  autoFitColumn(columnId: string): number;
61
- /** Returns the current drag guide state for integrations. */
127
+ /**
128
+ * Returns the current drag guide state for integrations.
129
+ *
130
+ * @api
131
+ */
62
132
  getResizeGuide(): ColumnResizeGuide;
63
- /** Resets the current drag guide state. */
133
+ /**
134
+ * Resets the current drag guide state.
135
+ *
136
+ * @api
137
+ */
64
138
  clearGuide(): void;
139
+ private canResizeColumn;
65
140
  private attachResizeHandles;
141
+ /** Restores the original header renderers that were wrapped with resize handles. */
142
+ private detachResizeHandles;
66
143
  private createResizeHandleNode;
67
144
  private bindPointerHandlers;
68
145
  private findLeafColumn;
@@ -71,5 +148,11 @@ export declare class ColumnResizePlugin<T> implements GridPlugin<T> {
71
148
  private resolveCellValue;
72
149
  private updateColumns;
73
150
  }
151
+ /**
152
+ * {@link ColumnResizePlugin} with the `Column` prefix stripped for ergonomic construction.
153
+ */
74
154
  export declare const ResizePlugin: typeof ColumnResizePlugin;
155
+ /**
156
+ * Alias of {@link ColumnResizePluginOptions} with the `Column` prefix stripped for ergonomic construction.
157
+ */
75
158
  export type ResizePluginOptions<T> = ColumnResizePluginOptions<T>;
package/dist/index.js CHANGED
@@ -10,6 +10,7 @@ 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 };
14
15
  this.onResize = options.onResize;
15
16
  this.onResizeStart = options.onResizeStart;
@@ -23,10 +24,16 @@ export class ColumnResizePlugin {
23
24
  unregisterPointerHandlers();
24
25
  this.clearGuide();
25
26
  this.activeResize = undefined;
27
+ if (!api.isDestroyed())
28
+ api.setColumns(this.detachResizeHandles(api.getState().columns));
26
29
  this.api = undefined;
27
30
  };
28
31
  }
29
- /** Returns the current width of a leaf column. */
32
+ /**
33
+ * Returns the current width of a leaf column.
34
+ *
35
+ * @api
36
+ */
30
37
  getColumnWidth(columnId) {
31
38
  const column = this.findLeafColumn(columnId);
32
39
  if (!column)
@@ -37,14 +44,11 @@ export class ColumnResizePlugin {
37
44
  return this.api.getColumnWidth(columnId) ?? column.minWidth ?? 40;
38
45
  return 120;
39
46
  }
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. */
47
+ /**
48
+ * Sets a column width and clamps it to the configured min/max constraints.
49
+ *
50
+ * @api
51
+ */
48
52
  setColumnWidth(columnId, width) {
49
53
  if (!this.api)
50
54
  return width;
@@ -78,22 +82,38 @@ export class ColumnResizePlugin {
78
82
  this.onResize?.(columnId, nextWidth);
79
83
  return nextWidth;
80
84
  }
81
- /** Alias for `setColumnWidth`. */
85
+ /**
86
+ * Alias for `setColumnWidth`.
87
+ *
88
+ * @api
89
+ */
82
90
  setWidth(columnId, width) {
83
91
  return this.setColumnWidth(columnId, width);
84
92
  }
85
- /** Changes the width by a delta in pixels. */
93
+ /**
94
+ * Changes the width by a delta in pixels.
95
+ *
96
+ * @api
97
+ */
86
98
  resizeColumn(columnId, delta) {
87
99
  const current = this.getColumnWidth(columnId);
88
100
  if (current === undefined)
89
101
  return delta;
90
102
  return this.setColumnWidth(columnId, current + delta);
91
103
  }
92
- /** Alias for `resizeColumn`. */
104
+ /**
105
+ * Alias for `resizeColumn`.
106
+ *
107
+ * @api
108
+ */
93
109
  resize(columnId, delta) {
94
110
  return this.resizeColumn(columnId, delta);
95
111
  }
96
- /** Starts an interactive resize using the pointer x-coordinate. */
112
+ /**
113
+ * Starts an interactive resize using the pointer x-coordinate.
114
+ *
115
+ * @api
116
+ */
97
117
  beginResize(columnId, clientX) {
98
118
  const column = this.findLeafColumn(columnId);
99
119
  if (!this.canResizeColumn(column))
@@ -111,11 +131,19 @@ export class ColumnResizePlugin {
111
131
  this.onResizeStart?.(columnId, currentWidth);
112
132
  return currentWidth;
113
133
  }
114
- /** Adapter-friendly alias for pointer-down events. */
134
+ /**
135
+ * Adapter-friendly alias for pointer-down events.
136
+ *
137
+ * @api
138
+ */
115
139
  handlePointerDown(columnId, clientX) {
116
140
  return this.beginResize(columnId, clientX);
117
141
  }
118
- /** Updates the current resize using the new pointer x-coordinate. */
142
+ /**
143
+ * Updates the current resize using the new pointer x-coordinate.
144
+ *
145
+ * @api
146
+ */
119
147
  resizeTo(clientX) {
120
148
  const active = this.activeResize;
121
149
  if (!active)
@@ -124,11 +152,19 @@ export class ColumnResizePlugin {
124
152
  this.guide = { columnId: active.columnId, x: clientX, visible: true };
125
153
  return this.resizeBy(delta);
126
154
  }
127
- /** Adapter-friendly alias for pointer-move events. */
155
+ /**
156
+ * Adapter-friendly alias for pointer-move events.
157
+ *
158
+ * @api
159
+ */
128
160
  handlePointerMove(clientX) {
129
161
  return this.resizeTo(clientX);
130
162
  }
131
- /** Updates the current resize using an already-calculated delta. */
163
+ /**
164
+ * Updates the current resize using an already-calculated delta.
165
+ *
166
+ * @api
167
+ */
132
168
  resizeBy(delta) {
133
169
  if (!this.activeResize)
134
170
  return 0;
@@ -137,7 +173,11 @@ export class ColumnResizePlugin {
137
173
  this.guide = { columnId, x: this.guide.x, visible: true };
138
174
  return nextWidth;
139
175
  }
140
- /** Completes the active pointer drag and hides the resize guide. */
176
+ /**
177
+ * Completes the active pointer drag and hides the resize guide.
178
+ *
179
+ * @api
180
+ */
141
181
  endResize() {
142
182
  const active = this.activeResize;
143
183
  if (!active)
@@ -148,11 +188,19 @@ export class ColumnResizePlugin {
148
188
  this.activeResize = undefined;
149
189
  return width;
150
190
  }
151
- /** Adapter-friendly alias for double-click auto-fit. */
191
+ /**
192
+ * Adapter-friendly alias for double-click auto-fit.
193
+ *
194
+ * @api
195
+ */
152
196
  handleDoubleClick(columnId) {
153
197
  return this.autoSizeColumn(columnId);
154
198
  }
155
- /** Calculates the best-fit width from header text and visible row content. */
199
+ /**
200
+ * Calculates the best-fit width from header text and visible row content.
201
+ *
202
+ * @api
203
+ */
156
204
  autoSizeColumn(columnId) {
157
205
  const column = this.findLeafColumn(columnId);
158
206
  if (!column || !this.canResizeColumn(column) || !this.api)
@@ -169,18 +217,33 @@ export class ColumnResizePlugin {
169
217
  const maximum = column.maxWidth ?? Number.POSITIVE_INFINITY;
170
218
  return this.setColumnWidth(columnId, Math.min(Math.max(estimatedWidth, minimum), maximum));
171
219
  }
172
- /** Alias for `autoSizeColumn`. */
220
+ /**
221
+ * Alias for `autoSizeColumn`.
222
+ *
223
+ * @api
224
+ */
173
225
  autoFitColumn(columnId) {
174
226
  return this.autoSizeColumn(columnId);
175
227
  }
176
- /** Returns the current drag guide state for integrations. */
228
+ /**
229
+ * Returns the current drag guide state for integrations.
230
+ *
231
+ * @api
232
+ */
177
233
  getResizeGuide() {
178
234
  return { ...this.guide };
179
235
  }
180
- /** Resets the current drag guide state. */
236
+ /**
237
+ * Resets the current drag guide state.
238
+ *
239
+ * @api
240
+ */
181
241
  clearGuide() {
182
242
  this.guide = { columnId: "", x: 0, visible: false };
183
243
  }
244
+ canResizeColumn(column) {
245
+ return !!column && column.resizable !== false;
246
+ }
184
247
  attachResizeHandles(columns) {
185
248
  return columns.map((column) => {
186
249
  if (isColumnGroup(column)) {
@@ -190,13 +253,23 @@ export class ColumnResizePlugin {
190
253
  return column;
191
254
  }
192
255
  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
- },
256
+ const wrapped = (headerColumn) => {
257
+ const resolvedHeader = originalHeaderRenderer ? originalHeaderRenderer(headerColumn) : headerColumn.header ?? headerColumn.id;
258
+ return this.createResizeHandleNode(headerColumn, resolvedHeader);
199
259
  };
260
+ this.wrappedRenderers.set(wrapped, originalHeaderRenderer);
261
+ return { ...column, headerRenderer: wrapped };
262
+ });
263
+ }
264
+ /** Restores the original header renderers that were wrapped with resize handles. */
265
+ detachResizeHandles(columns) {
266
+ return columns.map((column) => {
267
+ if (isColumnGroup(column)) {
268
+ return { ...column, children: this.detachResizeHandles(column.children) };
269
+ }
270
+ if (!column.headerRenderer || !this.wrappedRenderers.has(column.headerRenderer))
271
+ return column;
272
+ return { ...column, headerRenderer: this.wrappedRenderers.get(column.headerRenderer) };
200
273
  });
201
274
  }
202
275
  createResizeHandleNode(column, content) {
@@ -327,4 +400,7 @@ export class ColumnResizePlugin {
327
400
  });
328
401
  }
329
402
  }
403
+ /**
404
+ * {@link ColumnResizePlugin} with the `Column` prefix stripped for ergonomic construction.
405
+ */
330
406
  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.2",
4
4
  "description": "Headless column resize plugin for OmniGrid",
5
5
  "keywords": [
6
6
  "omnigrid",