From 56af0b04216314e538418be896d85f7be8bd1378 Mon Sep 17 00:00:00 2001 From: Hufe921 Date: Sun, 26 Jun 2022 13:46:31 +0800 Subject: [PATCH] feat:add replace image contextmenu --- src/editor/assets/css/index.css | 4 +++ src/editor/assets/images/image-change.svg | 1 + src/editor/core/command/Command.ts | 6 +++++ src/editor/core/command/CommandAdapt.ts | 15 ++++++++++- .../core/contextmenu/menus/imageMenus.ts | 26 ++++++++++++++++++- 5 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 src/editor/assets/images/image-change.svg diff --git a/src/editor/assets/css/index.css b/src/editor/assets/css/index.css index 2d9bf80..eb68cf1 100644 --- a/src/editor/assets/css/index.css +++ b/src/editor/assets/css/index.css @@ -378,6 +378,10 @@ background-image: url(../../assets/images/image.svg); } +.contextmenu-image-change { + background-image: url(../../assets/images/image-change.svg); +} + .contextmenu-insert-row-col { background-image: url(../../assets/images/insert-row-col.svg); } diff --git a/src/editor/assets/images/image-change.svg b/src/editor/assets/images/image-change.svg new file mode 100644 index 0000000..04075f9 --- /dev/null +++ b/src/editor/assets/images/image-change.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/editor/core/command/Command.ts b/src/editor/core/command/Command.ts index 6e933b2..07d1cf8 100644 --- a/src/editor/core/command/Command.ts +++ b/src/editor/core/command/Command.ts @@ -55,6 +55,7 @@ export class Command { private static search: Function private static replace: Function private static print: Function + private static replaceImageElement: Function private static saveAsImageElement: Function private static getImage: Function private static getValue: Function @@ -112,6 +113,7 @@ export class Command { Command.search = adapt.search.bind(adapt) Command.replace = adapt.replace.bind(adapt) Command.print = adapt.print.bind(adapt) + Command.replaceImageElement = adapt.replaceImageElement.bind(adapt) Command.saveAsImageElement = adapt.saveAsImageElement.bind(adapt) Command.getImage = adapt.getImage.bind(adapt) Command.getValue = adapt.getValue.bind(adapt) @@ -311,6 +313,10 @@ export class Command { return Command.print() } + public executeReplaceImageElement(payload: string) { + return Command.replaceImageElement(payload) + } + public executeSaveAsImageElement() { return Command.saveAsImageElement() } diff --git a/src/editor/core/command/CommandAdapt.ts b/src/editor/core/command/CommandAdapt.ts index e6df51a..efadde6 100644 --- a/src/editor/core/command/CommandAdapt.ts +++ b/src/editor/core/command/CommandAdapt.ts @@ -1238,12 +1238,25 @@ export class CommandAdapt { } } + public replaceImageElement(payload: string) { + const { startIndex } = this.range.getRange() + const elementList = this.draw.getElementList() + const element = elementList[startIndex] + if (!element || element.type !== ElementType.IMAGE) return + // 替换图片 + element.id = getUUID() + element.value = payload + this.draw.render({ + isSetCursor: false + }) + } + public saveAsImageElement() { const { startIndex } = this.range.getRange() const elementList = this.draw.getElementList() const element = elementList[startIndex] if (!element || element.type !== ElementType.IMAGE) return - downloadFile(element.url!, `${element.id!}.png`) + downloadFile(element.value, `${element.id!}.png`) } public getImage(): string[] { diff --git a/src/editor/core/contextmenu/menus/imageMenus.ts b/src/editor/core/contextmenu/menus/imageMenus.ts index 2bd2fca..adbc0f1 100644 --- a/src/editor/core/contextmenu/menus/imageMenus.ts +++ b/src/editor/core/contextmenu/menus/imageMenus.ts @@ -3,6 +3,30 @@ import { IRegisterContextMenu } from '../../../interface/contextmenu/ContextMenu import { Command } from '../../command/Command' export const imageMenus: IRegisterContextMenu[] = [ + { + name: '更改图片', + icon: 'image-change', + when: (payload) => { + return !payload.editorHasSelection && payload.startElement?.type === ElementType.IMAGE + }, + callback: (command: Command) => { + // 创建代理元素 + const proxyInputFile = document.createElement('input') + proxyInputFile.type = 'file' + proxyInputFile.accept = '.png, .jpg, .jpeg' + // 监听上传 + proxyInputFile.onchange = () => { + const file = proxyInputFile.files![0]! + const fileReader = new FileReader() + fileReader.readAsDataURL(file) + fileReader.onload = () => { + const value = fileReader.result as string + command.executeReplaceImageElement(value) + } + } + proxyInputFile.click() + } + }, { name: '另存为图片', icon: 'image', @@ -12,5 +36,5 @@ export const imageMenus: IRegisterContextMenu[] = [ callback: (command: Command) => { command.executeSaveAsImageElement() } - }, + } ] \ No newline at end of file