@gradio/dataframe 0.14.0 → 0.16.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 (44) hide show
  1. package/CHANGELOG.md +45 -0
  2. package/Dataframe.stories.svelte +283 -7
  3. package/Index.svelte +22 -3
  4. package/dist/Index.svelte +18 -4
  5. package/dist/Index.svelte.d.ts +16 -0
  6. package/dist/shared/EditableCell.svelte +49 -7
  7. package/dist/shared/EditableCell.svelte.d.ts +1 -1
  8. package/dist/shared/Table.svelte +692 -411
  9. package/dist/shared/Table.svelte.d.ts +4 -0
  10. package/dist/shared/Toolbar.svelte +122 -30
  11. package/dist/shared/Toolbar.svelte.d.ts +4 -0
  12. package/dist/shared/VirtualTable.svelte +70 -26
  13. package/dist/shared/VirtualTable.svelte.d.ts +1 -0
  14. package/dist/shared/icons/FilterIcon.svelte +11 -0
  15. package/dist/shared/icons/FilterIcon.svelte.d.ts +16 -0
  16. package/dist/shared/icons/SortIcon.svelte +90 -0
  17. package/dist/shared/icons/SortIcon.svelte.d.ts +20 -0
  18. package/dist/shared/selection_utils.d.ts +30 -0
  19. package/dist/shared/selection_utils.js +139 -0
  20. package/dist/shared/types.d.ts +18 -0
  21. package/dist/shared/types.js +2 -0
  22. package/dist/shared/utils/menu_utils.d.ts +42 -0
  23. package/dist/shared/utils/menu_utils.js +58 -0
  24. package/dist/shared/utils/sort_utils.d.ts +7 -0
  25. package/dist/shared/utils/sort_utils.js +39 -0
  26. package/dist/shared/utils/table_utils.d.ts +12 -0
  27. package/dist/shared/utils/table_utils.js +148 -0
  28. package/package.json +8 -8
  29. package/shared/EditableCell.svelte +55 -7
  30. package/shared/Table.svelte +762 -478
  31. package/shared/Toolbar.svelte +125 -30
  32. package/shared/VirtualTable.svelte +73 -26
  33. package/shared/icons/FilterIcon.svelte +12 -0
  34. package/shared/icons/SortIcon.svelte +95 -0
  35. package/shared/selection_utils.ts +230 -0
  36. package/shared/types.ts +29 -0
  37. package/shared/utils/menu_utils.ts +115 -0
  38. package/shared/utils/sort_utils.test.ts +71 -0
  39. package/shared/utils/sort_utils.ts +55 -0
  40. package/shared/utils/table_utils.test.ts +114 -0
  41. package/shared/utils/table_utils.ts +206 -0
  42. package/dist/shared/table_utils.d.ts +0 -6
  43. package/dist/shared/table_utils.js +0 -27
  44. package/shared/table_utils.ts +0 -38
@@ -30,6 +30,9 @@ declare const __propDef: {
30
30
  show_fullscreen_button?: boolean | undefined;
31
31
  show_copy_button?: boolean | undefined;
32
32
  value_is_output?: boolean | undefined;
33
+ max_chars?: number | undefined;
34
+ show_search?: ("none" | "search" | "filter") | undefined;
35
+ pinned_columns?: number | undefined;
33
36
  display_value?: (string[][] | null) | undefined;
34
37
  styling?: (string[][] | null) | undefined;
35
38
  };
@@ -37,6 +40,7 @@ declare const __propDef: {
37
40
  change: CustomEvent<DataframeValue>;
38
41
  input: CustomEvent<undefined>;
39
42
  select: CustomEvent<SelectData>;
43
+ search: CustomEvent<string | null>;
40
44
  } & {
41
45
  [evt: string]: CustomEvent<any>;
42
46
  };
@@ -1,11 +1,19 @@
1
- <script>import { Maximize, Minimize, Copy, Check } from "@gradio/icons";
1
+ <script>import { Maximize, Minimize, Copy } from "@gradio/icons";
2
2
  import { onDestroy } from "svelte";
3
+ import { createEventDispatcher } from "svelte";
4
+ import FilterIcon from "./icons/FilterIcon.svelte";
3
5
  export let show_fullscreen_button = false;
4
6
  export let show_copy_button = false;
7
+ export let show_search = "none";
5
8
  export let is_fullscreen = false;
