diff --git a/index.html b/index.html index 1303bb4..0f14722 100644 --- a/index.html +++ b/index.html @@ -48,10 +48,12 @@
diff --git a/src/editor/core/command/Command.ts b/src/editor/core/command/Command.ts index c955bae..7801381 100644 --- a/src/editor/core/command/Command.ts +++ b/src/editor/core/command/Command.ts @@ -12,6 +12,8 @@ export class Command { private static italic: Function private static underline: Function private static strikeout: Function + private static color: Function + private static highlight: Function private static search: Function private static print: Function @@ -26,6 +28,8 @@ export class Command { Command.italic = adapt.italic.bind(adapt) Command.underline = adapt.underline.bind(adapt) Command.strikeout = adapt.strikeout.bind(adapt) + Command.color = adapt.color.bind(adapt) + Command.highlight = adapt.highlight.bind(adapt) Command.search = adapt.search.bind(adapt) Command.print = adapt.print.bind(adapt) } @@ -72,6 +76,14 @@ export class Command { return Command.strikeout() } + public executeColor(payload: string) { + return Command.color(payload) + } + + public executeHighlight(payload: string) { + return Command.highlight(payload) + } + // 搜索、打印 public executeSearch(payload: string | null) { return Command.search(payload) diff --git a/src/editor/core/command/CommandAdapt.ts b/src/editor/core/command/CommandAdapt.ts index 31f6a9a..231baba 100644 --- a/src/editor/core/command/CommandAdapt.ts +++ b/src/editor/core/command/CommandAdapt.ts @@ -33,7 +33,7 @@ export class CommandAdapt { if (!selection) return const painterStyle: IElementStyle = {} selection.forEach(s => { - const painterStyleKeys = ['bold', 'color', 'font', 'size', 'italic', 'underline', 'strikeout'] + const painterStyleKeys = ['bold', 'color', 'highlight', 'font', 'size', 'italic', 'underline', 'strikeout'] painterStyleKeys.forEach(p => { const key = p as keyof typeof ElementStyleKey if (painterStyle[key] === undefined) { @@ -130,6 +130,24 @@ export class CommandAdapt { this.draw.render({ isSetCursor: false }) } + public color(payload: string) { + const selection = this.range.getSelection() + if (!selection) return + selection.forEach(el => { + el.color = payload + }) + this.draw.render({ isSetCursor: false }) + } + + public highlight(payload: string) { + const selection = this.range.getSelection() + if (!selection) return + selection.forEach(el => { + el.highlight = payload + }) + this.draw.render({ isSetCursor: false }) + } + public search(payload: string | null) { if (payload) { const elementList = this.draw.getElementList() diff --git a/src/editor/core/draw/Draw.ts b/src/editor/core/draw/Draw.ts index f18358a..d68c997 100644 --- a/src/editor/core/draw/Draw.ts +++ b/src/editor/core/draw/Draw.ts @@ -11,6 +11,7 @@ import { HistoryManager } from "../history/HistoryManager" import { Position } from "../position/Position" import { RangeManager } from "../range/RangeManager" import { Background } from "./Background" +import { Highlight } from "./Highlight" import { Margin } from "./Margin" import { Search } from "./Search" import { Strikeout } from "./Strikeout" @@ -31,6 +32,7 @@ export class Draw { private search: Search private underline: Underline private strikeout: Strikeout + private highlight: Highlight private historyManager: HistoryManager private rowCount: number @@ -51,6 +53,7 @@ export class Draw { this.search = new Search(ctx, options, this) this.underline = new Underline(ctx, options) this.strikeout = new Strikeout(ctx, options) + this.highlight = new Highlight(ctx, options) const canvasEvent = new CanvasEvent(canvas, this) this.cursor = new Cursor(canvas, this, canvasEvent) @@ -218,6 +221,10 @@ export class Draw { if (element.strikeout) { this.strikeout.render(x, y + curRow.height / 2, metrics.width) } + // 文本高亮 + if (element.highlight) { + this.highlight.render(element.highlight, x, y, metrics.width, curRow.height) + } index++ x += metrics.width this.ctx.restore() diff --git a/src/editor/core/draw/Highlight.ts b/src/editor/core/draw/Highlight.ts new file mode 100644 index 0000000..9d29669 --- /dev/null +++ b/src/editor/core/draw/Highlight.ts @@ -0,0 +1,22 @@ +import { IEditorOption } from "../../interface/Editor" + +export class Highlight { + + private ctx: CanvasRenderingContext2D + private options: Required