-
Notifications
You must be signed in to change notification settings - Fork 1
/
attributecommand.js
51 lines (44 loc) · 1.31 KB
/
attributecommand.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
import Command from "@ckeditor/ckeditor5-core/src/command";
export default class AttributeCommand extends Command {
constructor(editor, attributeKey) {
super(editor);
this.attributeKey = attributeKey;
}
refresh() {
const model = this.editor.model;
const doc = model.document;
this.value = doc.selection.hasAttribute(this.attributeKey);
this.isEnabled = model.schema.checkAttributeInSelection(
doc.selection,
this.attributeKey
);
}
execute(options = {}) {
const model = this.editor.model;
const doc = model.document;
const selection = doc.selection;
const value =
options.forceValue === undefined ? !this.value : options.forceValue;
model.change(writer => {
if (selection.isCollapsed) {
if (value) {
writer.setSelectionAttribute(this.attributeKey, true);
} else {
writer.removeSelectionAttribute(this.attributeKey);
}
} else {
const ranges = model.schema.getValidRanges(
selection.getRanges(),
this.attributeKey
);
for (const range of ranges) {
if (value) {
writer.setAttribute(this.attributeKey, value, range);
} else {
writer.removeAttribute(this.attributeKey, range);
}
}
}
});
}
}