6
9
  export let on_copy;
10
+ export let on_commit_filter;
11
+ const dispatch = createEventDispatcher();
7
12
  let copied = false;
8
13
  let timer;
14
+ export let current_search_query = null;
15
+ $:
16
+ dispatch("search", current_search_query);
9
17
  function copy_feedback() {
10
18
  copied = true;
11
19
  if (timer)
@@ -25,41 +33,70 @@ onDestroy(() => {
25
33
  </script>
26
34
 
27
35
  <div class="toolbar" role="toolbar" aria-label="Table actions">
28
- {#if show_copy_button}
29
- <button
30
- class="toolbar-button"
31
- on:click={handle_copy}
32
- aria-label={copied ? "Copied to clipboard" : "Copy table data"}
33
- title={copied ? "Copied to clipboard" : "Copy table data"}
34
- >
35
- {#if copied}
36
- <Check />
37
- {:else}
38
- <Copy />
39
- {/if}
40
- </button>
41
- {/if}
42
- {#if show_fullscreen_button}
43
- <button
44
- class="toolbar-button"
45
- on:click
46
- aria-label={is_fullscreen ? "Exit fullscreen" : "Enter fullscreen"}
47
- title={is_fullscreen ? "Exit fullscreen" : "Enter fullscreen"}
48
- >
49
- {#if is_fullscreen}
50
- <Minimize />
51
- {:else}
52
- <Maximize />
53
- {/if}
54
- </button>
55
- {/if}
36
+ <div class="toolbar-buttons">
37
+ {#if show_search !== "none"}
38
+ <div class="search-container">
39
+ <input
40
+ type="text"
41
+ bind:value={current_search_query}
42
+ placeholder="Search..."
43
+ class="search-input"
44
+ />
45
+ {#if current_search_query && show_search === "filter"}
46
+ <button
47
+ class="toolbar-button check-button"
48
+ on:click={on_commit_filter}
49
+ aria-label="Apply filter and update dataframe values"
50
+ title="Apply filter and update dataframe values"
51
+ >
52
+ <FilterIcon />
53
+ </button>
54
+ {/if}
55
+ </div>
56
+ {/if}
57
+ {#if show_copy_button}
58
+ <button
59
+ class="toolbar-button"
60
+ on:click={handle_copy}
61
+ aria-label={copied ? "Copied to clipboard" : "Copy table data"}
62
+ title={copied ? "Copied to clipboard" : "Copy table data"}
63
+ >
64
+ {#if copied}
65
+ <FilterIcon />
66
+ {:else}
67
+ <Copy />
68
+ {/if}
69
+ </button>
70
+ {/if}
71
+ {#if show_fullscreen_button}
72
+ <button
73
+ class="toolbar-button"
74
+ on:click
75
+ aria-label={is_fullscreen ? "Exit fullscreen" : "Enter fullscreen"}
76
+ title={is_fullscreen ? "Exit fullscreen" : "Enter fullscreen"}
77
+ >
78
+ {#if is_fullscreen}
79
+ <Minimize />
80
+ {:else}
81
+ <Maximize />
82
+ {/if}
83
+ </button>
84
+ {/if}
85
+ </div>
56
86
  </div>
57
87
 
58
88
  <style>
59
89
  .toolbar {
60
90
  display: flex;
61
- justify-content: flex-end;
91
+ align-items: center;
92
+ gap: var(--size-2);
93
+ flex: 0 0 auto;
94
+ }
95
+
96
+ .toolbar-buttons {
97
+ display: flex;
62
98
  gap: var(--size-1);
99
+ flex-wrap: nowrap;
63
100
  }
64
101
 
65
102
  .toolbar-button {
@@ -86,4 +123,59 @@ onDestroy(() => {
86
123
  width: var(--size-4);
87
124
  height: var(--size-4);
88
125
  }
126
+
127
+ .search-container {
128
+ position: relative;
129
+ }
130
+
131
+ .search-input {
132
+ width: var(--size-full);
133
+ height: var(--size-6);
134
+ padding: var(--size-2);
135
+ padding-right: var(--size-8);
136
+ border: 1px solid var(--border-color-primary);
137
+ border-radius: var(--table-radius);
138
+ font-size: var(--text-sm);
139
+ color: var(--body-text-color);
140
+ background: var(--background-fill-secondary);
141
+ transition: all 0.2s ease;
142
+ }
143
+
144
+ .search-input:hover {
145
+ border-color: var(--border-color-secondary);
146
+ background: var(--background-fill-primary);
147
+ }
148
+
149
+ .search-input:focus {
150
+ outline: none;
151
+ border-color: var(--color-accent);
152
+ background: var(--background-fill-primary);
153
+ box-shadow: 0 0 0 1px var(--color-accent);
154
+ }
155
+
156
+ .check-button {
157
+ position: absolute;
158
+ right: var(--size-1);
159
+ top: 50%;
160
+ transform: translateY(-50%);
161
+ background: var(--color-accent);
162
+ color: white;
163
+ border: none;
164
+ width: var(--size-4);
165
+ height: var(--size-4);
166
+ border-radius: var(--radius-sm);
167
+ display: flex;
168
+ align-items: center;
169
+ justify-content: center;
170
+ padding: var(--size-1);
171
+ }
172
+
173
+ .check-button :global(svg) {
174
+ width: var(--size-3);
175
+ height: var(--size-3);
176
+ }
177
+
178
+ .check-button:hover {
179
+ background: var(--color-accent-soft);
180
+ }
89
181
  </style>
@@ -3,11 +3,15 @@ declare const __propDef: {
3
3
  props: {
4
4
  show_fullscreen_button?: boolean | undefined;
5
5
  show_copy_button?: boolean | undefined;
6
+ show_search?: ("none" | "search" | "filter") | undefined;
6
7
  is_fullscreen?: boolean | undefined;
7
8
  on_copy: () => Promise<void>;
9
+ on_commit_filter: () => void;
10
+ current_search_query?: (string | null) | undefined;
8
11
  };
9
12
  events: {
10
13
  click: MouseEvent;
14
+ search: CustomEvent<string | null>;
11
15
  } & {
12
16
  [evt: string]: CustomEvent<any>;
13
17
  };
@@ -7,6 +7,7 @@ export let table_scrollbar_width;
7
7
  export let start = 0;
8
8
  export let end = 20;
9
9
  export let selected;
10
+ export let disable_scroll = false;
10
11
  let height = "100%";
11
12
  let average_height = 30;
12
13
  let bottom = 0;
@@ -32,6 +33,8 @@ async function refresh_height_map(_items) {
32
33
  if (viewport_height === 0) {
33
34
  return;
34
35
  }
36
+ head_height = viewport.querySelector(".thead")?.getBoundingClientRect().height || 0;
37
+ await tick();
35
38
  const { scrollTop } = viewport;
36
39
  table_scrollbar_width = viewport.offsetWidth - viewport.clientWidth;
37
40
  content_height = top - (scrollTop - head_height);
@@ -202,29 +205,32 @@ onMount(() => {
202
205
  </script>
203
206
 
204
207
  <svelte-virtual-table-viewport>
205
- <table
206
- class="table"
207
- bind:this={viewport}
208
- bind:contentRect={viewport_box}
209
- on:scroll={handle_scroll}
210
- style="height: {height}; --bw-svt-p-top: {top}px; --bw-svt-p-bottom: {bottom}px; --bw-svt-head-height: {head_height}px; --bw-svt-foot-height: {foot_height}px; --bw-svt-avg-row-height: {average_height}px"
211
- >
212
- <thead class="thead" bind:offsetHeight={head_height}>
213
- <slot name="thead" />
214
- </thead>
215
- <tbody bind:this={contents} class="tbody">
216
- {#if visible.length && visible[0].data.length}
217
- {#each visible as item (item.data[0].id)}
218
- <slot name="tbody" item={item.data} index={item.index}>
219
- Missing Table Row
220
- </slot>
221
- {/each}
222
- {/if}
223
- </tbody>
224
- <tfoot class="tfoot" bind:offsetHeight={foot_height}>
225
- <slot name="tfoot" />
226
- </tfoot>
227
- </table>
208
+ <div>
209
+ <table
210
+ class="table"
211
+ class:disable-scroll={disable_scroll}
212
+ bind:this={viewport}
213
+ bind:contentRect={viewport_box}
214
+ on:scroll={handle_scroll}
215
+ style="height: {height}; --bw-svt-p-top: {top}px; --bw-svt-p-bottom: {bottom}px; --bw-svt-head-height: {head_height}px; --bw-svt-foot-height: {foot_height}px; --bw-svt-avg-row-height: {average_height}px; --max-height: {max_height}px"
216
+ >
217
+ <thead class="thead" bind:offsetHeight={head_height}>
218
+ <slot name="thead" />
219
+ </thead>
220
+ <tbody bind:this={contents} class="tbody">
221
+ {#if visible.length && visible[0].data.length}
222
+ {#each visible as item (item.data[0].id)}
223
+ <slot name="tbody" item={item.data} index={item.index}>
224
+ Missing Table Row
225
+ </slot>
226
+ {/each}
227
+ {/if}
228
+ </tbody>
229
+ <tfoot class="tfoot" bind:offsetHeight={foot_height}>
230
+ <slot name="tfoot" />
231
+ </tfoot>
232
+ </table>
233
+ </div>
228
234
  </svelte-virtual-table-viewport>
229
235
 
230
236
  <style>
@@ -233,7 +239,7 @@ onMount(() => {
233
239
  overflow-y: scroll;
234
240
  overflow-x: scroll;
235
241
  -webkit-overflow-scrolling: touch;
236
- max-height: 100vh;
242
+ max-height: var(--max-height);
237
243
  box-sizing: border-box;
238
244
  display: block;
239
245
  padding: 0;
@@ -281,11 +287,49 @@ onMount(() => {
281
287
  background: var(--table-even-background-fill);
282
288
  }
283
289
 
290
+ tbody :global(td.frozen-column) {
291
+ position: sticky;
292
+ z-index: var(--layer-2);
293
+ }
294
+
295
+ tbody :global(tr:nth-child(odd)) :global(td.frozen-column) {
296
+ background: var(--table-odd-background-fill);
297
+ }
298
+
299
+ tbody :global(tr:nth-child(even)) :global(td.frozen-column) {
300
+ background: var(--table-even-background-fill);
301
+ }
302
+
303
+ tbody :global(td.always-frozen) {
304
+ z-index: var(--layer-3);
305
+ }
306
+
307
+ tbody :global(td.last-frozen) {
308
+ border-right: 2px solid var(--border-color-primary);
309
+ }
310
+
284
311
  thead {
285
312
  position: sticky;
286
313
  top: 0;
287
314
  left: 0;
288
- z-index: var(--layer-1);
289
- overflow: hidden;
315
+ z-index: var(--layer-3);
316
+ background: var(--background-fill-primary);
317
+ }
318
+
319
+ thead :global(th) {
320
+ background: var(--table-even-background-fill) !important;
321
+ }
322
+
323
+ thead :global(th.frozen-column) {
324
+ position: sticky;
325
+ z-index: var(--layer-4);
326
+ }
327
+
328
+ thead :global(th.always-frozen) {
329
+ z-index: var(--layer-5);
330
+ }
331
+
332
+ .table.disable-scroll {
333
+ overflow: hidden !important;
290
334
  }
291
335
  </style>
@@ -8,6 +8,7 @@ declare const __propDef: {
8
8
  start?: number | undefined;
9
9
  end?: number | undefined;
10
10
  selected: number | false;
11
+ disable_scroll?: boolean | undefined;
11
12
  scroll_to_index?: ((index: number, opts: ScrollToOptions, align_end?: boolean) => Promise<void>) | undefined;
12
13
  };
13
14
  events: {
@@ -0,0 +1,11 @@
1
+ <script></script>
2
+
3
+ <svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
4
+ <path
5
+ d="M4 4h16v2.67l-6.67 6.67v8L9.33 19v-5.66L2.67 6.67V4h1.33z"
6
+ stroke="currentColor"
7
+ stroke-width="2"
8
+ stroke-linecap="round"
9
+ stroke-linejoin="round"
10
+ />
11
+ </svg>
@@ -0,0 +1,16 @@
1
+ import { SvelteComponent } from "svelte";
2
+ declare const __propDef: {
3
+ props: {
4
+ [x: string]: never;
5
+ };
6
+ events: {
7
+ [evt: string]: CustomEvent<any>;
8
+ };
9
+ slots: {};
10
+ };
11
+ export type FilterIconProps = typeof __propDef.props;
12
+ export type FilterIconEvents = typeof __propDef.events;
13
+ export type FilterIconSlots = typeof __propDef.slots;
14
+ export default class FilterIcon extends SvelteComponent<FilterIconProps, FilterIconEvents, FilterIconSlots> {
15
+ }
16
+ export {};
@@ -0,0 +1,90 @@
1
+ <script>import { createEventDispatcher } from "svelte";
2
+ export let direction = null;
3
+ export let i18n;
4
+ const dispatch = createEventDispatcher();
5
+ </script>
6
+
7
+ <div class="sort-icons" role="group" aria-label={i18n("dataframe.sort_column")}>
8
+ <button
9
+ class="sort-button up"
10
+ class:active={direction === "asc"}
11
+ on:click={() => dispatch("sort", "asc")}
12
+ aria-label={i18n("dataframe.sort_ascending")}
13
+ aria-pressed={direction === "asc"}
14
+ >
15
+ <svg
16
+ viewBox="0 0 24 24"
17
+ fill="none"
18
+ xmlns="http://www.w3.org/2000/svg"
19
+ aria-hidden="true"
20
+ focusable="false"
21
+ >
22
+ <path
23
+ d="M7 14l5-5 5 5"
24
+ stroke="currentColor"
25
+ stroke-width="2"
26
+ stroke-linecap="round"
27
+ stroke-linejoin="round"
28
+ />
29
+ </svg>
30
+ </button>
31
+ <button
32
+ class="sort-button down"
33
+ class:active={direction === "des"}
34
+ on:click={() => dispatch("sort", "des")}
35
+ aria-label={i18n("dataframe.sort_descending")}
36
+ aria-pressed={direction === "des"}
37
+ >
38
+ <svg
39
+ viewBox="0 0 24 24"
40
+ fill="none"
41
+ xmlns="http://www.w3.org/2000/svg"
42
+ aria-hidden="true"
43
+ focusable="false"
44
+ >
45
+ <path
46
+ d="M7 10l5 5 5-5"
47
+ stroke="currentColor"
48
+ stroke-width="2"
49
+ stroke-linecap="round"
50
+ stroke-linejoin="round"
51
+ />
52
+ </svg>
53
+ </button>
54
+ </div>
55
+
56
+ <style>
57
+ .sort-icons {
58
+ display: flex;
59
+ flex-direction: column;
60
+ gap: 0;
61
+ margin-right: var(--spacing-md);
62
+ }
63
+
64
+ .sort-button {
65
+ display: flex;
66
+ align-items: center;
67
+ justify-content: center;
68
+ padding: 0;
69
+ background: none;
70
+ border: none;
71
+ cursor: pointer;
72
+ opacity: 0.5;
73
+ transition: opacity 150ms;
74
+ }
75
+
76
+ .sort-button:hover {
77
+ opacity: 0.8;
78
+ }
79
+
80
+ .sort-button.active {
81
+ opacity: 1;
82
+ color: var(--color-accent);
83
+ }
84
+
85
+ svg {
86
+ width: var(--size-3);
87
+ height: var(--size-3);
88
+ display: block;
89
+ }
90
+ </style>
@@ -0,0 +1,20 @@
1
+ import { SvelteComponent } from "svelte";
2
+ import type { I18nFormatter } from "@gradio/utils";
3
+ declare const __propDef: {
4
+ props: {
5
+ direction?: (("asc" | "des") | null) | undefined;
6
+ i18n: I18nFormatter;
7
+ };
8
+ events: {
9
+ sort: CustomEvent<"asc" | "des">;
10
+ } & {
11
+ [evt: string]: CustomEvent<any>;
12
+ };
13
+ slots: {};
14
+ };
15
+ export type SortIconProps = typeof __propDef.props;
16
+ export type SortIconEvents = typeof __propDef.events;
17
+ export type SortIconSlots = typeof __propDef.slots;
18
+ export default class SortIcon extends SvelteComponent<SortIconProps, SortIconEvents, SortIconSlots> {
19
+ }
20
+ export {};
@@ -0,0 +1,30 @@
1
+ import type { CellCoordinate, EditingState } from "./types";
2
+ export type CellData = {
3
+ id: string;
4
+ value: string | number;
5
+ };
6
+ export declare function is_cell_selected(cell: CellCoordinate, selected_cells: CellCoordinate[]): string;
7
+ export declare function get_range_selection(start: CellCoordinate, end: CellCoordinate): CellCoordinate[];
8
+ export declare function handle_selection(current: CellCoordinate, selected_cells: CellCoordinate[], event: {
9
+ shiftKey: boolean;
10
+ metaKey: boolean;
11
+ ctrlKey: boolean;
12
+ }): CellCoordinate[];
13
+ export declare function handle_delete_key(data: CellData[][], selected_cells: CellCoordinate[]): CellData[][];
14
+ export declare function handle_editing_state(current: CellCoordinate, editing: EditingState, selected_cells: CellCoordinate[], editable: boolean): EditingState;
15
+ export declare function should_show_cell_menu(cell: CellCoordinate, selected_cells: CellCoordinate[], editable: boolean): boolean;
16
+ export declare function get_next_cell_coordinates(current: CellCoordinate, data: CellData[][], shift_key: boolean): CellCoordinate | false;
17
+ export declare function move_cursor(key: "ArrowRight" | "ArrowLeft" | "ArrowDown" | "ArrowUp", current_coords: CellCoordinate, data: CellData[][]): CellCoordinate | false;
18
+ export declare function get_current_indices(id: string, data: CellData[][]): [number, number];
19
+ export declare function handle_click_outside(event: Event, parent: HTMLElement): boolean;
20
+ export declare function select_column(data: any[][], col: number): CellCoordinate[];
21
+ export declare function select_row(data: any[][], row: number): CellCoordinate[];
22
+ export declare function calculate_selection_positions(selected: CellCoordinate, data: {
23
+ id: string;
24
+ value: string | number;
25
+ }[][], els: Record<string, {
26
+ cell: HTMLTableCellElement | null;
27
+ }>, parent: HTMLElement, table: HTMLElement): {
28
+ col_pos: string;
29
+ row_pos: string | undefined;
30
+ };
@@ -0,0 +1,139 @@
1
+ export function is_cell_selected(cell, selected_cells) {
2
+ const [row, col] = cell;
3
+ if (!selected_cells.some(([r, c]) => r === row && c === col))
4
+ return "";
5
+ const up = selected_cells.some(([r, c]) => r === row - 1 && c === col);
6
+ const down = selected_cells.some(([r, c]) => r === row + 1 && c === col);
7
+ const left = selected_cells.some(([r, c]) => r === row && c === col - 1);
8
+ const right = selected_cells.some(([r, c]) => r === row && c === col + 1);
9
+ return `cell-selected${up ? " no-top" : ""}${down ? " no-bottom" : ""}${left ? " no-left" : ""}${right ? " no-right" : ""}`;
10
+ }
11
+ export function get_range_selection(start, end) {
12
+ const [start_row, start_col] = start;
13
+ const [end_row, end_col] = end;
14
+ const min_row = Math.min(start_row, end_row);
15
+ const max_row = Math.max(start_row, end_row);
16
+ const min_col = Math.min(start_col, end_col);
17
+ const max_col = Math.max(start_col, end_col);
18
+ const cells = [];
19
+ for (let i = min_row; i <= max_row; i++) {
20
+ for (let j = min_col; j <= max_col; j++) {
21
+ cells.push([i, j]);
22
+ }
23
+ }
24
+ return cells;
25
+ }
26
+ export function handle_selection(current, selected_cells, event) {
27
+ if (event.shiftKey && selected_cells.length > 0) {
28
+ return get_range_selection(selected_cells[selected_cells.length - 1], current);
29
+ }
30
+ if (event.metaKey || event.ctrlKey) {
31
+ const is_cell_match = ([r, c]) => r === current[0] && c === current[1];
32
+ const index = selected_cells.findIndex(is_cell_match);
33
+ return index === -1
34
+ ? [...selected_cells, current]
35
+ : selected_cells.filter((_, i) => i !== index);
36
+ }
37
+ return [current];
38
+ }
39
+ export function handle_delete_key(data, selected_cells) {
40
+ const new_data = data.map((row) => [...row]);
41
+ selected_cells.forEach(([row, col]) => {
42
+ if (new_data[row] && new_data[row][col]) {
43
+ new_data[row][col] = { ...new_data[row][col], value: "" };
44
+ }
45
+ });
46
+ return new_data;
47
+ }
48
+ export function handle_editing_state(current, editing, selected_cells, editable) {
49
+ const [row, col] = current;
50
+ if (!editable)
51
+ return false;
52
+ if (editing && editing[0] === row && editing[1] === col)
53
+ return editing;
54
+ if (selected_cells.length === 1 &&
55
+ selected_cells[0][0] === row &&
56
+ selected_cells[0][1] === col) {
57
+ return [row, col];
58
+ }
59
+ return false;
60
+ }
61
+ export function should_show_cell_menu(cell, selected_cells, editable) {
62
+ const [row, col] = cell;
63
+ return (editable &&
64
+ selected_cells.length === 1 &&
65
+ selected_cells[0][0] === row &&
66
+ selected_cells[0][1] === col);
67
+ }
68
+ export function get_next_cell_coordinates(current, data, shift_key) {
69
+ const [row, col] = current;
70
+ const direction = shift_key ? -1 : 1;
71
+ if (data[row]?.[col + direction]) {
72
+ return [row, col + direction];
73
+ }
74
+ const next_row = row + (direction > 0 ? 1 : 0);
75
+ const prev_row = row + (direction < 0 ? -1 : 0);
76
+ if (direction > 0 && data[next_row]?.[0]) {
77
+ return [next_row, 0];
78
+ }
79
+ if (direction < 0 && data[prev_row]?.[data[0].length - 1]) {
80
+ return [prev_row, data[0].length - 1];
81
+ }
82
+ return false;
83
+ }
84
+ export function move_cursor(key, current_coords, data) {
85
+ const dir = {
86
+ ArrowRight: [0, 1],
87
+ ArrowLeft: [0, -1],
88
+ ArrowDown: [1, 0],
89
+ ArrowUp: [-1, 0]
90
+ }[key];
91
+ const i = current_coords[0] + dir[0];
92
+ const j = current_coords[1] + dir[1];
93
+ if (i < 0 && j <= 0) {
94
+ return false;
95
+ }
96
+ const is_data = data[i]?.[j];
97
+ if (is_data) {
98
+ return [i, j];
99
+ }
100
+ return false;
101
+ }
102
+ export function get_current_indices(id, data) {
103
+ return data.reduce((acc, arr, i) => {
104
+ const j = arr.reduce((_acc, _data, k) => (id === _data.id ? k : _acc), -1);
105
+ return j === -1 ? acc : [i, j];
106
+ }, [-1, -1]);
107
+ }
108
+ export function handle_click_outside(event, parent) {
109
+ const [trigger] = event.composedPath();
110
+ return !parent.contains(trigger);
111
+ }
112
+ export function select_column(data, col) {
113
+ return Array.from({ length: data.length }, (_, i) => [i, col]);
114
+ }
115
+ export function select_row(data, row) {
116
+ return Array.from({ length: data[0].length }, (_, i) => [row, i]);
117
+ }
118
+ export function calculate_selection_positions(selected, data, els, parent, table) {
119
+ const [row, col] = selected;
120
+ if (!data[row]?.[col]) {
121
+ return { col_pos: "0px", row_pos: undefined };
122
+ }
123
+ let offset = 0;
124
+ for (let i = 0; i < col; i++) {
125
+ offset += parseFloat(getComputedStyle(parent).getPropertyValue(`--cell-width-${i}`));
126
+ }
127
+ const cell_id = data[row][col].id;
128
+ const cell_el = els[cell_id]?.cell;
129
+ if (!cell_el) {
130
+ // if we cant get the row position, just return the column position which is static
131
+ return { col_pos: "0px", row_pos: undefined };
132
+ }
133
+ const cell_rect = cell_el.getBoundingClientRect();
134
+ const table_rect = table.getBoundingClientRect();
135
+ const col_pos = `${cell_rect.left - table_rect.left + cell_rect.width / 2}px`;
136
+ const relative_top = cell_rect.top - table_rect.top;
137
+ const row_pos = `${relative_top + cell_rect.height / 2}px`;
138
+ return { col_pos, row_pos };
139
+ }
@@ -0,0 +1,18 @@
1
+ export type CellCoordinate = [number, number];
2
+ export type EditingState = CellCoordinate | false;
3
+ export type Headers = (string | null)[];
4
+ export interface HeadersWithIDs {
5
+ id: string;
6
+ value: string;
7
+ }
8
+ export interface TableCell {
9
+ id: string;
10
+ value: string | number;
11
+ }
12
+ export type TableData = TableCell[][];
13
+ export type CountConfig = [number, "fixed" | "dynamic"];
14
+ export type ElementRefs = Record<string, {
15
+ cell: null | HTMLTableCellElement;
16
+ input: null | HTMLInputElement;
17
+ }>;
18
+ export type DataBinding = Record<string, TableCell>;