@itrocks/table 0.4.2 → 0.7.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.
package/README.md CHANGED
@@ -263,22 +263,42 @@ Callbacks own safe rendering, row identity and data access. If row elements chan
263
263
  error rather than applying stale positional values. Before retry, the consumer updates its callbacks' row data to
264
264
  match the current view.
265
265
 
266
- - `add(source): Promise<'added' | 'existing'>` appends before right-frozen columns, reveals and highlights. It resolves
267
- once the real placeholder column is committed, independently of optional values. A repeated id reveals/highlights its
266
+ - `add(source, options?): Promise<'added' | 'existing'>` appends before right-frozen columns, reveals and highlights.
267
+ It resolves once the real placeholder column is committed, independently of optional values. A repeated id reveals/highlights its
268
268
  existing position, without invoking its creation/loading callbacks or issuing another commit.
269
269
  - `reveal(id): void` scrolls the horizontal container only as needed, accounting for frozen bounds.
270
- - `highlight(id): void` applies the same green/light/theme flash for new and existing columns, cleans up after 900 ms,
271
- honours reduced motion and preserves keyboard focus and row selection.
270
+ - `highlight(id): void` applies the same green/light/theme flash for new and existing columns, cleans up after 350 ms,
271
+ honours reduced motion and preserves keyboard focus and row selection. Existing columns keep their content and width.
272
+ - Columns appended at the right edge (before any right-frozen columns) appear at full width immediately, with their
273
+ heading, filter and placeholder cells visible in green. Loaded values appear immediately and scroll the table to its
274
+ right edge over 100 ms if needed. Reduced motion makes this scroll immediate.
275
+ - Columns inserted between existing columns open from zero to their measured width over 100 ms.
276
+ The flash stays green until values arrive, then
277
+ fades over the remainder of 350 ms from opening, with a minimum fade of 100 ms. Content remains hidden until the
278
+ fade ends for value cells; the heading and filter become visible after the 100 ms opening. A non-breaking space
279
+ keeps masked cells painted and holds their measured width. Asynchronous values continue
280
+ opening from the current width towards the greater of the heading width and the loaded content width.
281
+ - `remove(id, signal?): Promise<boolean>` reveals a column, shrinks it over 100 ms during a 300 ms red flash,
282
+ hiding cell content immediately, then removes the header, body/footer cells and matching `col`. It returns false for
283
+ a missing column or cancellation. Repeated calls share the pending removal; abort, disposal and reset clear the flash
284
+ without deletion. Late values cannot restore a removed column. Cancellation restores the natural width.
285
+ Reduced motion skips width animation and uses a static red outline.
286
+ The host persists the resulting configuration.
272
287
  - `retry(id): Promise<void>` retries a failed value callback. Failure retains the committed column and marks it with
273
- `.column-add-error`; the consumer presents the error and a retry control. It never confirms another addition.
274
- - `begin(source): ColumnTransaction` creates a real provisional column without committing it. Its `ready` promise waits
275
- for optional values and resolves after error notification as well. `moveTo(index)` reuses public reorder placement
288
+ `.column-add-error`; data cells receive a discreet red background and the addition flash stops.
289
+ It never confirms another addition. The plugin adds no error text, retry control or automatic retry.
290
+ - `begin(source, options?): ColumnTransaction` creates a real provisional column without committing it.
291
+ Its `ready` promise waits for optional values and resolves after error notification as well. `moveTo(index)` reuses public reorder placement
276
292
  and its existing boundaries. `commit()` validates it and returns `added` or `existing`; repeating commit returns
277
293
  `existing`. `cancel()` aborts values, removes new provisional cells or restores an existing column's position.
278
294
  Completed/cancelled handles cannot move a column. `begin()` itself installs no pointer transport.
279
295
  - `dispose(): void` cancels provisional operations, aborts outstanding values and removes flash resources. Committed
280
296
  columns remain. `Table.reset()` invokes disposal automatically; call disposal when removing the owning view.
281
297
 
298
+ `ColumnCreationOptions` is shared by `add` and `begin`. `hidden: true` inserts hidden cells and a hidden `col`;
299
+ loaded replacements retain that visibility. `effects: false` skips width animation, flash and automatic scroll for both
300
+ creation and loaded values. Loading still starts immediately. Pass both options to prepare a column for pointer entry.
301
+
282
302
  `TableColumnAddOptions.identify(header)` defaults to `header.dataset.columnId`. Added and loaded headers carry this
283
303
  opaque attribute so identity survives plugin resets. A custom callback can identify pre-existing columns differently.
284
304
  `onChange(change)` runs synchronously for `provisional`, `committed`, `loaded`, `error` and `cancelled` observations;
@@ -288,10 +308,17 @@ An aborted or removed operation cannot be recreated by late values.
288
308
 
289
309
  `TableColumnReorder.moveColumn(header, index)` is public placement without header gesture callbacks. It rejects an
290
310
  active header interaction and clamps movement to that header's reorderable boundaries.
311
+ `TableColumnReorder.removeColumn(header): boolean` removes a reorderable column and its `col` without header callbacks.
312
+ It rejects an active header interaction and returns false for a missing or fixed column.
291
313
  `TableColumnReorder.replaceCell(previous, replacement)`
292
314
  preserves an active header gesture during asynchronous cell replacement. `TableFreeze.refresh()` recalculates sticky
293
315
  offsets after structural changes. `Table.refreshStyleSheets()` reapplies the managed styles.
294
316
 
317
+ Internal and external drags share one placement rule. Hovering the source column leaves its position unchanged.
318
+ Over another column, moving left places the source before it; moving right places the source after it.
319
+ Placement uses the current pointer or automatic scrolling direction, without retaining the previous hovered column.
320
+ Cells and the corresponding `<col>` are moved only when the resulting index changes.
321
+
295
322
  `TableColumnReorder.scrollContainer()` exposes the existing horizontal scroll owner for table plugins.
296
323
  `TableFreeze.visibleInnerRect()` returns the area between rendered sticky cells, including tables configured through
297
324
  `colgroup`. `reveal()` fully exposes columns that fit that area and uses its maximum width for wider columns, with
@@ -300,9 +327,11 @@ including header-backed tables after addition, asynchronous replacement or reset
300
327
 
301
328
  ### External pointer transport
302
329
 
303
- `beginExternal(source, sample): ColumnOperation` immediately creates a provisional real column, or reuses an existing
304
- id. The structural `PointerSample` contains `pointerId`, `pointerType`, `clientX` and `clientY`. The source owns
305
- capture.
330
+ `beginExternal(source, sample, options?): ColumnOperation` immediately creates a hidden provisional column and starts
331
+ loading its values. The cells and optional `col` stay hidden until the pointer first enters the visible table; loaded replacements
332
+ preserve that visibility. On entry, the column appears with any values already received and starts following reorder
333
+ geometry. Reusing an existing id preserves its visibility, including a committed hidden column prepared with `begin`.
334
+ The structural `PointerSample` contains `pointerId`, `pointerType`, `clientX` and `clientY`. The source owns capture.
306
335
  `move(sample)` uses the existing reorder engine, including frozen boundaries and visible-edge autoscroll.
