Skip to content

Commit

Permalink
fix: lint —fix & test tuning
Browse files Browse the repository at this point in the history
  • Loading branch information
藜灰 committed Jul 10, 2024
1 parent 451a481 commit 26c254b
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 31 deletions.
6 changes: 3 additions & 3 deletions packages/basic-modules/__tests__/image/render-elem.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ describe('image render elem', () => {
src,
alt: 'logo',
href,
style: { width: '100', height: '80' },
style: { width: '100' },
children: [{ text: '' }], // void node 必须包含一个空 text
}

const containerVnode = renderImageConf.renderElem(elem, null, editor) as any
expect(containerVnode.sel).toBe('div')
expect(containerVnode.data.className).toBe('w-e-image-container')
expect(containerVnode.data.style.width).toBe('100')
expect(containerVnode.data.style.height).toBe('80')
// expect(containerVnode.data.style.height).toBe('80')

const imageVnode = containerVnode.children[0] as any
expect(imageVnode.sel).toBe('img')
Expand All @@ -57,7 +57,7 @@ describe('image render elem', () => {
src,
alt: 'logo',
href,
style: { width: '100', height: '80' },
style: { width: '100' },
children: [{ text: '' }], // void node 必须包含一个空 text
}

Expand Down
13 changes: 8 additions & 5 deletions packages/core/src/editor/dom-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,14 @@ export const DomEditor = {

return (
// 祖先节点中包括 data-slate-editor 属性,即 textarea
targetEl.closest(`[data-slate-editor]`) === editorEl &&
// 通过参数 editable 控制开启是否验证是可编辑元素或零宽字符
// 补全 data-slate-string 可参考本文代码
//(data-slate-zero-width、data-slate-string)判断一起出现,唯独此处欠缺,补全
(!editable || targetEl.isContentEditable || !!targetEl.getAttribute('data-slate-zero-width')) || !!targetEl.getAttribute('data-slate-string')
(targetEl.closest(`[data-slate-editor]`) === editorEl &&
// 通过参数 editable 控制开启是否验证是可编辑元素或零宽字符
// 补全 data-slate-string 可参考本文代码
//(data-slate-zero-width、data-slate-string)判断一起出现,唯独此处欠缺,补全
(!editable ||
targetEl.isContentEditable ||
!!targetEl.getAttribute('data-slate-zero-width'))) ||
!!targetEl.getAttribute('data-slate-string')
)
},

Expand Down
10 changes: 7 additions & 3 deletions packages/table-module/__tests__/elem-to-html.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('TableModule module', () => {
.mockReturnValueOnce({ type: 'table', children: [{ text: '' }] } as Ancestor)

const res = tableCellToHtmlConf.elemToHtml(element, '<span>123</span>')
expect(res).toBe('<td colSpan="1" rowSpan="1" width="auto"><span>123</span></td>')
expect(res).toBe('<td colSpan="1" rowSpan="1" width="auto" style=""><span>123</span></td>')
})

test('tableRowToHtmlConf should return object that include "type" and "elemToHtml" property', () => {
Expand Down Expand Up @@ -87,7 +87,9 @@ describe('TableModule module', () => {
children: [],
}
const res = tableToHtmlConf.elemToHtml(element, '<tr><td>123</td></tr>')
expect(res).toBe('<table style="width: auto;"><tbody><tr><td>123</td></tr></tbody></table>')
expect(res).toBe(
'<table style="width: auto;table-layout: fixed;"><tbody><tr><td>123</td></tr></tbody></table>'
)
})

test('tableToHtmlConf should return html table string with full width style if element is set fullWith value true', () => {
Expand All @@ -97,7 +99,9 @@ describe('TableModule module', () => {
children: [],
}
const res = tableToHtmlConf.elemToHtml(element, '<tr><td>123</td></tr>')
expect(res).toBe('<table style="width: 100%;"><tbody><tr><td>123</td></tr></tbody></table>')
expect(res).toBe(
'<table style="width: 100%;table-layout: fixed;"><tbody><tr><td>123</td></tr></tbody></table>'
)
})
})
})
12 changes: 10 additions & 2 deletions packages/table-module/__tests__/render-elem.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,23 @@ describe('table module - render elem', () => {
expect(renderTableConf.type).toBe('table')

const elem = { type: 'table', children: [] }
const containerVnode = renderTableConf.renderElem(elem, null, editor) as any

/**
* 改变了结构,新增外层 DIV
*/
const observerVnode = renderTableConf.renderElem(elem, null, editor) as any
expect(observerVnode.sel).toBe('div')
const containerVnode = observerVnode.children[0] as any
expect(containerVnode.sel).toBe('div')
const tableVnode = containerVnode.children[0] as any
expect(tableVnode.sel).toBe('table')
})

