Skip to content

Commit

Permalink
Merge branch 'cycleccc:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
wjw020206 authored Aug 17, 2024
2 parents 93a2d4b + e37a391 commit e80bed6
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 43 deletions.
53 changes: 53 additions & 0 deletions packages/basic-modules/__tests__/common/enter-menu.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* @description enter menu test
* @author cycleccc
*/

import { Editor, Transforms } from 'slate'
import createEditor from '../../../../tests/utils/create-editor'
import EnterMenu from '../../src/modules/common/menu/EnterMenu'

describe('enter menu', () => {
const menu = new EnterMenu()
let editor: any
let startLocation: any

beforeEach(() => {
editor = createEditor()
startLocation = Editor.start(editor, [])
})

afterEach(() => {
editor = null
startLocation = null
})

it('get value', () => {
expect(menu.getValue(editor)).toBe('')
})

it('is active', () => {
expect(menu.isActive(editor)).toBeFalsy()
})

it('is disabled', () => {
editor.deselect()
expect(menu.isDisabled(editor)).toBeTruthy()
editor.select(startLocation)
expect(menu.isDisabled(editor)).toBeFalsy()
editor.insertText('hello')
editor.select([])
expect(menu.isDisabled(editor)).toBeTruthy()
})

it('exec', () => {
editor.deselect()
expect(menu.exec(editor, '')).toBeUndefined()
editor.select(startLocation)
menu.exec(editor, '')
expect(editor.children).toStrictEqual([
{ children: [{ text: '' }], type: 'paragraph' },
{ children: [{ text: '' }], type: 'paragraph' },
])
})
})
13 changes: 11 additions & 2 deletions packages/basic-modules/__tests__/justify/menus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ describe('justify menus', () => {
startLocation = null
})

// getValue getActive 不需要测试
it('get value', () => {
expect(centerMenu.getValue(editor)).toBe('')
})

it('is active', () => {
expect(centerMenu.isActive(editor)).toBeFalsy()
})

