intable 0.0.5 → 0.0.7
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 +1 -2
- package/docs/index-BaMALNy6.css +1 -0
- package/docs/index-CDN48t9E.js +3 -0
- package/docs/index-Cc4RNkLY.css +1 -0
- package/docs/index-MRnbkYmU.js +3 -0
- package/docs/index.html +15 -0
- package/docs/vite.svg +1 -0
- package/index.html +14 -0
- package/package.json +30 -37
- package/packages/intable/README.md +379 -0
- package/packages/intable/package.json +51 -0
- package/packages/intable/src/assets/ClearFormat.svg +3 -0
- package/packages/intable/src/assets/Forms.svg +4 -0
- package/packages/intable/src/assets/MergeCell.svg +4 -0
- package/packages/intable/src/assets/SplitCell.svg +4 -0
- package/packages/intable/src/assets/gap.svg +3 -0
- package/packages/intable/src/assets/loading.svg +12 -0
- package/packages/intable/src/assets/paint.svg +9 -0
- package/packages/intable/src/assets/solid.svg +1 -0
- package/packages/intable/src/components/Columns.tsx +86 -0
- package/packages/intable/src/components/DocTree.tsx +36 -0
- package/packages/intable/src/components/Menu.tsx +109 -0
- package/packages/intable/src/components/Popover.tsx +55 -0
- package/packages/intable/src/components/RecycleList.tsx +99 -0
- package/packages/intable/src/components/Render.tsx +26 -0
- package/packages/intable/src/components/Split.tsx +56 -0
- package/packages/intable/src/components/Tree.tsx +115 -0
- package/packages/intable/src/components/utils.tsx +12 -0
- package/packages/intable/src/hooks/index.ts +200 -0
- package/packages/intable/src/hooks/useDir.ts +78 -0
- package/packages/intable/src/hooks/useSelector.ts +91 -0
- package/packages/intable/src/hooks/useSort.tsx +118 -0
- package/packages/intable/src/hooks/useVirtualizer.ts +180 -0
- package/packages/intable/src/index.tsx +481 -0
- package/packages/intable/src/plugins/CellChangeHighlightPlugin.tsx +5 -0
- package/packages/intable/src/plugins/CellMergePlugin.tsx +153 -0
- package/packages/intable/src/plugins/CellSelectionPlugin.tsx +175 -0
- package/packages/intable/src/plugins/CommandPlugin.tsx +74 -0
- package/packages/intable/src/plugins/CopyPastePlugin.tsx +63 -0
- package/packages/intable/src/plugins/DiffPlugin.tsx +107 -0
- package/packages/intable/src/plugins/DragPlugin.tsx +81 -0
- package/packages/intable/src/plugins/EditablePlugin.tsx +252 -0
- package/packages/intable/src/plugins/ExpandPlugin.tsx +80 -0
- package/packages/intable/src/plugins/HeaderGroup.tsx +289 -0
- package/packages/intable/src/plugins/HistoryPlugin.tsx +49 -0
- package/packages/intable/src/plugins/MenuPlugin.tsx +195 -0
- package/packages/intable/src/plugins/RenderPlugin/components.tsx +51 -0
- package/packages/intable/src/plugins/RenderPlugin/index.tsx +81 -0
- package/packages/intable/src/plugins/ResizePlugin.tsx +122 -0
- package/packages/intable/src/plugins/RowGroupPlugin.tsx +122 -0
- package/packages/intable/src/plugins/RowSelectionPlugin.tsx +65 -0
- package/packages/intable/src/plugins/TreePlugin.tsx +212 -0
- package/packages/intable/src/plugins/VirtualScrollPlugin.tsx +190 -0
- package/packages/intable/src/plugins/ZodValidatorPlugin.tsx +61 -0
- package/packages/intable/src/style.scss +244 -0
- package/{dist → packages/intable/src}/theme/antd.scss +14 -5
- package/{dist → packages/intable/src}/theme/element-plus.scss +6 -5
- package/packages/intable/src/tree.ts +13 -0
- package/packages/intable/src/types/auto-imports.d.ts +13 -0
- package/packages/intable/src/utils.ts +122 -0
- package/packages/intable/src/wc.tsx +35 -0
- package/packages/intable/src/web-component.ts +1 -0
- package/packages/react/package.json +31 -0
- package/packages/react/src/index.ts +44 -0
- package/packages/react/src/plugins/antd.ts +94 -0
- package/packages/react/src/style.scss +12 -0
- package/packages/react/src/types/auto-imports.d.ts +10 -0
- package/packages/vue/package.json +34 -0
- package/packages/vue/src/index.ts +63 -0
- package/packages/vue/src/plugins/element-plus.ts +69 -0
- package/packages/vue/src/style.scss +12 -0
- package/packages/vue/src/types/auto-imports.d.ts +10 -0
- package/pnpm-workspace.yaml +2 -0
- package/public/vite.svg +1 -0
- package/scripts/build.js +184 -0
- package/scripts/publish.js +95 -0
- package/src/assets/ClearFormat.svg +3 -0
- package/src/assets/Forms.svg +4 -0
- package/src/assets/MergeCell.svg +4 -0
- package/src/assets/SplitCell.svg +4 -0
- package/src/assets/gap.svg +3 -0
- package/src/assets/loading.svg +12 -0
- package/src/assets/paint.svg +9 -0
- package/src/assets/solid.svg +1 -0
- package/src/demo-vue.ts +54 -0
- package/src/demo.tsx +107 -0
- package/src/index.scss +105 -0
- package/src/styles/index.scss +172 -0
- package/src/types/auto-imports.d.ts +13 -0
- package/stats.html +4949 -0
- package/tsconfig.app.json +34 -0
- package/tsconfig.json +7 -0
- package/tsconfig.node.json +26 -0
- package/vite.config.ts +63 -0
- package/dist/__uno.css +0 -1
- package/dist/chevron-right.js +0 -6
- package/dist/components/Columns.d.ts +0 -3
- package/dist/components/Columns.js +0 -71
- package/dist/components/DocTree.d.ts +0 -4
- package/dist/components/DocTree.js +0 -32
- package/dist/components/Menu.d.ts +0 -1
- package/dist/components/Menu.js +0 -107
- package/dist/components/Popover.d.ts +0 -14
- package/dist/components/Popover.js +0 -41
- package/dist/components/Render.d.ts +0 -4
- package/dist/components/Render.js +0 -20
- package/dist/components/Split.d.ts +0 -15
- package/dist/components/Split.js +0 -76
- package/dist/components/Tree.d.ts +0 -37
- package/dist/components/Tree.js +0 -82
- package/dist/components/utils.d.ts +0 -3
- package/dist/components/utils.js +0 -8
- package/dist/hooks/index.d.ts +0 -40
- package/dist/hooks/index.js +0 -157
- package/dist/hooks/useDir.d.ts +0 -11
- package/dist/hooks/useDir.js +0 -42
- package/dist/hooks/useSelector.d.ts +0 -16
- package/dist/hooks/useSelector.js +0 -35
- package/dist/hooks/useSort.d.ts +0 -18
- package/dist/hooks/useSort.js +0 -83
- package/dist/hooks/useVirtualizer.d.ts +0 -25
- package/dist/hooks/useVirtualizer.js +0 -67
- package/dist/index.d.ts +0 -130
- package/dist/index.js +0 -347
- package/dist/loading.js +0 -6
- package/dist/plugins/CellChangeHighlightPlugin.d.ts +0 -2
- package/dist/plugins/CellChangeHighlightPlugin.js +0 -4
- package/dist/plugins/CellMergePlugin.d.ts +0 -12
- package/dist/plugins/CellMergePlugin.js +0 -2
- package/dist/plugins/CellSelectionPlugin.d.ts +0 -15
- package/dist/plugins/CellSelectionPlugin.js +0 -115
- package/dist/plugins/CommandPlugin.d.ts +0 -14
- package/dist/plugins/CommandPlugin.js +0 -12
- package/dist/plugins/CopyPastePlugin.d.ts +0 -14
- package/dist/plugins/CopyPastePlugin.js +0 -42
- package/dist/plugins/DiffPlugin.d.ts +0 -23
- package/dist/plugins/DiffPlugin.js +0 -56
- package/dist/plugins/DragPlugin.d.ts +0 -14
- package/dist/plugins/DragPlugin.js +0 -47
- package/dist/plugins/EditablePlugin.d.ts +0 -48
- package/dist/plugins/EditablePlugin.js +0 -141
- package/dist/plugins/ExpandPlugin.d.ts +0 -18
- package/dist/plugins/ExpandPlugin.js +0 -50
- package/dist/plugins/HistoryPlugin.d.ts +0 -10
- package/dist/plugins/HistoryPlugin.js +0 -30
- package/dist/plugins/MenuPlugin.d.ts +0 -18
- package/dist/plugins/MenuPlugin.js +0 -107
- package/dist/plugins/RenderPlugin/components.d.ts +0 -5
- package/dist/plugins/RenderPlugin/components.js +0 -87
- package/dist/plugins/RenderPlugin/index.d.ts +0 -30
- package/dist/plugins/RenderPlugin/index.js +0 -49
- package/dist/plugins/ResizePlugin.d.ts +0 -27
- package/dist/plugins/ResizePlugin.js +0 -81
- package/dist/plugins/RowGroupPlugin.d.ts +0 -17
- package/dist/plugins/RowGroupPlugin.js +0 -83
- package/dist/plugins/RowSelectionPlugin.d.ts +0 -20
- package/dist/plugins/RowSelectionPlugin.js +0 -42
- package/dist/plugins/VirtualScrollPlugin.d.ts +0 -15
- package/dist/plugins/VirtualScrollPlugin.js +0 -96
- package/dist/plus.js +0 -6
- package/dist/style.css +0 -3
- package/dist/types/auto-imports.d.js +0 -0
- package/dist/utils.d.ts +0 -30
- package/dist/utils.js +0 -70
- package/dist/wc.d.ts +0 -1
- package/dist/wc.js +0 -21
- package/dist/web-component.d.ts +0 -1
- package/dist/web-component.js +0 -2
- package/dist/x.js +0 -6
|
@@ -0,0 +1,481 @@
|
|
|
1
|
+
import { createContext, createMemo, createSignal, For, useContext, createEffect, type JSX, type Component, createComputed, onMount, mergeProps, mapArray, onCleanup, getOwner, runWithOwner, on, untrack, batch, Index, $PROXY } from 'solid-js'
|
|
2
|
+
import { createMutable, reconcile } from 'solid-js/store'
|
|
3
|
+
import { combineProps } from '@solid-primitives/props'
|
|
4
|
+
import { createLazyMemo } from '@solid-primitives/memo'
|
|
5
|
+
import { createElementSize, createResizeObserver } from '@solid-primitives/resize-observer'
|
|
6
|
+
import { createScrollPosition } from '@solid-primitives/scroll'
|
|
7
|
+
import { difference, mapValues, memoize, sumBy } from 'es-toolkit'
|
|
8
|
+
import { toReactive, useMemo, useMemoState } from './hooks'
|
|
9
|
+
import { log, unFn } from './utils'
|
|
10
|
+
|
|
11
|
+
import 'virtual:uno.css'
|
|
12
|
+
import './style.scss'
|
|
13
|
+
|
|
14
|
+
import { CellSelectionPlugin } from './plugins/CellSelectionPlugin'
|
|
15
|
+
import { ClipboardPlugin } from './plugins/CopyPastePlugin'
|
|
16
|
+
import { EditablePlugin } from './plugins/EditablePlugin'
|
|
17
|
+
import { RenderPlugin } from './plugins/RenderPlugin'
|
|
18
|
+
import { MenuPlugin } from './plugins/MenuPlugin'
|
|
19
|
+
import { CommandPlugin } from './plugins/CommandPlugin'
|
|
20
|
+
import { RowSelectionPlugin } from './plugins/RowSelectionPlugin'
|
|
21
|
+
import { ResizePlugin } from './plugins/ResizePlugin'
|
|
22
|
+
import { DragPlugin } from './plugins/DragPlugin'
|
|
23
|
+
import { solidComponent } from './components/utils'
|
|
24
|
+
import { RowGroupPlugin } from './plugins/RowGroupPlugin'
|
|
25
|
+
import { ExpandPlugin } from './plugins/ExpandPlugin'
|
|
26
|
+
import { CellMergePlugin } from './plugins/CellMergePlugin'
|
|
27
|
+
import { TreePlugin } from './plugins/TreePlugin'
|
|
28
|
+
import { HeaderGroupPlugin } from './plugins/HeaderGroup'
|
|
29
|
+
|
|
30
|
+
export const Ctx = createContext({
|
|
31
|
+
props: {} as TableProps2,
|
|
32
|
+
store: {} as TableStore,
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
type Requireds<T, K extends keyof T> = Pri<Omit<T, K> & Required<Pick<T, K>>>
|
|
36
|
+
type Pri<T> = { [K in keyof T]: T[K] }
|
|
37
|
+
type TableProps2 = Requireds<TableProps, (
|
|
38
|
+
'Table' | 'Thead' | 'Tbody' | 'Tr' | 'Th' | 'Td' | 'EachRows' | 'EachCells' |
|
|
39
|
+
'rowKey' | 'data' | 'columns' |
|
|
40
|
+
'newRow'
|
|
41
|
+
)>
|
|
42
|
+
|
|
43
|
+
type Each<T = any> = (props: { each: T[]; children: (e: () => any, i: () => number) => JSX.Element }) => JSX.Element
|
|
44
|
+
|
|
45
|
+
type ProcessProps = {
|
|
46
|
+
[K in keyof TableProps]?: (prev: TableProps2, ctx: { store: TableStore }) => TableProps[K]
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface Plugin {
|
|
50
|
+
name: string
|
|
51
|
+
priority?: number
|
|
52
|
+
store?: (store: TableStore) => Partial<TableStore> | void
|
|
53
|
+
rewriteProps?: ProcessProps
|
|
54
|
+
layers?: Component<TableStore>[]
|
|
55
|
+
onMount?: (store: TableStore) => void
|
|
56
|
+
/**
|
|
57
|
+
* Declare keyboard shortcuts for this plugin.
|
|
58
|
+
* Collected and registered as a **single** keydown listener by CommandPlugin.
|
|
59
|
+
* Keys use tinykeys syntax, e.g. `'$mod+Z'`, `'$mod+Shift+K'`.
|
|
60
|
+
*/
|
|
61
|
+
keybindings?: (store: TableStore) => Record<string, (e?: KeyboardEvent) => void>
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export type Plugin$0 = Plugin | ((store: TableStore) => Plugin)
|
|
65
|
+
|
|
66
|
+
export interface TableProps {
|
|
67
|
+
columns?: TableColumn[]
|
|
68
|
+
data?: any[]
|
|
69
|
+
index?: boolean
|
|
70
|
+
border?: boolean
|
|
71
|
+
stickyHeader?: boolean
|
|
72
|
+
class?: any
|
|
73
|
+
style?: any
|
|
74
|
+
rowKey?: any
|
|
75
|
+
size?: string
|
|
76
|
+
newRow?: (i: number) => any
|
|
77
|
+
// Component
|
|
78
|
+
Table?: Component<any>
|
|
79
|
+
Thead?: Component<any>
|
|
80
|
+
Tbody?: Component<any>
|
|
81
|
+
Td?: TD
|
|
82
|
+
Th?: Component<THProps>
|
|
83
|
+
Tr?: Component<{ y?: number; data?: any; style?: any; children: JSX.Element }>
|
|
84
|
+
EachRows?: Each
|
|
85
|
+
EachCells?: Each<TableColumn>
|
|
86
|
+
//
|
|
87
|
+
cellClass?: ((props: Omit<TDProps, 'y' | 'data'> & { y?:number, data? }) => string) | string
|
|
88
|
+
cellStyle?: ((props: Omit<TDProps, 'y' | 'data'> & { y?:number, data? }) => string) | string
|
|
89
|
+
//
|
|
90
|
+
renderer?: (comp: (props) => JSX.Element) => ((props) => JSX.Element)
|
|
91
|
+
// Plugin
|
|
92
|
+
plugins?: Plugin$0[]
|
|
93
|
+
/**
|
|
94
|
+
* Override or disable individual plugin keybindings.
|
|
95
|
+
* - Override: `{ '$mod+Z': (e) => myUndo() }`
|
|
96
|
+
* - Disable: `{ '$mod+Z': false }`
|
|
97
|
+
*/
|
|
98
|
+
keybindings?: Record<string, ((e?: KeyboardEvent) => void) | false>
|
|
99
|
+
|
|
100
|
+
onDataChange?: (data: any[]) => void
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export type THProps = { x: number; col: TableColumn; children: JSX.Element; rowspan?: number; colspan?: number; style?: any }
|
|
104
|
+
export type TDProps = { x: number; y: number; data: any; col: TableColumn; children: JSX.Element; rowspan?: number; colspan?: number }
|
|
105
|
+
export type TD = Component<TDProps>
|
|
106
|
+
|
|
107
|
+
type Obj = Record<string | symbol, any>
|
|
108
|
+
|
|
109
|
+
export interface TableColumn extends Obj {
|
|
110
|
+
id?: any
|
|
111
|
+
name?: string
|
|
112
|
+
width?: number
|
|
113
|
+
fixed?: 'left' | 'right'
|
|
114
|
+
class?: any
|
|
115
|
+
style?: any
|
|
116
|
+
props?: (props) => JSX.HTMLAttributes<any>
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
type Nullable<T> = T | undefined
|
|
120
|
+
|
|
121
|
+
export interface TableStore extends Obj {
|
|
122
|
+
scroll_el?: HTMLElement
|
|
123
|
+
table: HTMLElement
|
|
124
|
+
thead: HTMLElement
|
|
125
|
+
tbody: HTMLElement
|
|
126
|
+
ths: Nullable<Element>[]
|
|
127
|
+
thSizes: Nullable<{ width: number; height: number }>[]
|
|
128
|
+
trs: Nullable<Element>[]
|
|
129
|
+
trSizes: Nullable<{ width: number; height: number }>[]
|
|
130
|
+
internal: symbol
|
|
131
|
+
raw: symbol
|
|
132
|
+
props: TableProps2
|
|
133
|
+
rawProps: TableProps
|
|
134
|
+
plugins: Plugin[]
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export const Intable = (props: TableProps) => {
|
|
138
|
+
props = mergeProps({ rowKey: 'id' } as Partial<TableProps>, props)
|
|
139
|
+
const owner = getOwner()!
|
|
140
|
+
|
|
141
|
+
const store = createMutable({
|
|
142
|
+
get rawProps() { return props },
|
|
143
|
+
get plugins() { return plugins() }
|
|
144
|
+
} as TableStore)
|
|
145
|
+
|
|
146
|
+
const unplugin = memoize((e: Plugin$0) => runWithOwner(owner, () => unFn(e, store)) as Plugin)
|
|
147
|
+
|
|
148
|
+
const plugins = createMemo(() => [
|
|
149
|
+
...defaultsPlugins,
|
|
150
|
+
...props.plugins || [],
|
|
151
|
+
RenderPlugin
|
|
152
|
+
].map(unplugin).sort((a, b) => (b.priority || 0) - (a.priority || 0)))
|
|
153
|
+
|
|
154
|
+
// init store
|
|
155
|
+
createComputed((old: Plugin[]) => {
|
|
156
|
+
const added = difference(plugins(), old)
|
|
157
|
+
runWithOwner(owner, () => {
|
|
158
|
+
added.forEach(e => Object.assign(store, e.store?.(store)))
|
|
159
|
+
})
|
|
160
|
+
return plugins()
|
|
161
|
+
}, [])
|
|
162
|
+
|
|
163
|
+
// init processProps
|
|
164
|
+
const pluginsProps = mapArray(plugins, () => createSignal<Partial<TableProps>>())
|
|
165
|
+
store.props = toReactive(createMemo(() => pluginsProps()[pluginsProps().length - 1][0]() || props)) as TableProps2
|
|
166
|
+
// store.props = useMemoState(createMemo(() => pluginsProps()[pluginsProps().length - 1][0]() || props)) as TableProps2
|
|
167
|
+
|
|
168
|
+
createComputed(mapArray(plugins, (e, i) => {
|
|
169
|
+
const prev = createMemo(() => pluginsProps()[i() - 1]?.[0]() || props)
|
|
170
|
+
const ret = mergeProps(prev, toReactive(mapValues(e.rewriteProps || {}, v => useMemo(() => v(prev(), { store })) )))
|
|
171
|
+
pluginsProps()[i()][1](ret)
|
|
172
|
+
}))
|
|
173
|
+
|
|
174
|
+
// on mount
|
|
175
|
+
onMount(() => {
|
|
176
|
+
createEffect(mapArray(plugins, e => e.onMount?.(store)))
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
const ctx = createMutable({ props: store.props, store })
|
|
180
|
+
|
|
181
|
+
window.store = store
|
|
182
|
+
window.ctx = ctx
|
|
183
|
+
|
|
184
|
+
return (
|
|
185
|
+
<Ctx.Provider value={ctx}>
|
|
186
|
+
<ctx.props.Table>
|
|
187
|
+
<THead />
|
|
188
|
+
<TBody />
|
|
189
|
+
</ctx.props.Table>
|
|
190
|
+
</Ctx.Provider>
|
|
191
|
+
)
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const THead = () => {
|
|
195
|
+
const { props } = useContext(Ctx)
|
|
196
|
+
return (
|
|
197
|
+
<props.Thead>
|
|
198
|
+
<props.Tr>
|
|
199
|
+
<props.EachCells each={props.columns || []}>
|
|
200
|
+
{(col, colIndex) => <props.Th col={col()} x={colIndex()}>{col().name}</props.Th>}
|
|
201
|
+
</props.EachCells>
|
|
202
|
+
</props.Tr>
|
|
203
|
+
</props.Thead>
|
|
204
|
+
)
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const TBody = () => {
|
|
208
|
+
const { props } = useContext(Ctx)
|
|
209
|
+
return (
|
|
210
|
+
<props.Tbody>
|
|
211
|
+
<props.EachRows each={props.data}>{(row, rowIndex) => (
|
|
212
|
+
<props.Tr y={rowIndex()} data={row()}>
|
|
213
|
+
<props.EachCells each={props.columns}>{(col, colIndex) => (
|
|
214
|
+
<props.Td col={col()} x={colIndex()} y={rowIndex()} data={row()}>
|
|
215
|
+
{row()[col().id]}
|
|
216
|
+
</props.Td>
|
|
217
|
+
)}</props.EachCells>
|
|
218
|
+
</props.Tr>
|
|
219
|
+
)}</props.EachRows>
|
|
220
|
+
</props.Tbody>
|
|
221
|
+
)
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export default Intable
|
|
225
|
+
|
|
226
|
+
// process ===================================================================================================================================================================================================
|
|
227
|
+
|
|
228
|
+
function BasePlugin(): Plugin$0 {
|
|
229
|
+
const omits = { col: null, data: null }
|
|
230
|
+
|
|
231
|
+
const table = o => <table {...o} /> as any
|
|
232
|
+
const thead = o => <thead {...o} /> as any
|
|
233
|
+
const tbody = o => <tbody {...o} /> as any
|
|
234
|
+
const tr = o => <tr {...o} {...omits} /> as any
|
|
235
|
+
const th = o => <th {...o} {...omits} /> as any
|
|
236
|
+
const td = o => <td {...o} {...omits} /> as any
|
|
237
|
+
|
|
238
|
+
return {
|
|
239
|
+
name: 'base',
|
|
240
|
+
priority: Infinity,
|
|
241
|
+
store: (store) => {
|
|
242
|
+
// 共享一个 ResizeObserver 观察所有 th,回调按 index 分发,替代每列独立 createElementSize
|
|
243
|
+
createResizeObserver(
|
|
244
|
+
() => store.ths?.filter(Boolean) as Element[] || [],
|
|
245
|
+
(_, el, e) => {
|
|
246
|
+
const { inlineSize: width, blockSize: height } = e.borderBoxSize[0]
|
|
247
|
+
const x = store.ths?.indexOf(el as HTMLElement)
|
|
248
|
+
if (x >= 0 && el.parentElement) store.thSizes[x] = { width, height }
|
|
249
|
+
}
|
|
250
|
+
)
|
|
251
|
+
// 共享一个 ResizeObserver 观察所有 tr,回调按 index 分发,替代每行独立 createElementSize
|
|
252
|
+
createResizeObserver(
|
|
253
|
+
() => store.trs?.filter(Boolean) as Element[] || [],
|
|
254
|
+
(_, el, e) => {
|
|
255
|
+
const { inlineSize: width, blockSize: height } = e.borderBoxSize[0]
|
|
256
|
+
const y = store.trs?.indexOf(el as HTMLElement)
|
|
257
|
+
if (y >= 0 && el.parentElement) store.trSizes[y] = { width, height }
|
|
258
|
+
}
|
|
259
|
+
)
|
|
260
|
+
return {
|
|
261
|
+
ths: [],
|
|
262
|
+
thSizes: [],
|
|
263
|
+
trs: [],
|
|
264
|
+
trSizes: [],
|
|
265
|
+
internal: Symbol('internal'),
|
|
266
|
+
raw: Symbol('raw'),
|
|
267
|
+
}
|
|
268
|
+
},
|
|
269
|
+
rewriteProps: {
|
|
270
|
+
data: ({ data = [] }) => data,
|
|
271
|
+
columns: ({ columns = [] }) => columns,
|
|
272
|
+
newRow: ({ newRow = () => ({}) }) => newRow,
|
|
273
|
+
Table: ({ Table = table }, { store }) => o => {
|
|
274
|
+
const [el, setEl] = createSignal<HTMLElement>()
|
|
275
|
+
const { props } = useContext(Ctx)
|
|
276
|
+
o = combineProps({
|
|
277
|
+
ref: setEl,
|
|
278
|
+
get class() { return `data-table ${props.class} ${props.border && 'data-table--border'} data-table--${props.size}` },
|
|
279
|
+
get style() { return props.style }
|
|
280
|
+
}, o)
|
|
281
|
+
return <Table {...o} />
|
|
282
|
+
},
|
|
283
|
+
Thead: ({ Thead = thead }, { store }) => o => {
|
|
284
|
+
o = combineProps({ ref: el => store.thead = el }, o)
|
|
285
|
+
return <Thead {...o} />
|
|
286
|
+
},
|
|
287
|
+
Tbody: ({ Tbody = tbody }, { store }) => o => {
|
|
288
|
+
o = combineProps({ ref: el => store.tbody = el }, o)
|
|
289
|
+
return <Tbody {...o} />
|
|
290
|
+
},
|
|
291
|
+
Tr: ({ Tr = tr }, { store }) => o => {
|
|
292
|
+
const [el, setEl] = createSignal<HTMLElement>()
|
|
293
|
+
o = combineProps({ ref: setEl }, o)
|
|
294
|
+
|
|
295
|
+
createEffect(() => {
|
|
296
|
+
const { y } = o
|
|
297
|
+
if (y == null) return
|
|
298
|
+
store.trs[y] = el()
|
|
299
|
+
onCleanup(() => { store.trSizes[y] = store.trs[y] = void 0 })
|
|
300
|
+
})
|
|
301
|
+
|
|
302
|
+
return <Tr {...o} />
|
|
303
|
+
},
|
|
304
|
+
Th: ({ Th = th }, { store }) => o => {
|
|
305
|
+
const [el, setEl] = createSignal<HTMLElement>()
|
|
306
|
+
|
|
307
|
+
const { props } = useContext(Ctx)
|
|
308
|
+
const mProps = combineProps(
|
|
309
|
+
o,
|
|
310
|
+
{ ref: setEl },
|
|
311
|
+
{ get class() { return unFn(props.cellClass, o) }, get style() { return unFn(props.cellStyle, o) } },
|
|
312
|
+
{ get class() { return o.col.class }, get style() { return o.col.style } },
|
|
313
|
+
{ get style() { return o.col.width ? `width: ${o.col.width}px` : '' } },
|
|
314
|
+
)
|
|
315
|
+
|
|
316
|
+
createEffect(() => {
|
|
317
|
+
if (o.covered) return
|
|
318
|
+
if ((o.colspan ?? 1) != 1) return
|
|
319
|
+
const { x } = o
|
|
320
|
+
store.ths[x] = el()
|
|
321
|
+
onCleanup(() => { store.thSizes[x] = store.ths[x] = void 0 })
|
|
322
|
+
})
|
|
323
|
+
|
|
324
|
+
return <Th {...mProps}>{o.children}</Th>
|
|
325
|
+
},
|
|
326
|
+
Td: ({ Td = td }, { store }) => o => {
|
|
327
|
+
const { props } = useContext(Ctx)
|
|
328
|
+
const mProps = combineProps(
|
|
329
|
+
o,
|
|
330
|
+
{ get class() { return unFn(props.cellClass, o) }, get style() { return unFn(props.cellStyle, o) } },
|
|
331
|
+
{ get class() { return o.col.class }, get style() { return o.col.style } },
|
|
332
|
+
{ get style() { return o.col.width ? `width: ${o.col.width}px` : '' } },
|
|
333
|
+
// todo
|
|
334
|
+
() => store.props.tdProps?.(o) || {}
|
|
335
|
+
)
|
|
336
|
+
return <Td {...mProps}>{o.children}</Td>
|
|
337
|
+
},
|
|
338
|
+
EachRows: ({ EachRows }) => EachRows || (o => <For each={o.each}>{(e, i) => o.children(() => e, i)}</For>),
|
|
339
|
+
EachCells: ({ EachCells }) => EachCells || (o => <For each={o.each}>{(e, i) => o.children(() => e, i)}</For>),
|
|
340
|
+
// EachRows: ({ EachRows }) => EachRows || (o => <Index each={o.each}>{(e, i) => o.children(e, () => i)}</Index>),
|
|
341
|
+
// EachCells: ({ EachCells }) => EachCells || (o => <Index each={o.each}>{(e, i) => o.children(e, () => i)}</Index>),
|
|
342
|
+
renderer: ({ renderer = a => a }) => renderer
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
const IndexPlugin: Plugin = {
|
|
348
|
+
name: 'index',
|
|
349
|
+
priority: -Infinity,
|
|
350
|
+
store: (store) => ({
|
|
351
|
+
$index: { name: '', id: Symbol('index'), fixed: 'left', [store.internal]: 1, width: 40, style: 'text-align: center', class: 'index', render: solidComponent((o) => <>{o.y + 1}</>) } as TableColumn
|
|
352
|
+
}),
|
|
353
|
+
rewriteProps: {
|
|
354
|
+
columns: ({ columns }, { store }) => store.props?.index ? [store.$index, ...columns || []] : columns
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
const StickyHeaderPlugin: Plugin = {
|
|
359
|
+
name: 'sticky-header',
|
|
360
|
+
rewriteProps: {
|
|
361
|
+
Thead: ({ Thead }) => o => {
|
|
362
|
+
const { props } = useContext(Ctx)
|
|
363
|
+
o = combineProps({ get class() { return props.stickyHeader ? 'sticky-header' : '' } }, o)
|
|
364
|
+
return <Thead {...o} />
|
|
365
|
+
},
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
const FixedColumnPlugin: Plugin$0 = store => {
|
|
370
|
+
const fixedOffsets = createLazyMemo(() => {
|
|
371
|
+
const offsets = {}
|
|
372
|
+
for (const [i, col] of store.props.columns.entries()) {
|
|
373
|
+
if (col.fixed === 'left') offsets[i] = sumBy(store.thSizes.slice(0, i), s => s?.width || 0)
|
|
374
|
+
if (col.fixed === 'right') offsets[i] = sumBy(store.thSizes.slice(i + 1), s => s?.width || 0)
|
|
375
|
+
}
|
|
376
|
+
return offsets
|
|
377
|
+
})
|
|
378
|
+
const last = createLazyMemo(() => store.props.columns.filter(e => e.fixed == 'left').length - 1)
|
|
379
|
+
const first = createLazyMemo(() => store.props.columns.length - store.props.columns.filter(e => e.fixed == 'right').length)
|
|
380
|
+
return {
|
|
381
|
+
name: 'fixed-column',
|
|
382
|
+
rewriteProps: {
|
|
383
|
+
columns: ({ columns }) => [
|
|
384
|
+
...columns?.filter(e => e.fixed == 'left') || [],
|
|
385
|
+
...columns?.filter(e => !e.fixed) || [],
|
|
386
|
+
...columns?.filter(e => e.fixed == 'right') || [],
|
|
387
|
+
],
|
|
388
|
+
cellClass: ({ cellClass }) => o => (unFn(cellClass, o) || '') + (o.col.fixed ? ` fixed-${o.col.fixed} ${o.x == last() ? 'is-last' : ''} ${o.x == first() ? 'is-first' : ''}` : ''),
|
|
389
|
+
cellStyle: ({ cellStyle }) => o => (unFn(cellStyle, o) || '') + (o.col.fixed ? `; ${o.col.fixed}: ${fixedOffsets()[o.x]}px` : '')
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
const FitColWidthPlugin: Plugin$0 = store => {
|
|
395
|
+
const size = createMutable({ width: 0 })
|
|
396
|
+
createResizeObserver(() => store.scroll_el!, (_, el, e) => size.width = e.contentBoxSize[0].inlineSize)
|
|
397
|
+
const __fit_col_width__cols_temp = createMutable([] as any[])
|
|
398
|
+
|
|
399
|
+
let lock = false
|
|
400
|
+
createEffect(on(() => [size.width, store.props.columns.map(e => e.width)], async () => {
|
|
401
|
+
if (!size.width) return
|
|
402
|
+
if (lock) return
|
|
403
|
+
__fit_col_width__cols_temp.length = 0
|
|
404
|
+
lock = true
|
|
405
|
+
await Promise.resolve()
|
|
406
|
+
const gap = (size.width - store.table.getBoundingClientRect().width) / store.props!.columns.filter(e => !e.width).length
|
|
407
|
+
const cols = store.props!.columns.map((e, i) => (e.width ? null : { width: Math.max((store.ths[i]?.getBoundingClientRect().width || 0) + gap, 80) }))
|
|
408
|
+
__fit_col_width__cols_temp.push(...cols)
|
|
409
|
+
lock = false
|
|
410
|
+
}))
|
|
411
|
+
return {
|
|
412
|
+
name: 'fit-col-width',
|
|
413
|
+
priority: -Infinity,
|
|
414
|
+
rewriteProps: {
|
|
415
|
+
columns: ({ columns }, { store }) => (
|
|
416
|
+
columns = columns.map((e, i) => ({ ...e, ...__fit_col_width__cols_temp?.[i], [store.raw]: e[store.raw] ?? e })),
|
|
417
|
+
untrack(() => batch(() => reconcile(columns, { key: store.raw })(store.__fit_col_width__cols ??= [])))
|
|
418
|
+
)
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
export const ScrollPlugin: Plugin = {
|
|
424
|
+
name: 'scroll',
|
|
425
|
+
priority: Infinity,
|
|
426
|
+
rewriteProps: {
|
|
427
|
+
Table: (prev, { store }) => o => {
|
|
428
|
+
const pos = createScrollPosition(() => store.scroll_el)
|
|
429
|
+
const size = createElementSize(() => store.scroll_el)
|
|
430
|
+
|
|
431
|
+
const clazz = createMemo(() => {
|
|
432
|
+
const el = store.scroll_el
|
|
433
|
+
if (!el) return
|
|
434
|
+
const isleft = pos.x == 0
|
|
435
|
+
const isright = pos.x >= el.scrollWidth - (size.width || 0)
|
|
436
|
+
return (
|
|
437
|
+
isleft && isright ? '' :
|
|
438
|
+
!isleft && !isright ? 'is-scroll-mid' :
|
|
439
|
+
isleft ? 'is-scroll-left' :
|
|
440
|
+
isright ? 'is-scroll-right' :
|
|
441
|
+
''
|
|
442
|
+
)
|
|
443
|
+
})
|
|
444
|
+
|
|
445
|
+
o = combineProps(o, { ref: el => store.scroll_el = el, class: 'data-table--scroll-view' }, { get class() { return clazz() } })
|
|
446
|
+
|
|
447
|
+
const layers = mapArray(() => store.plugins.flatMap(e => e.layers ?? []), Layer => <Layer {...store} />)
|
|
448
|
+
|
|
449
|
+
return (
|
|
450
|
+
<div tabindex={-1} {...o}>
|
|
451
|
+
<div class='data-table__layers'>
|
|
452
|
+
{layers()}
|
|
453
|
+
</div>
|
|
454
|
+
<table ref={el => store.table = el} class={`data-table--table`}>{o.children}</table>
|
|
455
|
+
</div>
|
|
456
|
+
)
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
export const defaultsPlugins = [
|
|
462
|
+
ScrollPlugin,
|
|
463
|
+
BasePlugin,
|
|
464
|
+
CommandPlugin,
|
|
465
|
+
MenuPlugin,
|
|
466
|
+
CellSelectionPlugin,
|
|
467
|
+
StickyHeaderPlugin,
|
|
468
|
+
HeaderGroupPlugin,
|
|
469
|
+
FixedColumnPlugin,
|
|
470
|
+
DragPlugin,
|
|
471
|
+
ClipboardPlugin,
|
|
472
|
+
ExpandPlugin,
|
|
473
|
+
RowSelectionPlugin,
|
|
474
|
+
IndexPlugin,
|
|
475
|
+
EditablePlugin,
|
|
476
|
+
CellMergePlugin,
|
|
477
|
+
TreePlugin,
|
|
478
|
+
FitColWidthPlugin,
|
|
479
|
+
RowGroupPlugin,
|
|
480
|
+
ResizePlugin,
|
|
481
|
+
]
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { createMemo, Show } from 'solid-js'
|
|
2
|
+
import { combineProps } from '@solid-primitives/props'
|
|
3
|
+
import { type Plugin, type TableColumn, type TableStore } from '../index'
|
|
4
|
+
|
|
5
|
+
declare module '../index' {
|
|
6
|
+
interface TableProps {
|
|
7
|
+
/**
|
|
8
|
+
* Per-cell merge config. Return `{ rowspan?, colspan? }` for an anchor cell.
|
|
9
|
+
* Covered cells are hidden automatically.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* merge={(row, col, y, x) => {
|
|
13
|
+
* if (col.id === 'name' && row.name === data[y - 1]?.name) return null
|
|
14
|
+
* if (col.id === 'name') {
|
|
15
|
+
* let rowspan = 1
|
|
16
|
+
* while (data[y + rowspan]?.name === row.name) rowspan++
|
|
17
|
+
* return rowspan > 1 ? { rowspan } : null
|
|
18
|
+
* }
|
|
19
|
+
* }}
|
|
20
|
+
*/
|
|
21
|
+
merge?: (row: any, col: TableColumn, y: number, x: number) => { rowspan?: number; colspan?: number } | null | void
|
|
22
|
+
}
|
|
23
|
+
interface TableColumn {
|
|
24
|
+
/**
|
|
25
|
+
* Shorthand: auto-merge consecutive rows in this column that share the same cell value.
|
|
26
|
+
* Equivalent to writing a `merge` function that compares adjacent row values.
|
|
27
|
+
*/
|
|
28
|
+
mergeRow?: boolean
|
|
29
|
+
}
|
|
30
|
+
interface TableStore {
|
|
31
|
+
/** Pre-computed merge result: anchor spans and covered cell keys. */
|
|
32
|
+
_mergeMap?: ReturnType<typeof createMemo<MergeMap>>
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
type CellKey = `${number},${number}`
|
|
37
|
+
interface MergeMap {
|
|
38
|
+
spans: Map<CellKey, { rowspan: number; colspan: number }>
|
|
39
|
+
covered: Set<CellKey>
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Build a { spans, covered } map in O(rows × cols) from the merge function. */
|
|
43
|
+
function buildMergeMap(
|
|
44
|
+
data: any[],
|
|
45
|
+
columns: TableColumn[],
|
|
46
|
+
merge: TableStore['props']['merge'],
|
|
47
|
+
): MergeMap {
|
|
48
|
+
const spans = new Map<CellKey, { rowspan: number; colspan: number }>()
|
|
49
|
+
const covered = new Set<CellKey>()
|
|
50
|
+
|
|
51
|
+
for (let y = 0; y < data.length; y++) {
|
|
52
|
+
for (let x = 0; x < columns.length; x++) {
|
|
53
|
+
const key: CellKey = `${y},${x}`
|
|
54
|
+
if (covered.has(key)) continue
|
|
55
|
+
|
|
56
|
+
const col = columns[x]
|
|
57
|
+
const row = data[y]
|
|
58
|
+
|
|
59
|
+
// Resolve span: explicit `merge` prop wins, then column.mergeRow shorthand
|
|
60
|
+
let rs = 1, cs = 1
|
|
61
|
+
if (merge) {
|
|
62
|
+
const r = merge(row, col, y, x)
|
|
63
|
+
if (r) { rs = r.rowspan ?? 1; cs = r.colspan ?? 1 }
|
|
64
|
+
} else if (col.mergeRow) {
|
|
65
|
+
// Count forward while consecutive rows have the equal value
|
|
66
|
+
while (y + rs < data.length && data[y + rs]?.[col.id] === row[col.id]) rs++
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (rs > 1 || cs > 1) {
|
|
70
|
+
spans.set(key, { rowspan: rs, colspan: cs })
|
|
71
|
+
for (let dy = 0; dy < rs; dy++) {
|
|
72
|
+
for (let dx = 0; dx < cs; dx++) {
|
|
73
|
+
if (dy === 0 && dx === 0) continue
|
|
74
|
+
covered.add(`${y + dy},${x + dx}`)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return { spans, covered }
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export const CellMergePlugin: Plugin = {
|
|
85
|
+
name: 'cell-merge',
|
|
86
|
+
rewriteProps: {
|
|
87
|
+
/**
|
|
88
|
+
* Build the merge map once per data/columns change inside the Table wrapper
|
|
89
|
+
* so it's available to every Td without redundant recomputation.
|
|
90
|
+
*/
|
|
91
|
+
Table: ({ Table }, { store }) => o => {
|
|
92
|
+
store._mergeMap ??= createMemo<MergeMap>(() => {
|
|
93
|
+
const { merge, data, columns } = store.props!
|
|
94
|
+
if ((!merge && !columns?.some(c => c.mergeRow)) || !data?.length || !columns?.length) {
|
|
95
|
+
return { spans: new Map(), covered: new Set() }
|
|
96
|
+
}
|
|
97
|
+
return buildMergeMap(data, columns, merge)
|
|
98
|
+
})
|
|
99
|
+
return <Table {...o} />
|
|
100
|
+
},
|
|
101
|
+
|
|
102
|
+
Td: ({ Td }, { store }) => o => {
|
|
103
|
+
const key = (): CellKey => `${o.y},${o.x}`
|
|
104
|
+
const isCovered = () => store._mergeMap?.().covered.has(key()) ?? false
|
|
105
|
+
const span = () => store._mergeMap?.().spans.get(key())
|
|
106
|
+
|
|
107
|
+
o = combineProps(o, {
|
|
108
|
+
get rowspan() { return span()?.rowspan },
|
|
109
|
+
get colspan() { return span()?.colspan },
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
return (
|
|
113
|
+
// Covered cells must not render any DOM node — HTML collapses the layout automatically
|
|
114
|
+
<Show when={!isCovered()}>
|
|
115
|
+
<Td {...o} />
|
|
116
|
+
</Show>
|
|
117
|
+
)
|
|
118
|
+
},
|
|
119
|
+
|
|
120
|
+
newRow: ({ newRow }, { store }) => function (i) {
|
|
121
|
+
const row = newRow(i)
|
|
122
|
+
const { data, columns, merge } = store.props!
|
|
123
|
+
if (!data?.length || !columns?.length) return row
|
|
124
|
+
|
|
125
|
+
columns.forEach((col, x) => {
|
|
126
|
+
// mergeRow shorthand: copy the value from the row at the insertion point
|
|
127
|
+
// so the new row automatically joins the same merge group.
|
|
128
|
+
if (col.mergeRow) {
|
|
129
|
+
const ref = data[i] ?? data[i - 1]
|
|
130
|
+
if (ref != null) row[col.id] ??= ref[col.id]
|
|
131
|
+
return
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Explicit merge function: if (i, x) is covered by a span, walk up to
|
|
135
|
+
// find the anchor row and inherit its cell value.
|
|
136
|
+
if (merge && store._mergeMap) {
|
|
137
|
+
const key: CellKey = `${i},${x}`
|
|
138
|
+
if (!store._mergeMap().covered.has(key) || !store._mergeMap().spans.has(key)) return
|
|
139
|
+
for (let ay = i; ay >= 0; ay--) {
|
|
140
|
+
const anchorKey: CellKey = `${ay},${x}`
|
|
141
|
+
const span = store._mergeMap().spans.get(anchorKey)
|
|
142
|
+
if (span && span.rowspan > 1) {
|
|
143
|
+
row[col.id] ??= data[ay]?.[col.id]
|
|
144
|
+
break
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
return row
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
}
|