feat:字体斜体

pr675
Hufe921 4 years ago
parent 7d46aa014c
commit 59fd84c94c

@ -9,6 +9,7 @@ export class Command {
private static sizeAdd: Function private static sizeAdd: Function
private static sizeMinus: Function private static sizeMinus: Function
private static bold: Function private static bold: Function
private static italic: Function
private static search: Function private static search: Function
private static print: Function private static print: Function
@ -20,6 +21,7 @@ export class Command {
Command.sizeAdd = adapt.sizeAdd.bind(adapt) Command.sizeAdd = adapt.sizeAdd.bind(adapt)
Command.sizeMinus = adapt.sizeMinus.bind(adapt) Command.sizeMinus = adapt.sizeMinus.bind(adapt)
Command.bold = adapt.bold.bind(adapt) Command.bold = adapt.bold.bind(adapt)
Command.italic = adapt.italic.bind(adapt)
Command.search = adapt.search.bind(adapt) Command.search = adapt.search.bind(adapt)
Command.print = adapt.print.bind(adapt) Command.print = adapt.print.bind(adapt)
} }
@ -54,6 +56,10 @@ export class Command {
return Command.bold() return Command.bold()
} }
public executeItalic() {
return Command.italic()
}
// 搜索、打印 // 搜索、打印
public executeSearch(payload: string | null) { public executeSearch(payload: string | null) {
return Command.search(payload) return Command.search(payload)

@ -100,6 +100,16 @@ export class CommandAdapt {
this.draw.render({ isSetCursor: false }) this.draw.render({ isSetCursor: false })
} }
public italic() {
const selection = this.range.getSelection()
if (!selection) return
const noItalicIndex = selection.findIndex(s => !s.italic)
selection.forEach(el => {
el.italic = !!~noItalicIndex
})
this.draw.render({ isSetCursor: false })
}
public search(payload: string | null) { public search(payload: string | null) {
if (payload) { if (payload) {
const elementList = this.draw.getElementList() const elementList = this.draw.getElementList()

@ -108,6 +108,11 @@ export class Draw {
this.searchMatchList = payload this.searchMatchList = payload
} }
private getFont(el: IElement): string {
const { defaultSize, defaultFont } = this.options
return `${el.italic ? 'italic ' : ''}${el.bold ? 'bold ' : ''}${el.size || defaultSize}px ${el.font || defaultFont}`
}
public render(payload?: IDrawOption) { public render(payload?: IDrawOption) {
let { curIndex, isSubmitHistory = true, isSetCursor = true } = payload || {} let { curIndex, isSubmitHistory = true, isSetCursor = true } = payload || {}
// 清除光标 // 清除光标
@ -116,7 +121,6 @@ export class Draw {
this.position.setPositionList([]) this.position.setPositionList([])
const positionList = this.position.getPositionList() const positionList = this.position.getPositionList()
// 基础信息 // 基础信息
const { defaultSize, defaultFont } = this.options
const canvasRect = this.canvas.getBoundingClientRect() const canvasRect = this.canvas.getBoundingClientRect()
// 绘制背景 // 绘制背景
this.background.render(canvasRect) this.background.render(canvasRect)
@ -140,7 +144,7 @@ export class Draw {
this.ctx.save() this.ctx.save()
const curRow: IRow = rowList[rowList.length - 1] const curRow: IRow = rowList[rowList.length - 1]
const element = this.elementList[i] const element = this.elementList[i]
this.ctx.font = `${element.bold ? 'bold ' : ''}${element.size || defaultSize}px ${element.font || defaultFont}` this.ctx.font = this.getFont(element)
const metrics = this.ctx.measureText(element.value) const metrics = this.ctx.measureText(element.value)
const width = metrics.width const width = metrics.width
const fontBoundingBoxAscent = metrics.fontBoundingBoxAscent const fontBoundingBoxAscent = metrics.fontBoundingBoxAscent
@ -174,7 +178,7 @@ export class Draw {
this.ctx.save() this.ctx.save()
const element = curRow.elementList[j] const element = curRow.elementList[j]
const metrics = element.metrics const metrics = element.metrics
this.ctx.font = `${element.bold ? 'bold ' : ''}${element.size || defaultSize}px ${element.font || defaultFont}` this.ctx.font = this.getFont(element)
if (element.color) { if (element.color) {
this.ctx.fillStyle = element.color this.ctx.fillStyle = element.color
} }

@ -76,6 +76,10 @@ window.onload = function () {
console.log('bold') console.log('bold')
instance.command.executeBold() instance.command.executeBold()
} }
document.querySelector<HTMLDivElement>('.menu-item__italic')!.onclick = function () {
console.log('italic')
instance.command.executeItalic()
}
// 搜索、打印 // 搜索、打印
const collspanDom = document.querySelector<HTMLDivElement>('.menu-item__search__collapse') const collspanDom = document.querySelector<HTMLDivElement>('.menu-item__search__collapse')

Loading…
Cancel
Save