From 6a87ef0e304c1051b26a312da7e02211b9743924 Mon Sep 17 00:00:00 2001 From: yanmao <55792257+yanmao-cc@users.noreply.github.com> Date: Wed, 5 Jan 2022 00:41:46 +0800 Subject: [PATCH] fix: table to html --- packages/engine/src/range.ts | 1 - packages/engine/src/schema.ts | 7 ++- plugins/table/src/index.css | 4 +- plugins/table/src/index.ts | 88 +++++++++++++++++------------------ 4 files changed, 48 insertions(+), 52 deletions(-) diff --git a/packages/engine/src/range.ts b/packages/engine/src/range.ts index 97318a7e..293a6595 100644 --- a/packages/engine/src/range.ts +++ b/packages/engine/src/range.ts @@ -12,7 +12,6 @@ import { DATA_ELEMENT, DATA_ID, DATA_TRANSIENT_ELEMENT, - EDITABLE_SELECTOR, UI, } from './constants/root'; import Selection from './selection'; diff --git a/packages/engine/src/schema.ts b/packages/engine/src/schema.ts index 9002972b..f95aa423 100644 --- a/packages/engine/src/schema.ts +++ b/packages/engine/src/schema.ts @@ -17,6 +17,7 @@ import { DATA_ID } from './constants'; /** * 标签规则 */ +const SCHEMA_KEYS = ['blocks', 'inlines', 'marks', 'globals']; class Schema implements SchemaInterface { private _all: Array = []; private _typeMap: { @@ -150,7 +151,7 @@ class Schema implements SchemaInterface { */ find(callback: (rule: SchemaRule) => boolean): Array { const schemas: Array = []; - Object.keys(this.data).forEach((key) => { + SCHEMA_KEYS.forEach((key) => { if (key !== 'globals') { const rules = (this.data[key] as Array).filter( callback, @@ -368,9 +369,7 @@ class Schema implements SchemaInterface { const rule = this.getRule(node); if (!rule) return; const { globals } = this.data; - const globalRule = Object.keys(globals).find( - (dataType) => rule.type === dataType, - ); + const globalRule = globals[rule.type] ? rule.type : undefined; const allRule = { ...omit(rule, 'attributes'), attributes: merge( diff --git a/plugins/table/src/index.css b/plugins/table/src/index.css index 7b33c13f..d2c6058f 100644 --- a/plugins/table/src/index.css +++ b/plugins/table/src/index.css @@ -469,8 +469,8 @@ div[data-card-key="table"].card-selected .data-table, div[data-card-key="table"] } .table-wrapper .table-main-content { - margin: 4px 4px; - padding: 0 2px; + margin: 2px 2px; + padding: 4px 4px; position: relative; z-index: 3; overflow: hidden; diff --git a/plugins/table/src/index.ts b/plugins/table/src/index.ts index 52dd6c38..58a54aad 100644 --- a/plugins/table/src/index.ts +++ b/plugins/table/src/index.ts @@ -377,54 +377,52 @@ class Table extends Plugin { `[${CARD_KEY}="${TableComponent.cardName}"],[${READY_CARD_KEY}="${TableComponent.cardName}"]`, ).each((tableNode) => { const node = $(tableNode); - const value = decodeCardValue( - node.attributes(CARD_VALUE_KEY), - ); - if (value && value.html) { - let table = node.find('table'); + let table = node.find('table'); + if (table.length === 0) { + const value = decodeCardValue( + node.attributes(CARD_VALUE_KEY), + ); + if (!value || !value.html) return; + // 表格值里面的卡片都是没有被转换过的,所以需要先把卡片转换过来 + table = $(value.html); if (table.length === 0) { - // 表格值里面的卡片都是没有被转换过的,所以需要先把卡片转换过来 - table = $(value.html); - if (table.length === 0) { - node.remove(); - return; - } else { - const cards = table.find(CARD_SELECTOR).toArray(); - cards.forEach((componentNode) => { - this.editor.trigger('parse:html', componentNode); - }); - } - } - const width = table.attributes('width') || table.css('width'); - table.css({ - outline: 'none', - 'border-collapse': 'collapse', - width: '100%', - }); - table.attributes('data-width', width); - const tds = table.find('td'); - tds.each((_, index) => { - const tdElement = tds.eq(index); - tdElement?.css({ - 'min-width': 'auto', - 'white-space': 'flat', - 'word-wrap': 'break-word', - margin: '4px 8px', - border: !!table.attributes('data-table-no-border') - ? '0 none' - : '1px solid #d9d9d9', - padding: '4px 8px', - cursor: 'default', - 'vertical-align': - tdElement.css('vertical-align') || 'top', + node.remove(); + return; + } else { + const cards = table.find(CARD_SELECTOR).toArray(); + cards.forEach((componentNode) => { + this.editor.trigger('parse:html', componentNode); }); - }); - table.find(Template.TABLE_TD_BG_CLASS).remove(); - table.find(Template.TABLE_TD_CONTENT_CLASS).each((content) => { - this.editor.node.unwrap($(content)); - }); - node.replaceWith(table); + } } + const width = table.attributes('width') || table.css('width'); + table.css({ + outline: 'none', + 'border-collapse': 'collapse', + width: '100%', + }); + table.attributes('data-width', width); + const tds = table.find('td'); + tds.each((_, index) => { + const tdElement = tds.eq(index); + tdElement?.css({ + 'min-width': 'auto', + 'white-space': 'flat', + 'word-wrap': 'break-word', + margin: '4px 8px', + border: !!table.attributes('data-table-no-border') + ? '0 none' + : '1px solid #d9d9d9', + padding: '4px 8px', + cursor: 'default', + 'vertical-align': tdElement.css('vertical-align') || 'top', + }); + }); + table.find(Template.TABLE_TD_BG_CLASS).remove(); + table.find(Template.TABLE_TD_CONTENT_CLASS).each((content) => { + this.editor.node.unwrap($(content)); + }); + node.replaceWith(table); }); }