307
336
  `finish(sample)` resolves to `added`, `moved` or `cancelled`; an outside drop cancels without a fallback insertion.
308
337
  `cancel()` restores an existing column's original position or removes a new preview. Neither operation invokes the
@@ -313,9 +342,17 @@ Optional `ColumnSource.accept(signal)` validates host admission independently of
313
342
  waiting for `load`. Rejection rolls back and rejects `finish`. An omitted callback accepts immediately. Value errors
314
343
  remain retryable and do not reject a valid drop. The host must keep admission independent of its data request.
315
344
 
316
- `TableColumnReorder.beginExternal(header, sample): ExternalColumnOperation` is the lower-level public extension for
317
- an already attached column. Its `move`, `hold`, `finish` and `cancel` methods share header-origin geometry and
345
+ `TableColumnReorder.beginExternal(header, sample, options?): ExternalColumnOperation` is the lower-level public
346
+ extension for an already attached column. Its `move`, `hold`, `finish` and `cancel` methods share header-origin geometry and
318
347
  placement.
319
348
  `hold(sample)` settles and pauses autoscroll while retaining exclusivity; it returns whether the target is valid.
320
- `finish(sample)` releases the gesture and returns acceptance. It never calls header reorder/removal notifications;
349
+ `finish(sample)` releases the gesture and returns acceptance. External reordering leaves persistence to its owner;
321
350
  the owning addition transaction emits its distinct commit notification. Header-origin gestures retain their callbacks.
