feat:paste html with textAlign info

pr675
Hufe921 3 years ago
parent 34799d7cb9
commit eb0086a4b8

@ -6,7 +6,7 @@ import { ElementType } from '../dataset/enum/Element'
import { DeepRequired } from '../interface/Common' import { DeepRequired } from '../interface/Common'
import { ITd } from '../interface/table/Td' import { ITd } from '../interface/table/Td'
import { ITr } from '../interface/table/Tr' import { ITr } from '../interface/table/Tr'
import { zipElementList } from './element' import { getElementRowFlex, zipElementList } from './element'
export function writeClipboardItem(text: string, html: string) { export function writeClipboardItem(text: string, html: string) {
if (!text || !html) return if (!text || !html) return
@ -103,7 +103,8 @@ export function writeElementList(elementList: IElement[], options: DeepRequired<
dom.innerText = text.replace(new RegExp(`${ZERO}`, 'g'), '\n') dom.innerText = text.replace(new RegExp(`${ZERO}`, 'g'), '\n')
dom.style.fontFamily = element.font || options.defaultFont dom.style.fontFamily = element.font || options.defaultFont
if (element.rowFlex) { if (element.rowFlex) {
dom.style.textAlign = element.rowFlex const isAlignment = element.rowFlex === RowFlex.ALIGNMENT
dom.style.textAlign = isAlignment ? 'justify' : element.rowFlex
} }
if (element.color) { if (element.color) {
dom.style.color = element.color dom.style.color = element.color
@ -140,16 +141,22 @@ export function getElementListByHTML(htmlText: string, options: IGetElementListB
const elementList: IElement[] = [] const elementList: IElement[] = []
function findTextNode(dom: Element | Node) { function findTextNode(dom: Element | Node) {
if (dom.nodeType === 3) { if (dom.nodeType === 3) {
const style = window.getComputedStyle(dom.parentNode as Element) const parentNode = <HTMLElement>dom.parentNode
const rowFlex = getElementRowFlex(parentNode)
const value = dom.textContent const value = dom.textContent
if (value && dom.parentNode?.nodeName !== 'STYLE') { const style = window.getComputedStyle(parentNode)
elementList.push({ if (value && parentNode.nodeName !== 'STYLE') {
const element: IElement = {
value, value,
color: style.color, color: style.color,
bold: Number(style.fontWeight) > 500, bold: Number(style.fontWeight) > 500,
italic: style.fontStyle.includes('italic'), italic: style.fontStyle.includes('italic'),
size: Math.floor(Number(style.fontSize.replace('px', ''))) size: Math.floor(Number(style.fontSize.replace('px', '')))
}) }
if (rowFlex !== RowFlex.LEFT) {
element.rowFlex = rowFlex
}
elementList.push(element)
} }
} else if (dom.nodeType === 1) { } else if (dom.nodeType === 1) {
const childNodes = dom.childNodes const childNodes = dom.childNodes

@ -1,5 +1,5 @@
import { deepClone, getUUID, splitText } from '.' import { deepClone, getUUID, splitText } from '.'
import { ElementType, IEditorOption, IElement } from '..' import { ElementType, IEditorOption, IElement, RowFlex } from '..'
import { LaTexParticle } from '../core/draw/particle/latex/LaTexParticle' import { LaTexParticle } from '../core/draw/particle/latex/LaTexParticle'
import { defaultCheckboxOption } from '../dataset/constant/Checkbox' import { defaultCheckboxOption } from '../dataset/constant/Checkbox'
import { ZERO } from '../dataset/constant/Common' import { ZERO } from '../dataset/constant/Common'
@ -397,3 +397,21 @@ export function zipElementList(payload: IElement[]): IElement[] {
} }
return zipElementListData return zipElementListData
} }
export function getElementRowFlex(node: HTMLElement) {
const textAlign = window.getComputedStyle(node).textAlign
switch (textAlign) {
case 'left':
case 'start':
return RowFlex.LEFT
case 'center':
return RowFlex.CENTER
case 'right':
case 'end':
return RowFlex.RIGHT
case 'justify':
return RowFlex.ALIGNMENT
default:
return RowFlex.LEFT
}
}
Loading…
Cancel
Save