From 0a21a0bbf5024a73343212ac6c72adf941f9089e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E4=BA=91=E9=A3=9E?= Date: Mon, 15 Nov 2021 20:18:49 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=A2=9E=E5=8A=A0=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E9=80=82=E9=85=8D=E5=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/editor/core/command/Command.ts | 24 +++++++------ src/editor/core/command/CommandAdapt.ts | 46 +++++++++++++++++++++++++ src/editor/index.ts | 3 +- src/main.ts | 4 +++ 4 files changed, 66 insertions(+), 11 deletions(-) create mode 100644 src/editor/core/command/CommandAdapt.ts diff --git a/src/editor/core/command/Command.ts b/src/editor/core/command/Command.ts index 6b281ae..4f6b7d1 100644 --- a/src/editor/core/command/Command.ts +++ b/src/editor/core/command/Command.ts @@ -1,17 +1,17 @@ -import { printImageBase64 } from "../../utils/print" -import { Draw } from "../draw/Draw" +import { CommandAdapt } from "./CommandAdapt" export class Command { private static undo: Function private static redo: Function - private static getDataURL: Function - - constructor(draw: Draw) { - const historyManager = draw.getHistoryManager() - Command.undo = historyManager.undo.bind(historyManager) - Command.redo = historyManager.redo.bind(historyManager) - Command.getDataURL = draw.getDataURL.bind(draw) + private static format: Function + private static print: Function + + constructor(adapt: CommandAdapt) { + Command.undo = adapt.undo.bind(adapt) + Command.redo = adapt.redo.bind(adapt) + Command.print = adapt.print.bind(adapt) + Command.format = adapt.format.bind(adapt) } // 撤销、重做、格式刷、清除格式 @@ -23,11 +23,15 @@ export class Command { return Command.redo() } + public executeFormat() { + return Command.format() + } + // 字体变大、字体变小、加粗、斜体、下划线、删除线、字体颜色、背景色 // 搜索、打印 public executePrint() { - return printImageBase64(Command.getDataURL()) + return Command.print() } } \ No newline at end of file diff --git a/src/editor/core/command/CommandAdapt.ts b/src/editor/core/command/CommandAdapt.ts new file mode 100644 index 0000000..3ac6ee2 --- /dev/null +++ b/src/editor/core/command/CommandAdapt.ts @@ -0,0 +1,46 @@ +import { printImageBase64 } from "../../utils/print" +import { Draw } from "../draw/Draw" +import { HistoryManager } from "../history/HistoryManager" +import { RangeManager } from "../range/RangeManager" + +export class CommandAdapt { + + private draw: Draw + private range: RangeManager + private historyManager: HistoryManager + + constructor(draw: Draw) { + this.draw = draw + this.range = draw.getRange() + this.historyManager = draw.getHistoryManager() + } + + public undo() { + return this.historyManager.undo() + } + + public redo() { + return this.historyManager.redo() + } + + public format() { + const { startIndex, endIndex } = this.range.getRange() + if (startIndex === endIndex) return + const elementList = this.draw.getElementList() + elementList.slice(startIndex, endIndex) + .forEach(el => { + el.font = '' + el.color = '' + el.bold = false + el.italic = false + el.underline = false + el.strikeout = false + }) + this.draw.render({ isSetCursor: false }) + } + + public print() { + return printImageBase64(this.draw.getDataURL()) + } + +} \ No newline at end of file diff --git a/src/editor/index.ts b/src/editor/index.ts index 85b210b..73e8a2e 100644 --- a/src/editor/index.ts +++ b/src/editor/index.ts @@ -4,6 +4,7 @@ import { IEditorOption } from './interface/Editor' import { IElement } from './interface/Element' import { Draw } from './core/draw/Draw' import { Command } from './core/command/Command' +import { CommandAdapt } from './core/command/CommandAdapt' export default class Editor { @@ -41,7 +42,7 @@ export default class Editor { const draw = new Draw(canvas, ctx, editorOptions, elementList) draw.render() // 对外命令 - this.command = new Command(draw) + this.command = new Command(new CommandAdapt(draw)) } } \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index fd8d2b0..e3675c1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -54,6 +54,10 @@ window.onload = function () { console.log('redo') instance.command.executeRedo() } + document.querySelector('.menu-item__format')!.onclick = function () { + console.log('format') + instance.command.executeFormat() + } // 字体变大、字体变小、加粗、斜体、下划线、删除线、字体颜色、背景色