-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRadioButtonEditing.js
113 lines (104 loc) · 3.29 KB
/
RadioButtonEditing.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 FormElementEditing from "../FormElementEditing";
import { toWidget } from "@ckeditor/ckeditor5-widget/src/utils";
import formElements from "../form_elements.json";
import CheckboxCommand from "../checkbox/CheckboxCommand";
import checkedIcon from "../theme/icons/radio-button.svg";
import uncheckedIcon from "../theme/icons/radio-button-unchecked.svg";
export default class RadioButtonEditing extends FormElementEditing {
constructor(editor) {
super(editor);
this.command = "insertRadioButton";
// names
this.viewElementName = "input";
this.modelElementName = "radiobutton";
this.label = "Radio Button";
// fields
this.fields = formElements.radiobutton;
// attributes
this.allowAttributes = ["type", "name", "value", "class", "checked"];
this.defaultAttributes = {
type: "radio",
class: "ck-field__radiobutton",
'data-radio': "true"
};
}
/**
* @inheritDoc
*/
init() {
this._defineSchema();
this._defineConverters();
this.editor.commands.add(
this.command,
new CheckboxCommand(this.editor, this.fields, this.modelElementName)
);
}
/**
* @private
* Defaine schema
*/
_defineSchema() {
const schema = this.editor.model.schema;
// Configure the schema.
schema.register(`${this.modelElementName}`, {
isObject: true,
isInline: true,
allowAttributesOf: "$text",
allowWhere: "$text",
allowAttributes: [...this.allowAttributes]
});
}
/**
* @private
* Define converter for model to view, view to model
*/
_defineConverters() {
const conversion = this.editor.conversion;
// Upcast (view to modal) converter
conversion.for("upcast").elementToElement({
model: (viewElement, { writer }) => {
let attrs = {};
viewElement._attrs.forEach((value, key) => (attrs[key] = value));
if (attrs.checked == '') {
attrs.checked = true;
} else if (attrs.checked == undefined) {
attrs.checked = false;
}
return writer.createElement(`${this.modelElementName}`, attrs);
},
view: {
name: this.viewElementName,
attributes: ['data-radio']
},
});
// Data Downcast (modal to view)
conversion.for("dataDowncast").elementToElement({
model: `${this.modelElementName}`,
view: (modelElement, { writer: viewWriter }) => {
let attrs = this.defaultAttributes || {};
modelElement._attrs.forEach((value, key) => (attrs[key] = value));
if (!attrs.checked) {
delete attrs.checked;
}
delete attrs['src'];
return viewWriter.createContainerElement(this.viewElementName, attrs);
}
});
// Editing Downcast (modal to view)
conversion.for("editingDowncast").elementToElement({
model: `${this.modelElementName}`,
view: (modelElement, { writer: viewWriter }) => {
let attrs = this.defaultAttributes || {};
modelElement._attrs.forEach((value, key) => (attrs[key] = value));
if (!attrs.checked) {
delete attrs.checked;
attrs["src"] = uncheckedIcon;
} else {
attrs["src"] = checkedIcon;
}
const section = viewWriter.createContainerElement("img", attrs);
return toWidget(section, viewWriter);
}
});
}
}