-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathletter-case.js
160 lines (133 loc) · 4.27 KB
/
letter-case.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import Plugin from "@ckeditor/ckeditor5-core/src/plugin";
import Command from "@ckeditor/ckeditor5-core/src/command";
import { addListToDropdown, createDropdown } from "@ckeditor/ckeditor5-ui/src/dropdown/utils";
import Collection from "@ckeditor/ckeditor5-utils/src/collection";
import Model from "@ckeditor/ckeditor5-ui/src/model";
import { upperCase, lowerCase, titleCase, sentenceCase, toggleCase, getText } from "./utils";
import LetterCaseIcon from "./../themes/icons/letter-case.svg";
// Lis of Change Case Options Available
const LetterCaseOptions = [
{
text: "Sentence case",
mode: "sentenceCase"
},
{
text: "lower case",
mode: "lowerCase"
},
{
text: "UPPER CASE",
mode: "upperCase"
},
{
text: "Title Case",
mode: "titleCase"
},
{
text: "tOOGLE cASE",
mode: "toggleCase"
}
];
export default class LetterCase extends Plugin {
init() {
// Register the Change Case Command
this.editor.commands.add("LetterCase", new LetterCaseCommand(this.editor));
// Initializing UI
this.editor.ui.componentFactory.add("LetterCase", locale => {
const dropdownView = createDropdown(locale);
addListToDropdown(
dropdownView,
this.getDropdownItemsDefinitions(LetterCaseOptions)
);
dropdownView.buttonView.set({
label: "Change Case",
tooltip: "Change Case",
withText: false,
icon: LetterCaseIcon
});
// bindi UI with the Command
const command = this.editor.commands.get("LetterCase");
dropdownView.bind("isOn", "isEnabled").to(command, "value", "isEnabled");
// Execute the Command through the UI
this.listenTo(dropdownView, "execute", evt => {
const command = evt.source.commandParam;
this.editor.execute('LetterCase', command)
});
return dropdownView;
});
}
// Generate Item Definitions for UI
getDropdownItemsDefinitions() {
const itemDefinitions = new Collection();
for (const option of LetterCaseOptions) {
const definition = {
type: "button",
model: new Model({
commandParam: option.mode,
label: option.text,
withText: true
})
};
itemDefinitions.add(definition);
}
return itemDefinitions;
}
}
class LetterCaseCommand extends Command {
execute(mode) {
const startPos = this.editor.model.document.selection.getFirstPosition();
const endPos = this.editor.model.document.selection.getLastPosition();
this.editor.model.enqueueChange(writer => {
// get selected text
const range = writer.createRange(startPos, endPos);
const sourceText = getText(range);
// convert previously selected text to new one
let targetText;
switch (mode) {
case 'upperCase':
targetText = upperCase(sourceText);
break;
case 'lowerCase':
targetText = lowerCase(sourceText);
break;
case 'titleCase':
targetText = titleCase(sourceText);
break;
case 'sentenceCase':
targetText = sentenceCase(sourceText);
break;
case 'toggleCase':
targetText = toggleCase(sourceText);
break;
default:
targetText = sourceText;
break;
}
this._replaceText(targetText, range);
});
}
_replaceText(replaceWith, replaceRange) {
this.editor.model.enqueueChange(writer => {
const parent = replaceRange.start.parent;
// Get the attributes of selected node before replacing the text
const styles = Array.from(parent.getChildren()).map(a => {
return {
attributes: Array.from(a.getAttributes()),
range: writer.createRangeOn(a)
};
});
// Replace the text with new one
let newText = writer.createText(replaceWith, {});
this.editor.model.insertContent(newText, replaceRange);
// Revert the text attributes into its original attributes
styles.forEach(item => {
writer.setAttributes(item.attributes, item.range);
});
});
}
refresh() {
// the command only enabled when there is only one selected block
const blocks = Array.from(this.editor.model.document.selection.getSelectedBlocks());
this.isEnabled = blocks.length === 1;
}
}