351
+
352
+ Both external entry points accept `ColumnDragOptions`. `adding: true` applies `.adding` for a green addition indicator
353
+ instead of `.moving`, and removes the newly created column on cancellation. `removal: true` enables the standard
354
+ header-origin removal on an outside drop, including a hidden column that never entered the table. Removal invokes
355
+ `onRemove` once; the higher-level operation resolves to `removed`. The defaults preserve preview cancellation.
356
+ Hidden cells and their `col` become visible on first pointer entry, without an opening animation or automatic reveal
357
+ scroll. `onReveal()` observes this transition; the addition wrapper also refreshes frozen geometry. Replacement cells
358
+ preserve hidden state and active indicators while values load.
package/column-add.css CHANGED
@@ -1,5 +1,44 @@
1
+ @charset "UTF-8";
2
+ table.itrocks > colgroup > col[hidden],
3
+ table.itrocks > :is(thead, tbody, tfoot) > tr > :is(th, td)[hidden] {
4
+ display: none !important;
5
+ }
6
+
1
7
  table.itrocks > :is(thead, tbody, tfoot) > tr > :is(th, td).column-add-highlight {
2
- animation: itrocks-column-add-flash 850ms ease-out;
8
+ animation: itrocks-column-add-flash var(--column-fade, 350ms) ease-out;
9
+ }
10
+ table.itrocks > :is(thead, tbody, tfoot) > tr > :is(th, td).column-add-highlight.column-add-loading {
11
+ animation: none;
12
+ background-color: color-mix(in srgb, #58b46b 35%, Canvas);
13
+ }
14
+ table.itrocks > :is(thead, tbody, tfoot) > tr > :is(th, td).column-add-opening::after {
15
+ animation: itrocks-column-open 100ms ease-out both;
16
+ }
17
+ table.itrocks > :is(thead, tbody, tfoot) > tr > :is(th, td).column-remove-highlight {
18
+ animation: itrocks-column-remove-flash 300ms ease-out;
19
+ }
20
+ table.itrocks > :is(thead, tbody, tfoot) > tr > :is(th, td).column-remove-highlight::after {
21
+ animation: itrocks-column-close 100ms ease-out both;
22
+ }
23
+ table.itrocks > :is(thead, tbody, tfoot) > tr > :is(th, td).column-remove-highlight, table.itrocks > :is(thead, tbody, tfoot) > tr > :is(th, td).column-add-new:is(.column-add-opening, td, tbody th, tfoot th) {
24
+ box-sizing: border-box;
25
+ font-size: 0 !important;
26
+ height: var(--column-height);
27
+ overflow: hidden;
28
+ padding: 0 !important;
29
+ }
30
+ table.itrocks > :is(thead, tbody, tfoot) > tr > :is(th, td).column-remove-highlight > *, table.itrocks > :is(thead, tbody, tfoot) > tr > :is(th, td).column-add-new:is(.column-add-opening, td, tbody th, tfoot th) > * {
31
+ display: none !important;
32
+ }
33
+ table.itrocks > :is(thead, tbody, tfoot) > tr > :is(th, td).column-remove-highlight::before, table.itrocks > :is(thead, tbody, tfoot) > tr > :is(th, td).column-add-new:is(.column-add-opening, td, tbody th, tfoot th)::before {
34
+ content: none !important;
35
+ }
36
+ table.itrocks > :is(thead, tbody, tfoot) > tr > :is(th, td).column-remove-highlight::after, table.itrocks > :is(thead, tbody, tfoot) > tr > :is(th, td).column-add-new:is(.column-add-opening, td, tbody th, tfoot th)::after {
37
+ content: " " !important;
38
+ display: block;
39
+ font-size: 1px;
40
+ line-height: 1px;
41
+ width: var(--column-width);
3
42
  }
4
43
  table.itrocks > :is(thead, tbody, tfoot) > tr > :is(th, td).column-add-loading {
5
44
  cursor: progress;
@@ -7,11 +46,26 @@ table.itrocks > :is(thead, tbody, tfoot) > tr > :is(th, td).column-add-loading {
7
46
  table.itrocks > :is(thead, tbody, tfoot) > tr > :is(th, td).column-add-provisional {
8
47
  background-color: color-mix(in srgb, #58b46b 5%, Canvas);
9
48
  }
10
- table.itrocks > :is(thead, tbody, tfoot) > tr > :is(th, td).column-add-error {
11
- outline: 1px solid #c33;
12
- outline-offset: -2px;
49
+ table.itrocks > :is(thead, tbody, tfoot) > tr > :is(th, td).column-add-error:is(td, tbody th, tfoot th) {
50
+ background-color: color-mix(in srgb, #d33 12%, Canvas);
13
51
  }
14
52
 
53
+ @keyframes itrocks-column-open {
54
+ from {
55
+ width: var(--column-from, 0px);
56
+ }
57
+ to {
58
+ width: var(--column-width);
59
+ }
60
+ }
61
+ @keyframes itrocks-column-close {
62
+ from {
63
+ width: var(--column-width);
64
+ }
65
+ to {
66
+ width: 0;
67
+ }
68
+ }
15
69
  @keyframes itrocks-column-add-flash {
16
70
  0% {
17
71
  background-color: color-mix(in srgb, #58b46b 35%, Canvas);
@@ -20,9 +74,29 @@ table.itrocks > :is(thead, tbody, tfoot) > tr > :is(th, td).column-add-error {
20
74
  background-color: color-mix(in srgb, white 20%, Canvas);
21
75
  }
22
76
  }
77
+ @keyframes itrocks-column-remove-flash {
78
+ 0% {
79
+ background-color: color-mix(in srgb, #d33 50%, Canvas);
80
+ }
81
+ }
23
82
  @media (prefers-reduced-motion: reduce) {
24
- table.itrocks > :is(thead, tbody, tfoot) > tr > :is(th, td).column-add-highlight {
83
+ table.itrocks > :is(thead, tbody, tfoot) > tr > :is(th, td).column-remove-highlight {
84
+ animation: none;
85
+ }
86
+ table.itrocks > :is(thead, tbody, tfoot) > tr > :is(th, td).column-remove-highlight::after {
25
87
  animation: none;
88
+ }
89
+ table.itrocks > :is(thead, tbody, tfoot) > tr > :is(th, td).column-remove-highlight {
90
+ outline: 2px solid #c33;
91
+ outline-offset: -2px;
92
+ }
93
+ table.itrocks > :is(thead, tbody, tfoot) > tr > :is(th, td):is(.column-add-highlight, .column-add-opening) {
94
+ animation: none;
95
+ }
96
+ table.itrocks > :is(thead, tbody, tfoot) > tr > :is(th, td):is(.column-add-highlight, .column-add-opening)::after {
97
+ animation: none;
98
+ }
99
+ table.itrocks > :is(thead, tbody, tfoot) > tr > :is(th, td):is(.column-add-highlight, .column-add-opening) {
26
100
  outline: 2px solid color-mix(in srgb, #58b46b 65%, Canvas);
27
101
  outline-offset: -2px;
28
102
  }
package/column-add.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { Plugin } from '../plugin/plugin.js';
2
2
  import { PluginOptions } from '../plugin/plugin.js';
3
+ import type { ColumnDragOptions } from './column-reorder.js';
3
4
  import type { PointerSample } from './column-reorder.js';
4
5
  import { TableColumnReorder } from './column-reorder.js';
5
6
  import { Table } from './table.js';
@@ -13,9 +14,13 @@ export type ColumnContent = {
13
14
  cells: readonly HTMLTableCellElement[];
14
15
  header: HTMLTableCellElement;
15
16
  };
17
+ export type ColumnCreationOptions = {
18
+ effects?: boolean;
19
+ hidden?: boolean;
20
+ };
16
21
  export interface ColumnOperation {
17
22
  cancel(): void;
18
- finish(sample: PointerSample): Promise<'added' | 'moved' | 'cancelled'>;
23
+ finish(sample: PointerSample): Promise<'added' | 'moved' | 'removed' | 'cancelled'>;
19
24
  move(sample: PointerSample): void;
20
25
  }
21
26
  export type ColumnResult = 'added' | 'existing';
@@ -39,36 +44,50 @@ interface Pending {
39
44
  abort: AbortController;
40
45
  col?: HTMLTableColElement;
41
46
  content: ColumnContent;
47
+ effects: boolean;
42
48
  initial: number;
43
49
  rows: HTMLTableRowElement[];
44
50
  settled: boolean;
45
51
  source: ColumnSource;
52
+ started?: number;
46
53
  transaction: ColumnTransaction;
47
54
  }
48
55
  export declare class TableColumnAdd extends Plugin<Table, TableColumnAddOptions> {
49
56
  protected disposed: boolean;
50
57
  protected external?: ColumnOperation;
51
- protected highlights: Map<HTMLTableCellElement, number>;
58
+ protected highlights: Map<HTMLTableCellElement, number | undefined>;
52
59
  protected identities: WeakMap<HTMLTableCellElement, string>;
53
60
  protected pending: Map<string, Pending>;
61
+ protected removals: Map<string, {
62
+ abort: AbortController;
63
+ promise: Promise<boolean>;
64
+ }>;
54
65
  protected reorder: TableColumnReorder;
55
- add(source: ColumnSource): Promise<ColumnResult>;
66
+ protected scrollFrame?: number;
67
+ add(source: ColumnSource, options?: ColumnCreationOptions): Promise<ColumnResult>;
56
68
  /** Create a provisional column without installing pointer transport. */
57
- begin(source: ColumnSource): ColumnTransaction;
69
+ begin(source: ColumnSource, options?: ColumnCreationOptions): ColumnTransaction;
58
70
  /** Add or move an opaque column through the existing reorder geometry. */
59
- beginExternal(source: ColumnSource, sample: PointerSample): ColumnOperation;
71
+ beginExternal(source: ColumnSource, sample: PointerSample, options?: ColumnDragOptions): ColumnOperation;
72
+ protected create(source: ColumnSource, options?: ColumnCreationOptions): ColumnTransaction;
60
73
  defaultOptions(): TableColumnAddOptions;
61
74
  dispose(): void;
62
75
  protected find(id: string): HTMLTableCellElement | undefined;
63
76
  protected headers(): HTMLTableRowElement;
64
- highlight(id: string): void;
77
+ highlight(id: string, opening?: boolean): void;
65
78
  init(): void;
79
+ protected isLastColumn(header: HTMLTableCellElement): boolean;
66
80
  protected live(entry: Pending): boolean;
67
81
  protected load(entry: Pending): Promise<void>;
68
82
  protected mark(entry: Pending, state: 'error' | 'loading' | 'provisional', present: boolean): void;
83
+ protected measureColumn(header: HTMLTableCellElement, width?: number): void;
69
84
  protected notify(entry: Pending, state: ColumnState, error?: unknown): void;
70
85
  protected refresh(): void;
86
+ /** Reveal and briefly mark a column in red before removing it. */
87
+ remove(id: string, signal?: AbortSignal): Promise<boolean>;
88
+ protected removeColumn(id: string, signal: AbortSignal): Promise<boolean>;
71
89
  reveal(id: string): void;
90
+ protected scrollToEnd(): void;
72
91
  retry(id: string): Promise<void>;
73
92
  protected validate(content: ColumnContent, rows: HTMLTableRowElement[]): void;
74
93
  }
package/column-add.js CHANGED
@@ -8,20 +8,24 @@ export class TableColumnAdd extends Plugin {
8
8
  highlights = new Map();
9
9
  identities = new WeakMap();
10
10
  pending = new Map();
11
+ removals = new Map();
11
12
  reorder;
12
- add(source) {
13
+ scrollFrame;
14
+ add(source, options = {}) {
13
15
  try {
14
16
  if (this.disposed)
15
17
  throw new Error('Column addition is disposed');
16
18
  if (this.find(source.id)) {
17
- this.reveal(source.id);
18
- this.highlight(source.id);
19
+ if (options.effects !== false) {
20
+ this.reveal(source.id);
21
+ this.highlight(source.id);
22
+ }
19
23
  return Promise.resolve('existing');
20
24
  }
21
- const operation = this.begin(source);
25
+ const operation = this.begin(source, options);
22
26
  const result = operation.commit();
23
- this.reveal(source.id);
24
- this.highlight(source.id);
27
+ if (options.effects !== false)
28
+ this.reveal(source.id);
25
29
  return Promise.resolve(result);
26
30
  }
27
31
  catch (error) {
@@ -29,9 +33,118 @@ export class TableColumnAdd extends Plugin {
29
33
  }
30
34
  }
31
35
  /** Create a provisional column without installing pointer transport. */
32
- begin(source) {
36
+ begin(source, options = {}) {
37
+ return this.create(source, options);
38
+ }
39
+ /** Add or move an opaque column through the existing reorder geometry. */
40
+ beginExternal(source, sample, options = {}) {
41
+ if (this.disposed || this.external)
42
+ throw new Error('Column addition is unavailable');
43
+ if (this.pending.get(source.id)?.transaction.state === 'provisional')
44
+ throw new Error('Column addition is pending');
45
+ const existing = this.find(source.id);
46
+ const transaction = existing ? undefined : this.create(source, { effects: false, hidden: true });
47
+ let reorder;
48
+ const enter = (point) => {
49
+ if (reorder || point.pointerId !== sample.pointerId)
50
+ return;
51
+ const table = this.of.element;
52
+ if (!existing && !table.contains(table.ownerDocument.elementFromPoint(point.clientX, point.clientY)))
53
+ return;
54
+ try {
55
+ if (transaction) {
56
+ const entry = this.pending.get(source.id);
57
+ for (const cell of [entry.content.header, ...entry.content.cells])
58
+ cell.hidden = false;
59
+ if (entry.col)
60
+ entry.col.hidden = false;
61
+ this.refresh();
62
+ }
63
+ reorder = this.reorder.beginExternal(existing ?? transaction.header, point, {
64
+ ...options, onReveal: () => {
65
+ this.refresh();
66
+ options.onReveal?.();
67
+ }
68
+ });
69
+ }
70
+ catch (error) {
71
+ operation.cancel();
72
+ throw error;
73
+ }
74
+ };
75
+ let complete = false;
76
+ let finishing = false;
77
+ const abort = new AbortController();
78
+ const acceptance = Promise.resolve().then(() => source.accept?.(abort.signal));
79
+ // Keep a rejected admission observable at finish without an unhandled rejection during transport.
80
+ void acceptance.catch(() => { });
81
+ const operation = {
82
+ cancel: () => {
83
+ if (complete)
84
+ return;
85
+ complete = true;
86
+ abort.abort();
87
+ reorder?.cancel();
88
+ if (!this.find(source.id)) {
89
+ this.pending.get(source.id)?.abort.abort();
90
+ this.pending.delete(source.id);
91
+ }
92
+ transaction?.cancel();
93
+ this.external = undefined;
94
+ this.refresh();
95
+ },
96
+ finish: async (point) => {
97
+ if (complete || finishing)
98
+ return 'cancelled';
99
+ finishing = true;
100
+ enter(point);
101
+ if (!reorder?.hold(point)) {
102
+ operation.cancel();
103
+ return 'cancelled';
104
+ }
105
+ try {
106
+ await acceptance;
107
+ }
108
+ catch (error) {
109
+ operation.cancel();
110
+ throw error;
111
+ }
112
+ if (complete)
113
+ return 'cancelled';
114
+ if (!reorder.finish(point)) {
115
+ operation.cancel();
116
+ return 'cancelled';
117
+ }
118
+ complete = true;
119
+ this.external = undefined;
120
+ if (!this.find(source.id)) {
121
+ this.pending.get(source.id)?.abort.abort();
122
+ this.pending.delete(source.id);
123
+ this.refresh();
124
+ return 'removed';
125
+ }
126
+ if (transaction)
127
+ transaction.commit();
128
+ else
129
+ this.options.onChange?.({ header: this.find(source.id), id: source.id, state: 'committed' });
130
+ this.refresh();
131
+ return existing ? 'moved' : 'added';
132
+ },
133
+ move: point => {
134
+ if (complete || finishing)
135
+ return;
136
+ enter(point);
137
+ reorder?.move(point);
138
+ }
139
+ };
140
+ this.external = operation;
141
+ enter(sample);
142
+ return operation;
143
+ }
144
+ create(source, options = {}) {
33
145
  if (this.disposed)
34
146
  throw new Error('Column addition is disposed');
147
+ const hidden = options.hidden ?? false;
35
148
  const pending = this.pending.get(source.id);
36
149
  if (pending && pending.content.header.parentElement === this.headers())
37
150
  return pending.transaction;
@@ -81,6 +194,12 @@ export class TableColumnAdd extends Plugin {
81
194
  this.mark(entry, 'provisional', false);
82
195
  if (!existing || (entry.content.header.cellIndex !== entry.initial))
83
196
  this.notify(entry, 'committed');
197
+ if (!existing && entry.effects) {
198
+ entry.started = performance.now();
199
+ this.highlight(source.id, true);
200
+ if (entry.settled && this.isLastColumn(entry.content.header))
201
+ this.scrollToEnd();
202
+ }
84
203
  if (existing || !source.load || (entry.settled && !entry.content.header.classList.contains('column-add-error')))
85
204
  this.pending.delete(source.id);
86
205
  return existing ? 'existing' : 'added';
@@ -94,11 +213,14 @@ export class TableColumnAdd extends Plugin {
94
213
  this.refresh();
95
214
  }
96
215
  };
97
- const entry = { abort, content, initial: existing?.cellIndex ?? -1,
216
+ const entry = { abort, content, effects: options.effects ?? true, initial: existing?.cellIndex ?? -1,
98
217
  rows, settled: false, source, transaction };
99
218
  this.pending.set(source.id, entry);
100
219
  if (existing)
101
220
  return transaction;
221
+ if (hidden)
222
+ for (const cell of [content.header, ...content.cells])
223
+ cell.hidden = true;
102
224
  this.identities.set(content.header, source.id);
103
225
  content.header.dataset.columnId = source.id;
104
226
  const freeze = this.of.plugins.TableFreeze;
@@ -108,6 +230,7 @@ export class TableColumnAdd extends Plugin {
108
230
  const group = this.of.element.querySelector('colgroup');
109
231
  if (group) {
110
232
  entry.col = document.createElement('col');
233
+ entry.col.hidden = hidden;
111
234
  group.insertBefore(entry.col, group.children[index] ?? null);
112
235
  }
113
236
  this.mark(entry, 'provisional', true);
@@ -117,84 +240,18 @@ export class TableColumnAdd extends Plugin {
117
240
  ready = this.load(entry);
118
241
  return transaction;
119
242
  }
120
- /** Add or move an opaque column through the existing reorder geometry. */
121
- beginExternal(source, sample) {
122
- if (this.disposed || this.external)
123
- throw new Error('Column addition is unavailable');
124
- if (this.pending.get(source.id)?.transaction.state === 'provisional')
125
- throw new Error('Column addition is pending');
126
- const existing = this.find(source.id);
127
- const initial = existing?.cellIndex;
128
- const transaction = existing ? undefined : this.begin(source);
129
- let reorder;
130
- try {
131
- reorder = this.reorder.beginExternal(existing ?? transaction.header, sample);
132
- }
133
- catch (error) {
134
- transaction?.cancel();
135
- throw error;
136
- }
137
- let complete = false;
138
- let finishing = false;
139
- const abort = new AbortController();
140
- const acceptance = Promise.resolve().then(() => source.accept?.(abort.signal));
141
- // Keep a rejected admission observable at finish without an unhandled rejection during transport.
142
- void acceptance.catch(() => { });
143
- const operation = {
144
- cancel: () => {
145
- if (complete)
146
- return;
147
- complete = true;
148
- abort.abort();
149
- reorder.cancel();
150
- if (initial !== undefined)
151
- this.reorder.moveColumn(this.find(source.id), initial);
152
- transaction?.cancel();
153
- this.external = undefined;
154
- this.refresh();
155
- },
156
- finish: async (point) => {
157
- if (complete || finishing)
158
- return 'cancelled';
159
- finishing = true;
160
- if (!reorder.hold(point)) {
161
- operation.cancel();
162
- return 'cancelled';
163
- }
164
- try {
165
- await acceptance;
166
- }
167
- catch (error) {
168
- operation.cancel();
169
- throw error;
170
- }
171
- if (complete)
172
- return 'cancelled';
173
- if (!reorder.finish(point)) {
174
- operation.cancel();
175
- return 'cancelled';
176
- }
177
- complete = true;
178
- this.external = undefined;
179
- if (transaction)
180
- transaction.commit();
181
- else
182
- this.options.onChange?.({ header: this.find(source.id), id: source.id, state: 'committed' });
183
- this.refresh();
184
- return existing ? 'moved' : 'added';
185
- },
186
- move: point => { if (!complete)
187
- reorder.move(point); }
188
- };
189
- this.external = operation;
190
- return operation;
191
- }
192
243
  defaultOptions() {
193
244
  return new TableColumnAddOptions();
194
245
  }
195
246
  dispose() {
196
247
  this.external?.cancel();
197
248
  this.disposed = true;
249
+ if (this.scrollFrame !== undefined)
250
+ cancelAnimationFrame(this.scrollFrame);
251
+ this.scrollFrame = undefined;
252
+ for (const removal of this.removals.values())
253
+ removal.abort.abort();
254
+ this.removals.clear();
198
255
  for (const entry of [...this.pending.values()]) {
199
256
  entry.transaction.cancel();
200
257
  entry.abort.abort();
@@ -203,7 +260,9 @@ export class TableColumnAdd extends Plugin {
203
260
  this.pending.clear();
204
261
  for (const [cell, timer] of this.highlights) {
205
262
  clearTimeout(timer);
206
- cell.classList.remove('column-add-highlight');
263
+ cell.classList.remove('column-add-highlight', 'column-add-new', 'column-add-opening');
264
+ for (const name of ['fade', 'from', 'height', 'width'])
265
+ cell.style?.removeProperty(`--column-${name}`);
207
266
  }
208
267
  this.highlights.clear();
209
268
  }
@@ -216,23 +275,47 @@ export class TableColumnAdd extends Plugin {
216
275
  throw new Error('Column addition requires one header row');
217
276
  return row;
218
277
  }
219
- highlight(id) {
278
+ highlight(id, opening = false) {
220
279
  const header = this.find(id);
221
280
  if (!header || this.disposed)
222
281
  return;
282
+ const started = opening ? this.pending.get(id)?.started : undefined;
283
+ const duration = started === undefined ? 350 : Math.max(100, 350 - (performance.now() - started));
284
+ const waiting = opening && header.classList.contains('column-add-loading');
285
+ const expanding = opening && !this.isLastColumn(header);
223
286
  for (const row of Array.from(this.of.element.rows)) {
224
287
  const cell = row.cells[header.cellIndex];
225
288
  if (!cell)
226
289
  continue;
227
290
  clearTimeout(this.highlights.get(cell));
228
- cell.classList.remove('column-add-highlight');
229
- void cell.offsetWidth;
291
+ cell.classList.remove('column-add-highlight', 'column-add-new', 'column-add-opening');
292
+ }
293
+ this.measureColumn(header);
294
+ void header.offsetWidth;
295
+ for (const row of Array.from(this.of.element.rows)) {
296
+ const cell = row.cells[header.cellIndex];
297
+ if (!cell)
298
+ continue;
299
+ cell.classList.toggle('column-add-new', expanding);
300
+ cell.classList.toggle('column-add-opening', expanding);
230
301
  cell.classList.add('column-add-highlight');
231
- this.highlights.set(cell, setTimeout(() => {
232
- cell.classList.remove('column-add-highlight');
302
+ cell.style?.setProperty('--column-fade', `${duration}ms`);
303
+ this.highlights.set(cell, waiting ? undefined : setTimeout(() => {
304
+ cell.classList.remove('column-add-highlight', 'column-add-new', 'column-add-opening');
233
305
  this.highlights.delete(cell);
234
- }, 900));
306
+ for (const name of ['fade', 'from', 'height', 'width'])
307
+ cell.style?.removeProperty(`--column-${name}`);
308
+ if (!this.highlights.size)
309
+ this.refresh();
310
+ }, duration));
235
311
  }
312
+ if (expanding && header.ownerDocument)
313
+ setTimeout(() => {
314
+ if (this.disposed || !header.isConnected)
315
+ return;
316
+ header.classList.remove('column-add-opening');
317
+ this.refresh();
318
+ }, 100);
236
319
  }
237
320
  init() {
238
321
  this.disposed = false;
@@ -242,6 +325,11 @@ export class TableColumnAdd extends Plugin {
242
325
  this.reorder = reorder;
243
326
  this.of.onReset.push(() => this.dispose());
244
327
  }
328
+ isLastColumn(header) {
329
+ const freeze = this.of.plugins.TableFreeze;
330
+ const right = freeze instanceof TableFreeze ? freeze.rightColumnCount : 0;
331
+ return header.cellIndex === this.headers().cells.length - right - 1;
332
+ }
245
333
  live(entry) {
246
334
  return !this.disposed && !entry.abort.signal.aborted && (this.pending.get(entry.source.id) === entry)
247
335
  && entry.content.header.parentElement === this.headers();
@@ -261,18 +349,41 @@ export class TableColumnAdd extends Plugin {
261
349
  throw new Error('Column row view changed during loading');
262
350
  }
263
351
  const old = entry.content;
352
+ const opening = old.header.classList.contains('column-add-opening');
353
+ const width = old.header.getBoundingClientRect?.().width ?? 0;
354
+ const target = parseFloat(old.header.style?.getPropertyValue('--column-width') ?? '') || width;
355
+ for (const cell of [old.header, ...old.cells]) {
356
+ clearTimeout(this.highlights.get(cell));
357
+ this.highlights.delete(cell);
358
+ }
359
+ for (const cell of [content.header, ...content.cells])
360
+ cell.hidden = old.header.hidden;
264
361
  this.reorder.replaceCell(old.header, content.header);
265
362
  old.cells.forEach((cell, index) => this.reorder.replaceCell(cell, content.cells[index]));
266
363
  entry.content = content;
267
364
  this.identities.set(content.header, entry.source.id);
268
365
  content.header.dataset.columnId = entry.source.id;
269
- const highlighted = old.header.classList.contains('column-add-highlight');
366
+ const removing = old.header.classList.contains('column-remove-highlight');
367
+ if (removing) {
368
+ this.measureColumn(content.header, width);
369
+ for (const cell of [content.header, ...content.cells])
370
+ cell.classList.add('column-remove-highlight');
371
+ }
270
372
  this.mark(entry, 'provisional', entry.transaction.state === 'provisional');
271
373
  this.mark(entry, 'loading', false);
272
374
  this.refresh();
273
375
  this.notify(entry, 'loaded');
274
- if (highlighted)
275
- this.highlight(entry.source.id);
376
+ if (!removing && entry.effects && entry.transaction.state === 'committed') {
377
+ for (const cell of [content.header, ...content.cells]) {
378
+ cell.style?.setProperty('--column-from', `${width}px`);
379
+ cell.style?.setProperty('--column-width', `${target}px`);
380
+ }
381
+ this.highlight(entry.source.id, true);
382
+ if (!opening)
383
+ content.header.classList.remove('column-add-opening');
384
+ if (this.isLastColumn(content.header))
385
+ this.scrollToEnd();
386
+ }
276
387
  }
277
388
  catch (error) {
278
389
  if (!this.live(entry))
@@ -280,6 +391,14 @@ export class TableColumnAdd extends Plugin {
280
391
  this.mark(entry, 'loading', false);
281
392
  this.mark(entry, 'error', true);
282
393
  this.notify(entry, 'error', error);
394
+ for (const cell of [entry.content.header, ...entry.content.cells]) {
395
+ clearTimeout(this.highlights.get(cell));
396
+ this.highlights.delete(cell);
397
+ cell.classList.remove('column-add-highlight', 'column-add-new', 'column-add-opening');
398
+ for (const name of ['fade', 'from', 'height', 'width'])
399
+ cell.style?.removeProperty(`--column-${name}`);
400
+ }
401
+ this.refresh();
283
402
  }
284
403
  finally {
285
404
  entry.settled = true;
@@ -295,6 +414,17 @@ export class TableColumnAdd extends Plugin {
295
414
  present ? cell.setAttribute('aria-busy', 'true') : cell.removeAttribute('aria-busy');
296
415
  }
297
416
  }
417
+ measureColumn(header, width) {
418
+ if (!header.ownerDocument)
419
+ return;
420
+ width ??= Math.max(header.getBoundingClientRect().width, parseFloat(header.style.getPropertyValue('--column-width')) || 0);
421
+ const cells = Array.from(this.of.element.rows).map(row => row.cells[header.cellIndex]);
422
+ const heights = cells.map(cell => cell.getBoundingClientRect().height);
423
+ cells.forEach((cell, index) => {
424
+ cell.style.setProperty('--column-width', `${width}px`);
425
+ cell.style.setProperty('--column-height', `${heights[index]}px`);
426
+ });
427
+ }
298
428
  notify(entry, state, error) {
299
429
  this.options.onChange?.({ error, header: entry.content.header, id: entry.source.id, state });
300
430
  }
@@ -304,6 +434,68 @@ export class TableColumnAdd extends Plugin {
304
434
  freeze.refresh();
305
435
  this.of.refreshStyleSheets();
306
436
  }
437
+ /** Reveal and briefly mark a column in red before removing it. */
438
+ remove(id, signal) {
439
+ const pending = this.removals.get(id);
440
+ if (pending)
441
+ return pending.promise;
442
+ if (this.disposed || signal?.aborted || !this.find(id))
443
+ return Promise.resolve(false);
444
+ if (this.external || this.pending.get(id)?.transaction.state === 'provisional')
445
+ return Promise.reject(new Error('A column interaction is active'));
446
+ const abort = new AbortController();
447
+ const cancelled = AbortSignal.any([abort.signal, ...(signal ? [signal] : [])]);
448
+ const promise = this.removeColumn(id, cancelled).finally(() => {
449
+ if (this.removals.get(id)?.abort === abort)
450
+ this.removals.delete(id);
451
+ });
452
+ this.removals.set(id, { abort, promise });
453
+ return promise;
454
+ }
455
+ async removeColumn(id, signal) {
456
+ this.reveal(id);
457
+ const header = this.find(id);
458
+ const cells = Array.from(this.of.element.rows).map(row => row.cells[header.cellIndex]).filter(Boolean);
459
+ this.measureColumn(header, header.getBoundingClientRect?.().width ?? 0);
460
+ for (const cell of cells) {
461
+ clearTimeout(this.highlights.get(cell));
462
+ this.highlights.delete(cell);
463
+ cell.classList.remove('column-add-highlight', 'column-add-new', 'column-add-opening');
464
+ cell.classList.add('column-remove-highlight');
465
+ }
466
+ try {
467
+ await new Promise(resolve => {
468
+ const finish = () => {
469
+ clearTimeout(timer);
470
+ signal.removeEventListener('abort', finish);
471
+ resolve();
472
+ };
473
+ const timer = setTimeout(finish, 300);
474
+ signal.addEventListener('abort', finish, { once: true });
475
+ });
476
+ const current = this.find(id);
477
+ if (this.disposed || signal.aborted || !current)
478
+ return false;
479
+ if (!this.reorder.removeColumn(current))
480
+ return false;
481
+ const entry = this.pending.get(id);
482
+ entry?.abort.abort();
483
+ this.pending.delete(id);
484
+ this.refresh();
485
+ return true;
486
+ }
487
+ finally {
488
+ const current = this.find(id);
489
+ if (current)
490
+ cells.push(...Array.from(this.of.element.rows).map(row => row.cells[current.cellIndex]));
491
+ for (const cell of cells) {
492
+ cell?.classList.remove('column-remove-highlight');
493
+ for (const name of ['fade', 'from', 'height', 'width'])
494
+ cell?.style?.removeProperty(`--column-${name}`);
495
+ }
496
+ this.refresh();
497
+ }
498
+ }
307
499
  reveal(id) {
308
500
  const header = this.find(id);
309
501
  if (!header || this.disposed)
@@ -325,6 +517,33 @@ export class TableColumnAdd extends Plugin {
325
517
  else if ((rect.right > right) && (rect.left > left))
326
518
  scroll.scrollLeft += Math.min(rect.right - right, rect.left - left);
327
519
  }
520
+ scrollToEnd() {
521
+ if (this.scrollFrame !== undefined)
522
+ cancelAnimationFrame(this.scrollFrame);
523
+ this.scrollFrame = undefined;
524
+ const scroll = this.reorder.scrollContainer();
525
+ if (!scroll || this.disposed)
526
+ return;
527
+ const from = scroll.scrollLeft;
528
+ const to = Math.max(from, scroll.scrollWidth - scroll.clientWidth);
529
+ if (to <= from)
530
+ return;
531
+ if (scroll.ownerDocument?.defaultView?.matchMedia('(prefers-reduced-motion: reduce)').matches) {
532
+ scroll.scrollLeft = to;
533
+ return;
534
+ }
535
+ const started = performance.now();
536
+ const step = (time) => {
537
+ this.scrollFrame = undefined;
538
+ if (this.disposed)
539
+ return;
540
+ const progress = Math.min(1, Math.max(0, (time - started) / 100));
541
+ scroll.scrollLeft = from + (to - from) * (1 - (1 - progress) ** 2);
542
+ if (progress < 1)
543
+ this.scrollFrame = requestAnimationFrame(step);
544
+ };
545
+ this.scrollFrame = requestAnimationFrame(step);
546
+ }
328
547
  retry(id) {
329
548
  const entry = this.pending.get(id);
330
549
  if (!entry || !this.live(entry) || !entry.content.header.classList.contains('column-add-error'))
@@ -1,6 +1,11 @@
1
1
  import { Plugin } from '../plugin/plugin.js';
2
2
  import { PluginOptions } from '../plugin/plugin.js';
3
3
  import { Table } from './table.js';
4
+ export type ColumnDragOptions = {
5
+ adding?: boolean;
6
+ onReveal?(): void;
7
+ removal?: boolean;
8
+ };
4
9
  export type PointerSample = {
5
10
  pointerId: number;
6
11
  pointerType: string;
@@ -15,14 +20,17 @@ export interface ExternalColumnOperation {
15
20
  }
16
21
  type Direction = -1 | 0 | 1;
17
22
  interface Drag extends Press {
18
- external: boolean;
23
+ adding: boolean;
19
24
  cells: HTMLTableCellElement[];
20
25
  col?: HTMLTableColElement;
21
26
  direction: Direction;
27
+ external: boolean;
22
28
  index: number;
23
29
  initial: number;
24
30
  maximum: number;
25
31
  minimum: number;
32
+ onReveal?: () => void;
33
+ removal: boolean;
26
34
  remove: boolean;
27
35
  rows: HTMLTableRowElement[];
28
36
  scroll?: HTMLElement;
@@ -43,10 +51,12 @@ export declare class TableColumnReorder extends Plugin<Table, TableColumnReorder
43
51
  protected suppressClick: boolean;
44
52
  protected animate(time: number): void;
45
53
  /** The source owns pointer capture; this operation owns target geometry and rollback. */
46
- beginExternal(header: HTMLTableCellElement, sample: PointerSample): ExternalColumnOperation;
54
+ beginExternal(header: HTMLTableCellElement, sample: PointerSample, options?: ColumnDragOptions): ExternalColumnOperation;
47
55
  defaultOptions(): TableColumnReorderOptions;
48
56
  /** Move a column inside its existing reorderable boundaries, without committing a header gesture. */
49
57
  moveColumn(header: HTMLTableCellElement, targetIndex: number): void;
58
+ /** Remove a reorderable column without committing a header gesture or invoking its callbacks. */
59
+ removeColumn(header: HTMLTableCellElement): boolean;
50
60
  /** Retain the active gesture when asynchronous rendering replaces one of its cells. */
51
61
  replaceCell(previous: HTMLTableCellElement, replacement: HTMLTableCellElement): void;
52
62
  protected finish(commit: boolean): void;
@@ -58,7 +68,7 @@ export declare class TableColumnReorder extends Plugin<Table, TableColumnReorder
58
68
  protected rows(): HTMLTableRowElement[];
59
69
  protected scroll(drag: Drag, time: number): Direction;
60
70
  scrollContainer(): HTMLElement | undefined;
61
- protected start(press: Press, x: number, y: number, external?: boolean): void;
71
+ protected start(press: Press, x: number, y: number, external?: boolean, options?: ColumnDragOptions): void;
62
72
  protected target(drag: Drag, direction: Direction): number;
63
73
  protected triggerRow(): HTMLTableRowElement;
64
74
  protected bounds(drag: Drag): {
package/column-reorder.js CHANGED
@@ -3,7 +3,6 @@ import { PluginOptions } from '../plugin/plugin.js';
3
3
  let activeDrag;
4
4
  const clamp = (value) => Math.max(0, Math.min(1, value));
5
5
  const maxScrollSpeed = 800;
6
- const penetration = 10;
7
6
  const scrollZone = 50;
8
7
  export class TableColumnReorder extends Plugin {
9
8
  drag;
@@ -14,21 +13,23 @@ export class TableColumnReorder extends Plugin {
14
13
  this.frame = undefined;
15
14
  const drag = this.drag;
16
15
  this.updateState(drag);
16
+ if (drag.header.hidden)
17
+ return;
17
18
  const pointerDirection = drag.direction;
18
19
  drag.direction = 0;
19
20
  const scrollDirection = this.scroll(drag, time);
20
21
  if (scrollDirection)
21
22
  this.requestFrame();
22
23
  const direction = pointerDirection || scrollDirection;
23
- const target = direction ? this.target(drag, direction) : drag.index;
24
+ const target = this.target(drag, direction);
24
25
  if (target !== drag.index)
25
26
  this.move(drag, target);
26
27
  }
27
28
  /** The source owns pointer capture; this operation owns target geometry and rollback. */
28
- beginExternal(header, sample) {
29
+ beginExternal(header, sample, options = {}) {
29
30
  if (activeDrag || this.press)
30
31
  throw new Error('A column interaction is active');
31
- this.start({ header, id: sample.pointerId, origin: header.getBoundingClientRect().left, y: sample.clientY }, sample.clientX, sample.clientY, true);
32
+ this.start({ header, id: sample.pointerId, origin: header.getBoundingClientRect().left, y: sample.clientY }, sample.clientX, sample.clientY, true, options);
32
33
  const drag = this.drag;
33
34
  if (!drag)
34
35
  throw new Error('Column cannot be reordered');
@@ -51,7 +52,7 @@ export class TableColumnReorder extends Plugin {
51
52
  cancelAnimationFrame(this.frame);
52
53
  this.frame = undefined;
53
54
  held = true;
54
- return !drag.remove && this.of.element.isConnected;
55
+ return (options.removal || !drag.remove) && this.of.element.isConnected;
55
56
  };
56
57
  return {
57
58
  cancel: () => { if (live())
@@ -87,6 +88,19 @@ export class TableColumnReorder extends Plugin {
87
88
  minimum--;
88
89
  this.moveCells(index, Math.max(minimum, Math.min(maximum, Math.trunc(targetIndex))));
89
90
  }
91
+ /** Remove a reorderable column without committing a header gesture or invoking its callbacks. */
92
+ removeColumn(header) {
93
+ if (this.drag || this.press)
94
+ throw new Error('A header interaction is active');
95
+ if (header.parentElement !== this.triggerRow() || !(this.options.isReorderable?.(header) ?? true))
96
+ return false;
97
+ const index = header.cellIndex;
98
+ const column = this.of.element.querySelectorAll(':scope > colgroup > col')[index];
99
+ for (const row of this.rows())
100
+ row.cells[index]?.remove();
101
+ column?.remove();
102
+ return true;
103
+ }
90
104
  /** Retain the active gesture when asynchronous rendering replaces one of its cells. */
91
105
  replaceCell(previous, replacement) {
92
106
  if (this.press?.header === previous)
@@ -95,7 +109,7 @@ export class TableColumnReorder extends Plugin {
95
109
  if (this.drag.header === previous)
96
110
  this.drag.header = replacement;
97
111
  this.drag.cells = this.drag.cells.map(cell => cell === previous ? replacement : cell);
98
- for (const name of ['column-reorder', 'moving', 'removing']) {
112
+ for (const name of ['adding', 'column-reorder', 'moving', 'removing']) {
99
113
  if (previous.classList.contains(name))
100
114
  replacement.classList.add(name);
101
115
  }
@@ -114,7 +128,7 @@ export class TableColumnReorder extends Plugin {
114
128
  const changed = drag.index !== drag.initial;
115
129
  if (!commit && changed)
116
130
  this.move(drag, drag.initial);
117
- const removed = commit && drag.remove && !drag.external;
131
+ const removed = (commit && drag.remove) || (!commit && drag.adding);
118
132
  if (removed) {
119
133
  for (const row of this.rows()) {
120
134
  row.cells[drag.index]?.remove();
@@ -122,7 +136,7 @@ export class TableColumnReorder extends Plugin {
122
136
  drag.col?.remove();
123
137
  }
124
138
  for (const cell of drag.cells) {
125
- cell.classList.remove('column-reorder', 'moving', 'removing');
139
+ cell.classList.remove('adding', 'column-reorder', 'moving', 'removing');
126
140
  }
127
141
  document.documentElement.classList.remove('column-reorder');
128
142
  activeDrag = undefined;
@@ -279,14 +293,13 @@ export class TableColumnReorder extends Plugin {
279
293
  }
280
294
  return undefined;
281
295
  }
282
- start(press, x, y, external = false) {
296
+ start(press, x, y, external = false, options = {}) {
283
297
  if (activeDrag)
284
298
  return;
285
299
  const headers = Array.from(this.triggerRow().cells);
286
300
  const initial = headers.indexOf(press.header);
287
301
  const rows = this.rows();
288
302
  const columns = this.of.element.querySelectorAll(':scope > colgroup > col');
289
- const source = press.header.getBoundingClientRect();
290
303
  const reorderable = headers.map(header => (external || !header.classList.contains('column-add-provisional'))
291
304
  && (this.options.isReorderable?.(header) ?? true));
292
305
  if (!reorderable[initial])
@@ -301,11 +314,13 @@ export class TableColumnReorder extends Plugin {
301
314
  this.of.element.setPointerCapture(press.id);
302
315
  const drag = {
303
316
  ...press,
317
+ adding: options.adding ?? false,
318
+ onReveal: options.onReveal,
319
+ removal: options.removal ?? !external,
304
320
  external,
305
321
  cells: rows.map(row => row.cells[initial]),
306
322
  col: columns[initial],
307
- direction: external ? (x < source.left ? -1 : x > source.right ? 1 : 0)
308
- : Math.sign(x - press.origin),
323
+ direction: Math.sign(x - press.origin),
309
324
  index: initial,
310
325
  initial,
311
326
  maximum,
@@ -327,16 +342,20 @@ export class TableColumnReorder extends Plugin {
327
342
  this.requestFrame();
328
343
  }
329
344
  target(drag, direction) {
330
- const headers = this.triggerRow().cells;
331
- let target = drag.index;
332
- for (let index = drag.index + direction; (index >= drag.minimum) && (index <= drag.maximum); index += direction) {
333
- const rect = headers[index].getBoundingClientRect();
334
- const edge = direction > 0 ? rect.left : rect.right;
335
- if (direction * (drag.x - edge) < penetration)
336
- break;
337
- target = index;
338
- }
339
- return target;
345
+ if (!direction)
346
+ return drag.index;
347
+ const headers = Array.from(this.triggerRow().cells);
348
+ const hovered = headers.slice(drag.minimum, drag.maximum + 1)
349
+ .find(header => drag.x < header.getBoundingClientRect().right) ?? headers[drag.maximum];
350
+ if (hovered === drag.header)
351
+ return drag.index;
352
+ let index = headers.indexOf(hovered);
353
+ // Removing the source shifts the hovered column's index when it was to its right.
354
+ if (drag.index < index)
355
+ index--;
356
+ if (direction > 0)
357
+ index++;
358
+ return index;
340
359
  }
341
360
  triggerRow() {
342
361
  return this.of.element.tHead?.rows[0] ?? this.of.element.tBodies[0]?.rows[0];
@@ -344,8 +363,8 @@ export class TableColumnReorder extends Plugin {
344
363
  bounds(drag) {
345
364
  const rect = this.of.element.getBoundingClientRect();
346
365
  const headers = this.triggerRow().cells;
347
- const first = headers[drag.minimum].getBoundingClientRect();
348
- const last = headers[drag.maximum].getBoundingClientRect();
366
+ const first = drag.header.hidden ? rect : headers[drag.minimum].getBoundingClientRect();
367
+ const last = drag.header.hidden ? rect : headers[drag.maximum].getBoundingClientRect();
349
368
  const viewport = drag.external ? drag.scroll?.getBoundingClientRect() : undefined;
350
369
  return {
351
370
  left: Math.max(rect.left, first.left, headers[drag.minimum - 1]?.getBoundingClientRect().right ?? rect.left, viewport?.left ?? rect.left),
@@ -355,13 +374,25 @@ export class TableColumnReorder extends Plugin {
355
374
  };
356
375
  }
357
376
  updateState(drag) {
377
+ if (drag.header.hidden) {
378
+ const table = this.of.element;
379
+ if (table.contains(table.ownerDocument.elementFromPoint(drag.x, drag.y))) {
380
+ for (const cell of drag.cells)
381
+ cell.hidden = false;
382
+ if (drag.col)
383
+ drag.col.hidden = false;
384
+ drag.onReveal?.();
385
+ drag.scroll = this.scrollContainer();
386
+ }
387
+ }
358
388
  if (this.options.onRemove || drag.external) {
359
389
  const rect = this.bounds(drag);
360
390
  drag.remove = (drag.x < rect.left) || (drag.x > rect.right) || (drag.y < rect.top) || (drag.y > rect.bottom);
361
391
  }
362
392
  for (const cell of drag.cells) {
363
- cell.classList.toggle('removing', drag.remove && !drag.external);
364
- cell.classList.toggle('moving', !drag.remove && (drag.index !== drag.initial)
393
+ cell.classList.toggle('adding', drag.adding && !drag.remove);
394
+ cell.classList.toggle('removing', drag.remove && drag.removal);
395
+ cell.classList.toggle('moving', !drag.adding && !drag.remove && (drag.index !== drag.initial)
365
396
  && !drag.header.classList.contains('column-add-provisional'));
366
397
  }
367
398
  }
@@ -369,9 +400,7 @@ export class TableColumnReorder extends Plugin {
369
400
  const drag = this.drag;
370
401
  if ((x === drag.x) && (y === drag.y))
371
402
  return;
372
- const source = drag.external ? drag.header.getBoundingClientRect() : undefined;
373
- drag.direction = source ? (x === drag.x ? 0 : x < source.left ? -1 : x > source.right ? 1 : 0)
374
- : Math.sign(x - drag.x);
403
+ drag.direction = Math.sign(x - drag.x);
375
404
  drag.x = x;
376
405
  drag.y = y;
377
406
  this.updateState(drag);
package/package.json CHANGED
@@ -53,5 +53,5 @@
53
53
  "scripts": {
54
54
  "build": "tsc && sass --no-source-map demo:demo src/column-add.scss:column-add.css && node ../prepare-module/prepare-module.js"
55
55
  },
56
- "version": "0.4.2"
56
+ "version": "0.7.3"
57
57
  }