From a324ecc417fd2993ecdc22fc6d4299178d27de60 Mon Sep 17 00:00:00 2001 From: Hufe921 Date: Fri, 12 Jan 2024 23:08:30 +0800 Subject: [PATCH] feat: add defaultColor option #405 --- docs/en/guide/option.md | 1 + docs/guide/option.md | 1 + src/editor/core/draw/particle/TextParticle.ts | 10 ++++++---- src/editor/index.ts | 1 + src/editor/interface/Editor.ts | 1 + 5 files changed, 10 insertions(+), 4 deletions(-) diff --git a/docs/en/guide/option.md b/docs/en/guide/option.md index 434c85b..a11904e 100644 --- a/docs/en/guide/option.md +++ b/docs/en/guide/option.md @@ -16,6 +16,7 @@ new Editor(container, IEditorData | IElement[], { interface IEditorOption { mode?: EditorMode // Editor mode: Edit, Clean (Visual aids are not displayed, For example: page break), ReadOnly, Form (Only editable within the control), Print (Visual aids are not displayed, Unwritten content control). default: Edit defaultType?: string // Default element type. default: TEXT + defaultColor?: string // Default color. default: #000000 defaultFont?: string // Default font. default: Microsoft YaHei defaultSize?: number // Default font size. default: 16 minSize?: number // Min font size。default: 5 diff --git a/docs/guide/option.md b/docs/guide/option.md index 0d0c858..ea17b3b 100644 --- a/docs/guide/option.md +++ b/docs/guide/option.md @@ -16,6 +16,7 @@ new Editor(container, IEditorData | IElement[], { interface IEditorOption { mode?: EditorMode // 编辑器模式:编辑、清洁(不显示视觉辅助元素。如:分页符)、只读、表单(仅控件内可编辑)、打印(不显示辅助元素、未书写控件及前后括号)。默认:编辑 defaultType?: string // 默认元素类型。默认:TEXT + defaultColor?: string // 默认字体颜色。默认:#000000 defaultFont?: string // 默认字体。默认:Microsoft YaHei defaultSize?: number // 默认字号。默认:16 minSize?: number // 最小字号。默认:5 diff --git a/src/editor/core/draw/particle/TextParticle.ts b/src/editor/core/draw/particle/TextParticle.ts index b3c216d..21b6178 100644 --- a/src/editor/core/draw/particle/TextParticle.ts +++ b/src/editor/core/draw/particle/TextParticle.ts @@ -1,5 +1,6 @@ -import { ElementType, IElement } from '../../..' +import { ElementType, IEditorOption, IElement } from '../../..' import { PUNCTUATION_LIST } from '../../../dataset/constant/Common' +import { DeepRequired } from '../../../interface/Common' import { IRowElement } from '../../../interface/Row' import { Draw } from '../Draw' @@ -10,6 +11,8 @@ export interface IMeasureWordResult { export class TextParticle { private draw: Draw + private options: DeepRequired + private ctx: CanvasRenderingContext2D private curX: number private curY: number @@ -20,6 +23,7 @@ export class TextParticle { constructor(draw: Draw) { this.draw = draw + this.options = draw.getOptions() this.ctx = draw.getCtx() this.curX = -1 this.curY = -1 @@ -129,9 +133,7 @@ export class TextParticle { if (!this.text || !~this.curX || !~this.curX) return this.ctx.save() this.ctx.font = this.curStyle - if (this.curColor) { - this.ctx.fillStyle = this.curColor - } + this.ctx.fillStyle = this.curColor || this.options.defaultColor this.ctx.fillText(this.text, this.curX, this.curY) this.ctx.restore() } diff --git a/src/editor/index.ts b/src/editor/index.ts index a79aadb..f6be623 100644 --- a/src/editor/index.ts +++ b/src/editor/index.ts @@ -149,6 +149,7 @@ export default class Editor { const editorOptions: DeepRequired = { mode: EditorMode.EDIT, defaultType: 'TEXT', + defaultColor: '#000000', defaultFont: 'Microsoft YaHei', defaultSize: 16, minSize: 5, diff --git a/src/editor/interface/Editor.ts b/src/editor/interface/Editor.ts index 7b92b3e..ced4b5b 100644 --- a/src/editor/interface/Editor.ts +++ b/src/editor/interface/Editor.ts @@ -30,6 +30,7 @@ export interface IEditorData { export interface IEditorOption { mode?: EditorMode defaultType?: string + defaultColor?: string defaultFont?: string defaultSize?: number minSize?: number