@itrocks/table 0.8.2 → 0.10.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 (4) hide show
  1. package/README.md +20 -0
  2. package/edit.d.ts +8 -2
  3. package/edit.js +63 -19
  4. package/package.json +1 -1
package/README.md CHANGED
@@ -54,6 +54,26 @@ const table = tableByElement(element)
54
54
  For more advanced usage (plugins, styling, demos), check out the
55
55
  [GitHub demo folder](https://github.com/itrocks-ts/table/tree/master/demo).
56
56
 
57
+ ### Form controls in cells
58
+
59
+ Use `TableEdit` with a `control` callback to select existing controls already rendered in the cells.
60
+ For example, a form can render an input in its first column and a textarea in its second column.
61
+
62
+ ```ts
63
+ import { TableEdit } from '@itrocks/table/edit.js'
64
+ import { tableBySelector } from '@itrocks/table/table.js'
65
+
66
+ tableBySelector('.js-form-table', {
67
+ plugins: [new TableEdit({
68
+ control: (cell, column) => cell.querySelector<HTMLElement>(column === 0 ? 'input:not([type=hidden])' : 'textarea')
69
+ })]
70
+ })
71
+ ```
72
+
73
+ The callback receives the cell and its zero-based index in the row when the cell is selected or receives focus.
74
+ Return its focusable control to keep it in the form, `null` to skip the cell, or `undefined` to use the text editor.
75
+ Clicking the cell background focuses its control and places the caret at the end of text fields.
76
+
57
77
  ## API
58
78
 
59
79
  The public API is intentionally small and focuses on a single `Table`
package/edit.d.ts CHANGED
@@ -1,6 +1,10 @@
1
1
  import { HTMLEditableElement } from '../contenteditable/contenteditable.js';
2
2
  import { Plugin } from '../plugin/plugin.js';
3
+ import { PluginOptions } from '../plugin/plugin.js';
3
4
  import { Table } from './table.js';
5
+ declare class Options extends PluginOptions {
6
+ control?: (cell: HTMLTableCellElement, column: number) => HTMLElement | null | undefined;
7
+ }
4
8
  export declare class RangeCopy {
5
9
  commonAncestorContainer: Node;
6
10
  endContainer: Node;
@@ -10,11 +14,12 @@ export declare class RangeCopy {
10
14
  constructor(range: Range);
11
15
  toRange(): Range;
12
16
  }
13
- export declare class TableEdit extends Plugin<Table> {
17
+ export declare class TableEdit extends Plugin<Table, Options> {
14
18
  zIndex: string;
15
19
  init(): void;
16
20
  closestEditable(node: Node | Range | RangeCopy): HTMLDivElement;
17
21
  closestEditableCell(target: any): HTMLTableCellElement | undefined;
22
+ private controlOf;
18
23
  createEditable(selected: HTMLTableCellElement, selectedStyle: CSSStyleDeclaration): HTMLEditableElement;
19
24
  editable(): HTMLEditableElement | undefined;
20
25
  editableEndRange(node: Node | Range | RangeCopy): Range;
@@ -26,7 +31,7 @@ export declare class TableEdit extends Plugin<Table> {
26
31
  selectCell(cell: HTMLTableCellElement): void;
27
32
  /** Returns the currently selected cell, or undefined if no cell is selected */
28
33
  selected(): HTMLTableCellElement | undefined;
29
- /** Sets selected cell to this cell, and set its content to div[contenteditable] */
34
+ /** Selects the cell and focuses its control or opens the text editor. */
30
35
  setSelectedCell(cell: HTMLTableCellElement): void;
31
36
  protected setSelectedText(newText: string): void;
32
37
  setSelectionRange(range: Range): void;
@@ -34,3 +39,4 @@ export declare class TableEdit extends Plugin<Table> {
34
39
  textContentBeforeRange(): string;
35
40
  unselectCell(): void;
36
41
  }
42
+ export {};
package/edit.js CHANGED
@@ -1,6 +1,11 @@
1
1
  import { ContentEditable } from '../contenteditable/contenteditable.js';
2
2
  import { Plugin } from '../plugin/plugin.js';
3
+ import { PluginOptions } from '../plugin/plugin.js';
4
+ class Options extends PluginOptions {
5
+ control;
6
+ }
3
7
  let editable;
8
+ let editing;
4
9
  let selected;
5
10
  let selectedStyle;
6
11
  let selectedText;
@@ -38,7 +43,29 @@ export class TableEdit extends Plugin {
38
43
  const cell = this.closestEditableCell(event.target);
39
44
  if (!cell)
40
45
  return;
41
- this.selectCell(cell);
46
+ const control = this.controlOf(cell);
47
+ if (control === undefined) {
48
+ this.selectCell(cell);
49
+ }
50
+ else if (control && event.target === cell) {
51
+ event.preventDefault();
52
+ control.focus();
53
+ if (((control instanceof HTMLInputElement) || (control instanceof HTMLTextAreaElement))
54
+ && (control.selectionEnd !== null)) {
55
+ control.setSelectionRange(control.value.length, control.value.length);
56
+ }
57
+ }
58
+ });
59
+ table.addEventListener(table.element, 'focusin', event => {
60
+ const cell = this.closestEditableCell(event.target);
61
+ if (!cell || cell === selected || !this.controlOf(cell))
62
+ return;
63
+ this.unselectCell();
64
+ selected = cell;
65
+ });
66
+ table.onReset.push(() => {
67
+ if (selected?.closest('table') === table.element)
68
+ this.unselectCell();
42
69
  });
43
70
  }
44
71
  closestEditable(node) {
@@ -55,9 +82,13 @@ export class TableEdit extends Plugin {
55
82
  return parent;
56
83
  }
57
84
  closestEditableCell(target) {
58
- return (target instanceof Element)
59
- ? target.closest('table.itrocks > * > tr > *, table.itrocks > * > tr > *') ?? undefined
85
+ const cell = (target instanceof Element)
86
+ ? target.closest('td, th')
60
87
  : undefined;
88
+ return (cell?.closest('table') === this.of.element) ? (cell ?? undefined) : undefined;
89
+ }
90
+ controlOf(cell) {
91
+ return this.options.control?.(cell, cell.cellIndex);
61
92
  }
62
93
  createEditable(selected, selectedStyle) {
63
94
  const contentEditable = new ContentEditable(document.createElement('div'));
@@ -144,21 +175,27 @@ export class TableEdit extends Plugin {
144
175
  selected() {
145
176
  return selected;
146
177
  }
147
- /** Sets selected cell to this cell, and set its content to div[contenteditable] */
178
+ /** Selects the cell and focuses its control or opens the text editor. */
148
179
  setSelectedCell(cell) {
180
+ const control = this.controlOf(cell);
181
+ if (control === null)
182
+ return;
183
+ if (control) {
184
+ this.unselectCell();
185
+ selected = cell;
186
+ control.focus();
187
+ return;
188
+ }
149
189
  selected = cell;
150
190
  selectedText = selected.innerHTML;
151
191
  const originSelectedStyle = getComputedStyle(selected);
152
192
  selectedStyle = selected.getAttribute('style') ?? '';
153
193
  selected.setAttribute('contenteditable', '');
154
- setTimeout(() => {
155
- if (!selected) {
156
- console.error('cell:', cell);
157
- throw 'Unexpected failure: cell was unselected before contenteditable was effective';
158
- }
194
+ editing = setTimeout(() => {
195
+ editing = undefined;
159
196
  const range = new RangeCopy(this.getSelectionRange());
160
- selected.removeAttribute('contenteditable');
161
- editable = this.createEditable(selected, originSelectedStyle);
197
+ cell.removeAttribute('contenteditable');
198
+ editable = this.createEditable(cell, originSelectedStyle);
162
199
  this.setSelectionRange(this.inEditable(range) ? range.toRange() : this.editableFullRange(editable));
163
200
  });
164
201
  }
@@ -194,15 +231,22 @@ export class TableEdit extends Plugin {
194
231
  return this.rangeTextContent(previous);
195
232
  }
196
233
  unselectCell() {
197
- if (!editable || !selected)
234
+ if (!selected)
198
235
  return;
199
- editable.editable.deactivate();
200
- selected.innerHTML = editable.innerHTML;
201
- selectedStyle.length
202
- ? selected.setAttribute('style', selectedStyle)
203
- : selected.removeAttribute('style');
204
- this.setSelectedText(selected.innerHTML);
205
- editable = undefined;
236
+ if (editing !== undefined) {
237
+ clearTimeout(editing);
238
+ editing = undefined;
239
+ selected.removeAttribute('contenteditable');
240
+ }
241
+ if (editable) {
242
+ editable.editable.deactivate();
243
+ selected.innerHTML = editable.innerHTML;
244
+ selectedStyle.length
245
+ ? selected.setAttribute('style', selectedStyle)
246
+ : selected.removeAttribute('style');
247
+ this.setSelectedText(selected.innerHTML);
248
+ editable = undefined;
249
+ }
206
250
  selected = undefined;
207
251
  }
208
252
  }
package/package.json CHANGED
@@ -53,5 +53,5 @@
53
53
  "scripts": {
54
54
  "build": "tsc && sass --no-source-map src/column-add.scss:column-add.css && node ../prepare-module/prepare-module.js && tsc -p tsconfig.demo.json && sass --no-source-map demo:demo"
55
55
  },
56
- "version": "0.8.2"
56
+ "version": "0.10.0"
57
57
  }