feat: add title element

This commit is contained in:
Hufe921
2023-04-06 21:13:07 +08:00
committed by Hufe
parent 189ca73c32
commit 9701b2153e
20 changed files with 392 additions and 90 deletions
+8 -1
View File
@@ -1,4 +1,4 @@
import { IElement, ImageDisplay, INavigateInfo, TableBorder, VerticalAlign } from '../..'
import { IElement, ImageDisplay, INavigateInfo, TableBorder, TitleLevel, VerticalAlign } from '../..'
import { EditorMode, PageMode, PaperDirection } from '../../dataset/enum/Editor'
import { RowFlex } from '../../dataset/enum/Row'
import { IDrawImagePayload, IPainterOptions } from '../../interface/Draw'
@@ -33,6 +33,7 @@ export class Command {
private static subscript: CommandAdapt['subscript']
private static color: CommandAdapt['color']
private static highlight: CommandAdapt['highlight']
private static title: CommandAdapt['title']
private static left: CommandAdapt['rowFlex']
private static center: CommandAdapt['rowFlex']
private static right: CommandAdapt['rowFlex']
@@ -109,6 +110,7 @@ export class Command {
Command.subscript = adapt.subscript.bind(adapt)
Command.color = adapt.color.bind(adapt)
Command.highlight = adapt.highlight.bind(adapt)
Command.title = adapt.title.bind(adapt)
Command.left = adapt.rowFlex.bind(adapt)
Command.center = adapt.rowFlex.bind(adapt)
Command.right = adapt.rowFlex.bind(adapt)
@@ -260,6 +262,11 @@ export class Command {
return Command.highlight(payload)
}
// 标题、对齐方式
public executeTitle(payload: TitleLevel | null) {
return Command.title(payload)
}
public executeLeft() {
return Command.left(RowFlex.LEFT)
}
+59
View File
@@ -1,5 +1,6 @@
import { WRAP, ZERO } from '../../dataset/constant/Common'
import { EDITOR_ELEMENT_STYLE_ATTR } from '../../dataset/constant/Element'
import { titleSizeMapping } from '../../dataset/constant/Title'
import { defaultWatermarkOption } from '../../dataset/constant/Watermark'
import { ControlComponent, ImageDisplay } from '../../dataset/enum/Control'
import { EditorContext, EditorMode, PageMode, PaperDirection } from '../../dataset/enum/Editor'
@@ -7,6 +8,7 @@ import { ElementType } from '../../dataset/enum/Element'
import { ElementStyleKey } from '../../dataset/enum/ElementStyle'
import { RowFlex } from '../../dataset/enum/Row'
import { TableBorder } from '../../dataset/enum/table/Table'
import { TitleLevel } from '../../dataset/enum/Title'
import { VerticalAlign } from '../../dataset/enum/VerticalAlign'
import { IDrawImagePayload, IPainterOptions } from '../../interface/Draw'
import { IEditorOption, IEditorResult } from '../../interface/Editor'
@@ -379,6 +381,63 @@ export class CommandAdapt {
})
}
public title(payload: TitleLevel | null) {
const isReadonly = this.draw.isReadonly()
if (isReadonly) return
const { startIndex, endIndex } = this.range.getRange()
if (!~startIndex && !~endIndex) return
// 需要改变的元素列表
let changeElementList: IElement[] = []
const elementList = this.draw.getElementList()
if (startIndex === endIndex) {
// 选区行信息
const rangeRow = this.range.getRangeRow()
if (!rangeRow) return
const positionList = this.position.getPositionList()
for (let p = 0; p < positionList.length; p++) {
const position = positionList[p]
const rowSet = rangeRow.get(position.pageNo)
if (!rowSet) continue
if (rowSet.has(position.rowNo)) {
changeElementList.push(elementList[p])
}
}
} else {
changeElementList = elementList.slice(startIndex + 1, endIndex + 1)
}
// 检验数据合法性
const isExistNotTextElement = !!changeElementList.find(el =>
el.type &&
el.type !== ElementType.TEXT &&
el.type !== ElementType.TITLE
)
if (isExistNotTextElement) return
// 设置值
const titleId = getUUID()
const titleOptions = this.draw.getOptions().title
changeElementList.forEach(el => {
if (payload) {
el.type = ElementType.TITLE
el.level = payload
el.titleId = titleId
el.size = titleOptions[titleSizeMapping[payload]]
el.bold = true
} else {
if (el.type === ElementType.TITLE) {
delete el.type
delete el.titleId
delete el.level
delete el.size
delete el.bold
}
}
})
// 光标定位
const isSetCursor = startIndex === endIndex
const curIndex = isSetCursor ? endIndex : startIndex
this.draw.render({ curIndex, isSetCursor })
}
public rowFlex(payload: RowFlex) {
const isReadonly = this.draw.isReadonly()
if (isReadonly) return
+2 -1
View File
@@ -27,7 +27,7 @@ export function input(data: string, host: CanvasEvent) {
cursor.clearAgentDomValue()
}
const activeControl = control.getActiveControl()
const { TEXT, HYPERLINK, SUBSCRIPT, SUPERSCRIPT, DATE } = ElementType
const { TEXT, HYPERLINK, SUBSCRIPT, SUPERSCRIPT, DATE, TITLE } = ElementType
const text = data.replaceAll(`\n`, ZERO)
const rangeManager = draw.getRange()
const { startIndex, endIndex } = rangeManager.getRange()
@@ -55,6 +55,7 @@ export function input(data: string, host: CanvasEvent) {
|| (!copyElement.type && copyElement.value !== ZERO)
|| (copyElement.type === HYPERLINK && nextElement?.type === HYPERLINK)
|| (copyElement.type === DATE && nextElement?.type === DATE)
|| copyElement.type === TITLE
|| (copyElement.type === SUBSCRIPT && nextElement?.type === SUBSCRIPT)
|| (copyElement.type === SUPERSCRIPT && nextElement?.type === SUPERSCRIPT)
) {
+5 -2
View File
@@ -152,6 +152,7 @@ export class RangeManager {
const rowFlex = curElement.rowFlex || null
const rowMargin = curElement.rowMargin || this.options.defaultRowMargin
const dashArray = curElement.dashArray || []
const level = curElement.level || null
// 菜单
const painter = !!this.draw.getPainterStyle()
const undo = this.historyManager.isCanUndo()
@@ -171,7 +172,8 @@ export class RangeManager {
highlight,
rowFlex,
rowMargin,
dashArray
dashArray,
level
})
}
@@ -198,7 +200,8 @@ export class RangeManager {
highlight: null,
rowFlex: null,
rowMargin,
dashArray: []
dashArray: [],
level: null
})
}
+7 -3
View File
@@ -26,7 +26,9 @@ export const EDITOR_ELEMENT_COPY_ATTR: Array<keyof IElement> = [
'url',
'hyperlinkId',
'dateId',
'dateFormat'
'dateFormat',
'level',
'titleId'
]
export const EDITOR_ELEMENT_ZIP_ATTR: Array<keyof IElement> = [
@@ -52,7 +54,8 @@ export const EDITOR_ELEMENT_ZIP_ATTR: Array<keyof IElement> = [
'control',
'checkbox',
'dateFormat',
'block'
'block',
'level'
]
export const TEXTLIKE_ELEMENT_TYPE: ElementType[] = [
@@ -61,7 +64,8 @@ export const TEXTLIKE_ELEMENT_TYPE: ElementType[] = [
ElementType.SUBSCRIPT,
ElementType.SUPERSCRIPT,
ElementType.CONTROL,
ElementType.DATE
ElementType.DATE,
ElementType.TITLE
]
export const INLINE_ELEMENT_TYPE: ElementType[] = [
+38
View File
@@ -0,0 +1,38 @@
import { ITitleOption, ITitleSizeOption } from '../../interface/Title'
import { TitleLevel } from '../enum/Title'
export const defaultTitleOption: Readonly<Required<ITitleOption>> = {
defaultFirstSize: 26,
defaultSecondSize: 24,
defaultThirdSize: 22,
defaultFourthSize: 20,
defaultFifthSize: 18,
defaultSixthSize: 16
}
export const titleSizeMapping: Record<TitleLevel, keyof ITitleSizeOption> = {
[TitleLevel.FIRST]: 'defaultFirstSize',
[TitleLevel.SECOND]: 'defaultSecondSize',
[TitleLevel.THIRD]: 'defaultThirdSize',
[TitleLevel.FOURTH]: 'defaultFourthSize',
[TitleLevel.FIFTH]: 'defaultFifthSize',
[TitleLevel.SIXTH]: 'defaultSixthSize'
}
export const titleOrderNumberMapping: Record<TitleLevel, number> = {
[TitleLevel.FIRST]: 1,
[TitleLevel.SECOND]: 2,
[TitleLevel.THIRD]: 3,
[TitleLevel.FOURTH]: 4,
[TitleLevel.FIFTH]: 5,
[TitleLevel.SIXTH]: 6
}
export const titleNodeNameMapping: Record<string, TitleLevel> = {
'H1': TitleLevel.FIRST,
'H2': TitleLevel.SECOND,
'H3': TitleLevel.THIRD,
'H4': TitleLevel.FOURTH,
'H5': TitleLevel.FIFTH,
'H6': TitleLevel.SIXTH
}
+2 -1
View File
@@ -12,5 +12,6 @@ export enum ElementType {
LATEX = 'latex',
TAB = 'tab',
DATE = 'date',
BLOCK = 'block'
BLOCK = 'block',
TITLE = 'title'
}
+8
View File
@@ -0,0 +1,8 @@
export enum TitleLevel {
FIRST = 'first',
SECOND = 'second',
THIRD = 'third',
FOURTH = 'fourth',
FIFTH = 'fifth',
SIXTH = 'sixth'
}
+11 -2
View File
@@ -38,6 +38,9 @@ import { TableBorder } from './dataset/enum/table/Table'
import { IFooter } from './interface/Footer'
import { defaultFooterOption } from './dataset/constant/Footer'
import { MaxHeightRatio, NumberType } from './dataset/enum/Common'
import { ITitleOption } from './interface/Title'
import { defaultTitleOption } from './dataset/constant/Title'
import { TitleLevel } from './dataset/enum/Title'
export default class Editor {
@@ -75,6 +78,10 @@ export default class Editor {
...defaultCursorOption,
...options.cursor
}
const titleOptions: Required<ITitleOption> = {
...defaultTitleOption,
...options.title
}
const editorOptions: DeepRequired<IEditorOption> = {
mode: EditorMode.EDIT,
@@ -117,7 +124,8 @@ export default class Editor {
watermark: waterMarkOptions,
control: controlOptions,
checkbox: checkboxOptions,
cursor: cursorOptions
cursor: cursorOptions,
title: titleOptions
}
// 数据处理
let headerElementList: IElement[] = []
@@ -190,7 +198,8 @@ export {
PaperDirection,
TableBorder,
MaxHeightRatio,
NumberType
NumberType,
TitleLevel
}
// 对外类型
+2
View File
@@ -7,6 +7,7 @@ import { IFooter } from './Footer'
import { IHeader } from './Header'
import { IMargin } from './Margin'
import { IPageNumber } from './PageNumber'
import { ITitleOption } from './Title'
import { IWatermark } from './Watermark'
export interface IEditorData {
@@ -56,6 +57,7 @@ export interface IEditorOption {
control?: IControlOption;
checkbox?: ICheckboxOption;
cursor?: ICursorOption;
title?: ITitleOption;
}
export interface IEditorResult {
+8
View File
@@ -1,6 +1,7 @@
import { ControlComponent, ImageDisplay } from '../dataset/enum/Control'
import { ElementType } from '../dataset/enum/Element'
import { RowFlex } from '../dataset/enum/Row'
import { TitleLevel } from '../dataset/enum/Title'
import { TableBorder } from '../dataset/enum/table/Table'
import { IBlock } from './Block'
import { ICheckbox } from './Checkbox'
@@ -30,6 +31,12 @@ export interface IElementStyle {
letterSpacing?: number;
}
export interface ITitleElement {
valueList?: IElement[];
level?: TitleLevel;
titleId?: string;
}
export interface ITableAttr {
colgroup?: IColgroup[];
trList?: ITr[];
@@ -97,6 +104,7 @@ export type IElement = IElementBasic
& IDateElement
& IImageElement
& IBlockElement
& ITitleElement
export interface IElementMetrics {
width: number;
+2 -1
View File
@@ -1,4 +1,4 @@
import { EditorZone, ElementType, PageMode } from '..'
import { EditorZone, ElementType, PageMode, TitleLevel } from '..'
import { RowFlex } from '../dataset/enum/Row'
import { IControl } from './Control'
import { IEditorResult } from './Editor'
@@ -19,6 +19,7 @@ export interface IRangeStyle {
rowFlex: RowFlex | null;
rowMargin: number;
dashArray: number[];
level: TitleLevel | null;
}
export type IRangeStyleChange = (payload: IRangeStyle) => void
+10
View File
@@ -0,0 +1,10 @@
export interface ITitleSizeOption {
defaultFirstSize?: number;
defaultSecondSize?: number;
defaultThirdSize?: number;
defaultFourthSize?: number;
defaultFifthSize?: number;
defaultSixthSize?: number;
}
export type ITitleOption = ITitleSizeOption & {}
+54 -23
View File
@@ -1,6 +1,7 @@
import { IEditorOption, IElement, RowFlex } from '..'
import { ZERO } from '../dataset/constant/Common'
import { TEXTLIKE_ELEMENT_TYPE } from '../dataset/constant/Element'
import { titleNodeNameMapping, titleOrderNumberMapping } from '../dataset/constant/Title'
import { ControlComponent } from '../dataset/enum/Control'
import { ElementType } from '../dataset/enum/Element'
import { DeepRequired } from '../interface/Common'
@@ -35,6 +36,33 @@ export function writeClipboardItem(text: string, html: string) {
}
}
export function convertElementToDom(element: IElement, options: DeepRequired<IEditorOption>): HTMLElement {
const isBlock = element.rowFlex === RowFlex.CENTER || element.rowFlex === RowFlex.RIGHT
const dom = document.createElement(isBlock ? 'p' : 'span')
dom.style.fontFamily = element.font || options.defaultFont
if (element.rowFlex) {
const isAlignment = element.rowFlex === RowFlex.ALIGNMENT
dom.style.textAlign = isAlignment ? 'justify' : element.rowFlex
}
if (element.color) {
dom.style.color = element.color
}
if (element.bold) {
dom.style.fontWeight = '600'
}
if (element.italic) {
dom.style.fontStyle = 'italic'
}
if (element.size) {
dom.style.fontSize = `${element.size}px`
}
if (element.highlight) {
dom.style.backgroundColor = element.highlight
}
dom.innerText = element.value.replace(new RegExp(`${ZERO}`, 'g'), '\n')
return dom
}
export function writeElementList(elementList: IElement[], options: DeepRequired<IEditorOption>) {
const clipboardDom: HTMLDivElement = document.createElement('div')
function buildDomFromElementList(payload: IElement[]) {
@@ -61,11 +89,18 @@ export function writeElementList(elementList: IElement[], options: DeepRequired<
clipboardDom.append(tableDom)
} else if (element.type === ElementType.HYPERLINK) {
const a = document.createElement('a')
a.innerText = element.valueList![0].value
a.innerText = element.valueList!.map(v => v.value).join('')
if (element.url) {
a.href = element.url
}
clipboardDom.append(a)
} else if (element.type === ElementType.TITLE) {
const h = document.createElement(`h${titleOrderNumberMapping[element.level!]}`)
element.valueList!.forEach(el => {
const dom = convertElementToDom(el, options)
h.append(dom)
})
clipboardDom.append(h)
} else if (element.type === ElementType.IMAGE) {
const img = document.createElement('img')
if (element.value) {
@@ -98,29 +133,12 @@ export function writeElementList(elementList: IElement[], options: DeepRequired<
text = element.value
}
if (!text) continue
const isBlock = element.rowFlex === RowFlex.CENTER || element.rowFlex === RowFlex.RIGHT
const dom = document.createElement(isBlock ? 'p' : 'span')
// 前一个元素是标题,移除首行换行符
if (payload[e - 1]?.type === ElementType.TITLE) {
text = text.replace(/^\n/, '')
}
const dom = convertElementToDom(element, options)
dom.innerText = text.replace(new RegExp(`${ZERO}`, 'g'), '\n')
dom.style.fontFamily = element.font || options.defaultFont
if (element.rowFlex) {
const isAlignment = element.rowFlex === RowFlex.ALIGNMENT
dom.style.textAlign = isAlignment ? 'justify' : element.rowFlex
}
if (element.color) {
dom.style.color = element.color
}
if (element.bold) {
dom.style.fontWeight = '600'
}
if (element.italic) {
dom.style.fontStyle = 'italic'
}
if (element.size) {
dom.style.fontSize = `${element.size}px`
}
if (element.highlight) {
dom.style.backgroundColor = element.highlight
}
clipboardDom.append(dom)
}
}
@@ -186,6 +204,19 @@ export function getElementListByHTML(htmlText: string, options: IGetElementListB
url: aElement.href
})
}
} else if (/H[1-6]/.test(node.nodeName)) {
const hElement = node as HTMLTitleElement
const valueList = getElementListByHTML(hElement.innerHTML, options)
.filter(el => !el.type || el.type === ElementType.TEXT)
elementList.push({
value: '',
type: ElementType.TITLE,
level: titleNodeNameMapping[node.nodeName],
valueList
})
elementList.push({
value: '\n'
})
} else if (node.nodeName === 'HR') {
elementList.push({
value: '\n',
+67 -20
View File
@@ -5,9 +5,22 @@ import { defaultCheckboxOption } from '../dataset/constant/Checkbox'
import { ZERO } from '../dataset/constant/Common'
import { defaultControlOption } from '../dataset/constant/Control'
import { EDITOR_ELEMENT_ZIP_ATTR } from '../dataset/constant/Element'
import { titleSizeMapping } from '../dataset/constant/Title'
import { ControlComponent, ControlType } from '../dataset/enum/Control'
import { ITd } from '../interface/table/Td'
export function unzipElementList(elementList: IElement[]): IElement[] {
const result: IElement[] = []
for (let v = 0; v < elementList.length; v++) {
const valueItem = elementList[v]
const textList = splitText(valueItem.value)
for (let d = 0; d < textList.length; d++) {
result.push({ ...valueItem, value: textList[d] })
}
}
return result
}
interface IFormatElementListOption {
isHandleFirstElement?: boolean;
editorOptions: Required<IEditorOption>;
@@ -58,19 +71,12 @@ export function formatElementList(elementList: IElement[], options: IFormatEleme
}
}
} else if (el.type === ElementType.HYPERLINK) {
const valueList = el.valueList || []
// 移除父节点
elementList.splice(i, 1)
// 追加字节点
// 元素展开
const valueList = unzipElementList(el.valueList || [])
// 追加节点
if (valueList.length) {
// 元素展开
if (valueList[0].value.length > 1) {
const deleteValue = valueList.splice(0, 1)[0]
const deleteTextList = splitText(deleteValue.value)
for (let d = 0; d < deleteTextList.length; d++) {
valueList.splice(d, 0, { ...deleteValue, value: deleteTextList[d] })
}
}
const hyperlinkId = getUUID()
for (let v = 0; v < valueList.length; v++) {
const value = valueList[v]
@@ -83,19 +89,12 @@ export function formatElementList(elementList: IElement[], options: IFormatEleme
}
i--
} else if (el.type === ElementType.DATE) {
const valueList = el.valueList || []
// 移除父节点
elementList.splice(i, 1)
// 追加字节点
// 元素展开
const valueList = unzipElementList(el.valueList || [])
// 追加节点
if (valueList.length) {
// 元素展开
if (valueList[0].value.length > 1) {
const deleteValue = valueList.splice(0, 1)[0]
const deleteTextList = splitText(deleteValue.value)
for (let d = 0; d < deleteTextList.length; d++) {
valueList.splice(d, 0, { ...deleteValue, value: deleteTextList[d] })
}
}
const dateId = getUUID()
for (let v = 0; v < valueList.length; v++) {
const value = valueList[v]
@@ -107,6 +106,31 @@ export function formatElementList(elementList: IElement[], options: IFormatEleme
}
}
i--
} else if (el.type === ElementType.TITLE) {
// 移除父节点
elementList.splice(i, 1)
// 元素展开
const valueList = unzipElementList(el.valueList || [])
// 追加节点
if (valueList.length) {
const titleId = getUUID()
const titleOptions = editorOptions.title
for (let v = 0; v < valueList.length; v++) {
const value = valueList[v]
value.titleId = titleId
value.type = el.type
value.level = el.level
if (!value.size) {
value.size = titleOptions[titleSizeMapping[value.level!]]
}
if (value.bold === undefined) {
value.bold = true
}
elementList.splice(i, 0, value)
i++
}
}
i--
} else if (el.type === ElementType.CONTROL) {
const { prefix, postfix, value, placeholder, code, type, valueSets } = el.control!
const controlId = getUUID()
@@ -370,6 +394,29 @@ export function zipElementList(payload: IElement[]): IElement[] {
}
dateElement.valueList = zipElementList(valueList)
element = dateElement
} else if (element.type === ElementType.TITLE) {
// 标题处理
const titleId = element.titleId
const level = element.level
const titleElement: IElement = {
type: ElementType.TITLE,
value: '',
level
}
const valueList: IElement[] = []
while (e < elementList.length) {
const titleE = elementList[e]
if (titleId !== titleE.titleId) {
e--
break
}
delete titleE.type
delete titleE.level
valueList.push(titleE)
e++
}
titleElement.valueList = zipElementList(valueList)
element = titleElement
} else if (element.type === ElementType.CONTROL) {
// 控件处理
const controlId = element.controlId