feat:add replace image contextmenu
This commit is contained in:
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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[] {
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
},
|
||||
}
|
||||
]
|
||||
Reference in New Issue
Block a user