import { colord } from 'colord'; import type { MarkInterface, NodeInterface, SchemaInterface } from '../types'; import { READY_CARD_KEY, READY_CARD_SELECTOR, CARD_KEY, } from '../constants/card'; import Parser from '../parser'; import { EngineInterface } from '../types/engine'; import { $ } from '../node'; export default class Paste { protected source: string; protected engine: EngineInterface; protected schema: SchemaInterface; constructor(source: string, engine: EngineInterface) { this.source = source; this.engine = engine; this.schema = this.engine.schema.clone(); } parser() { const conversion = this.engine.conversion.clone(); this.engine.trigger('paste:schema', this.schema); const parser = new Parser(this.source, this.engine, (root) => { this.engine.trigger('paste:origin', root); }); return parser.toDOM(this.schema, conversion); } getDefaultStyle(container = this.engine.container) { const defaultStyle = { color: colord(container.css('color')).toHex(), 'background-color': colord( container.css('background-color'), ).toHex(), 'font-size': container.css('font-size'), }; return defaultStyle; } elementNormalize(fragment: DocumentFragment) { const defaultStyle = this.getDefaultStyle(); const { inline, list } = this.engine; const nodeApi = this.engine.node; const markApi = this.engine.mark; const blockApi = this.engine.block; const currentMarkPlugins: { plugin: MarkInterface | undefined; node: NodeInterface; }[] = []; $(fragment).traverse( (node) => { let parent = node.parent(); // 跳过已被删除的节点 if (!parent || node.isCard() || node.fragment === fragment) { return undefined; } if (node.isText()) { let text = node.text(); // if (/\x20/.test(text)) { // text = text.replace(/\x20/g, ' '); // node.text(text); // } if (/\u200b/.test(text)) { let isRemove = true; const next = node.next(); const prev = node.prev(); const parent = node.parent(); if (parent && nodeApi.isMark(parent, this.schema)) isRemove = false; else if ( parent && nodeApi.isInline(parent, this.schema) ) isRemove = false; else if (next && nodeApi.isInline(next, this.schema)) isRemove = false; else if (prev && nodeApi.isInline(prev, this.schema)) isRemove = false; if (isRemove) { text = text.replace(/\u200b/g, ''); node.text(text); } } else if (/^\n(\t)[0,]$/.test(text)) { node.remove(); if (parent.get()?.childNodes.length === 0) { parent.after('


'); parent.remove(); } return; } else if (/^\n$/.test(text)) { if (nodeApi.isList(parent)) { node.remove(); return; } const next = node.next(); if (next && nodeApi.isBlock(next)) node.remove(); } if (nodeApi.isList(parent)) { const next = node.next(); const prev = node.prev(); const addCardToCustomize = ( node: NodeInterface, target: NodeInterface, ) => { if (nodeApi.isCustomize(node)) { const first = node.first(); if (first && first.isCard()) { const cardName = first.attributes(CARD_KEY) || first.attributes(READY_CARD_KEY); if (cardName) list.addCardToCustomize( target, cardName, ); } } }; let cloneLi = null; if (next?.name === 'li') { cloneLi = next.clone(); addCardToCustomize(next, cloneLi); } else if (prev?.name === 'li') { cloneLi = prev.clone(); addCardToCustomize(prev, cloneLi); } else { cloneLi = $(`
  • `); } node.before(cloneLi); cloneLi.append(node); return cloneLi; } return undefined; } const styles = node.css(); for (const key in defaultStyle) { let value = styles[key]; if (!value) continue; if (key.endsWith('color')) { value = colord(value).toHex(); } if ( value.toLowerCase() === defaultStyle[key].toLowerCase() ) { node.css(key, ''); } } //处理后如果不是一个有效的节点就移除包裹 let type = this.schema.getType(node); if (!type) { const first = node.first(); nodeApi.unwrap(node); return first; } nodeApi.removeMinusStyle(node, 'text-indent'); if (nodeApi.isList(node)) { node.css('padding-left', ''); } let attributes: { [k: string]: string } | undefined = node.attributes(); // 删除空 style 属性 if (attributes.style && attributes.style.trim() === '') { node.removeAttributes('style'); } // 删除空 span while (node.name === 'span' && nodeApi.isEmpty(node)) { const children = node.children(); if (children.length > 0) { break; } parent = node.parent(); node.remove(); if (!parent) return undefined; node = parent; parent = node.parent(); if (!parent) return undefined; type = undefined; attributes = undefined; } if (!attributes) attributes = node.attributes(); // 跳过Card if (attributes[READY_CARD_KEY]) { return undefined; } const nodeIsBlock = type ? type === 'block' : nodeApi.isBlock(node, this.schema); const nodeIsVoid = nodeApi.isVoid(node, this.schema); let parentIsBlock = nodeApi.isBlock(parent, this.schema); // 删除零高度的空行 if ( nodeIsBlock && attributes['data-type'] !== 'p' && !nodeIsVoid && !parentIsBlock && //!node.isSolid() && nodeApi.html(node) === '' ) { node.remove(); return undefined; } // 段落 if (attributes['data-type'] === 'p') { node.removeAttributes('data-type'); } if (nodeIsBlock && parent?.name === 'p') { nodeApi.unwrap(parent); parent = node.parent(); if (parent?.fragment === fragment) parent = undefined; parentIsBlock = parent ? nodeApi.isBlock(parent, this.schema) : false; } const parentIsList = parent ? nodeApi.isList(parent) : false; // 补齐 ul 或 ol if (node.name === 'li' && parent && !parentIsList) { const ul = $('