From 236d28ef18116bc51583c66141691c6943144d15 Mon Sep 17 00:00:00 2001 From: Hufe921 Date: Sun, 10 Jul 2022 15:20:09 +0800 Subject: [PATCH] feat:add control context menu --- src/editor/core/command/Command.ts | 6 ++++++ src/editor/core/command/CommandAdapt.ts | 16 ++++++++++++++++ src/editor/core/contextmenu/ContextMenu.ts | 4 +++- .../core/contextmenu/menus/controlMenus.ts | 15 +++++++++++++++ 4 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 src/editor/core/contextmenu/menus/controlMenus.ts diff --git a/src/editor/core/command/Command.ts b/src/editor/core/command/Command.ts index 07d1cf8..1a48616 100644 --- a/src/editor/core/command/Command.ts +++ b/src/editor/core/command/Command.ts @@ -65,6 +65,7 @@ export class Command { private static pageScaleMinus: Function private static pageScaleAdd: Function private static insertElementList: Function + private static removeControl: Function constructor(adapt: CommandAdapt) { Command.mode = adapt.mode.bind(adapt) @@ -123,6 +124,7 @@ export class Command { Command.pageScaleMinus = adapt.pageScaleMinus.bind(adapt) Command.pageScaleAdd = adapt.pageScaleAdd.bind(adapt) Command.insertElementList = adapt.insertElementList.bind(adapt) + Command.removeControl = adapt.removeControl.bind(adapt) } // 全局命令 @@ -355,4 +357,8 @@ export class Command { return Command.insertElementList(payload) } + public executeRemoveControl() { + return Command.removeControl() + } + } \ No newline at end of file diff --git a/src/editor/core/command/CommandAdapt.ts b/src/editor/core/command/CommandAdapt.ts index efadde6..5c04925 100644 --- a/src/editor/core/command/CommandAdapt.ts +++ b/src/editor/core/command/CommandAdapt.ts @@ -1305,4 +1305,20 @@ export class CommandAdapt { this.draw.insertElementList(payload) } + public removeControl() { + const { startIndex, endIndex } = this.range.getRange() + if (startIndex !== endIndex) return + const elementList = this.draw.getElementList() + const element = elementList[startIndex] + if (element.type !== ElementType.CONTROL) return + // 删除控件 + const control = this.draw.getControl() + const newIndex = control.removeControl(startIndex) + // 重新渲染 + this.range.setRange(newIndex, newIndex) + this.draw.render({ + curIndex: newIndex + }) + } + } \ No newline at end of file diff --git a/src/editor/core/contextmenu/ContextMenu.ts b/src/editor/core/contextmenu/ContextMenu.ts index d231395..68bef9a 100644 --- a/src/editor/core/contextmenu/ContextMenu.ts +++ b/src/editor/core/contextmenu/ContextMenu.ts @@ -6,6 +6,7 @@ import { Command } from '../command/Command' import { Draw } from '../draw/Draw' import { Position } from '../position/Position' import { RangeManager } from '../range/RangeManager' +import { controlMenus } from './menus/controlMenus' import { globalMenus } from './menus/globalMenus' import { imageMenus } from './menus/imageMenus' import { tableMenus } from './menus/tableMenus' @@ -38,7 +39,8 @@ export class ContextMenu { this.contextMenuList = [ ...globalMenus, ...tableMenus, - ...imageMenus + ...imageMenus, + ...controlMenus ] this.contextMenuContainerList = [] this.contextMenuRelationShip = new Map() diff --git a/src/editor/core/contextmenu/menus/controlMenus.ts b/src/editor/core/contextmenu/menus/controlMenus.ts new file mode 100644 index 0000000..76497b2 --- /dev/null +++ b/src/editor/core/contextmenu/menus/controlMenus.ts @@ -0,0 +1,15 @@ +import { ElementType } from '../../../dataset/enum/Element' +import { IRegisterContextMenu } from '../../../interface/contextmenu/ContextMenu' +import { Command } from '../../command/Command' + +export const controlMenus: IRegisterContextMenu[] = [ + { + name: '删除控件', + when: (payload) => { + return !payload.editorHasSelection && payload.startElement?.type === ElementType.CONTROL + }, + callback: (command: Command) => { + command.executeRemoveControl() + } + } +] \ No newline at end of file