it('is disabled', () => {
editor.deselect()
Expand All @@ -40,7 +46,10 @@ describe('justify menus', () => {

editor.insertNode({ type: 'pre', children: [{ type: 'code', children: [{ text: 'var' }] }] })
expect(centerMenu.isDisabled(editor)).toBeTruthy()
// Transforms.removeNodes(editor, { mode: 'highest' }) // 移除 pre/code
Transforms.removeNodes(editor, { mode: 'highest' })

editor.insertNode({ type: 'divider', children: [{ text: '' }] })
expect(centerMenu.isDisabled(editor)).toBeTruthy()
})

it('exec', () => {
Expand Down
17 changes: 17 additions & 0 deletions packages/basic-modules/__tests__/utils/dom.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @description utils dom
* @author cycleccc
*/

import { getTagName } from '../../src/utils/dom'
import { Dom7Array } from 'dom7'

describe('redo menu', () => {
it('get tag name', () => {
// 模拟一个空的 Dom7Array 对象
const emptyElem: Dom7Array = [] as unknown as Dom7Array

const result = getTagName(emptyElem)
expect(result).toBe('') // 验证返回的是空字符串
})
})
24 changes: 0 additions & 24 deletions packages/basic-modules/src/modules/justify/menu/BaseMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,6 @@ abstract class BaseMenu implements IButtonMenu {
return false
}

/**
* 获取 node 节点
* @param editor editor
*/
protected getMatchNode(editor: IDomEditor): Node | null {
const [nodeEntry] = Editor.nodes(editor, {
match: n => {
const type = DomEditor.getNodeType(n)

// 只可用于 p blockquote header
if (type === 'paragraph') return true
if (type === 'blockquote') return true
if (type.startsWith('header')) return true

return false
},
universal: true,
mode: 'highest', // 匹配最高层级
})

if (nodeEntry == null) return null
return nodeEntry[0]
}

isDisabled(editor: IDomEditor): boolean {
if (editor.selection == null) return true

Expand Down
15 changes: 15 additions & 0 deletions packages/code-highlight/__tests__/decorate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { IDomEditor } from '@wangeditor-next/core'
import createEditor from '../../../tests/utils/create-editor'
import codeHighLightDecorate from '../src/decorate/index'
import { content, textNode, textNodePath } from './content'
import { getPrismTokenLength } from '../src/vendor/prism'

describe('code-highlight decorate', () => {
let editor: IDomEditor | null = null
Expand All @@ -29,4 +30,18 @@ describe('code-highlight decorate', () => {
const ranges = codeHighLightDecorate([textNode, textNodePath])
expect(ranges.length).toBe(4) // 把 textNode 内容拆分为 4 段
})

it('getPrismTokenLength', () => {
const token = {
type: 'example',
content: [
'hello', // length 5
{ type: 'nested', content: 'world' }, // length 5
{ type: 'nested', content: ['foo', { type: 'deepNested', content: 'bar' }] }, // length 3 + 3 = 6
],
}

const result = getPrismTokenLength(token)
expect(result).toBe(16) // 'hello' (5) + 'world' (5) + 'foo' (3) + 'bar' (3) = 16
})
})
6 changes: 4 additions & 2 deletions packages/code-highlight/__tests__/elem-to-html.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { IDomEditor } from '@wangeditor-next/core'
import createEditor from '../../../tests/utils/create-editor'
import { codeToHtmlConf } from '../src/module/elem-to-html'
import { content, codeNode, language } from './content'
import { content, codeNode, preNode, language } from './content'

describe('code-highlight elem to html', () => {
let editor: IDomEditor | null = null
Expand All @@ -30,7 +30,9 @@ describe('code-highlight elem to html', () => {

if (editor == null) throw new Error('editor is null')
const text = 'var n = 100;'
const html = codeToHtmlConf.elemToHtml(codeNode, text)
let html = codeToHtmlConf.elemToHtml(codeNode, text)
expect(html).toBe(`<code class="language-${language}">${text}</code>`)
html = codeToHtmlConf.elemToHtml(preNode, text)
expect(html).toBe(`<code >${text}</code>`)
})
})
16 changes: 16 additions & 0 deletions packages/code-highlight/__tests__/select-lang-menu.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,20 @@ describe('code-highlight select lang menu', () => {
done()
})
})

it('menu exec (without lang)', done => {
if (editor == null || menu == null) throw new Error('editor or menu is null')

// select codeNode
editor.select(codeLocation)
menu.exec(editor, 'hello') // change lang

setTimeout(() => {
if (editor == null || menu == null) return

editor.select(codeLocation)
expect(menu.getValue(editor)).toBe('')
done()
})
})
})
15 changes: 1 addition & 14 deletions packages/code-highlight/src/utils/vdom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @author wangfupeng
*/

import { VNode, VNodeStyle } from 'snabbdom'
import { VNode } from 'snabbdom'

/**
* 给 vnode 添加 className
Expand All @@ -17,16 +17,3 @@ export function addVnodeClassName(vnode: VNode, className: string) {

Object.assign(data.props, { className })
}

/**
* 给 vnode 添加样式
* @param vnode vnode
* @param newStyle { key: val }
*/
export function addVnodeStyle(vnode: VNode, newStyle: VNodeStyle) {
if (vnode.data == null) vnode.data = {}
const data = vnode.data
if (data.style == null) data.style = {}

Object.assign(data.style, newStyle)
}
20 changes: 19 additions & 1 deletion packages/editor/__tests__/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import { createEditor, createToolbar } from '../../../packages/editor/src/index'
import { ICreateEditorOption, ICreateToolbarOption } from '../../../packages/editor/src/create'
import { DOMElement } from '../../../packages/editor/src/utils/dom'
import Boot from '../../../packages/editor/src/Boot'

function customCreateEditor(config: Partial<ICreateEditorOption> = {}) {
const editorContainer = document.createElement('div')
Expand All @@ -29,14 +31,30 @@ function customCreateToolbar(config: Partial<ICreateToolbarOption> = {}) {
// create toolbar
const toolbar = createToolbar({
editor,
selector: toolbarContainer,
selector: toolbarContainer as DOMElement,
...config,
})

return toolbar
}

describe('create editor and toolbar', () => {
test('create editor selector undefind', () => {
const editor = customCreateEditor()
expect(() => {
createToolbar({
editor,
selector: undefined as any,
})
}).toThrow(`Cannot find 'selector' when create toolbar`)
})

test('test new Boot and registerModule', () => {
expect(() => {
new Boot()
}).toThrow('不能实例化\nCan not construct a instance')
})

test('create editor with default mode', () => {
const editor = customCreateEditor()

Expand Down

0 comments on commit e80bed6

Please sign in to comment.