export class KeyEventManager {
	stateManager: any
	constructor(options) {
		this.stateManager = options.stateManager;
		this._handleKeyDown = this.handleKeyDown.bind(this)
	}
	onMounted() { }

	/** 处理键盘事件 */
	_handleKeyDown: any
	handleKeyDown(event: any) {
		const activeID = this.stateManager.layerManager.activeID.value
		const ctrl = event.ctrlKey ? 'ctrl-' : "";
		const shift = event.shiftKey ? 'shift-' : "";
		const key = event.key;
		const reg = new RegExp(`^${ctrl}${shift}${key}$`, 'i')
		const list = [
			{ key: "ctrl-c", handler: () => this.stateManager.layerManager.copyLayerById(activeID) },
			{ key: "delete", handler: () => this.stateManager.layerManager.deleteLayerById(activeID) },
			{ key: "ctrl-z", handler: () => this.stateManager.undoState() },
			{ key: "ctrl-shift-z", handler: () => this.stateManager.redoState() },
		]
		list.forEach((v: any) => {
			if (reg.test(v.key)) v.handler(event)
		})
	}
	/** 注册事件 */
	registerEvents() {
		document.addEventListener('keydown', this._handleKeyDown)
	}
	/** 删除事件 */
	removeEvents() {
		document.removeEventListener('keydown', this._handleKeyDown)
	}
	dispose() {
		this.removeEvents()
	}
}
