-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
plugin.ts
324 lines (286 loc) · 8.97 KB
/
plugin.ts
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
* This file is part of the "iconpack" Extension for TYPO3 CMS.
*
* Conceived and written by Stephan Kellermayr
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/
import { Plugin, Command } from '@ckeditor/ckeditor5-core';
import { Widget, toWidget } from '@ckeditor/ckeditor5-widget';
import { ButtonView } from '@ckeditor/ckeditor5-ui';
import { DomEventObserver } from '@ckeditor/ckeditor5-engine';
import IconpackModal from '@quellenform/iconpack-modal.js';
const iconpackIcon = '<svg viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><g stroke="#000" stroke-linecap="round" stroke-linejoin="bevel" stroke-width=".295"><path d="m0.2628 1.9151 6.6836 3.9405 12.791-2.9519-8.6056-2.7421z" fill="#a5a6a5"/><path d="m0.2628 1.9151 1.2416 11.367 5.8931 6.5566-0.45127-13.983z" fill="#8c8d8c"/><path d="m19.737 2.9037-1.4892 12.311-10.851 4.6234-0.45127-13.983z" fill="#bec0be" style="paint-order:normal"/></g></svg>';
interface UrlParams {
fieldType: string,
iconfigString: string,
}
/**
* Add observer for double click and extend a generic DomEventObserver class by a native DOM dblclick event.
* https://ckeditor.com/docs/ckeditor5/latest/examples/how-tos.html#how-to-listen-on-a-double-click-eg-link-elements
*/
class DoubleClickObserver extends DomEventObserver<'dblclick'> {
get domEventType(): 'dblclick' {
return 'dblclick';
}
onDomEvent(domEvent: MouseEvent): void {
this.fire('dblclick', domEvent);
}
}
export class Iconpack extends Plugin {
static pluginName = 'Iconpack';
static get requires() {
return [
IconpackCommand,
IconpackEditing,
IconpackUI
];
}
}
export class IconpackEditing extends Plugin {
private attributes: Array<string> = null;
/**
* @inheritdoc
*/
static get requires() {
return [Widget];
}
/**
* @inheritdoc
*/
init() {
this.attributes = ['data-iconfig', 'name', 'id', 'class', 'style', 'alt', 'title'];
this._defineSchema();
this._defineWebfontConverters();
this._defineImageConverters();
this.editor.commands.add(
'iconpack',
new IconpackCommand(this.editor),
);
}
/**
* Define schema
*/
_defineSchema() {
const schema = this.editor.model.schema;
schema.register('iconpackWebfont', {
allowAttributes: Object.values(this.attributes),
allowWhere: '$text',
isInline: true,
isObject: true,
isLimit: true
});
schema.register('iconpackImage', {
allowAttributes: Object.values(this.attributes).concat('src'),
allowWhere: '$text',
isInline: true,
isObject: true,
isLimit: true
});
this.editor.editing.view.domConverter.inlineObjectElements.push('iconpackWebfont');
this.editor.editing.view.domConverter.inlineObjectElements.push('iconpackImage');
}
/**
* Define webfont converters
*/
_defineWebfontConverters() {
const conversion = this.editor.conversion;
conversion.for('upcast').elementToElement({
view: {
name: 'span',
attributes: ['data-iconfig'],
},
model: (viewElement, {
writer: modelWriter
}) => {
return modelWriter.createElement(
'iconpackWebfont',
viewElement.getAttributes()
);
},
converterPriority: 'highest',
});
conversion.for('dataDowncast').elementToElement({
model: 'iconpackWebfont',
view: (modelElement, {
writer
}) => {
return writer.createEmptyElement(
'span',
modelElement.getAttributes()
);
}
});
conversion.for('editingDowncast').elementToElement({
model: 'iconpackWebfont',
view: (modelElement, {
writer: viewWriter
}) => {
const viewElement = viewWriter.createContainerElement(
'span',
modelElement.getAttributes()
);
viewElement._addClass('ck-widget-iconpack');
return toWidget(viewElement, viewWriter, {
label: 'Iconpack widget'
});
}
});
}
/**
* Define image converters
*/
_defineImageConverters() {
const conversion = this.editor.conversion;
conversion.for('upcast').elementToElement({
view: {
name: 'img',
attributes: ['data-iconfig'],
},
model: (viewElement, {
writer: modelWriter
}) => {
return modelWriter.createElement(
'iconpackImage',
viewElement.getAttributes()
);
},
converterPriority: 'highest',
});
conversion.for('dataDowncast').elementToElement({
model: 'iconpackImage',
view: (modelElement, {
writer
}) => {
modelElement._removeAttribute('htmlImgAttributes');
modelElement._removeAttribute('loading');
return writer.createEmptyElement(
'img',
modelElement.getAttributes()
);
}
});
conversion.for('editingDowncast').elementToElement({
model: 'iconpackImage',
view: (modelElement, {
writer: viewWriter
}) => {
const viewElement = viewWriter.createContainerElement(
'img',
modelElement.getAttributes()
);
viewElement._addClass('ck-widget-iconpack');
return toWidget(viewElement, viewWriter, {
label: 'Iconpack widget'
});
}
});
}
}
export class IconpackCommand extends Command {
/**
* @inheritdoc
*/
execute() {
const model = this.editor.model;
const selection = model.document.selection;
this.editor.model.change(writer => {
const selectedElement = selection.getSelectedElement();
let iconfigString: string = null;
if (selectedElement) {
iconfigString = <string>selectedElement.getAttribute('data-iconfig');
}
const urlParams: UrlParams = {
fieldType: 'rte',
iconfigString: iconfigString
};
IconpackModal.openIconpackModal(
TYPO3.lang['js.label.iconRte'],
urlParams,
this._addIconToRte.bind(this),
this._removeIconFromRte.bind(this)
);
});
}
/**
* @inheritdoc
*/
refresh() {
const model = this.editor.model;
const selection = model.document.selection;
// Note: iconpackWebfont and iconpackImage are allowed inside same parents
const allowedIn = model.schema.findAllowedParent(
selection.getFirstPosition(),
'iconpackWebfont'
);
// Flag indicating whether a command is enabled or disabled
this.isEnabled = allowedIn !== null;
}
/**
* Add icon to the initiating RTE.
*/
private _addIconToRte(iconfigString: string, iconMarkup: string) {
console.log('⮜ CKEditor: Add icon to RTE'); //# DEBUG MESSAGE
if (iconMarkup) {
// https://stackoverflow.com/questions/47729450/ckeditor-5-how-to-insert-some-html-aka-wheres-the-source-mode
const viewFragment = this.editor.data.processor.toView(iconMarkup);
const modelFragment = this.editor.data.toModel(viewFragment);
this.editor.model.insertContent(modelFragment);
}
}
/**
* Remove icon from the initiating RTE.
*/
private _removeIconFromRte() {
console.log('⮜ CKEditor: Remove icon from RTE'); //# DEBUG MESSAGE
const selection = this.editor.model.document.selection;
const range = selection.getFirstRange();
this.editor.model.change(writer => {
writer.remove(range);
});
}
}
export class IconpackUI extends Plugin {
/**
* @inheritdoc
*/
init() {
const editor = this.editor;
const t = editor.t;
// Add the "iconpack" button to the toolbar
editor.ui.componentFactory.add('iconpack', (locale) => {
// The state of the button will be bound to the widget command
const command = <any>editor.commands.get('iconpack');
// The button will be an instance of ButtonView
const buttonView = new ButtonView(locale);
buttonView.set({
withText: true,
tooltip: t('Insert Icon'),
icon: iconpackIcon,
isToggleable: true
});
// Bind the state of the button to the command.
buttonView.bind('isOn', 'isEnabled').to(command, 'value', 'isEnabled');
// Execute the command when the button is clicked (executed)
this.listenTo(buttonView, 'execute', () => {
console.log('⭘ CKEditor: Toolbar button has been clicked!'); //# DEBUG MESSAGE
editor.execute('iconpack')
});
return buttonView;
});
const view = editor.editing.view;
const viewDocument = view.document;
view.addObserver(DoubleClickObserver);
editor.listenTo(viewDocument, 'dblclick', (evt, data) => {
const modelElement = editor.editing.mapper.toModelElement(data.target);
if (modelElement && typeof modelElement.name !== 'undefined' && (
modelElement.name === 'iconpackWebfont' || modelElement.name === 'iconpackImage'
)) {
console.log('⭘ CKEditor: Iconpack element has been clicked!'); //# DEBUG MESSAGE
editor.execute('iconpack')
}
});
}
}
export default Iconpack;