forked from marktext/marktext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeleteCtrl.js
More file actions
82 lines (72 loc) · 2.45 KB
/
deleteCtrl.js
File metadata and controls
82 lines (72 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import selection from '../selection'
const deleteCtrl = ContentState => {
// Handle `delete` keydown event on document.
ContentState.prototype.docDeleteHandler = function (event) {
// handle delete selected image
const { selectedImage } = this
if (selectedImage) {
event.preventDefault()
this.selectedImage = null
return this.deleteImage(selectedImage)
}
if (this.selectedTableCells) {
event.preventDefault()
return this.deleteSelectedTableCells()
}
}
ContentState.prototype.deleteHandler = function (event) {
const { start, end } = selection.getCursorRange()
if (!start || !end) {
return
}
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 (/span/.test(type) && start.offset === 0 && text[1] === '\n') {
event.preventDefault()
startBlock.text = text.substring(2)
this.cursor = {
start: { key, offset: 0 },
end: { key, offset: 0 }
}
return this.singleRender(startBlock)
}
if (
/h\d|span/.test(type) &&
start.offset === text.length
) {
event.preventDefault()
if (nextBlock && /h\d|span/.test(nextBlock.type)) {
// if cursor at the end of code block-language input, do nothing!
if (nextBlock.functionType === 'codeContent' && startBlock.functionType === 'languageInput') {
return
}
startBlock.text += nextBlock.text
const toBeRemoved = [nextBlock]
let parent = this.getParent(nextBlock)
let target = nextBlock
while (this.isOnlyRemoveableChild(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