Merge pull request #151 from zhoujingfu/catalogue

feat: font size setting api
This commit is contained in:
Hufe
2023-03-22 14:02:18 +08:00
committed by GitHub
12 changed files with 157 additions and 18 deletions
+7 -1
View File
@@ -22,6 +22,7 @@ export class Command {
private static applyPainterStyle: CommandAdapt['applyPainterStyle']
private static format: CommandAdapt['format']
private static font: CommandAdapt['font']
private static size: CommandAdapt['size']
private static sizeAdd: CommandAdapt['sizeAdd']
private static sizeMinus: CommandAdapt['sizeMinus']
private static bold: CommandAdapt['bold']
@@ -96,6 +97,7 @@ export class Command {
Command.applyPainterStyle = adapt.applyPainterStyle.bind(adapt)
Command.format = adapt.format.bind(adapt)
Command.font = adapt.font.bind(adapt)
Command.size = adapt.size.bind(adapt)
Command.sizeAdd = adapt.sizeAdd.bind(adapt)
Command.sizeMinus = adapt.sizeMinus.bind(adapt)
Command.bold = adapt.bold.bind(adapt)
@@ -207,11 +209,15 @@ export class Command {
return Command.format()
}
// 字体、字体变大、字体变小、加粗、斜体、下划线、删除线、字体颜色、背景色
// 字体、字体大小、字体变大、字体变小、加粗、斜体、下划线、删除线、字体颜色、背景色
public executeFont(payload: string) {
return Command.font(payload)
}
public executeSize(payload: number) {
return Command.size(payload)
}
public executeSizeAdd() {
return Command.sizeAdd()
}
+46 -16
View File
@@ -186,40 +186,70 @@ export class CommandAdapt {
this.draw.render({ isSetCursor: false })
}
public size(payload: number) {
const { minSize, maxSize, defaultSize } = this.options
if (payload < minSize || payload > maxSize) return
const isReadonly = this.draw.isReadonly()
if (isReadonly) return
const selection = this.range.getTextLikeSelection()
if (!selection || !selection.length) return
let isExistUpdate = false
selection.forEach(el => {
if ((!el.size && payload === defaultSize) || (el.size && el.size === payload)) return
el.size = payload
isExistUpdate = true
})
if (isExistUpdate) {
this.draw.render({ isSetCursor: false })
}
}
public sizeAdd() {
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)
const { defaultSize } = this.options
if (!~lessThanMaxSizeIndex) return
const selection = this.range.getTextLikeSelection()
if (!selection || !selection.length) return
const { defaultSize, maxSize } = this.options
let isExistUpdate = false
selection.forEach(el => {
if (!el.size) {
el.size = defaultSize
}
if (el.size + 2 > 72) return
el.size += 2
if (el.size >= maxSize) return
if (el.size + 2 > maxSize) {
el.size = maxSize
} else {
el.size += 2
}
isExistUpdate = true
})
this.draw.render({ isSetCursor: false })
if (isExistUpdate) {
this.draw.render({ isSetCursor: false })
}
}
public sizeMinus() {
const isReadonly = this.draw.isReadonly()
if (isReadonly) return
const selection = this.range.getSelection()
if (!selection) return
const greaterThanMaxSizeIndex = selection.findIndex(s => !s.size || s.size - 2 >= 8)
if (!~greaterThanMaxSizeIndex) return
const { defaultSize } = this.options
const selection = this.range.getTextLikeSelection()
if (!selection || !selection.length) return
const { defaultSize, minSize } = this.options
let isExistUpdate = false
selection.forEach(el => {
if (!el.size) {
el.size = defaultSize
}
if (el.size - 2 < 8) return
el.size -= 2
if (el.size <= minSize) return
if (el.size - 2 < minSize) {
el.size = minSize
} else {
el.size -= 2
}
isExistUpdate = true
})
this.draw.render({ isSetCursor: false })
if (isExistUpdate) {
this.draw.render({ isSetCursor: false })
}
}
public bold() {
+11
View File
@@ -1,5 +1,6 @@
import { ElementType } from '../..'
import { ZERO } from '../../dataset/constant/Common'
import { TEXTLIKE_ELEMENT_TYPE } from '../../dataset/constant/Element'
import { ControlComponent } from '../../dataset/enum/Control'
import { IEditorOption } from '../../interface/Editor'
import { IElement } from '../../interface/Element'
@@ -45,6 +46,12 @@ export class RangeManager {
return elementList.slice(startIndex + 1, endIndex + 1)
}
public getTextLikeSelection(): IElement[] | null {
const selection = this.getSelection()
if (!selection) return null
return selection.filter(s => !s.type || TEXTLIKE_ELEMENT_TYPE.includes(s.type))
}
// 获取光标所选位置行信息
public getRangeRow(): RangeRowMap | null {
const { startIndex, endIndex } = this.range
@@ -135,6 +142,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 +162,7 @@ export class RangeManager {
redo,
painter,
font,
size,
bold,
italic,
underline,
@@ -169,6 +178,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 +189,7 @@ export class RangeManager {
redo,
painter,
font,
size,
bold: false,
italic: false,
underline: false,
+2
View File
@@ -73,6 +73,8 @@ export default class Editor {
defaultType: 'TEXT',
defaultFont: 'Yahei',
defaultSize: 16,
minSize: 5,
maxSize: 72,
defaultRowMargin: 1,
defaultBasicRowMarginHeight: 8,
defaultTabWidth: 32,
+2
View File
@@ -19,6 +19,8 @@ export interface IEditorOption {
defaultType?: string;
defaultFont?: string;
defaultSize?: number;
minSize?: number;
maxSize?: number;
defaultBasicRowMarginHeight?: number;
defaultRowMargin?: number;
defaultTabWidth?: number;
+1
View File
@@ -9,6 +9,7 @@ export interface IRangeStyle {
redo: boolean;
painter: boolean;
font: string;
size: number;
bold: boolean;
italic: boolean;
underline: boolean;