feat:add superscript and subscript

This commit is contained in:
黄云飞
2022-01-14 14:40:19 +08:00
parent 8939d42295
commit ae3de7bf55
14 changed files with 122 additions and 13 deletions
+12
View File
@@ -20,6 +20,8 @@ export class Command {
private static italic: Function
private static underline: Function
private static strikeout: Function
private static superscript: Function
private static subscript: Function
private static color: Function
private static highlight: Function
private static left: Function
@@ -60,6 +62,8 @@ export class Command {
Command.italic = adapt.italic.bind(adapt)
Command.underline = adapt.underline.bind(adapt)
Command.strikeout = adapt.strikeout.bind(adapt)
Command.superscript = adapt.superscript.bind(adapt)
Command.subscript = adapt.subscript.bind(adapt)
Command.color = adapt.color.bind(adapt)
Command.highlight = adapt.highlight.bind(adapt)
Command.left = adapt.rowFlex.bind(adapt)
@@ -148,6 +152,14 @@ export class Command {
return Command.strikeout()
}
public executeSuperscript() {
return Command.superscript()
}
public executeSubscript() {
return Command.subscript()
}
public executeColor(payload: string) {
return Command.color(payload)
}
+44 -3
View File
@@ -1,5 +1,5 @@
import { ZERO } from "../../dataset/constant/Common"
import { EDITOR_ELEMENT_STYLE_ATTR } from "../../dataset/constant/Element"
import { EDITOR_ELEMENT_STYLE_ATTR, TEXTLIKE_ELEMENT_TYPE } from "../../dataset/constant/Element"
import { EditorContext } from "../../dataset/enum/Editor"
import { ElementType } from "../../dataset/enum/Element"
import { ElementStyleKey } from "../../dataset/enum/ElementStyle"
@@ -181,6 +181,48 @@ export class CommandAdapt {
this.draw.render({ isSetCursor: false })
}
public superscript() {
const selection = this.range.getSelection()
if (!selection) return
const superscriptIndex = selection.findIndex(s => s.type === ElementType.SUPERSCRIPT)
selection.forEach(el => {
// 取消上标
if (~superscriptIndex) {
if (el.type === ElementType.SUPERSCRIPT) {
el.type = ElementType.TEXT
delete el.actualSize
}
} else {
// 设置上标
if (!el.type || el.type === ElementType.TEXT || el.type === ElementType.SUBSCRIPT) {
el.type = ElementType.SUPERSCRIPT
}
}
})
this.draw.render({ isSetCursor: false })
}
public subscript() {
const selection = this.range.getSelection()
if (!selection) return
const subscriptIndex = selection.findIndex(s => s.type === ElementType.SUBSCRIPT)
selection.forEach(el => {
// 取消下标
if (~subscriptIndex) {
if (el.type === ElementType.SUBSCRIPT) {
el.type = ElementType.TEXT
delete el.actualSize
}
} else {
// 设置下标
if (!el.type || el.type === ElementType.TEXT || el.type === ElementType.SUPERSCRIPT) {
el.type = ElementType.SUBSCRIPT
}
}
})
this.draw.render({ isSetCursor: false })
}
public color(payload: string) {
const selection = this.range.getSelection()
if (!selection) return
@@ -911,8 +953,7 @@ export class CommandAdapt {
// 搜索文本
function searchClosure(payload: string | null, type: EditorContext, elementList: IElement[], restArgs?: ISearchResultRestArgs) {
if (!payload) return
const { TEXT, HYPERLINK } = ElementType
const text = elementList.map(e => !e.type || e.type === TEXT || e.type === HYPERLINK ? e.value : ZERO)
const text = elementList.map(e => !e.type || TEXTLIKE_ELEMENT_TYPE.includes(e.type) ? e.value : ZERO)
.filter(Boolean)
.join('')
const matchStartIndexList = []
+9 -2
View File
@@ -358,7 +358,9 @@ export class Draw {
private _getFont(el: IElement, scale: number = 1): string {
const { defaultSize, defaultFont } = this.options
return `${el.italic ? 'italic ' : ''}${el.bold ? 'bold ' : ''}${(el.size || defaultSize) * scale}px ${el.font || defaultFont}`
const font = el.font || defaultFont
const size = el.actualSize || el.size || defaultSize
return `${el.italic ? 'italic ' : ''}${el.bold ? 'bold ' : ''}${size * scale}px ${font}`
}
private _computeRowList(innerWidth: number, elementList: IElement[]) {
@@ -442,7 +444,12 @@ export class Draw {
metrics.boundingBoxDescent = elementHeight
metrics.boundingBoxAscent = 0
} else {
metrics.height = (element.size || this.options.defaultSize) * scale
// 设置上下标真实字体尺寸
const size = element.size || this.options.defaultSize
if (element.type === ElementType.SUPERSCRIPT || element.type === ElementType.SUBSCRIPT) {
element.actualSize = Math.ceil(size * 0.6)
}
metrics.height = (element.actualSize || size) * scale
ctx.font = this._getFont(element)
const fontMetrics = this.textParticle.measureText(ctx, element)
metrics.width = fontMetrics.width * scale
+5 -2
View File
@@ -368,7 +368,7 @@ export class CanvasEvent {
if (!this.cursor) return
const cursorPosition = this.position.getCursorPosition()
if (!data || !cursorPosition || this.isCompositing) return
const { TEXT, HYPERLINK } = ElementType
const { TEXT, HYPERLINK, SUBSCRIPT, SUPERSCRIPT } = ElementType
const text = data.replaceAll(`\n`, ZERO)
const elementList = this.draw.getElementList()
const agentDom = this.cursor.getAgentDom()
@@ -389,10 +389,13 @@ export class CanvasEvent {
value,
...restArg
}
const nextElement = elementList[endIndex + 1]
if (
element.type === TEXT
|| (!element.type && element.value !== ZERO)
|| (element.type === HYPERLINK && elementList[endIndex + 1]?.type === HYPERLINK)
|| (element.type === HYPERLINK && nextElement?.type === HYPERLINK)
|| (element.type === SUBSCRIPT && nextElement?.type === SUBSCRIPT)
|| (element.type === SUPERSCRIPT && nextElement?.type === SUPERSCRIPT)
) {
EDITOR_ELEMENT_COPY_ATTR.forEach(attr => {
const value = element[attr] as never
+5
View File
@@ -1,3 +1,4 @@
import { ElementType } from "../.."
import { IEditorOption } from "../../interface/Editor"
import { IElement } from "../../interface/Element"
import { IRange } from "../../interface/Range"
@@ -65,6 +66,8 @@ export class RangeManager {
}
const curElement = curElementList[curElementList.length - 1]
if (!curElement) return
// 类型
const type = curElement.type || ElementType.TEXT
// 富文本
const font = curElement.font || this.options.defaultFont
let bold = !~curElementList.findIndex(el => !el.bold)
@@ -80,6 +83,7 @@ export class RangeManager {
const undo = this.historyManager.isCanUndo()
const redo = this.historyManager.isCanRedo()
this.listener.rangeStyleChange({
type,
undo,
redo,
painter,
@@ -103,6 +107,7 @@ export class RangeManager {
const undo = this.historyManager.isCanUndo()
const redo = this.historyManager.isCanRedo()
this.listener.rangeStyleChange({
type: null,
undo,
redo,
painter,