From f4e759e20a44bbd40fcc76b161c8711246b0e277 Mon Sep 17 00:00:00 2001 From: cycleccc <2991205548@qq.com> Date: Thu, 26 Dec 2024 23:41:41 +0800 Subject: [PATCH] fix(table): allow to reduce width (#475) * fix(table): first calculate through tdList * fix(table): allow to reduce width * Create smooth-trains-fry.md --- .changeset/smooth-trains-fry.md | 6 ++++++ .../table-module/src/module/column-resize.ts | 7 +++++-- .../table-module/src/module/parse-elem-html.ts | 17 +++++++++-------- 3 files changed, 20 insertions(+), 10 deletions(-) create mode 100644 .changeset/smooth-trains-fry.md diff --git a/.changeset/smooth-trains-fry.md b/.changeset/smooth-trains-fry.md new file mode 100644 index 000000000..b6559a619 --- /dev/null +++ b/.changeset/smooth-trains-fry.md @@ -0,0 +1,6 @@ +--- +"@wangeditor-next/table-module": patch +"@wangeditor-next/editor": patch +--- + +375 复制excel数据到编辑器中格式错乱 diff --git a/packages/table-module/src/module/column-resize.ts b/packages/table-module/src/module/column-resize.ts index 1b8f6f18a..5ee7c7939 100644 --- a/packages/table-module/src/module/column-resize.ts +++ b/packages/table-module/src/module/column-resize.ts @@ -133,8 +133,11 @@ const onMouseMove = throttle((event: Event) => { // 如果拖动引起的宽度超过容器宽度,则不调整 const containerElement = document.querySelector('.table-container') - if (containerElement && remainWidth + newWith > containerElement.clientWidth) { - return + if (containerElement && newWith > cellWidthWhenMouseDown) { + // 允许缩小,但不允许放大 + if (remainWidth + newWith > containerElement.clientWidth) { + newWith = Math.max(30, cellWidthWhenMouseDown) // 确保不小于最小宽度 + } } const adjustColumnWidths = [...columnWidths].map(width => Math.floor(width)) diff --git a/packages/table-module/src/module/parse-elem-html.ts b/packages/table-module/src/module/parse-elem-html.ts index 1d9d1ec74..2bc248a99 100644 --- a/packages/table-module/src/module/parse-elem-html.ts +++ b/packages/table-module/src/module/parse-elem-html.ts @@ -128,23 +128,24 @@ function parseTableHtml( const tdList = $elem.find('tr')[0]?.children || [] const colgroupElments: HTMLCollection = $elem.find('colgroup')[0]?.children || null - if (colgroupElments) { - tableELement.columnWidths = Array.from(colgroupElments).map((col: any) => { - return parseInt(col.getAttribute('width'), 10) - }) - } else if (tdList.length > 0) { + if (tdList.length > 0) { const columnWidths: number[] = [] Array.from(tdList).forEach(td => { const colSpan = parseInt($(td).attr('colSpan') || '1', 10) // 获取 colSpan,默认为 1 - const width = parseInt(getStyleValue($(td), 'width') || '180', 10) // 获取 width,默认为 180 + const width = parseInt(getStyleValue($(td), 'width') || '90', 10) // 获取 width,默认为 180 // 根据 colSpan 的值来填充 columnWidths 数组 - for (let i = 0; i < colSpan; i += 1) { - columnWidths.push(width) + columnWidths[0] = width + for (let i = 1; i < colSpan; i += 1) { + columnWidths.push(90) } }) tableELement.columnWidths = columnWidths + } else if (colgroupElments) { + tableELement.columnWidths = Array.from(colgroupElments).map((col: any) => { + return parseInt(col.getAttribute('width'), 10) + }) } return tableELement }