@gradio/dataframe 0.17.4 → 0.17.6
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/CHANGELOG.md +25 -0
- package/Dataframe.stories.svelte +29 -0
- package/dist/shared/CellMenu.svelte.d.ts +1 -1
- package/dist/shared/EditableCell.svelte +8 -17
- package/dist/shared/EditableCell.svelte.d.ts +5 -3
- package/dist/shared/Table.svelte +111 -138
- package/dist/shared/TableCell.svelte +4 -6
- package/dist/shared/TableCell.svelte.d.ts +5 -1
- package/dist/shared/TableHeader.svelte +4 -3
- package/dist/shared/TableHeader.svelte.d.ts +1 -2
- package/dist/shared/context/dataframe_context.d.ts +147 -0
- package/dist/shared/context/dataframe_context.js +335 -0
- package/dist/shared/selection_utils.d.ts +1 -2
- package/dist/shared/selection_utils.js +0 -13
- package/dist/shared/utils/drag_utils.js +1 -0
- package/dist/shared/utils/keyboard_utils.d.ts +3 -2
- package/dist/shared/utils/keyboard_utils.js +107 -68
- package/package.json +6 -6
- package/shared/CellMenu.svelte +1 -1
- package/shared/EditableCell.svelte +9 -20
- package/shared/Table.svelte +147 -165
- package/shared/TableCell.svelte +9 -6
- package/shared/TableHeader.svelte +5 -8
- package/shared/context/dataframe_context.ts +576 -0
- package/shared/selection_utils.ts +1 -23
- package/shared/utils/drag_utils.ts +1 -0
- package/shared/utils/keyboard_utils.ts +142 -80
- package/{shared/utils → test}/sort_utils.test.ts +5 -1
- package/{shared/utils → test}/table_utils.test.ts +2 -2
- package/dist/shared/context/keyboard_context.d.ts +0 -37
- package/dist/shared/context/keyboard_context.js +0 -12
- package/dist/shared/context/selection_context.d.ts +0 -32
- package/dist/shared/context/selection_context.js +0 -107
- package/dist/shared/context/table_context.d.ts +0 -141
- package/dist/shared/context/table_context.js +0 -375
- package/shared/context/keyboard_context.ts +0 -65
- package/shared/context/selection_context.ts +0 -168
- package/shared/context/table_context.ts +0 -625
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
import { getContext, setContext } from "svelte";
|
|
2
|
-
import { tick } from "svelte";
|
|
3
|
-
import type { DataFrameContext } from "./table_context";
|
|
4
|
-
import type { CellData } from "../selection_utils";
|
|
5
|
-
import type { DataframeValue } from "../utils";
|
|
6
|
-
import { handle_selection } from "../selection_utils";
|
|
7
|
-
|
|
8
|
-
const SELECTION_KEY = Symbol("selection");
|
|
9
|
-
|
|
10
|
-
export type SelectionContext = {
|
|
11
|
-
df_actions: DataFrameContext["actions"];
|
|
12
|
-
dispatch: {
|
|
13
|
-
(e: "change", detail: DataframeValue): void;
|
|
14
|
-
(e: "input", detail?: undefined): void;
|
|
15
|
-
(e: "select", detail: any): void;
|
|
16
|
-
(e: "search", detail: string | null): void;
|
|
17
|
-
};
|
|
18
|
-
data: CellData[][];
|
|
19
|
-
els: Record<
|
|
20
|
-
string,
|
|
21
|
-
{ cell: null | HTMLTableCellElement; input: null | HTMLInputElement }
|
|
22
|
-
>;
|
|
23
|
-
editable: boolean;
|
|
24
|
-
show_row_numbers: boolean;
|
|
25
|
-
get_data_at: (row: number, col: number) => string | number;
|
|
26
|
-
clear_on_focus: boolean;
|
|
27
|
-
selected_cells: [number, number][];
|
|
28
|
-
parent_element: HTMLElement;
|
|
29
|
-
actions: {
|
|
30
|
-
handle_cell_click: (event: MouseEvent, row: number, col: number) => void;
|
|
31
|
-
toggle_cell_menu: (event: MouseEvent, row: number, col: number) => void;
|
|
32
|
-
toggle_cell_button: (row: number, col: number) => void;
|
|
33
|
-
handle_select_column: (col: number) => void;
|
|
34
|
-
handle_select_row: (row: number) => void;
|
|
35
|
-
};
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
export function create_selection_context(
|
|
39
|
-
context: Omit<SelectionContext, "actions">
|
|
40
|
-
): SelectionContext {
|
|
41
|
-
const instance_id = Symbol(
|
|
42
|
-
`selection_${Math.random().toString(36).substring(2)}`
|
|
43
|
-
);
|
|
44
|
-
const actions = {
|
|
45
|
-
handle_cell_click: (event: MouseEvent, row: number, col: number) => {
|
|
46
|
-
if (event.target instanceof HTMLAnchorElement) return;
|
|
47
|
-
|
|
48
|
-
event.preventDefault();
|
|
49
|
-
event.stopPropagation();
|
|
50
|
-
|
|
51
|
-
if (context.show_row_numbers && col === -1) return;
|
|
52
|
-
|
|
53
|
-
context.clear_on_focus = false;
|
|
54
|
-
context.df_actions.set_active_cell_menu(null);
|
|
55
|
-
context.df_actions.set_active_header_menu(null);
|
|
56
|
-
context.df_actions.set_selected_header(false);
|
|
57
|
-
context.df_actions.set_header_edit(false);
|
|
58
|
-
|
|
59
|
-
const new_selected_cells = handle_selection(
|
|
60
|
-
[row, col],
|
|
61
|
-
context.selected_cells || [],
|
|
62
|
-
event
|
|
63
|
-
);
|
|
64
|
-
|
|
65
|
-
context.df_actions.set_selected_cells(new_selected_cells);
|
|
66
|
-
context.df_actions.set_selected(new_selected_cells[0]);
|
|
67
|
-
|
|
68
|
-
if (context.editable) {
|
|
69
|
-
if (new_selected_cells.length === 1) {
|
|
70
|
-
context.df_actions.set_editing([row, col]);
|
|
71
|
-
tick().then(() => {
|
|
72
|
-
const input_el = context.els[context.data[row][col].id].input;
|
|
73
|
-
if (input_el) {
|
|
74
|
-
input_el.focus();
|
|
75
|
-
input_el.selectionStart = input_el.selectionEnd =
|
|
76
|
-
input_el.value.length;
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
} else {
|
|
80
|
-
context.df_actions.set_editing(false);
|
|
81
|
-
context.parent_element.focus();
|
|
82
|
-
}
|
|
83
|
-
} else {
|
|
84
|
-
context.parent_element.focus();
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
actions.toggle_cell_button(row, col);
|
|
88
|
-
|
|
89
|
-
context.dispatch("select", {
|
|
90
|
-
index: [row, col],
|
|
91
|
-
value: context.get_data_at(row, col),
|
|
92
|
-
row_value: context.data[row].map((d) => d.value)
|
|
93
|
-
});
|
|
94
|
-
},
|
|
95
|
-
|
|
96
|
-
toggle_cell_menu: (event: MouseEvent, row: number, col: number) => {
|
|
97
|
-
event.stopPropagation();
|
|
98
|
-
const current_menu = context.df_actions.get_active_cell_menu();
|
|
99
|
-
if (
|
|
100
|
-
current_menu &&
|
|
101
|
-
current_menu.row === row &&
|
|
102
|
-
current_menu.col === col
|
|
103
|
-
) {
|
|
104
|
-
context.df_actions.set_active_cell_menu(null);
|
|
105
|
-
} else {
|
|
106
|
-
const cell = (event.target as HTMLElement).closest("td");
|
|
107
|
-
if (cell) {
|
|
108
|
-
const rect = cell.getBoundingClientRect();
|
|
109
|
-
context.df_actions.set_active_cell_menu({
|
|
110
|
-
row,
|
|
111
|
-
col,
|
|
112
|
-
x: rect.right,
|
|
113
|
-
y: rect.bottom
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
},
|
|
118
|
-
|
|
119
|
-
toggle_cell_button: (row: number, col: number) => {
|
|
120
|
-
const current_button = context.df_actions.get_active_button();
|
|
121
|
-
const new_button =
|
|
122
|
-
current_button?.type === "cell" &&
|
|
123
|
-
current_button.row === row &&
|
|
124
|
-
current_button.col === col
|
|
125
|
-
? null
|
|
126
|
-
: { type: "cell" as const, row, col };
|
|
127
|
-
context.df_actions.set_active_button(new_button);
|
|
128
|
-
},
|
|
129
|
-
|
|
130
|
-
handle_select_column: (col: number) => {
|
|
131
|
-
const selected_cells = context.data.map(
|
|
132
|
-
(_, row) => [row, col] as [number, number]
|
|
133
|
-
);
|
|
134
|
-
context.df_actions.set_selected_cells(selected_cells);
|
|
135
|
-
context.df_actions.set_selected(selected_cells[0]);
|
|
136
|
-
context.df_actions.set_editing(false);
|
|
137
|
-
|
|
138
|
-
setTimeout(() => {
|
|
139
|
-
context.parent_element.focus();
|
|
140
|
-
}, 0);
|
|
141
|
-
},
|
|
142
|
-
|
|
143
|
-
handle_select_row: (row: number) => {
|
|
144
|
-
const selected_cells = context.data[0].map(
|
|
145
|
-
(_, col) => [row, col] as [number, number]
|
|
146
|
-
);
|
|
147
|
-
context.df_actions.set_selected_cells(selected_cells);
|
|
148
|
-
context.df_actions.set_selected(selected_cells[0]);
|
|
149
|
-
context.df_actions.set_editing(false);
|
|
150
|
-
|
|
151
|
-
setTimeout(() => {
|
|
152
|
-
context.parent_element.focus();
|
|
153
|
-
}, 0);
|
|
154
|
-
}
|
|
155
|
-
};
|
|
156
|
-
|
|
157
|
-
const selection_context = { ...context, actions };
|
|
158
|
-
setContext(instance_id, selection_context);
|
|
159
|
-
setContext(SELECTION_KEY, { instance_id, context: selection_context });
|
|
160
|
-
return selection_context;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
export function get_selection_context(): SelectionContext {
|
|
164
|
-
const ctx = getContext<{ instance_id: symbol; context: SelectionContext }>(
|
|
165
|
-
SELECTION_KEY
|
|
166
|
-
);
|
|
167
|
-
return ctx ? ctx.context : getContext<SelectionContext>(SELECTION_KEY);
|
|
168
|
-
}
|