Skip to content

Commit

Permalink
refactor(table): improve code readability
Browse files Browse the repository at this point in the history
  • Loading branch information
Ragnar-Oock committed Aug 14, 2024
1 parent b4e92c5 commit d0b9e8e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
11 changes: 8 additions & 3 deletions packages/extension-table/src/TableView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,19 @@ export function updateColumns(
fixedWidth = false
}

if (nextDOM === null) {
if (!nextDOM) {
const colElement = document.createElement('col')

colElement.style.setProperty(...getColStyleDeclaration(cellMinWidth, hasWidth))
const [propertyKey, propertyValue] = getColStyleDeclaration(cellMinWidth, hasWidth)

colElement.style.setProperty(propertyKey, propertyValue)

colgroup.appendChild(colElement)
} else {
if ((nextDOM as HTMLTableColElement).style.width !== cssWidth) {
(nextDOM as HTMLTableColElement).style.setProperty(...getColStyleDeclaration(cellMinWidth, hasWidth))
const [propertyKey, propertyValue] = getColStyleDeclaration(cellMinWidth, hasWidth);

(nextDOM as HTMLTableColElement).style.setProperty(propertyKey, propertyValue)
}

nextDOM = nextDOM.nextSibling
Expand Down
4 changes: 0 additions & 4 deletions packages/extension-table/src/utilities/colStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,3 @@ export function getColStyleDeclaration(minWidth: number, width: number | undefin
return ['min-width', `${minWidth}px`]

}

export function getColStyle(minWidth: number, width: number): string {
return getColStyleDeclaration(minWidth, width).join(': ')
}
6 changes: 4 additions & 2 deletions packages/extension-table/src/utilities/createColGroup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DOMOutputSpec, Node as ProseMirrorNode } from '@tiptap/pm/model'

import { getColStyle } from './colStyle.js'
import { getColStyleDeclaration } from './colStyle.js'

export type ColGroup = {
colgroup: DOMOutputSpec
Expand Down Expand Up @@ -54,9 +54,11 @@ export function createColGroup(
fixedWidth = false
}

const [property, value] = getColStyleDeclaration(cellMinWidth, hasWidth)

cols.push([
'col',
{ style: getColStyle(cellMinWidth, hasWidth) },
{ style: { [property]: value } },
])
}
}
Expand Down

0 comments on commit d0b9e8e

Please sign in to comment.