parent
19bb68253c
commit
adf64865bd
@ -1,17 +1,22 @@
|
||||
import { IRegisterContextMenu } from '../../interface/contextmenu/ContextMenu'
|
||||
import { IRegisterShortcut } from '../../interface/shortcut/Shortcut'
|
||||
import { ContextMenu } from '../contextmenu/ContextMenu'
|
||||
import { Shortcut } from '../shortcut/Shortcut'
|
||||
|
||||
interface IRegisterPayload {
|
||||
contextMenu: ContextMenu
|
||||
contextMenu: ContextMenu;
|
||||
shortcut: Shortcut;
|
||||
}
|
||||
|
||||
export class Register {
|
||||
|
||||
public contextMenuList: (payload: IRegisterContextMenu[]) => void
|
||||
public shortcutList: (payload: IRegisterShortcut[]) => void
|
||||
|
||||
constructor(payload: IRegisterPayload) {
|
||||
const { contextMenu } = payload
|
||||
const { contextMenu, shortcut } = payload
|
||||
this.contextMenuList = contextMenu.registerContextMenuList.bind(contextMenu)
|
||||
this.shortcutList = shortcut.registerShortcutList.bind(shortcut)
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,68 @@
|
||||
import { richtextKeys } from '../../interface/shortcut/keys/richtextKeys'
|
||||
import { IRegisterShortcut } from '../../interface/shortcut/Shortcut'
|
||||
import { Command } from '../command/Command'
|
||||
import { Draw } from '../draw/Draw'
|
||||
|
||||
export class Shortcut {
|
||||
|
||||
private command: Command
|
||||
private globalShortcutList: IRegisterShortcut[]
|
||||
private agentShortcutList: IRegisterShortcut[]
|
||||
|
||||
constructor(draw: Draw, command: Command) {
|
||||
this.command = command
|
||||
this.globalShortcutList = []
|
||||
this.agentShortcutList = []
|
||||
// 内部快捷键
|
||||
this._addShortcutList([
|
||||
...richtextKeys
|
||||
])
|
||||
// 全局快捷键
|
||||
document.addEventListener('keydown', this._globalKeydown.bind(this))
|
||||
// 编辑器快捷键
|
||||
const agentDom = draw.getCursor().getAgentDom()
|
||||
agentDom.addEventListener('keydown', this._agentKeydown.bind(this))
|
||||
}
|
||||
|
||||
private _addShortcutList(payload: IRegisterShortcut[]) {
|
||||
for (let s = 0; s < payload.length; s++) {
|
||||
const shortCut = payload[s]
|
||||
if (shortCut.isGlobal) {
|
||||
this.globalShortcutList.push(shortCut)
|
||||
} else {
|
||||
this.agentShortcutList.push(shortCut)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public registerShortcutList(payload: IRegisterShortcut[]) {
|
||||
this._addShortcutList(payload)
|
||||
}
|
||||
|
||||
private _globalKeydown(evt: KeyboardEvent) {
|
||||
if (!this.globalShortcutList.length) return
|
||||
this._execute(evt, this.globalShortcutList)
|
||||
}
|
||||
|
||||
private _agentKeydown(evt: KeyboardEvent) {
|
||||
if (!this.agentShortcutList.length) return
|
||||
this._execute(evt, this.agentShortcutList)
|
||||
}
|
||||
|
||||
private _execute(evt: KeyboardEvent, shortCutList: IRegisterShortcut[]) {
|
||||
for (let s = 0; s < shortCutList.length; s++) {
|
||||
const shortCut = shortCutList[s]
|
||||
if (
|
||||
evt.ctrlKey === !!shortCut.ctrl &&
|
||||
evt.shiftKey === !!shortCut.shift &&
|
||||
evt.altKey === !!shortCut.alt &&
|
||||
evt.key === shortCut.key
|
||||
) {
|
||||
shortCut.callback(this.command)
|
||||
evt.preventDefault()
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
import { Command } from '../../core/command/Command'
|
||||
import { KeyMap } from '../../dataset/enum/KeyMap'
|
||||
|
||||
export interface IRegisterShortcut {
|
||||
key: KeyMap;
|
||||
ctrl?: boolean;
|
||||
shift?: boolean;
|
||||
alt?: boolean;
|
||||
isGlobal?: boolean;
|
||||
callback: (command: Command) => any;
|
||||
}
|
||||
@ -0,0 +1,93 @@
|
||||
import { Command } from '../../..'
|
||||
import { KeyMap } from '../../../dataset/enum/KeyMap'
|
||||
import { IRegisterShortcut } from '../Shortcut'
|
||||
|
||||
export const richtextKeys: IRegisterShortcut[] = [
|
||||
{
|
||||
key: KeyMap.X_UPPERCASE,
|
||||
ctrl: true,
|
||||
shift: true,
|
||||
callback: (command: Command) => {
|
||||
command.executeStrikeout()
|
||||
}
|
||||
},
|
||||
{
|
||||
key: KeyMap.LEFT_BRACKET,
|
||||
ctrl: true,
|
||||
callback: (command: Command) => {
|
||||
command.executeSizeAdd()
|
||||
}
|
||||
},
|
||||
{
|
||||
key: KeyMap.RIGHT_BRACKET,
|
||||
ctrl: true,
|
||||
callback: (command: Command) => {
|
||||
command.executeSizeMinus()
|
||||
}
|
||||
},
|
||||
{
|
||||
key: KeyMap.B,
|
||||
ctrl: true,
|
||||
callback: (command: Command) => {
|
||||
command.executeBold()
|
||||
}
|
||||
},
|
||||
{
|
||||
key: KeyMap.I,
|
||||
ctrl: true,
|
||||
callback: (command: Command) => {
|
||||
command.executeItalic()
|
||||
}
|
||||
},
|
||||
{
|
||||
key: KeyMap.U,
|
||||
ctrl: true,
|
||||
callback: (command: Command) => {
|
||||
command.executeUnderline()
|
||||
}
|
||||
},
|
||||
{
|
||||
key: KeyMap.RIGHT_ANGLE_BRACKET,
|
||||
ctrl: true,
|
||||
shift: true,
|
||||
callback: (command: Command) => {
|
||||
command.executeSuperscript()
|
||||
}
|
||||
},
|
||||
{
|
||||
key: KeyMap.LEFT_ANGLE_BRACKET,
|
||||
ctrl: true,
|
||||
shift: true,
|
||||
callback: (command: Command) => {
|
||||
command.executeSubscript()
|
||||
}
|
||||
},
|
||||
{
|
||||
key: KeyMap.L,
|
||||
ctrl: true,
|
||||
callback: (command: Command) => {
|
||||
command.executeLeft()
|
||||
}
|
||||
},
|
||||
{
|
||||
key: KeyMap.E,
|
||||
ctrl: true,
|
||||
callback: (command: Command) => {
|
||||
command.executeCenter()
|
||||
}
|
||||
},
|
||||
{
|
||||
key: KeyMap.R,
|
||||
ctrl: true,
|
||||
callback: (command: Command) => {
|
||||
command.executeRight()
|
||||
}
|
||||
},
|
||||
{
|
||||
key: KeyMap.J,
|
||||
ctrl: true,
|
||||
callback: (command: Command) => {
|
||||
command.executeAlignment()
|
||||
}
|
||||
}
|
||||
]
|
||||
Loading…
Reference in new issue