fix: 修复了列表对齐的问题

This commit is contained in:
luochao 2021-01-11 23:32:59 +08:00
parent fe3e5034fe
commit a4e43fd1f3
2 changed files with 125 additions and 61 deletions

View File

@ -26,7 +26,7 @@ class Justify extends DropListMenu implements MenuActive {
${editor.i18next.t('menus.dropListMenu.justify.靠左')}
</p>`
),
value: 'justifyLeft',
value: 'left',
},
{
$elem: $(
@ -35,7 +35,7 @@ class Justify extends DropListMenu implements MenuActive {
${editor.i18next.t('menus.dropListMenu.justify.居中')}
</p>`
),
value: 'justifyCenter',
value: 'center',
},
{
$elem: $(
@ -44,7 +44,7 @@ class Justify extends DropListMenu implements MenuActive {
${editor.i18next.t('menus.dropListMenu.justify.靠右')}
</p>`
),
value: 'justifyRight',
value: 'right',
},
{
$elem: $(
@ -53,7 +53,7 @@ class Justify extends DropListMenu implements MenuActive {
${editor.i18next.t('menus.dropListMenu.justify.两端')}
</p>`
),
value: 'justifyFull',
value: 'justify',
},
],
clickHandler: (value: string) => {
@ -71,46 +71,54 @@ class Justify extends DropListMenu implements MenuActive {
const editor = this.editor
const selection = editor.selection
const $selectionElem = selection.getSelectionContainerElem()
// 保存选区
selection.saveRange()
// 定义对齐方式的type
type justifyType = {
[key: string]: string
}
// 数据项
const justifyClass: justifyType = {
justifyLeft: 'left',
justifyCenter: 'center',
justifyRight: 'right',
justifyFull: 'justify',
}
// 获取顶级元素
const $elems = editor.selection.getSelectionRangeTopNodes(editor)
if ($selectionElem) {
// 获取在css中对应style的值
const justifyValue = justifyClass[value]
$elems.forEach((el: DomElement) => {
el.css('text-align', justifyValue)
})
if ($selectionElem?.length) {
// list 在chrome下默认多包裹一个 p导致不能通过顶层元素判断所以单独加个判断
if (this.isSpecialNode($selectionElem) || this.isSpecialTopNode($elems[0])) {
$selectionElem.css('text-align', value)
} else {
$elems.forEach((el: DomElement) => {
el.css('text-align', value)
})
}
}
//恢复选区
selection.restoreSelection()
}
/**
*
* @param el DomElement
*/
private isSpecialNode(el: DomElement) {
const selectionNode = el.elems[0]
// 如果以后有类似的元素要这样处理,直接修改这个数组即可
const specialNodeLists = ['LI']
return specialNodeLists.indexOf(selectionNode?.nodeName) !== -1
}
/**
* top
* @param el DomElement
*/
private isSpecialTopNode(topEl: DomElement) {
const selectionNode = topEl.elems[0]
// 如果以后有类似的元素要这样处理,直接修改这个数组即可
const specialNodeLists = ['BLOCKQUOTE', 'UL']
return specialNodeLists.indexOf(selectionNode?.nodeName) !== -1
}
/**
*
* ,active进行高亮否则unActive
* ?
*/
public tryChangeActive(): void {
// const editor = this.editor
// let isjustify = ['justifyCenter', 'justifyRight'].some(e => editor.cmd.queryCommandState(e))
// if (isjustify) {
// this.active()
// } else {
// this.unActive()
// }
}
public tryChangeActive(): void {}
}
export default Justify

View File

@ -13,36 +13,92 @@ import { getMenuInstance } from '../../helpers/menus'
let editor: Editor
let justifyMenu: justify
test('justify 菜单dropList', () => {
editor = createEditor(document, 'div1') // 赋值给全局变量
justifyMenu = getMenuInstance(editor, justify) as justify // 赋值给全局变量
expect(justifyMenu.dropList).not.toBeNull()
justifyMenu.dropList.show()
expect(justifyMenu.dropList.isShow).toBe(true)
justifyMenu.dropList.hide()
expect(justifyMenu.dropList.isShow).toBe(false)
})
describe('Justify Menu', () => {
test('justify 菜单dropList', () => {
editor = createEditor(document, 'div1') // 赋值给全局变量
justifyMenu = getMenuInstance(editor, justify) as justify // 赋值给全局变量
expect(justifyMenu.dropList).not.toBeNull()
justifyMenu.dropList.show()
expect(justifyMenu.dropList.isShow).toBe(true)
justifyMenu.dropList.hide()
expect(justifyMenu.dropList.isShow).toBe(false)
})
test('justify 菜单:设置对齐方式', () => {
type justifyType = {
[key: string]: string
}
const justifyClass: justifyType = {
justifyLeft: 'left',
justifyCenter: 'center',
justifyRight: 'right',
justifyFull: 'justify',
}
const mockGetSelectionRangeTopNodes = (tagString: string) => {
const domArr = [$(tagString)]
jest.spyOn(editor.selection, 'getSelectionRangeTopNodes').mockImplementation(() => domArr)
return domArr
}
const $elems = mockGetSelectionRangeTopNodes('<p>123</p>')
for (let key in justifyClass) {
justifyMenu.command(key)
$elems.forEach((el: DomElement) => {
expect(el.elems[0].getAttribute('style')).toContain(`text-align:${justifyClass[key]}`)
})
}
test('justify 菜单:设置对齐方式', () => {
const justifyClasses = ['left', 'right', 'center', 'justify']
const mockGetSelectionRangeTopNodes = (tagString: string) => {
const domArr = [$(tagString)]
jest.spyOn(editor.selection, 'getSelectionRangeTopNodes').mockImplementation(
() => domArr
)
return domArr
}
const $elems = mockGetSelectionRangeTopNodes('<p>123</p>')
for (let value of justifyClasses) {
justifyMenu.command(value)
$elems.forEach((el: DomElement) => {
expect(el.elems[0].getAttribute('style')).toContain(`text-align:${value}`)
})
}
})
test('justify 菜单:设置对齐方式,如果当前选区是 list则只修改当前选区的元素对其样式', () => {
const justifyClasses = ['left', 'right', 'center', 'justify']
const mockGetSelectionRangeTopNodes = (tagString: string) => {
const domArr = [$(tagString)]
jest.spyOn(editor.selection, 'getSelectionRangeTopNodes').mockImplementation(
() => domArr
)
return domArr
}
const mockGetSelectionRangeContainer = (tagString: string) => {
const dom = $(tagString)
jest.spyOn(editor.selection, 'getSelectionContainerElem').mockImplementation(() => dom)
return dom
}
const topElems = mockGetSelectionRangeTopNodes('<ul><li>123</li></ul>')
const containerElems = mockGetSelectionRangeContainer('<li>123</li>')
for (let value of justifyClasses) {
justifyMenu.command(value)
topElems.forEach((el: DomElement) => {
expect(el.elems[0]).not.toHaveStyle(`text-align:${value}`)
})
expect(containerElems.elems[0]).toHaveStyle(`text-align:${value}`)
}
})
test('justify 菜单:设置对齐方式,如果当前选区是 blockquote则只修改当前选区的元素对其样式', () => {
const justifyClasses = ['left', 'right', 'center', 'justify']
const mockGetSelectionRangeTopNodes = (tagString: string) => {
const domArr = [$(tagString)]
jest.spyOn(editor.selection, 'getSelectionRangeTopNodes').mockImplementation(
() => domArr
)
return domArr
}
const mockGetSelectionRangeContainer = (tagString: string) => {
const dom = $(tagString)
jest.spyOn(editor.selection, 'getSelectionContainerElem').mockImplementation(() => dom)
return dom
}
const topElems = mockGetSelectionRangeTopNodes('<blockquote><p>123</p></blockquote>')
const containerElems = mockGetSelectionRangeContainer('<p>123</p>')
for (let value of justifyClasses) {
justifyMenu.command(value)
topElems.forEach((el: DomElement) => {
expect(el.elems[0]).not.toHaveStyle(`text-align:${value}`)
})
expect(containerElems.elems[0]).toHaveStyle(`text-align:${value}`)
}
})
})