Skip to content

Commit af05005

Browse files
committed
[inspector] Table actions
1 parent bf33c72 commit af05005

4 files changed

Lines changed: 96 additions & 35 deletions

File tree

src/ui-react-inspector/TableView.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ import {
1717
} from '../ui-react/index.ts';
1818
import {Details} from './Details.tsx';
1919
import {CellActions} from './actions/cell.tsx';
20-
import {ConfirmableActions} from './actions/common.tsx';
21-
import {rowActions, RowAdd} from './actions/row.tsx';
20+
import {rowActions, TableActions1, TableActions2} from './actions/tables.tsx';
2221
import {getUniqueId, SORT_CELL, STATE_TABLE, useEditable} from './common.ts';
2322
import type {StoreProp} from './types.ts';
2423

@@ -79,13 +78,14 @@ export const TableView = ({
7978
)}
8079
/>
8180
{editable ? (
82-
<p>
83-
<ConfirmableActions
84-
actions={[['add', 'Add Row', RowAdd]]}
85-
store={store}
86-
tableId={tableId}
87-
/>
88-
</p>
81+
<div className="actions">
82+
<div>
83+
<TableActions1 tableId={tableId} store={store} />
84+
</div>
85+
<div>
86+
<TableActions2 tableId={tableId} store={store} />
87+
</div>
88+
</div>
8989
) : null}
9090
</Details>
9191
);

src/ui-react-inspector/ValuesView.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ export const ValuesView = ({
3333
/>
3434
)}
3535
{editable ? (
36-
<p>
36+
<div className="actions">
3737
<ConfirmableActions
3838
actions={[['add', 'Add Value', AddValue]]}
3939
store={store}
4040
/>
41-
</p>
41+
</div>
4242
) : null}
4343
</Details>
4444
);

src/ui-react-inspector/actions/tables.tsx

Lines changed: 79 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import {objNew} from '../../common/obj.ts';
99
import {useCallback} from '../../common/react.ts';
1010
import {
1111
useDelRowCallback,
12+
useDelTableCallback,
1213
useSetCellCallback,
1314
useSetRowCallback,
15+
useSetTableCallback,
1416
useStoreOrStoreById,
1517
} from '../../ui-react/index.ts';
1618
import {
@@ -20,6 +22,83 @@ import {
2022
NewId,
2123
} from './common.tsx';
2224

25+
const useHasTableCallback = (storeOrStoreId: StoreOrStoreId | undefined) => {
26+
const store = useStoreOrStoreById(storeOrStoreId);
27+
return useCallback(
28+
(tableId: Id) => store?.hasTable(tableId) ?? false,
29+
[store],
30+
);
31+
};
32+
33+
const AddRow = ({
34+
onDone,
35+
tableId,
36+
store,
37+
}: {onDone: () => void} & TableProps) => {
38+
const has = useHasRowCallback(store, tableId);
39+
return (
40+
<NewId
41+
onDone={onDone}
42+
suggestedId={getNewIdFromSuggestedId('row', has)}
43+
has={has}
44+
set={useSetRowCallback(
45+
tableId,
46+
(newId) => newId,
47+
(_, store) =>
48+
objNew(
49+
arrayMap(store.getTableCellIds(tableId), (cellId) => [cellId, '']),
50+
),
51+
)}
52+
/>
53+
);
54+
};
55+
56+
export const CloneTable = ({
57+
onDone,
58+
tableId,
59+
store: storeOrStoreId,
60+
}: {onDone: () => void} & TableProps) => {
61+
const store = useStoreOrStoreById(storeOrStoreId)!;
62+
const has = useHasTableCallback(store);
63+
return (
64+
<NewId
65+
onDone={onDone}
66+
suggestedId={getNewIdFromSuggestedId(tableId, has)}
67+
has={has}
68+
set={useSetTableCallback(
69+
(tableId) => tableId,
70+
(_, store) => store.getTable(tableId),
71+
)}
72+
/>
73+
);
74+
};
75+
76+
const DeleteTable = ({
77+
onDone,
78+
tableId,
79+
store,
80+
}: {onDone: () => void} & TableProps) => (
81+
<Delete onClick={useDelTableCallback(tableId, store, onDone)} />
82+
);
83+
84+
export const TableActions1 = ({tableId, store}: TableProps) => (
85+
<ConfirmableActions
86+
actions={[['add', 'Add Row', AddRow]]}
87+
store={store}
88+
tableId={tableId}
89+
/>
90+
);
91+
export const TableActions2 = ({tableId, store}: TableProps) => (
92+
<ConfirmableActions
93+
actions={[
94+
['clone', 'Clone Table', CloneTable],
95+
['delete', 'Delete Table', DeleteTable],
96+
]}
97+
store={store}
98+
tableId={tableId}
99+
/>
100+
);
101+
23102
const useHasRowCallback = (
24103
storeOrStoreId: StoreOrStoreId | undefined,
25104
tableId: Id,
@@ -59,29 +138,6 @@ const AddCell = ({
59138
);
60139
};
61140

62-
export const AddRow = ({
63-
onDone,
64-
tableId,
65-
store,
66-
}: {onDone: () => void} & TableProps) => {
67-
const has = useHasRowCallback(store, tableId);
68-
return (
69-
<NewId
70-
onDone={onDone}
71-
suggestedId={getNewIdFromSuggestedId('row', has)}
72-
has={has}
73-
set={useSetRowCallback(
74-
tableId,
75-
(newId) => newId,
76-
(_, store) =>
77-
objNew(
78-
arrayMap(store.getTableCellIds(tableId), (cellId) => [cellId, '']),
79-
),
80-
)}
81-
/>
82-
);
83-
};
84-
85141
const CloneRow = ({
86142
onDone,
87143
tableId,

src/ui-react-inspector/style.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@ export const APP_STYLESHEET = arrayJoin(
195195
'img.edit': EDIT_SVG,
196196
'img.done': DONE_SVG,
197197

198-
p: {padding: '.25rem .5rem', margin: 0},
199198
caption: {
200199
color: 'var(--foreground2)',
201200
'text-align': 'left',
@@ -211,6 +210,12 @@ export const APP_STYLESHEET = arrayJoin(
211210
cursor: 'pointer',
212211
},
213212
'caption button[disabled]': {color: 'var(--foreground2)'},
213+
'.actions': {
214+
padding: '.75rem .5rem',
215+
margin: 0,
216+
display: 'flex',
217+
'justify-content': 'space-between',
218+
},
214219

215220
// tables
216221
table: {

0 commit comments

Comments
 (0)