diff --git a/src/editor/core/command/Command.ts b/src/editor/core/command/Command.ts index 4f6b7d1..2aeff15 100644 --- a/src/editor/core/command/Command.ts +++ b/src/editor/core/command/Command.ts @@ -5,6 +5,7 @@ export class Command { private static undo: Function private static redo: Function private static format: Function + private static bold: Function private static print: Function constructor(adapt: CommandAdapt) { @@ -12,6 +13,7 @@ export class Command { Command.redo = adapt.redo.bind(adapt) Command.print = adapt.print.bind(adapt) Command.format = adapt.format.bind(adapt) + Command.bold = adapt.bold.bind(adapt) } // 撤销、重做、格式刷、清除格式 @@ -28,6 +30,9 @@ export class Command { } // 字体变大、字体变小、加粗、斜体、下划线、删除线、字体颜色、背景色 + public executeBold() { + return Command.bold() + } // 搜索、打印 public executePrint() { diff --git a/src/editor/core/command/CommandAdapt.ts b/src/editor/core/command/CommandAdapt.ts index 3ac6ee2..df97d9d 100644 --- a/src/editor/core/command/CommandAdapt.ts +++ b/src/editor/core/command/CommandAdapt.ts @@ -27,7 +27,7 @@ export class CommandAdapt { const { startIndex, endIndex } = this.range.getRange() if (startIndex === endIndex) return const elementList = this.draw.getElementList() - elementList.slice(startIndex, endIndex) + elementList.slice(startIndex + 1, endIndex + 1) .forEach(el => { el.font = '' el.color = '' @@ -39,6 +39,17 @@ export class CommandAdapt { this.draw.render({ isSetCursor: false }) } + public bold() { + const { startIndex, endIndex } = this.range.getRange() + if (startIndex === endIndex) return + const elementList = this.draw.getElementList() + elementList.slice(startIndex + 1, endIndex + 1) + .forEach(el => { + el.bold = true + }) + this.draw.render({ isSetCursor: false }) + } + public print() { return printImageBase64(this.draw.getDataURL()) } diff --git a/src/main.ts b/src/main.ts index e3675c1..564f561 100644 --- a/src/main.ts +++ b/src/main.ts @@ -60,7 +60,10 @@ window.onload = function () { } // 字体变大、字体变小、加粗、斜体、下划线、删除线、字体颜色、背景色 - + document.querySelector('.menu-item__bold')!.onclick = function () { + console.log('bold') + instance.command.executeBold() + } // 搜索、打印 document.querySelector('.menu-item__print')!.onclick = function () {