mirror of
https://github.com/marktext/marktext.git
synced 2025-05-18 01:40:23 +08:00
51 lines
1.5 KiB
JavaScript
51 lines
1.5 KiB
JavaScript
import selection from '../selection'
|
|
|
|
const deleteCtrl = ContentState => {
|
|
ContentState.prototype.deleteHandler = function (event) {
|
|
const { start, end } = selection.getCursorRange()
|
|
const startBlock = this.getBlock(start.key)
|
|
const nextBlock = this.findNextBlockInLocation(startBlock)
|
|
|
|
// TODO: @jocs It will delete all the editor and cause error in console when there is only one empty table. same as #67
|
|
if (startBlock.type === 'figure') event.preventDefault()
|
|
// If select multiple paragraph or multiple characters in one paragraph, just let
|
|
// updateCtrl to handle this case.
|
|
if (start.key !== end.key || start.offset !== end.offset) {
|
|
return
|
|
}
|
|
// Only handle h1~h6 span block
|
|
const { type, text, key } = startBlock
|
|
if (
|
|
/h\d|span/.test(type) &&
|
|
start.offset === text.length
|
|
) {
|
|
event.preventDefault()
|
|
if (nextBlock && /h\d|span/.test(nextBlock.type)) {
|
|
startBlock.text += nextBlock.text
|
|
|
|
const toBeRemoved = [ nextBlock ]
|
|
|
|
let parent = this.getParent(nextBlock)
|
|
let target = nextBlock
|
|
|
|
while (this.isOnlyChild(target)) {
|
|
toBeRemoved.push(parent)
|
|
target = parent
|
|
parent = this.getParent(parent)
|
|
}
|
|
|
|
toBeRemoved.forEach(b => this.removeBlock(b))
|
|
|
|
const offset = start.offset
|
|
this.cursor = {
|
|
start: { key, offset },
|
|
end: { key, offset }
|
|
}
|
|
this.render()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
export default deleteCtrl
|