-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
375 lines (344 loc) · 12.7 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import { ComponentPublicInstance } from 'vue'
import XEUtils from 'xe-utils'
import { VXETableCore, VxeGlobalInterceptorHandles, VxePagerPrivateMethods, VxePagerConstructor, VxePagerProps } from 'vxe-table'
/**
* 功能键
*/
export const enum FUNC_NANE {
/**
* 只对 edit-config 启用后有效,当单元格处于选中状态时,则进入编辑
*/
TABLE_EDIT_ACTIVED = 'table.edit.actived',
/**
* 只对 edit-config 启用后有效,当单元格处于激活状态时,则退出编辑
*/
TABLE_EDIT_CLOSED = 'table.edit.closed',
/**
* 只对 edit-config / mouse-config 启用后有效,当单元格处于激活状态或者选中状态,则移动到左侧单元格
*/
TABLE_EDIT_TAB_LEFT_MOVE = 'table.edit.tab.leftMove',
/**
* 只对 edit-config / mouse-config 启用后有效,当单元格处于激活状态或者选中状态,则移动到右侧单元格
*/
TABLE_EDIT_TAB_RIGHT_MOVE = 'table.edit.tab.rightMove',
/**
* 只对 edit-config / mouse-config 启用后有效,当单元格处于激活状态或者选中状态,则移动到上面单元格
*/
TABLE_EDIT_ENTER_UP_MOVE = 'table.edit.enter.upMove',
/**
* 只对 edit-config / mouse-config 启用后有效,当单元格处于激活状态或者选中状态,则移动到下面单元格
*/
TABLE_EDIT_ENTER_DOWN_MOVE = 'table.edit.enter.downMove',
/**
* 只对 mouse-config 启用后有效,当单元格处于选中状态,则移动到左边的单元格
*/
TABLE_CELL_LEFT_MOVE = 'table.cell.leftMove',
/**
* 只对 mouse-config 启用后有效,当单元格处于选中状态,则移动到上面的单元格
*/
TABLE_CELL_UP_MOVE = 'table.cell.upMove',
/**
* 只对 mouse-config 启用后有效,当单元格处于选中状态,则移动到右边的单元格
*/
TABLE_CELL_RIGHT_MOVE = 'table.cell.rightMove',
/**
* 只对 mouse-config 启用后有效,当单元格处于选中状态,则移动到下面的单元格
*/
TABLE_CELL_DOWN_MOVE = 'table.cell.downMove',
/**
* 只对 highlight-current-row 启用后有效,高亮行向上移动
*/
TABLE_ROW_CURRENT_TOP_MOVE = 'table.row.current.topMove',
/**
* 只对 highlight-current-row 启用后有效,高亮行向上移动
*/
TABLE_ROW_CURRENT_DOWN_MOVE = 'table.row.current.downMove',
/**
* 只对 pager-config 启用后有效,则进入上一页
*/
PAGER_PREV_PAGE = 'pager.prevPage',
/**
* 只对 pager-config 启用后有效,则进入下一页
*/
PAGER_NEXT_PAGE = 'pager.nextPage',
/**
* 只对 pager-config 启用后有效,则向上翻页
*/
PAGER_PREV_JUMP = 'pager.prevJump',
/**
* 只对 pager-config 启用后有效,则向下翻页
*/
PAGER_NEXT_JUMP = 'pager.nextJump'
}
export const enum SKEY_NANE {
TRIGGER = 'trigger',
EMIT = 'emit'
}
export interface ShortcutKeyConf {
key: string;
callback: Function
}
export class SKey {
realKey: string;
specialKey: string;
funcName?: FUNC_NANE;
kConf?: ShortcutKeyConf;
constructor (realKey: string, specialKey: string, funcName?: FUNC_NANE, kConf?: ShortcutKeyConf) {
this.realKey = realKey
this.specialKey = specialKey
this.funcName = funcName
this.kConf = kConf
}
[SKEY_NANE.TRIGGER] (params: VxeGlobalInterceptorHandles.InterceptorKeydownParams, evnt: any) {
if (!this.specialKey || evnt[`${this.specialKey}Key`]) {
if (this.funcName && handleFuncs[this.funcName]) {
return handleFuncs[this.funcName](params, evnt)
}
}
}
[SKEY_NANE.EMIT] (params: VxeGlobalInterceptorHandles.InterceptorKeydownParams, evnt: any) {
if (!this.specialKey || evnt[`${this.specialKey}Key`]) {
if (this.kConf && this.kConf.callback) {
return this.kConf.callback(params, evnt)
}
}
}
}
interface KeyStoreMaps {
[propName: string]: SKey[];
}
export interface ShortcutKeySettingConfig {
[funcName: string]: string;
}
export interface ShortcutKeyListenerConfig {
[funcName: string]: (params: VxeGlobalInterceptorHandles.InterceptorKeydownParams, evnt: Event) => any;
}
export interface ShortcutKeyOptions {
disabled?: string[] | ShortcutKeyConf[];
listener?: ShortcutKeyListenerConfig;
setting?: ShortcutKeySettingConfig;
}
const arrowKeys = 'right,up,left,down'.split(',')
const specialKeys = 'alt,ctrl,shift,meta'.split(',')
const settingMaps: KeyStoreMaps = {}
const listenerMaps: KeyStoreMaps = {}
const disabledMaps: KeyStoreMaps = {}
function getEventKey (key: string): string {
if (arrowKeys.indexOf(key.toLowerCase()) > -1) {
return `Arrow${key}`
}
return key
}
function isTriggerPage (params: VxeGlobalInterceptorHandles.InterceptorKeydownParams): boolean {
return !params.$table.getActiveRecord()
}
function handleChangePage (func: 'handlePrevPage' | 'handleNextPage' | 'handlePrevJump' | 'handleNextJump') {
return function (params: VxeGlobalInterceptorHandles.InterceptorKeydownParams, evnt: KeyboardEvent & { target: HTMLElement }) {
const { $grid, $table } = params
const { props: tableProps } = $table
const { mouseConfig } = tableProps
const { computeMouseOpts } = $table.getComputeMaps()
const mouseOpts = computeMouseOpts.value
if ($grid && mouseConfig && mouseOpts.selected !== true && ['input', 'textarea'].indexOf(evnt.target.tagName.toLowerCase()) === -1 && isTriggerPage(params)) {
const { refPager } = $grid.getRefMaps()
const $pager = refPager.value as ComponentPublicInstance<VxePagerProps, VxePagerConstructor & VxePagerPrivateMethods>
if ($pager) {
evnt.preventDefault()
$pager[func](evnt)
}
}
}
}
function handleCellTabMove (isLeft: boolean) {
return function (params: VxeGlobalInterceptorHandles.InterceptorKeydownParams, evnt: Event) {
const { $table } = params
const targetParams = $table.getActiveRecord() || $table.getSelectedCell()
if (targetParams) {
$table.moveTabSelected(targetParams, isLeft, evnt)
}
return false
}
}
function handleCellEnterMove (isTop: boolean) {
return function (params: VxeGlobalInterceptorHandles.InterceptorKeydownParams, evnt: Event) {
const { $table } = params
const targetParams = $table.getActiveRecord() || $table.getSelectedCell()
if (targetParams) {
$table.moveSelected(targetParams, false, !isTop, false, isTop, evnt)
}
return false
}
}
function handleCellMove (arrowIndex: number) {
return function (params: VxeGlobalInterceptorHandles.InterceptorKeydownParams, evnt: Event) {
const { $table } = params
const selecteParams = $table.getSelectedCell()
const arrows: number[] = [0, 0, 0, 0]
arrows[arrowIndex] = 1
if (selecteParams) {
$table.moveSelected(selecteParams, arrows[0], arrows[1], arrows[2], arrows[3], evnt)
return false
}
}
}
function handleCurrentRowMove (isDown: boolean) {
return function (params: VxeGlobalInterceptorHandles.InterceptorKeydownParams, evnt: Event) {
const { $table } = params
const { props: tableProps } = $table
const { highlightCurrentRow } = tableProps
if (highlightCurrentRow) {
const currentRow = $table.getCurrentRecord()
if (currentRow) {
$table.moveCurrentRow(!isDown, isDown, evnt)
return false
}
}
}
}
/**
* 快捷键处理方法
*/
export const handleFuncs = {
[FUNC_NANE.TABLE_EDIT_ACTIVED] (params: VxeGlobalInterceptorHandles.InterceptorKeydownParams, evnt: Event) {
const { $table } = params
const selected = $table.getSelectedCell()
if (selected) {
evnt.preventDefault()
$table.setActiveCell(selected.row, selected.column.field)
return false
}
},
[FUNC_NANE.TABLE_EDIT_CLOSED] (params: VxeGlobalInterceptorHandles.InterceptorKeydownParams, evnt: Event) {
const { $table } = params
const { props } = $table
const { mouseConfig, editConfig } = props
const { computeMouseOpts } = $table.getComputeMaps()
const mouseOpts = computeMouseOpts.value
if (editConfig) {
const actived = $table.getActiveRecord()
if (actived) {
evnt.preventDefault()
const cleatRest = $table.clearActived()
if (mouseConfig && mouseOpts.selected) {
cleatRest.then(() => $table.setSelectCell(actived.row, actived.column.field))
}
return false
}
}
},
[FUNC_NANE.TABLE_EDIT_TAB_RIGHT_MOVE]: handleCellTabMove(false),
[FUNC_NANE.TABLE_EDIT_TAB_LEFT_MOVE]: handleCellTabMove(true),
[FUNC_NANE.TABLE_EDIT_ENTER_UP_MOVE]: handleCellEnterMove(false),
[FUNC_NANE.TABLE_EDIT_ENTER_DOWN_MOVE]: handleCellEnterMove(true),
[FUNC_NANE.TABLE_CELL_LEFT_MOVE]: handleCellMove(0),
[FUNC_NANE.TABLE_CELL_UP_MOVE]: handleCellMove(1),
[FUNC_NANE.TABLE_CELL_RIGHT_MOVE]: handleCellMove(2),
[FUNC_NANE.TABLE_CELL_DOWN_MOVE]: handleCellMove(3),
[FUNC_NANE.TABLE_ROW_CURRENT_TOP_MOVE]: handleCurrentRowMove(false),
[FUNC_NANE.TABLE_ROW_CURRENT_DOWN_MOVE]: handleCurrentRowMove(true),
[FUNC_NANE.PAGER_PREV_PAGE]: handleChangePage('handlePrevPage'),
[FUNC_NANE.PAGER_NEXT_PAGE]: handleChangePage('handleNextPage'),
[FUNC_NANE.PAGER_PREV_JUMP]: handleChangePage('handlePrevJump'),
[FUNC_NANE.PAGER_NEXT_JUMP]: handleChangePage('handleNextJump')
}
function runEvent (key: string, maps: any, prop: SKEY_NANE, params: VxeGlobalInterceptorHandles.InterceptorKeydownParams, evnt: Event) {
const skeyList: SKey[] = maps[key.toLowerCase()]
if (skeyList) {
return !skeyList.some((skey: SKey) => skey[prop](params, evnt) === false)
}
}
interface parseKeyRest {
realKey: string;
specialKey: string;
}
function parseKeys (key: string): parseKeyRest {
let specialKey = ''
let realKey = ''
const keys = key.split('+')
keys.forEach((item) => {
item = item.toLowerCase().trim()
if (specialKeys.indexOf(item) > -1) {
specialKey = item
} else {
realKey = item
}
})
if (!realKey || keys.length > 2 || (keys.length === 2 && !specialKey)) {
throw new Error(`[vxe-table-plugin-shortcut-key] Invalid shortcut key configuration '${key}'.`)
}
return { realKey, specialKey }
}
function setKeyQueue (maps: KeyStoreMaps, kConf: ShortcutKeyConf, funcName?: FUNC_NANE) {
const { realKey, specialKey } = parseKeys(kConf.key)
let skeyList: SKey[] = maps[realKey]
if (!skeyList) {
skeyList = maps[realKey] = []
}
if (skeyList.some((skey) => skey.realKey === realKey && skey.specialKey === specialKey)) {
throw new Error(`[vxe-table-plugin-shortcut-key] Shortcut key conflict '${kConf.key}'.`)
}
skeyList.push(new SKey(realKey, specialKey, funcName, kConf))
}
function parseDisabledKey (options: ShortcutKeyOptions) {
XEUtils.each(options.disabled, (conf: string | ShortcutKeyConf) => {
const opts = XEUtils.isString(conf) ? { key: conf } : conf
setKeyQueue(disabledMaps, XEUtils.assign({ callback: () => false }, opts))
})
}
function parseSettingKey (options: ShortcutKeyOptions) {
XEUtils.each(options.setting, (opts: string | ShortcutKeySettingConfig, funcName: any) => {
const kConf: any = XEUtils.isString(opts) ? { key: opts } : opts
if (!handleFuncs[funcName as FUNC_NANE]) {
console.error(`[vxe-table-plugin-shortcut-key] '${funcName}' not exist.`)
}
setKeyQueue(settingMaps, kConf, funcName)
})
}
function parseListenerKey (options: ShortcutKeyOptions) {
XEUtils.each(options.listener, (callback: Function, key: string) => {
if (!XEUtils.isFunction(callback)) {
console.error(`[vxe-table-plugin-shortcut-key] '${key}' requires the callback function to be set.`)
}
setKeyQueue(listenerMaps, { key, callback })
})
}
/**
* 设置参数
* @param options 参数
*/
function pluginSetup (options: ShortcutKeyOptions) {
if (options) {
parseDisabledKey(options)
parseSettingKey(options)
parseListenerKey(options)
}
}
/**
* 基于 vxe-table 表格的扩展插件,为键盘操作提供快捷键设置
*/
export const VXETablePluginShortcutKey = {
setup: pluginSetup,
install (vxetable: VXETableCore, options?: ShortcutKeyOptions) {
// 检查版本
if (!/^(4)\./.test(vxetable.version) && !/v4/i.test((vxetable as any).v)) {
console.error('[vxe-table-plugin-shortcut-key 4.x] Version vxe-table 4.x is required')
}
if (options) {
pluginSetup(options)
}
vxetable.interceptor.add('event.keydown', (params) => {
const evnt = params.$event as KeyboardEvent
const key: string = getEventKey(evnt.key)
if (!runEvent(key, disabledMaps, SKEY_NANE.EMIT, params, evnt)) {
if (runEvent(key, settingMaps, SKEY_NANE.TRIGGER, params, evnt) === false) {
return false
}
runEvent(key, listenerMaps, SKEY_NANE.EMIT, params, evnt)
}
})
}
}
if (typeof window !== 'undefined' && window.VXETable && window.VXETable.use) {
window.VXETable.use(VXETablePluginShortcutKey)
}
export default VXETablePluginShortcutKey