it('render table elem with full with', () => {
const elem = { type: 'table', children: [], width: '100%' }
const containerVnode = renderTableConf.renderElem(elem, null, editor) as any

const observerVnode = renderTableConf.renderElem(elem, null, editor) as any
const containerVnode = observerVnode.children[0] as any
const tableVnode = containerVnode.children[0] as any
expect(tableVnode.data.width).toBe('100%')
})
Expand Down
1 change: 0 additions & 1 deletion packages/table-module/src/constants/svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,3 @@ export const MERGE_CELL_SVG =
// 拆分单元格
export const SPLIT_CELL_SVG =
'<svg viewBox="0 0 1024 1024"><path d="M362.667 494.933v53.334l25.6-25.6zm0-241.066L460.8 352V78.933H57.6v98.134h305.067zm0 535.466v57.6H57.6v98.134h403.2V691.2zM661.333 494.933v53.334l-25.6-25.6zm0-241.066L563.2 352V78.933h403.2v98.134H661.333zm0 535.466v57.6H966.4v98.134H563.2V691.2z"/><path d="M753.067 341.333 693.333 281.6 512 460.8 330.667 281.6l-59.734 59.733 181.334 181.334L270.933 704l59.734 59.733L512 582.4l181.333 181.333L753.067 704 571.733 522.667z"/></svg>'

2 changes: 1 addition & 1 deletion packages/table-module/src/module/elem-to-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function tableCellToHtml(cellNode: Element, childrenHtml: string): string {
rowSpan = 1,
isHeader = false,
width = 'auto',
hidden = false
hidden = false,
} = cellNode as TableCellElement
const tag = isHeader ? 'th' : 'td'
const style = hidden ? 'display:none' : ''
Expand Down
1 change: 0 additions & 1 deletion packages/table-module/src/module/menu/DeleteCol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ class DeleteCol implements IButtonMenu {
at: tablePath,
})
})

}
}

Expand Down
2 changes: 0 additions & 2 deletions packages/table-module/src/module/menu/InsertCol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ class InsertCol implements IButtonMenu {
at: tablePath,
})
})


}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/table-module/src/module/menu/InsertTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function genTableNode(rowNum: number, colNum: number): TableElement {
type: 'table',
width: 'auto',
children: rows,
columnWidths
columnWidths,
}
}

Expand Down
1 change: 0 additions & 1 deletion packages/table-module/src/module/menu/SplitCell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ class SplitCell implements IButtonMenu {
}
continue out
}

}

for (let c = 1; c < colSpan; c++) {
Expand Down
1 change: 0 additions & 1 deletion packages/table-module/src/module/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ function withTable<T extends IDomEditor>(editor: T): T {
newEditor.select(newSelection) // 选中 table-cell 内部的全部文字
}


/**
* 光标选区行为新增
*/
Expand Down
14 changes: 9 additions & 5 deletions packages/table-module/src/module/render-elem/render-cell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* @author wangfupeng
*/


import { Element as SlateElement } from 'slate'
import { jsx, VNode } from 'snabbdom'
import { IDomEditor } from '@wangeditor-next/core'
Expand All @@ -17,14 +16,20 @@ function renderTableCell(
editor: IDomEditor
): VNode {
const isFirstRow = isCellInFirstRow(editor, cellNode as TableCellElement)
const { colSpan = 1, rowSpan = 1, isHeader = false, hidden = false, } = cellNode as TableCellElement
const {
colSpan = 1,
rowSpan = 1,
isHeader = false,
hidden = false,
} = cellNode as TableCellElement
const selected = TableCursor.isSelected(editor, cellNode)


// ------------------ 不是第一行,直接渲染 <td> ------------------
if (!isFirstRow) {
return (
<td colSpan={colSpan} rowSpan={rowSpan}
<td
colSpan={colSpan}
rowSpan={rowSpan}
/**
* 1. 添加一个方便寻址的 block-type
* 2. 选区颜色
Expand All @@ -46,7 +51,6 @@ function renderTableCell(
<Tag
colSpan={colSpan}
rowSpan={rowSpan}

/**
* 1. 添加一个方便寻址的 block-type
* 2. 选区颜色
Expand Down
13 changes: 8 additions & 5 deletions packages/table-module/src/module/render-elem/render-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ function renderTable(elemNode: SlateElement, children: VNode[] | null, editor: I
// 列宽之间比值
const columnWidthRatios = getColumnWidthRatios(columnWidths)


const vnode = (
<div
className="table-container"
Expand Down Expand Up @@ -104,14 +103,18 @@ function renderTable(elemNode: SlateElement, children: VNode[] | null, editor: I
},
}}
>
<table width={tableWidth} contentEditable={editable}
<table
width={tableWidth}
contentEditable={editable}
/**
* 1. 当表格处于选区状态,屏蔽 Chrome 自带的样式
* 2. table 宽度为 auto 时,宽度为 列宽之和
* 3. 鼠标移动到 单元格 边缘,设置 visible className
*/
className={'table ' + (isSelecting ? 'table-selection-none' : '')}
style={{ width: tableWidth == '100%' ? '' : (columnWidths.reduce((a, b) => a + b, 0) + 'px') }}
style={{
width: tableWidth == '100%' ? '' : columnWidths.reduce((a, b) => a + b, 0) + 'px',
}}
on={{
mousemove: debounce((e: MouseEvent) => handleCellBorderVisible(editor, elemNode, e), 25),
}}
Expand All @@ -125,7 +128,8 @@ function renderTable(elemNode: SlateElement, children: VNode[] | null, editor: I
*/
columnWidths.map(width => {
return <col width={width}></col>
})}
})
}
</colgroup>
<tbody>{children}</tbody>
</table>
Expand Down Expand Up @@ -163,7 +167,6 @@ function renderTable(elemNode: SlateElement, children: VNode[] | null, editor: I
)
})}
</div>

</div>
)

Expand Down

0 comments on commit 26c254b

Please sign in to comment.