Skip to content

Commit

Permalink
fix(table): allow to reduce width (#475)
Browse files Browse the repository at this point in the history
* fix(table): first calculate through tdList

* fix(table): allow to reduce width

* Create smooth-trains-fry.md
  • Loading branch information
cycleccc authored Dec 26, 2024
1 parent 163b2b3 commit f4e759e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .changeset/smooth-trains-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@wangeditor-next/table-module": patch
"@wangeditor-next/editor": patch
---

375 复制excel数据到编辑器中格式错乱
7 changes: 5 additions & 2 deletions packages/table-module/src/module/column-resize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
17 changes: 9 additions & 8 deletions packages/table-module/src/module/parse-elem-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit f4e759e

Please sign in to comment.