From d95153299880ef7139d3a2d64d7d3dc16fbef615 Mon Sep 17 00:00:00 2001 From: zhoujingfu <1751218699@qq.com> Date: Tue, 21 Mar 2023 10:00:01 +0800 Subject: [PATCH] feat: add fontSize settings API --- src/editor/core/command/Command.ts | 8 +++++++- src/editor/core/command/CommandAdapt.ts | 14 ++++++++++++++ src/editor/core/range/RangeManager.ts | 4 ++++ src/editor/interface/Listener.ts | 1 + 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/editor/core/command/Command.ts b/src/editor/core/command/Command.ts index 062afb8..10b5b41 100644 --- a/src/editor/core/command/Command.ts +++ b/src/editor/core/command/Command.ts @@ -22,6 +22,7 @@ export class Command { private static applyPainterStyle: CommandAdapt['applyPainterStyle'] private static format: CommandAdapt['format'] private static font: CommandAdapt['font'] + private static sizeSet: CommandAdapt['sizeSet'] private static sizeAdd: CommandAdapt['sizeAdd'] private static sizeMinus: CommandAdapt['sizeMinus'] private static bold: CommandAdapt['bold'] @@ -95,6 +96,7 @@ export class Command { Command.applyPainterStyle = adapt.applyPainterStyle.bind(adapt) Command.format = adapt.format.bind(adapt) Command.font = adapt.font.bind(adapt) + Command.sizeSet = adapt.sizeSet.bind(adapt) Command.sizeAdd = adapt.sizeAdd.bind(adapt) Command.sizeMinus = adapt.sizeMinus.bind(adapt) Command.bold = adapt.bold.bind(adapt) @@ -205,11 +207,15 @@ export class Command { return Command.format() } - // 字体、字体变大、字体变小、加粗、斜体、下划线、删除线、字体颜色、背景色 + // 字体、字体大小、字体变大、字体变小、加粗、斜体、下划线、删除线、字体颜色、背景色 public executeFont(payload: string) { return Command.font(payload) } + public executeSize(size: number) { + return Command.sizeSet(size) + } + public executeSizeAdd() { return Command.sizeAdd() } diff --git a/src/editor/core/command/CommandAdapt.ts b/src/editor/core/command/CommandAdapt.ts index 2b32b27..ef5a3c9 100644 --- a/src/editor/core/command/CommandAdapt.ts +++ b/src/editor/core/command/CommandAdapt.ts @@ -185,6 +185,20 @@ export class CommandAdapt { this.draw.render({ isSetCursor: false }) } + public sizeSet(size: number) { + const isReadonly = this.draw.isReadonly() + if (isReadonly) return + const selection = this.range.getSelection() + if (!selection) return + const lessThanMaxSizeIndex = selection.findIndex(s => !s.size || s.size + 2 <= 72) + if (!~lessThanMaxSizeIndex) return + selection.forEach(el => { + if (size > 72) return + el.size = size + }) + this.draw.render({ isSetCursor: false }) + } + public sizeAdd() { const isReadonly = this.draw.isReadonly() if (isReadonly) return diff --git a/src/editor/core/range/RangeManager.ts b/src/editor/core/range/RangeManager.ts index 15da647..04d6f09 100644 --- a/src/editor/core/range/RangeManager.ts +++ b/src/editor/core/range/RangeManager.ts @@ -135,6 +135,7 @@ export class RangeManager { const type = curElement.type || ElementType.TEXT // 富文本 const font = curElement.font || this.options.defaultFont + const size = curElement.size || this.options.defaultSize const bold = !~curElementList.findIndex(el => !el.bold) const italic = !~curElementList.findIndex(el => !el.italic) const underline = !~curElementList.findIndex(el => !el.underline) @@ -154,6 +155,7 @@ export class RangeManager { redo, painter, font, + size, bold, italic, underline, @@ -169,6 +171,7 @@ export class RangeManager { public recoveryRangeStyle() { if (!this.listener.rangeStyleChange) return const font = this.options.defaultFont + const size = this.options.defaultSize const rowMargin = this.options.defaultRowMargin const painter = !!this.draw.getPainterStyle() const undo = this.historyManager.isCanUndo() @@ -179,6 +182,7 @@ export class RangeManager { redo, painter, font, + size, bold: false, italic: false, underline: false, diff --git a/src/editor/interface/Listener.ts b/src/editor/interface/Listener.ts index f822a85..f8f1af2 100644 --- a/src/editor/interface/Listener.ts +++ b/src/editor/interface/Listener.ts @@ -9,6 +9,7 @@ export interface IRangeStyle { redo: boolean; painter: boolean; font: string; + size: number; bold: boolean; italic: boolean; underline: boolean;