forked from slab/quill
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlist.js
113 lines (98 loc) · 2.87 KB
/
list.js
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import Parchment from 'parchment';
import Block from '../blots/block';
import Container from '../blots/container';
class ListItem extends Block {
static formats(domNode) {
return domNode.tagName === this.tagName ? undefined : super.formats(domNode);
}
format(name, value) {
if (name === List.blotName && !value) {
this.replaceWith(Parchment.create(this.statics.scope));
} else {
super.format(name, value);
}
}
remove() {
if (this.prev == null && this.next == null) {
this.parent.remove();
} else {
super.remove();
}
}
replaceWith(name, value) {
this.parent.isolate(this.offset(this.parent), this.length());
if (name === this.parent.statics.blotName) {
this.parent.replaceWith(name, value);
return this;
} else {
this.parent.unwrap();
return super.replaceWith(name, value);
}
}
}
ListItem.blotName = 'list-item';
ListItem.tagName = 'LI';
class List extends Container {
static create(value) {
let tagName = value === 'ordered' ? 'OL' : 'UL';
let node = super.create(tagName);
if (value === 'checked' || value === 'unchecked') {
node.setAttribute('data-checked', value === 'checked');
}
return node;
}
static formats(domNode) {
if (domNode.tagName === 'OL') return 'ordered';
if (domNode.tagName === 'UL') {
if (domNode.hasAttribute('data-checked')) {
return domNode.getAttribute('data-checked') === 'true' ? 'checked' : 'unchecked';
} else {
return 'bullet';
}
}
return undefined;
}
format(name, value) {
if (this.children.length > 0) {
this.children.tail.format(name, value);
}
}
formats() {
// We don't inherit from FormatBlot
return { [this.statics.blotName]: this.statics.formats(this.domNode) };
}
insertBefore(blot, ref) {
if (blot instanceof ListItem) {
super.insertBefore(blot, ref);
} else {
let index = ref == null ? this.length() : ref.offset(this);
let after = this.split(index);
after.parent.insertBefore(blot, after);
}
}
optimize() {
super.optimize();
let next = this.next;
if (next != null && next.prev === this &&
next.statics.blotName === this.statics.blotName &&
next.domNode.tagName === this.domNode.tagName &&
next.domNode.getAttribute('data-checked') === this.domNode.getAttribute('data-checked')) {
next.moveChildren(this);
next.remove();
}
}
replace(target) {
if (target.statics.blotName !== this.statics.blotName) {
let item = Parchment.create(this.statics.defaultChild);
target.moveChildren(item);
this.appendChild(item);
}
super.replace(target);
}
}
List.blotName = 'list';
List.scope = Parchment.Scope.BLOCK_BLOT;
List.tagName = ['OL', 'UL'];
List.defaultChild = 'list-item';
List.allowedChildren = [ListItem];
export { ListItem, List as default };