Skip to content

Commit

Permalink
fix: 修复内容随后设置标题,会报错,而且无法再编辑的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Gavin-yh committed Oct 4, 2021
1 parent feac0af commit 5022ceb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
3 changes: 0 additions & 3 deletions src/menus/head/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ class Head extends DropListMenu implements MenuActive {
},
],
clickHandler: (value: string) => {
if (editor.$textElem.html() === EMPTY_P) {
return
}
// 注意 this 是指向当前的 Head 对象
this.command(value)
},
Expand Down
18 changes: 16 additions & 2 deletions src/text/event-hooks/del-to-keep-p.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,25 @@ function deleteToKeepP(editor: Editor, deleteUpEvents: Function[], deleteDownEve
// 内容空了
$textElem.html(EMPTY_P)

editor.selection.createRangeByElem($textElem, false, true)
/**
* 当编辑器 - 文本区内容为空的情况下,会插入一个空的P,此时应该将选区移动到这个空标签上,重置选区
* bug: 如果选区没有从$textElem上调整到p上,就会有问题,在清空内容,设置标题时,会报错。
*/
const containerElem = $textElem.getNode()

// 设置新的选区
editor.selection.createRangeByElems(
containerElem.childNodes[0],
containerElem.childNodes[0]
)

const $selectionElem = editor.selection.getSelectionContainerElem()!

editor.selection.restoreSelection()

// 设置折叠后的光标位置,在firebox等浏览器下
// 光标设置在end位置会自动换行
editor.selection.moveCursor($textElem.getNode(), 0)
editor.selection.moveCursor($selectionElem.getNode(), 0)
}
}
deleteUpEvents.push(upFn)
Expand Down

0 comments on commit 5022ceb

Please sign in to comment.