@ckeditor/ckeditor5-ckbox 0.0.0-internal-20241017.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/LICENSE.md +21 -0
- package/README.md +24 -0
- package/build/ckbox.js +5 -0
- package/build/translations/ar.js +1 -0
- package/build/translations/az.js +1 -0
- package/build/translations/bg.js +1 -0
- package/build/translations/bn.js +1 -0
- package/build/translations/ca.js +1 -0
- package/build/translations/cs.js +1 -0
- package/build/translations/da.js +1 -0
- package/build/translations/de.js +1 -0
- package/build/translations/el.js +1 -0
- package/build/translations/en-au.js +1 -0
- package/build/translations/es-co.js +1 -0
- package/build/translations/es.js +1 -0
- package/build/translations/et.js +1 -0
- package/build/translations/fa.js +1 -0
- package/build/translations/fi.js +1 -0
- package/build/translations/fr.js +1 -0
- package/build/translations/gl.js +1 -0
- package/build/translations/he.js +1 -0
- package/build/translations/hi.js +1 -0
- package/build/translations/hr.js +1 -0
- package/build/translations/hu.js +1 -0
- package/build/translations/id.js +1 -0
- package/build/translations/it.js +1 -0
- package/build/translations/ja.js +1 -0
- package/build/translations/ko.js +1 -0
- package/build/translations/lt.js +1 -0
- package/build/translations/lv.js +1 -0
- package/build/translations/ms.js +1 -0
- package/build/translations/nl.js +1 -0
- package/build/translations/no.js +1 -0
- package/build/translations/pl.js +1 -0
- package/build/translations/pt-br.js +1 -0
- package/build/translations/pt.js +1 -0
- package/build/translations/ro.js +1 -0
- package/build/translations/ru.js +1 -0
- package/build/translations/sk.js +1 -0
- package/build/translations/sq.js +1 -0
- package/build/translations/sr-latn.js +1 -0
- package/build/translations/sr.js +1 -0
- package/build/translations/sv.js +1 -0
- package/build/translations/th.js +1 -0
- package/build/translations/tr.js +1 -0
- package/build/translations/ug.js +1 -0
- package/build/translations/uk.js +1 -0
- package/build/translations/ur.js +1 -0
- package/build/translations/uz.js +1 -0
- package/build/translations/vi.js +1 -0
- package/build/translations/zh-cn.js +1 -0
- package/build/translations/zh.js +1 -0
- package/ckeditor5-metadata.json +58 -0
- package/dist/augmentation.d.ts +36 -0
- package/dist/ckbox.d.ts +41 -0
- package/dist/ckboxcommand.d.ts +130 -0
- package/dist/ckboxconfig.d.ts +434 -0
- package/dist/ckboxediting.d.ts +65 -0
- package/dist/ckboximageedit/ckboximageeditcommand.d.ts +109 -0
- package/dist/ckboximageedit/ckboximageeditediting.d.ts +36 -0
- package/dist/ckboximageedit/ckboximageeditui.d.ts +32 -0
- package/dist/ckboximageedit/utils.d.ts +14 -0
- package/dist/ckboximageedit.d.ts +32 -0
- package/dist/ckboxui.d.ts +60 -0
- package/dist/ckboxuploadadapter.d.ts +41 -0
- package/dist/ckboxutils.d.ts +58 -0
- package/dist/index-content.css +4 -0
- package/dist/index-editor.css +46 -0
- package/dist/index.css +55 -0
- package/dist/index.css.map +1 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +1787 -0
- package/dist/index.js.map +1 -0
- package/dist/translations/ar.d.ts +8 -0
- package/dist/translations/ar.js +5 -0
- package/dist/translations/ar.umd.js +11 -0
- package/dist/translations/az.d.ts +8 -0
- package/dist/translations/az.js +5 -0
- package/dist/translations/az.umd.js +11 -0
- package/dist/translations/bg.d.ts +8 -0
- package/dist/translations/bg.js +5 -0
- package/dist/translations/bg.umd.js +11 -0
- package/dist/translations/bn.d.ts +8 -0
- package/dist/translations/bn.js +5 -0
- package/dist/translations/bn.umd.js +11 -0
- package/dist/translations/ca.d.ts +8 -0
- package/dist/translations/ca.js +5 -0
- package/dist/translations/ca.umd.js +11 -0
- package/dist/translations/cs.d.ts +8 -0
- package/dist/translations/cs.js +5 -0
- package/dist/translations/cs.umd.js +11 -0
- package/dist/translations/da.d.ts +8 -0
- package/dist/translations/da.js +5 -0
- package/dist/translations/da.umd.js +11 -0
- package/dist/translations/de.d.ts +8 -0
- package/dist/translations/de.js +5 -0
- package/dist/translations/de.umd.js +11 -0
- package/dist/translations/el.d.ts +8 -0
- package/dist/translations/el.js +5 -0
- package/dist/translations/el.umd.js +11 -0
- package/dist/translations/en-au.d.ts +8 -0
- package/dist/translations/en-au.js +5 -0
- package/dist/translations/en-au.umd.js +11 -0
- package/dist/translations/en.d.ts +8 -0
- package/dist/translations/en.js +5 -0
- package/dist/translations/en.umd.js +11 -0
- package/dist/translations/es-co.d.ts +8 -0
- package/dist/translations/es-co.js +5 -0
- package/dist/translations/es-co.umd.js +11 -0
- package/dist/translations/es.d.ts +8 -0
- package/dist/translations/es.js +5 -0
- package/dist/translations/es.umd.js +11 -0
- package/dist/translations/et.d.ts +8 -0
- package/dist/translations/et.js +5 -0
- package/dist/translations/et.umd.js +11 -0
- package/dist/translations/fa.d.ts +8 -0
- package/dist/translations/fa.js +5 -0
- package/dist/translations/fa.umd.js +11 -0
- package/dist/translations/fi.d.ts +8 -0
- package/dist/translations/fi.js +5 -0
- package/dist/translations/fi.umd.js +11 -0
- package/dist/translations/fr.d.ts +8 -0
- package/dist/translations/fr.js +5 -0
- package/dist/translations/fr.umd.js +11 -0
- package/dist/translations/gl.d.ts +8 -0
- package/dist/translations/gl.js +5 -0
- package/dist/translations/gl.umd.js +11 -0
- package/dist/translations/he.d.ts +8 -0
- package/dist/translations/he.js +5 -0
- package/dist/translations/he.umd.js +11 -0
- package/dist/translations/hi.d.ts +8 -0
- package/dist/translations/hi.js +5 -0
- package/dist/translations/hi.umd.js +11 -0
- package/dist/translations/hr.d.ts +8 -0
- package/dist/translations/hr.js +5 -0
- package/dist/translations/hr.umd.js +11 -0
- package/dist/translations/hu.d.ts +8 -0
- package/dist/translations/hu.js +5 -0
- package/dist/translations/hu.umd.js +11 -0
- package/dist/translations/id.d.ts +8 -0
- package/dist/translations/id.js +5 -0
- package/dist/translations/id.umd.js +11 -0
- package/dist/translations/it.d.ts +8 -0
- package/dist/translations/it.js +5 -0
- package/dist/translations/it.umd.js +11 -0
- package/dist/translations/ja.d.ts +8 -0
- package/dist/translations/ja.js +5 -0
- package/dist/translations/ja.umd.js +11 -0
- package/dist/translations/ko.d.ts +8 -0
- package/dist/translations/ko.js +5 -0
- package/dist/translations/ko.umd.js +11 -0
- package/dist/translations/lt.d.ts +8 -0
- package/dist/translations/lt.js +5 -0
- package/dist/translations/lt.umd.js +11 -0
- package/dist/translations/lv.d.ts +8 -0
- package/dist/translations/lv.js +5 -0
- package/dist/translations/lv.umd.js +11 -0
- package/dist/translations/ms.d.ts +8 -0
- package/dist/translations/ms.js +5 -0
- package/dist/translations/ms.umd.js +11 -0
- package/dist/translations/nl.d.ts +8 -0
- package/dist/translations/nl.js +5 -0
- package/dist/translations/nl.umd.js +11 -0
- package/dist/translations/no.d.ts +8 -0
- package/dist/translations/no.js +5 -0
- package/dist/translations/no.umd.js +11 -0
- package/dist/translations/pl.d.ts +8 -0
- package/dist/translations/pl.js +5 -0
- package/dist/translations/pl.umd.js +11 -0
- package/dist/translations/pt-br.d.ts +8 -0
- package/dist/translations/pt-br.js +5 -0
- package/dist/translations/pt-br.umd.js +11 -0
- package/dist/translations/pt.d.ts +8 -0
- package/dist/translations/pt.js +5 -0
- package/dist/translations/pt.umd.js +11 -0
- package/dist/translations/ro.d.ts +8 -0
- package/dist/translations/ro.js +5 -0
- package/dist/translations/ro.umd.js +11 -0
- package/dist/translations/ru.d.ts +8 -0
- package/dist/translations/ru.js +5 -0
- package/dist/translations/ru.umd.js +11 -0
- package/dist/translations/sk.d.ts +8 -0
- package/dist/translations/sk.js +5 -0
- package/dist/translations/sk.umd.js +11 -0
- package/dist/translations/sq.d.ts +8 -0
- package/dist/translations/sq.js +5 -0
- package/dist/translations/sq.umd.js +11 -0
- package/dist/translations/sr-latn.d.ts +8 -0
- package/dist/translations/sr-latn.js +5 -0
- package/dist/translations/sr-latn.umd.js +11 -0
- package/dist/translations/sr.d.ts +8 -0
- package/dist/translations/sr.js +5 -0
- package/dist/translations/sr.umd.js +11 -0
- package/dist/translations/sv.d.ts +8 -0
- package/dist/translations/sv.js +5 -0
- package/dist/translations/sv.umd.js +11 -0
- package/dist/translations/th.d.ts +8 -0
- package/dist/translations/th.js +5 -0
- package/dist/translations/th.umd.js +11 -0
- package/dist/translations/tr.d.ts +8 -0
- package/dist/translations/tr.js +5 -0
- package/dist/translations/tr.umd.js +11 -0
- package/dist/translations/ug.d.ts +8 -0
- package/dist/translations/ug.js +5 -0
- package/dist/translations/ug.umd.js +11 -0
- package/dist/translations/uk.d.ts +8 -0
- package/dist/translations/uk.js +5 -0
- package/dist/translations/uk.umd.js +11 -0
- package/dist/translations/ur.d.ts +8 -0
- package/dist/translations/ur.js +5 -0
- package/dist/translations/ur.umd.js +11 -0
- package/dist/translations/uz.d.ts +8 -0
- package/dist/translations/uz.js +5 -0
- package/dist/translations/uz.umd.js +11 -0
- package/dist/translations/vi.d.ts +8 -0
- package/dist/translations/vi.js +5 -0
- package/dist/translations/vi.umd.js +11 -0
- package/dist/translations/zh-cn.d.ts +8 -0
- package/dist/translations/zh-cn.js +5 -0
- package/dist/translations/zh-cn.umd.js +11 -0
- package/dist/translations/zh.d.ts +8 -0
- package/dist/translations/zh.js +5 -0
- package/dist/translations/zh.umd.js +11 -0
- package/dist/utils.d.ts +67 -0
- package/lang/contexts.json +10 -0
- package/lang/translations/ar.po +50 -0
- package/lang/translations/az.po +50 -0
- package/lang/translations/bg.po +50 -0
- package/lang/translations/bn.po +50 -0
- package/lang/translations/ca.po +50 -0
- package/lang/translations/cs.po +50 -0
- package/lang/translations/da.po +50 -0
- package/lang/translations/de.po +50 -0
- package/lang/translations/el.po +50 -0
- package/lang/translations/en-au.po +50 -0
- package/lang/translations/en.po +50 -0
- package/lang/translations/es-co.po +50 -0
- package/lang/translations/es.po +50 -0
- package/lang/translations/et.po +50 -0
- package/lang/translations/fa.po +50 -0
- package/lang/translations/fi.po +50 -0
- package/lang/translations/fr.po +50 -0
- package/lang/translations/gl.po +50 -0
- package/lang/translations/he.po +50 -0
- package/lang/translations/hi.po +50 -0
- package/lang/translations/hr.po +50 -0
- package/lang/translations/hu.po +50 -0
- package/lang/translations/id.po +50 -0
- package/lang/translations/it.po +50 -0
- package/lang/translations/ja.po +50 -0
- package/lang/translations/ko.po +50 -0
- package/lang/translations/lt.po +50 -0
- package/lang/translations/lv.po +50 -0
- package/lang/translations/ms.po +50 -0
- package/lang/translations/nl.po +50 -0
- package/lang/translations/no.po +50 -0
- package/lang/translations/pl.po +50 -0
- package/lang/translations/pt-br.po +50 -0
- package/lang/translations/pt.po +50 -0
- package/lang/translations/ro.po +50 -0
- package/lang/translations/ru.po +50 -0
- package/lang/translations/sk.po +50 -0
- package/lang/translations/sq.po +50 -0
- package/lang/translations/sr-latn.po +50 -0
- package/lang/translations/sr.po +50 -0
- package/lang/translations/sv.po +50 -0
- package/lang/translations/th.po +50 -0
- package/lang/translations/tr.po +50 -0
- package/lang/translations/ug.po +50 -0
- package/lang/translations/uk.po +50 -0
- package/lang/translations/ur.po +50 -0
- package/lang/translations/uz.po +50 -0
- package/lang/translations/vi.po +50 -0
- package/lang/translations/zh-cn.po +50 -0
- package/lang/translations/zh.po +50 -0
- package/package.json +45 -0
- package/src/augmentation.d.ts +32 -0
- package/src/augmentation.js +5 -0
- package/src/ckbox.d.ts +37 -0
- package/src/ckbox.js +43 -0
- package/src/ckboxcommand.d.ts +126 -0
- package/src/ckboxcommand.js +364 -0
- package/src/ckboxconfig.d.ts +430 -0
- package/src/ckboxconfig.js +5 -0
- package/src/ckboxediting.d.ts +61 -0
- package/src/ckboxediting.js +389 -0
- package/src/ckboximageedit/ckboximageeditcommand.d.ts +105 -0
- package/src/ckboximageedit/ckboximageeditcommand.js +310 -0
- package/src/ckboximageedit/ckboximageeditediting.d.ts +32 -0
- package/src/ckboximageedit/ckboximageeditediting.js +42 -0
- package/src/ckboximageedit/ckboximageeditui.d.ts +28 -0
- package/src/ckboximageedit/ckboximageeditui.js +57 -0
- package/src/ckboximageedit/utils.d.ts +10 -0
- package/src/ckboximageedit/utils.js +48 -0
- package/src/ckboximageedit.d.ts +28 -0
- package/src/ckboximageedit.js +34 -0
- package/src/ckboxui.d.ts +56 -0
- package/src/ckboxui.js +136 -0
- package/src/ckboxuploadadapter.d.ts +37 -0
- package/src/ckboxuploadadapter.js +136 -0
- package/src/ckboxutils.d.ts +54 -0
- package/src/ckboxutils.js +189 -0
- package/src/index.d.ts +17 -0
- package/src/index.js +14 -0
- package/src/utils.d.ts +63 -0
- package/src/utils.js +175 -0
- package/theme/ckboximageedit.css +53 -0
- package/theme/icons/ckbox-image-edit.svg +1 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,1787 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
import { Plugin, icons, Command, PendingActions } from '@ckeditor/ckeditor5-core/dist/index.js';
|
|
6
|
+
import { ButtonView, MenuBarMenuListItemButtonView, Notification } from '@ckeditor/ckeditor5-ui/dist/index.js';
|
|
7
|
+
import { Range } from '@ckeditor/ckeditor5-engine/dist/index.js';
|
|
8
|
+
import { createElement, toMap, CKEditorError, logError, global, delay, abortableDebounce, retry } from '@ckeditor/ckeditor5-utils/dist/index.js';
|
|
9
|
+
import { decode } from 'blurhash';
|
|
10
|
+
import { FileRepository } from '@ckeditor/ckeditor5-upload/dist/index.js';
|
|
11
|
+
import { isEqual } from 'lodash-es';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Introduces UI components for the `CKBox` plugin.
|
|
15
|
+
*
|
|
16
|
+
* The plugin introduces two UI components to the {@link module:ui/componentfactory~ComponentFactory UI component factory}:
|
|
17
|
+
*
|
|
18
|
+
* * the `'ckbox'` toolbar button,
|
|
19
|
+
* * the `'menuBar:ckbox'` menu bar component, which is by default added to the `'Insert'` menu.
|
|
20
|
+
*
|
|
21
|
+
* It also integrates with the `insertImage` toolbar component and `menuBar:insertImage` menu component.
|
|
22
|
+
*/ class CKBoxUI extends Plugin {
|
|
23
|
+
/**
|
|
24
|
+
* @inheritDoc
|
|
25
|
+
*/ static get pluginName() {
|
|
26
|
+
return 'CKBoxUI';
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* @inheritDoc
|
|
30
|
+
*/ static get isOfficialPlugin() {
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* @inheritDoc
|
|
35
|
+
*/ afterInit() {
|
|
36
|
+
const editor = this.editor;
|
|
37
|
+
// Do not register the `ckbox` button if the command does not exist.
|
|
38
|
+
// This might happen when CKBox library is not loaded on the page.
|
|
39
|
+
if (!editor.commands.get('ckbox')) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
editor.ui.componentFactory.add('ckbox', ()=>this._createFileToolbarButton());
|
|
43
|
+
editor.ui.componentFactory.add('menuBar:ckbox', ()=>this._createFileMenuBarButton());
|
|
44
|
+
if (editor.plugins.has('ImageInsertUI')) {
|
|
45
|
+
editor.plugins.get('ImageInsertUI').registerIntegration({
|
|
46
|
+
name: 'assetManager',
|
|
47
|
+
observable: ()=>editor.commands.get('ckbox'),
|
|
48
|
+
buttonViewCreator: ()=>this._createImageToolbarButton(),
|
|
49
|
+
formViewCreator: ()=>this._createImageDropdownButton(),
|
|
50
|
+
menuBarButtonViewCreator: (isOnly)=>this._createImageMenuBarButton(isOnly ? 'insertOnly' : 'insertNested')
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Creates the base for various kinds of the button component provided by this feature.
|
|
56
|
+
*/ _createButton(ButtonClass) {
|
|
57
|
+
const editor = this.editor;
|
|
58
|
+
const locale = editor.locale;
|
|
59
|
+
const view = new ButtonClass(locale);
|
|
60
|
+
const command = editor.commands.get('ckbox');
|
|
61
|
+
view.bind('isOn', 'isEnabled').to(command, 'value', 'isEnabled');
|
|
62
|
+
view.on('execute', ()=>{
|
|
63
|
+
editor.execute('ckbox');
|
|
64
|
+
});
|
|
65
|
+
return view;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Creates a simple toolbar button for files management, with an icon and a tooltip.
|
|
69
|
+
*/ _createFileToolbarButton() {
|
|
70
|
+
const t = this.editor.locale.t;
|
|
71
|
+
const button = this._createButton(ButtonView);
|
|
72
|
+
button.icon = icons.browseFiles;
|
|
73
|
+
button.label = t('Open file manager');
|
|
74
|
+
button.tooltip = true;
|
|
75
|
+
return button;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Creates a simple toolbar button for images management, with an icon and a tooltip.
|
|
79
|
+
*/ _createImageToolbarButton() {
|
|
80
|
+
const t = this.editor.locale.t;
|
|
81
|
+
const imageInsertUI = this.editor.plugins.get('ImageInsertUI');
|
|
82
|
+
const button = this._createButton(ButtonView);
|
|
83
|
+
button.icon = icons.imageAssetManager;
|
|
84
|
+
button.bind('label').to(imageInsertUI, 'isImageSelected', (isImageSelected)=>isImageSelected ? t('Replace image with file manager') : t('Insert image with file manager'));
|
|
85
|
+
button.tooltip = true;
|
|
86
|
+
return button;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Creates a button for images management for the dropdown view, with an icon, text and no tooltip.
|
|
90
|
+
*/ _createImageDropdownButton() {
|
|
91
|
+
const t = this.editor.locale.t;
|
|
92
|
+
const imageInsertUI = this.editor.plugins.get('ImageInsertUI');
|
|
93
|
+
const button = this._createButton(ButtonView);
|
|
94
|
+
button.icon = icons.imageAssetManager;
|
|
95
|
+
button.withText = true;
|
|
96
|
+
button.bind('label').to(imageInsertUI, 'isImageSelected', (isImageSelected)=>isImageSelected ? t('Replace with file manager') : t('Insert with file manager'));
|
|
97
|
+
button.on('execute', ()=>{
|
|
98
|
+
imageInsertUI.dropdownView.isOpen = false;
|
|
99
|
+
});
|
|
100
|
+
return button;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Creates a button for files management for the menu bar.
|
|
104
|
+
*/ _createFileMenuBarButton() {
|
|
105
|
+
const t = this.editor.locale.t;
|
|
106
|
+
const button = this._createButton(MenuBarMenuListItemButtonView);
|
|
107
|
+
button.icon = icons.browseFiles;
|
|
108
|
+
button.withText = true;
|
|
109
|
+
button.label = t('File');
|
|
110
|
+
return button;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Creates a button for images management for the menu bar.
|
|
114
|
+
*/ _createImageMenuBarButton(type) {
|
|
115
|
+
const t = this.editor.locale.t;
|
|
116
|
+
const button = this._createButton(MenuBarMenuListItemButtonView);
|
|
117
|
+
button.icon = icons.imageAssetManager;
|
|
118
|
+
button.withText = true;
|
|
119
|
+
switch(type){
|
|
120
|
+
case 'insertOnly':
|
|
121
|
+
button.label = t('Image');
|
|
122
|
+
break;
|
|
123
|
+
case 'insertNested':
|
|
124
|
+
button.label = t('With file manager');
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
return button;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Converts image source set provided by the CKBox into an object containing:
|
|
133
|
+
* - responsive URLs for the "webp" image format,
|
|
134
|
+
* - one fallback URL for browsers that do not support the "webp" format.
|
|
135
|
+
*/ function getImageUrls(imageUrls) {
|
|
136
|
+
const responsiveUrls = [];
|
|
137
|
+
let maxWidth = 0;
|
|
138
|
+
for(const key in imageUrls){
|
|
139
|
+
const width = parseInt(key, 10);
|
|
140
|
+
if (!isNaN(width)) {
|
|
141
|
+
if (width > maxWidth) {
|
|
142
|
+
maxWidth = width;
|
|
143
|
+
}
|
|
144
|
+
responsiveUrls.push(`${imageUrls[key]} ${key}w`);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
const imageSources = [
|
|
148
|
+
{
|
|
149
|
+
srcset: responsiveUrls.join(','),
|
|
150
|
+
sizes: `(max-width: ${maxWidth}px) 100vw, ${maxWidth}px`,
|
|
151
|
+
type: 'image/webp'
|
|
152
|
+
}
|
|
153
|
+
];
|
|
154
|
+
return {
|
|
155
|
+
imageFallbackUrl: imageUrls.default,
|
|
156
|
+
imageSources
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Returns a workspace id to use for communication with the CKBox service.
|
|
161
|
+
*
|
|
162
|
+
* @param defaultWorkspaceId The default workspace to use taken from editor config.
|
|
163
|
+
*/ function getWorkspaceId(token, defaultWorkspaceId) {
|
|
164
|
+
const [, binaryTokenPayload] = token.value.split('.');
|
|
165
|
+
const payload = JSON.parse(atob(binaryTokenPayload));
|
|
166
|
+
const workspaces = payload.auth && payload.auth.ckbox && payload.auth.ckbox.workspaces || [
|
|
167
|
+
payload.aud
|
|
168
|
+
];
|
|
169
|
+
if (!defaultWorkspaceId) {
|
|
170
|
+
return workspaces[0];
|
|
171
|
+
}
|
|
172
|
+
const role = payload.auth && payload.auth.ckbox && payload.auth.ckbox.role;
|
|
173
|
+
if (role == 'superadmin' || workspaces.includes(defaultWorkspaceId)) {
|
|
174
|
+
return defaultWorkspaceId;
|
|
175
|
+
}
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Default resolution for decoding blurhash values.
|
|
180
|
+
* Relatively small values must be used in order to ensure acceptable performance.
|
|
181
|
+
*/ const BLUR_RESOLUTION = 32;
|
|
182
|
+
/**
|
|
183
|
+
* Generates an image data URL from its `blurhash` representation.
|
|
184
|
+
*/ function blurHashToDataUrl(hash) {
|
|
185
|
+
if (!hash) {
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
try {
|
|
189
|
+
const resolutionInPx = `${BLUR_RESOLUTION}px`;
|
|
190
|
+
const canvas = document.createElement('canvas');
|
|
191
|
+
canvas.setAttribute('width', resolutionInPx);
|
|
192
|
+
canvas.setAttribute('height', resolutionInPx);
|
|
193
|
+
const ctx = canvas.getContext('2d');
|
|
194
|
+
/* istanbul ignore next -- @preserve */ if (!ctx) {
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
const imageData = ctx.createImageData(BLUR_RESOLUTION, BLUR_RESOLUTION);
|
|
198
|
+
const decoded = decode(hash, BLUR_RESOLUTION, BLUR_RESOLUTION);
|
|
199
|
+
imageData.data.set(decoded);
|
|
200
|
+
ctx.putImageData(imageData, 0, 0);
|
|
201
|
+
return canvas.toDataURL();
|
|
202
|
+
} catch (e) {
|
|
203
|
+
return undefined;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Sends the HTTP request.
|
|
208
|
+
*
|
|
209
|
+
* @internal
|
|
210
|
+
* @param config.url the URL where the request will be sent.
|
|
211
|
+
* @param config.method The HTTP method.
|
|
212
|
+
* @param config.data Additional data to send.
|
|
213
|
+
* @param config.onUploadProgress A callback informing about the upload progress.
|
|
214
|
+
*/ function sendHttpRequest({ url, method = 'GET', data, onUploadProgress, signal, authorization }) {
|
|
215
|
+
const xhr = new XMLHttpRequest();
|
|
216
|
+
xhr.open(method, url.toString());
|
|
217
|
+
xhr.setRequestHeader('Authorization', authorization);
|
|
218
|
+
xhr.setRequestHeader('CKBox-Version', 'CKEditor 5');
|
|
219
|
+
xhr.responseType = 'json';
|
|
220
|
+
// The callback is attached to the `signal#abort` event.
|
|
221
|
+
const abortCallback = ()=>{
|
|
222
|
+
xhr.abort();
|
|
223
|
+
};
|
|
224
|
+
return new Promise((resolve, reject)=>{
|
|
225
|
+
signal.throwIfAborted();
|
|
226
|
+
signal.addEventListener('abort', abortCallback);
|
|
227
|
+
xhr.addEventListener('loadstart', ()=>{
|
|
228
|
+
signal.addEventListener('abort', abortCallback);
|
|
229
|
+
});
|
|
230
|
+
xhr.addEventListener('loadend', ()=>{
|
|
231
|
+
signal.removeEventListener('abort', abortCallback);
|
|
232
|
+
});
|
|
233
|
+
xhr.addEventListener('error', ()=>{
|
|
234
|
+
reject();
|
|
235
|
+
});
|
|
236
|
+
xhr.addEventListener('abort', ()=>{
|
|
237
|
+
reject();
|
|
238
|
+
});
|
|
239
|
+
xhr.addEventListener('load', ()=>{
|
|
240
|
+
const response = xhr.response;
|
|
241
|
+
if (!response || response.statusCode >= 400) {
|
|
242
|
+
return reject(response && response.message);
|
|
243
|
+
}
|
|
244
|
+
resolve(response);
|
|
245
|
+
});
|
|
246
|
+
/* istanbul ignore else -- @preserve */ if (onUploadProgress) {
|
|
247
|
+
xhr.upload.addEventListener('progress', (evt)=>{
|
|
248
|
+
onUploadProgress(evt);
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
// Send the request.
|
|
252
|
+
xhr.send(data);
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
const MIME_TO_EXTENSION = {
|
|
256
|
+
'image/gif': 'gif',
|
|
257
|
+
'image/jpeg': 'jpg',
|
|
258
|
+
'image/png': 'png',
|
|
259
|
+
'image/webp': 'webp',
|
|
260
|
+
'image/bmp': 'bmp',
|
|
261
|
+
'image/tiff': 'tiff'
|
|
262
|
+
};
|
|
263
|
+
/**
|
|
264
|
+
* Returns an extension a typical file in the specified `mimeType` format would have.
|
|
265
|
+
*/ function convertMimeTypeToExtension(mimeType) {
|
|
266
|
+
return MIME_TO_EXTENSION[mimeType];
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Tries to fetch the given `url` and returns 'content-type' of the response.
|
|
270
|
+
*/ async function getContentTypeOfUrl(url, options) {
|
|
271
|
+
try {
|
|
272
|
+
const response = await fetch(url, {
|
|
273
|
+
method: 'HEAD',
|
|
274
|
+
cache: 'force-cache',
|
|
275
|
+
...options
|
|
276
|
+
});
|
|
277
|
+
if (!response.ok) {
|
|
278
|
+
return '';
|
|
279
|
+
}
|
|
280
|
+
return response.headers.get('content-type') || '';
|
|
281
|
+
} catch {
|
|
282
|
+
return '';
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Returns an extension from the given value.
|
|
287
|
+
*/ function getFileExtension(file) {
|
|
288
|
+
const fileName = file.name;
|
|
289
|
+
const extensionRegExp = /\.(?<ext>[^.]+)$/;
|
|
290
|
+
const match = fileName.match(extensionRegExp);
|
|
291
|
+
return match.groups.ext.toLowerCase();
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// Defines the waiting time (in milliseconds) for inserting the chosen asset into the model. The chosen asset is temporarily stored in the
|
|
295
|
+
// `CKBoxCommand#_chosenAssets` and it is removed from there automatically after this time. See `CKBoxCommand#_chosenAssets` for more
|
|
296
|
+
// details.
|
|
297
|
+
const ASSET_INSERTION_WAIT_TIMEOUT = 1000;
|
|
298
|
+
/**
|
|
299
|
+
* The CKBox command. It is used by the {@link module:ckbox/ckboxediting~CKBoxEditing CKBox editing feature} to open the CKBox file manager.
|
|
300
|
+
* The file manager allows inserting an image or a link to a file into the editor content.
|
|
301
|
+
*
|
|
302
|
+
* ```ts
|
|
303
|
+
* editor.execute( 'ckbox' );
|
|
304
|
+
* ```
|
|
305
|
+
*
|
|
306
|
+
* **Note:** This command uses other features to perform the following tasks:
|
|
307
|
+
* - To insert images it uses the {@link module:image/image/insertimagecommand~InsertImageCommand 'insertImage'} command from the
|
|
308
|
+
* {@link module:image/image~Image Image feature}.
|
|
309
|
+
* - To insert links to other files it uses the {@link module:link/linkcommand~LinkCommand 'link'} command from the
|
|
310
|
+
* {@link module:link/link~Link Link feature}.
|
|
311
|
+
*/ class CKBoxCommand extends Command {
|
|
312
|
+
/**
|
|
313
|
+
* A set of all chosen assets. They are stored temporarily and they are automatically removed 1 second after being chosen.
|
|
314
|
+
* Chosen assets have to be "remembered" for a while to be able to map the given asset with the element inserted into the model.
|
|
315
|
+
* This association map is then used to set the ID on the model element.
|
|
316
|
+
*
|
|
317
|
+
* All chosen assets are automatically removed after the timeout, because (theoretically) it may happen that they will never be
|
|
318
|
+
* inserted into the model, even if the {@link module:link/linkcommand~LinkCommand `'link'`} command or the
|
|
319
|
+
* {@link module:image/image/insertimagecommand~InsertImageCommand `'insertImage'`} command is enabled. Such a case may arise when
|
|
320
|
+
* another plugin blocks the command execution. Then, in order not to keep the chosen (but not inserted) assets forever, we delete
|
|
321
|
+
* them automatically to prevent memory leakage. The 1 second timeout is enough to insert the asset into the model and extract the
|
|
322
|
+
* ID from the chosen asset.
|
|
323
|
+
*
|
|
324
|
+
* The assets are stored only if
|
|
325
|
+
* the {@link module:ckbox/ckboxconfig~CKBoxConfig#ignoreDataId `config.ckbox.ignoreDataId`} option is set to `false` (by default).
|
|
326
|
+
*
|
|
327
|
+
* @internal
|
|
328
|
+
*/ _chosenAssets = new Set();
|
|
329
|
+
/**
|
|
330
|
+
* The DOM element that acts as a mounting point for the CKBox dialog.
|
|
331
|
+
*/ _wrapper = null;
|
|
332
|
+
/**
|
|
333
|
+
* @inheritDoc
|
|
334
|
+
*/ constructor(editor){
|
|
335
|
+
super(editor);
|
|
336
|
+
this._initListeners();
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* @inheritDoc
|
|
340
|
+
*/ refresh() {
|
|
341
|
+
this.value = this._getValue();
|
|
342
|
+
this.isEnabled = this._checkEnabled();
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* @inheritDoc
|
|
346
|
+
*/ execute() {
|
|
347
|
+
this.fire('ckbox:open');
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Indicates if the CKBox dialog is already opened.
|
|
351
|
+
*
|
|
352
|
+
* @protected
|
|
353
|
+
* @returns {Boolean}
|
|
354
|
+
*/ _getValue() {
|
|
355
|
+
return this._wrapper !== null;
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* Checks whether the command can be enabled in the current context.
|
|
359
|
+
*/ _checkEnabled() {
|
|
360
|
+
const imageCommand = this.editor.commands.get('insertImage');
|
|
361
|
+
const linkCommand = this.editor.commands.get('link');
|
|
362
|
+
if (!imageCommand.isEnabled && !linkCommand.isEnabled) {
|
|
363
|
+
return false;
|
|
364
|
+
}
|
|
365
|
+
return true;
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* Creates the options object for the CKBox dialog.
|
|
369
|
+
*
|
|
370
|
+
* @returns The object with properties:
|
|
371
|
+
* - theme The theme for CKBox dialog.
|
|
372
|
+
* - language The language for CKBox dialog.
|
|
373
|
+
* - tokenUrl The token endpoint URL.
|
|
374
|
+
* - serviceOrigin The base URL of the API service.
|
|
375
|
+
* - forceDemoLabel Whether to force "Powered by CKBox" link.
|
|
376
|
+
* - assets.onChoose The callback function invoked after choosing the assets.
|
|
377
|
+
* - dialog.onClose The callback function invoked after closing the CKBox dialog.
|
|
378
|
+
* - dialog.width The dialog width in pixels.
|
|
379
|
+
* - dialog.height The dialog height in pixels.
|
|
380
|
+
* - categories.icons Allows setting custom icons for categories.
|
|
381
|
+
* - view.openLastView Sets if the last view visited by the user will be reopened
|
|
382
|
+
* on the next startup.
|
|
383
|
+
* - view.startupFolderId Sets the ID of the folder that will be opened on startup.
|
|
384
|
+
* - view.startupCategoryId Sets the ID of the category that will be opened on startup.
|
|
385
|
+
* - view.hideMaximizeButton Sets whether to hide the ‘Maximize’ button.
|
|
386
|
+
* - view.componentsHideTimeout Sets timeout after which upload components are hidden
|
|
387
|
+
* after completed upload.
|
|
388
|
+
* - view.dialogMinimizeTimeout Sets timeout after which upload dialog is minimized
|
|
389
|
+
* after completed upload.
|
|
390
|
+
*/ _prepareOptions() {
|
|
391
|
+
const editor = this.editor;
|
|
392
|
+
const ckboxConfig = editor.config.get('ckbox');
|
|
393
|
+
const dialog = ckboxConfig.dialog;
|
|
394
|
+
const categories = ckboxConfig.categories;
|
|
395
|
+
const view = ckboxConfig.view;
|
|
396
|
+
const upload = ckboxConfig.upload;
|
|
397
|
+
return {
|
|
398
|
+
theme: ckboxConfig.theme,
|
|
399
|
+
language: ckboxConfig.language,
|
|
400
|
+
tokenUrl: ckboxConfig.tokenUrl,
|
|
401
|
+
serviceOrigin: ckboxConfig.serviceOrigin,
|
|
402
|
+
forceDemoLabel: ckboxConfig.forceDemoLabel,
|
|
403
|
+
choosableFileExtensions: ckboxConfig.choosableFileExtensions,
|
|
404
|
+
assets: {
|
|
405
|
+
onChoose: (assets)=>this.fire('ckbox:choose', assets)
|
|
406
|
+
},
|
|
407
|
+
dialog: {
|
|
408
|
+
onClose: ()=>this.fire('ckbox:close'),
|
|
409
|
+
width: dialog && dialog.width,
|
|
410
|
+
height: dialog && dialog.height
|
|
411
|
+
},
|
|
412
|
+
categories: categories && {
|
|
413
|
+
icons: categories.icons
|
|
414
|
+
},
|
|
415
|
+
view: view && {
|
|
416
|
+
openLastView: view.openLastView,
|
|
417
|
+
startupFolderId: view.startupFolderId,
|
|
418
|
+
startupCategoryId: view.startupCategoryId,
|
|
419
|
+
hideMaximizeButton: view.hideMaximizeButton
|
|
420
|
+
},
|
|
421
|
+
upload: upload && {
|
|
422
|
+
componentsHideTimeout: upload.componentsHideTimeout,
|
|
423
|
+
dialogMinimizeTimeout: upload.dialogMinimizeTimeout
|
|
424
|
+
}
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* Initializes various event listeners for the `ckbox:*` events, because all functionality of the `ckbox` command is event-based.
|
|
429
|
+
*/ _initListeners() {
|
|
430
|
+
const editor = this.editor;
|
|
431
|
+
const model = editor.model;
|
|
432
|
+
const shouldInsertDataId = !editor.config.get('ckbox.ignoreDataId');
|
|
433
|
+
// Refresh the command after firing the `ckbox:*` event.
|
|
434
|
+
this.on('ckbox', ()=>{
|
|
435
|
+
this.refresh();
|
|
436
|
+
}, {
|
|
437
|
+
priority: 'low'
|
|
438
|
+
});
|
|
439
|
+
// Handle opening of the CKBox dialog.
|
|
440
|
+
this.on('ckbox:open', ()=>{
|
|
441
|
+
if (!this.isEnabled || this.value) {
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
this._wrapper = createElement(document, 'div', {
|
|
445
|
+
class: 'ck ckbox-wrapper'
|
|
446
|
+
});
|
|
447
|
+
document.body.appendChild(this._wrapper);
|
|
448
|
+
window.CKBox.mount(this._wrapper, this._prepareOptions());
|
|
449
|
+
});
|
|
450
|
+
// Handle closing of the CKBox dialog.
|
|
451
|
+
this.on('ckbox:close', ()=>{
|
|
452
|
+
if (!this.value) {
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
this._wrapper.remove();
|
|
456
|
+
this._wrapper = null;
|
|
457
|
+
editor.editing.view.focus();
|
|
458
|
+
});
|
|
459
|
+
// Handle choosing the assets.
|
|
460
|
+
this.on('ckbox:choose', (evt, assets)=>{
|
|
461
|
+
if (!this.isEnabled) {
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
const imageCommand = editor.commands.get('insertImage');
|
|
465
|
+
const linkCommand = editor.commands.get('link');
|
|
466
|
+
const assetsToProcess = prepareAssets({
|
|
467
|
+
assets,
|
|
468
|
+
isImageAllowed: imageCommand.isEnabled,
|
|
469
|
+
isLinkAllowed: linkCommand.isEnabled
|
|
470
|
+
});
|
|
471
|
+
const assetsCount = assetsToProcess.length;
|
|
472
|
+
if (assetsCount === 0) {
|
|
473
|
+
return;
|
|
474
|
+
}
|
|
475
|
+
// All assets are inserted in one undo step.
|
|
476
|
+
model.change((writer)=>{
|
|
477
|
+
for (const asset of assetsToProcess){
|
|
478
|
+
const isLastAsset = asset === assetsToProcess[assetsCount - 1];
|
|
479
|
+
const isSingleAsset = assetsCount === 1;
|
|
480
|
+
this._insertAsset(asset, isLastAsset, writer, isSingleAsset);
|
|
481
|
+
// If asset ID must be set for the inserted model element, store the asset temporarily and remove it automatically
|
|
482
|
+
// after the timeout.
|
|
483
|
+
if (shouldInsertDataId) {
|
|
484
|
+
setTimeout(()=>this._chosenAssets.delete(asset), ASSET_INSERTION_WAIT_TIMEOUT);
|
|
485
|
+
this._chosenAssets.add(asset);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
});
|
|
489
|
+
editor.editing.view.focus();
|
|
490
|
+
});
|
|
491
|
+
// Clean up after the editor is destroyed.
|
|
492
|
+
this.listenTo(editor, 'destroy', ()=>{
|
|
493
|
+
this.fire('ckbox:close');
|
|
494
|
+
this._chosenAssets.clear();
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
/**
|
|
498
|
+
* Inserts the asset into the model.
|
|
499
|
+
*
|
|
500
|
+
* @param asset The asset to be inserted.
|
|
501
|
+
* @param isLastAsset Indicates if the current asset is the last one from the chosen set.
|
|
502
|
+
* @param writer An instance of the model writer.
|
|
503
|
+
* @param isSingleAsset It's true when only one asset is processed.
|
|
504
|
+
*/ _insertAsset(asset, isLastAsset, writer, isSingleAsset) {
|
|
505
|
+
const editor = this.editor;
|
|
506
|
+
const model = editor.model;
|
|
507
|
+
const selection = model.document.selection;
|
|
508
|
+
// Remove the `linkHref` attribute to not affect the asset to be inserted.
|
|
509
|
+
writer.removeSelectionAttribute('linkHref');
|
|
510
|
+
if (asset.type === 'image') {
|
|
511
|
+
this._insertImage(asset);
|
|
512
|
+
} else {
|
|
513
|
+
this._insertLink(asset, writer, isSingleAsset);
|
|
514
|
+
}
|
|
515
|
+
// Except for the last chosen asset, move the selection to the end of the current range to avoid overwriting other, already
|
|
516
|
+
// inserted assets.
|
|
517
|
+
if (!isLastAsset) {
|
|
518
|
+
writer.setSelection(selection.getLastPosition());
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* Inserts the image by calling the `insertImage` command.
|
|
523
|
+
*
|
|
524
|
+
* @param asset The asset to be inserted.
|
|
525
|
+
*/ _insertImage(asset) {
|
|
526
|
+
const editor = this.editor;
|
|
527
|
+
const { imageFallbackUrl, imageSources, imageTextAlternative, imageWidth, imageHeight, imagePlaceholder } = asset.attributes;
|
|
528
|
+
editor.execute('insertImage', {
|
|
529
|
+
source: {
|
|
530
|
+
src: imageFallbackUrl,
|
|
531
|
+
sources: imageSources,
|
|
532
|
+
alt: imageTextAlternative,
|
|
533
|
+
width: imageWidth,
|
|
534
|
+
height: imageHeight,
|
|
535
|
+
...imagePlaceholder ? {
|
|
536
|
+
placeholder: imagePlaceholder
|
|
537
|
+
} : null
|
|
538
|
+
}
|
|
539
|
+
});
|
|
540
|
+
}
|
|
541
|
+
/**
|
|
542
|
+
* Inserts the link to the asset by calling the `link` command.
|
|
543
|
+
*
|
|
544
|
+
* @param asset The asset to be inserted.
|
|
545
|
+
* @param writer An instance of the model writer.
|
|
546
|
+
* @param isSingleAsset It's true when only one asset is processed.
|
|
547
|
+
*/ _insertLink(asset, writer, isSingleAsset) {
|
|
548
|
+
const editor = this.editor;
|
|
549
|
+
const model = editor.model;
|
|
550
|
+
const selection = model.document.selection;
|
|
551
|
+
const { linkName, linkHref } = asset.attributes;
|
|
552
|
+
// If the selection is collapsed, insert the asset name as the link label and select it.
|
|
553
|
+
if (selection.isCollapsed) {
|
|
554
|
+
const selectionAttributes = toMap(selection.getAttributes());
|
|
555
|
+
const textNode = writer.createText(linkName, selectionAttributes);
|
|
556
|
+
if (!isSingleAsset) {
|
|
557
|
+
const selectionLastPosition = selection.getLastPosition();
|
|
558
|
+
const parentElement = selectionLastPosition.parent;
|
|
559
|
+
// Insert new `paragraph` when selection is not in an empty `paragraph`.
|
|
560
|
+
if (!(parentElement.name === 'paragraph' && parentElement.isEmpty)) {
|
|
561
|
+
editor.execute('insertParagraph', {
|
|
562
|
+
position: selectionLastPosition
|
|
563
|
+
});
|
|
564
|
+
}
|
|
565
|
+
const range = model.insertContent(textNode);
|
|
566
|
+
writer.setSelection(range);
|
|
567
|
+
editor.execute('link', linkHref);
|
|
568
|
+
return;
|
|
569
|
+
}
|
|
570
|
+
const range = model.insertContent(textNode);
|
|
571
|
+
writer.setSelection(range);
|
|
572
|
+
}
|
|
573
|
+
editor.execute('link', linkHref);
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
/**
|
|
577
|
+
* Parses the chosen assets into the internal data format. Filters out chosen assets that are not allowed.
|
|
578
|
+
*/ function prepareAssets({ assets, isImageAllowed, isLinkAllowed }) {
|
|
579
|
+
return assets.map((asset)=>isImage(asset) ? {
|
|
580
|
+
id: asset.data.id,
|
|
581
|
+
type: 'image',
|
|
582
|
+
attributes: prepareImageAssetAttributes(asset)
|
|
583
|
+
} : {
|
|
584
|
+
id: asset.data.id,
|
|
585
|
+
type: 'link',
|
|
586
|
+
attributes: prepareLinkAssetAttributes(asset)
|
|
587
|
+
}).filter((asset)=>asset.type === 'image' ? isImageAllowed : isLinkAllowed);
|
|
588
|
+
}
|
|
589
|
+
/**
|
|
590
|
+
* Parses the assets attributes into the internal data format.
|
|
591
|
+
*
|
|
592
|
+
* @internal
|
|
593
|
+
*/ function prepareImageAssetAttributes(asset) {
|
|
594
|
+
const { imageFallbackUrl, imageSources } = getImageUrls(asset.data.imageUrls);
|
|
595
|
+
const { description, width, height, blurHash } = asset.data.metadata;
|
|
596
|
+
const imagePlaceholder = blurHashToDataUrl(blurHash);
|
|
597
|
+
return {
|
|
598
|
+
imageFallbackUrl,
|
|
599
|
+
imageSources,
|
|
600
|
+
imageTextAlternative: description || '',
|
|
601
|
+
imageWidth: width,
|
|
602
|
+
imageHeight: height,
|
|
603
|
+
...imagePlaceholder ? {
|
|
604
|
+
imagePlaceholder
|
|
605
|
+
} : null
|
|
606
|
+
};
|
|
607
|
+
}
|
|
608
|
+
/**
|
|
609
|
+
* Parses the assets attributes into the internal data format.
|
|
610
|
+
*
|
|
611
|
+
* @param origin The base URL for assets inserted into the editor.
|
|
612
|
+
*/ function prepareLinkAssetAttributes(asset) {
|
|
613
|
+
return {
|
|
614
|
+
linkName: asset.data.name,
|
|
615
|
+
linkHref: getAssetUrl(asset)
|
|
616
|
+
};
|
|
617
|
+
}
|
|
618
|
+
/**
|
|
619
|
+
* Checks whether the asset is an image.
|
|
620
|
+
*/ function isImage(asset) {
|
|
621
|
+
const metadata = asset.data.metadata;
|
|
622
|
+
if (!metadata) {
|
|
623
|
+
return false;
|
|
624
|
+
}
|
|
625
|
+
return metadata.width && metadata.height;
|
|
626
|
+
}
|
|
627
|
+
/**
|
|
628
|
+
* Creates the URL for the asset.
|
|
629
|
+
*
|
|
630
|
+
* @param origin The base URL for assets inserted into the editor.
|
|
631
|
+
*/ function getAssetUrl(asset) {
|
|
632
|
+
const url = new URL(asset.data.url);
|
|
633
|
+
url.searchParams.set('download', 'true');
|
|
634
|
+
return url.toString();
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
const DEFAULT_CKBOX_THEME_NAME = 'lark';
|
|
638
|
+
/**
|
|
639
|
+
* The CKBox utilities plugin.
|
|
640
|
+
*/ class CKBoxUtils extends Plugin {
|
|
641
|
+
/**
|
|
642
|
+
* CKEditor Cloud Services access token.
|
|
643
|
+
*/ _token;
|
|
644
|
+
/**
|
|
645
|
+
* @inheritDoc
|
|
646
|
+
*/ static get pluginName() {
|
|
647
|
+
return 'CKBoxUtils';
|
|
648
|
+
}
|
|
649
|
+
/**
|
|
650
|
+
* @inheritDoc
|
|
651
|
+
*/ static get isOfficialPlugin() {
|
|
652
|
+
return true;
|
|
653
|
+
}
|
|
654
|
+
/**
|
|
655
|
+
* @inheritDoc
|
|
656
|
+
*/ static get requires() {
|
|
657
|
+
return [
|
|
658
|
+
'CloudServices'
|
|
659
|
+
];
|
|
660
|
+
}
|
|
661
|
+
/**
|
|
662
|
+
* @inheritDoc
|
|
663
|
+
*/ init() {
|
|
664
|
+
const editor = this.editor;
|
|
665
|
+
const hasConfiguration = !!editor.config.get('ckbox');
|
|
666
|
+
const isLibraryLoaded = !!window.CKBox;
|
|
667
|
+
// Proceed with plugin initialization only when the integrator intentionally wants to use it, i.e. when the `config.ckbox` exists or
|
|
668
|
+
// the CKBox JavaScript library is loaded.
|
|
669
|
+
if (!hasConfiguration && !isLibraryLoaded) {
|
|
670
|
+
return;
|
|
671
|
+
}
|
|
672
|
+
editor.config.define('ckbox', {
|
|
673
|
+
serviceOrigin: 'https://api.ckbox.io',
|
|
674
|
+
defaultUploadCategories: null,
|
|
675
|
+
ignoreDataId: false,
|
|
676
|
+
language: editor.locale.uiLanguage,
|
|
677
|
+
theme: DEFAULT_CKBOX_THEME_NAME,
|
|
678
|
+
tokenUrl: editor.config.get('cloudServices.tokenUrl')
|
|
679
|
+
});
|
|
680
|
+
const cloudServices = editor.plugins.get('CloudServices');
|
|
681
|
+
const cloudServicesTokenUrl = editor.config.get('cloudServices.tokenUrl');
|
|
682
|
+
const ckboxTokenUrl = editor.config.get('ckbox.tokenUrl');
|
|
683
|
+
if (!ckboxTokenUrl) {
|
|
684
|
+
/**
|
|
685
|
+
* The {@link module:ckbox/ckboxconfig~CKBoxConfig#tokenUrl `config.ckbox.tokenUrl`} or the
|
|
686
|
+
* {@link module:cloud-services/cloudservicesconfig~CloudServicesConfig#tokenUrl `config.cloudServices.tokenUrl`}
|
|
687
|
+
* configuration is required for the CKBox plugin.
|
|
688
|
+
*
|
|
689
|
+
* ```ts
|
|
690
|
+
* ClassicEditor.create( document.createElement( 'div' ), {
|
|
691
|
+
* ckbox: {
|
|
692
|
+
* tokenUrl: "YOUR_TOKEN_URL"
|
|
693
|
+
* // ...
|
|
694
|
+
* }
|
|
695
|
+
* // ...
|
|
696
|
+
* } );
|
|
697
|
+
* ```
|
|
698
|
+
*
|
|
699
|
+
* @error ckbox-plugin-missing-token-url
|
|
700
|
+
*/ throw new CKEditorError('ckbox-plugin-missing-token-url', this);
|
|
701
|
+
}
|
|
702
|
+
if (ckboxTokenUrl == cloudServicesTokenUrl) {
|
|
703
|
+
this._token = Promise.resolve(cloudServices.token);
|
|
704
|
+
} else {
|
|
705
|
+
this._token = cloudServices.registerTokenUrl(ckboxTokenUrl);
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
/**
|
|
709
|
+
* Returns a token used by the CKBox plugin for communication with the CKBox service.
|
|
710
|
+
*/ getToken() {
|
|
711
|
+
return this._token;
|
|
712
|
+
}
|
|
713
|
+
/**
|
|
714
|
+
* The ID of workspace to use when uploading an image.
|
|
715
|
+
*/ async getWorkspaceId() {
|
|
716
|
+
const t = this.editor.t;
|
|
717
|
+
const cannotAccessDefaultWorkspaceError = t('Cannot access default workspace.');
|
|
718
|
+
const defaultWorkspaceId = this.editor.config.get('ckbox.defaultUploadWorkspaceId');
|
|
719
|
+
const workspaceId = getWorkspaceId(await this._token, defaultWorkspaceId);
|
|
720
|
+
if (workspaceId == null) {
|
|
721
|
+
/**
|
|
722
|
+
* The user is not authorized to access the workspace defined in the`ckbox.defaultUploadWorkspaceId` configuration.
|
|
723
|
+
*
|
|
724
|
+
* @error ckbox-access-default-workspace-error
|
|
725
|
+
*/ logError('ckbox-access-default-workspace-error');
|
|
726
|
+
throw cannotAccessDefaultWorkspaceError;
|
|
727
|
+
}
|
|
728
|
+
return workspaceId;
|
|
729
|
+
}
|
|
730
|
+
/**
|
|
731
|
+
* Resolves a promise with an object containing a category with which the uploaded file is associated or an error code.
|
|
732
|
+
*/ async getCategoryIdForFile(fileOrUrl, options) {
|
|
733
|
+
const t = this.editor.t;
|
|
734
|
+
const cannotFindCategoryError = t('Cannot determine a category for the uploaded file.');
|
|
735
|
+
const defaultCategories = this.editor.config.get('ckbox.defaultUploadCategories');
|
|
736
|
+
const allCategoriesPromise = this._getAvailableCategories(options);
|
|
737
|
+
const extension = typeof fileOrUrl == 'string' ? convertMimeTypeToExtension(await getContentTypeOfUrl(fileOrUrl, options)) : getFileExtension(fileOrUrl);
|
|
738
|
+
const allCategories = await allCategoriesPromise;
|
|
739
|
+
// Couldn't fetch all categories. Perhaps the authorization token is invalid.
|
|
740
|
+
if (!allCategories) {
|
|
741
|
+
throw cannotFindCategoryError;
|
|
742
|
+
}
|
|
743
|
+
// If a user specifies the plugin configuration, find the first category that accepts the uploaded file.
|
|
744
|
+
if (defaultCategories) {
|
|
745
|
+
const userCategory = Object.keys(defaultCategories).find((category)=>{
|
|
746
|
+
return defaultCategories[category].find((e)=>e.toLowerCase() == extension);
|
|
747
|
+
});
|
|
748
|
+
// If found, return its ID if the category exists on the server side.
|
|
749
|
+
if (userCategory) {
|
|
750
|
+
const serverCategory = allCategories.find((category)=>category.id === userCategory || category.name === userCategory);
|
|
751
|
+
if (!serverCategory) {
|
|
752
|
+
throw cannotFindCategoryError;
|
|
753
|
+
}
|
|
754
|
+
return serverCategory.id;
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
// Otherwise, find the first category that accepts the uploaded file and returns its ID.
|
|
758
|
+
const category = allCategories.find((category)=>category.extensions.find((e)=>e.toLowerCase() == extension));
|
|
759
|
+
if (!category) {
|
|
760
|
+
throw cannotFindCategoryError;
|
|
761
|
+
}
|
|
762
|
+
return category.id;
|
|
763
|
+
}
|
|
764
|
+
/**
|
|
765
|
+
* Resolves a promise with an array containing available categories with which the uploaded file can be associated.
|
|
766
|
+
*
|
|
767
|
+
* If the API returns limited results, the method will collect all items.
|
|
768
|
+
*/ async _getAvailableCategories(options) {
|
|
769
|
+
const ITEMS_PER_REQUEST = 50;
|
|
770
|
+
const editor = this.editor;
|
|
771
|
+
const token = this._token;
|
|
772
|
+
const { signal } = options;
|
|
773
|
+
const serviceOrigin = editor.config.get('ckbox.serviceOrigin');
|
|
774
|
+
const workspaceId = await this.getWorkspaceId();
|
|
775
|
+
try {
|
|
776
|
+
const result = [];
|
|
777
|
+
let offset = 0;
|
|
778
|
+
let remainingItems;
|
|
779
|
+
do {
|
|
780
|
+
const data = await fetchCategories(offset);
|
|
781
|
+
result.push(...data.items);
|
|
782
|
+
remainingItems = data.totalCount - (offset + ITEMS_PER_REQUEST);
|
|
783
|
+
offset += ITEMS_PER_REQUEST;
|
|
784
|
+
}while (remainingItems > 0)
|
|
785
|
+
return result;
|
|
786
|
+
} catch {
|
|
787
|
+
signal.throwIfAborted();
|
|
788
|
+
/**
|
|
789
|
+
* Fetching a list of available categories with which an uploaded file can be associated failed.
|
|
790
|
+
*
|
|
791
|
+
* @error ckbox-fetch-category-http-error
|
|
792
|
+
*/ logError('ckbox-fetch-category-http-error');
|
|
793
|
+
return undefined;
|
|
794
|
+
}
|
|
795
|
+
async function fetchCategories(offset) {
|
|
796
|
+
const categoryUrl = new URL('categories', serviceOrigin);
|
|
797
|
+
categoryUrl.searchParams.set('limit', String(ITEMS_PER_REQUEST));
|
|
798
|
+
categoryUrl.searchParams.set('offset', String(offset));
|
|
799
|
+
categoryUrl.searchParams.set('workspaceId', workspaceId);
|
|
800
|
+
return sendHttpRequest({
|
|
801
|
+
url: categoryUrl,
|
|
802
|
+
signal,
|
|
803
|
+
authorization: (await token).value
|
|
804
|
+
});
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
/**
|
|
810
|
+
* A plugin that enables file uploads in CKEditor 5 using the CKBox server–side connector.
|
|
811
|
+
* See the {@glink features/file-management/ckbox CKBox file manager integration} guide to learn how to configure
|
|
812
|
+
* and use this feature as well as find out more about the full integration with the file manager
|
|
813
|
+
* provided by the {@link module:ckbox/ckbox~CKBox} plugin.
|
|
814
|
+
*
|
|
815
|
+
* Check out the {@glink features/images/image-upload/image-upload Image upload overview} guide to learn about
|
|
816
|
+
* other ways to upload images into CKEditor 5.
|
|
817
|
+
*/ class CKBoxUploadAdapter extends Plugin {
|
|
818
|
+
/**
|
|
819
|
+
* @inheritDoc
|
|
820
|
+
*/ static get requires() {
|
|
821
|
+
return [
|
|
822
|
+
'ImageUploadEditing',
|
|
823
|
+
'ImageUploadProgress',
|
|
824
|
+
FileRepository,
|
|
825
|
+
CKBoxEditing
|
|
826
|
+
];
|
|
827
|
+
}
|
|
828
|
+
/**
|
|
829
|
+
* @inheritDoc
|
|
830
|
+
*/ static get pluginName() {
|
|
831
|
+
return 'CKBoxUploadAdapter';
|
|
832
|
+
}
|
|
833
|
+
/**
|
|
834
|
+
* @inheritDoc
|
|
835
|
+
*/ static get isOfficialPlugin() {
|
|
836
|
+
return true;
|
|
837
|
+
}
|
|
838
|
+
/**
|
|
839
|
+
* @inheritDoc
|
|
840
|
+
*/ async afterInit() {
|
|
841
|
+
const editor = this.editor;
|
|
842
|
+
const hasConfiguration = !!editor.config.get('ckbox');
|
|
843
|
+
const isLibraryLoaded = !!window.CKBox;
|
|
844
|
+
// Editor supports only one upload adapter. Register the CKBox upload adapter (and potentially overwrite other one) only when the
|
|
845
|
+
// integrator intentionally wants to use the CKBox plugin, i.e. when the `config.ckbox` exists or the CKBox JavaScript library is
|
|
846
|
+
// loaded.
|
|
847
|
+
if (!hasConfiguration && !isLibraryLoaded) {
|
|
848
|
+
return;
|
|
849
|
+
}
|
|
850
|
+
const fileRepository = editor.plugins.get(FileRepository);
|
|
851
|
+
const ckboxUtils = editor.plugins.get(CKBoxUtils);
|
|
852
|
+
fileRepository.createUploadAdapter = (loader)=>new Adapter(loader, editor, ckboxUtils);
|
|
853
|
+
const shouldInsertDataId = !editor.config.get('ckbox.ignoreDataId');
|
|
854
|
+
const imageUploadEditing = editor.plugins.get('ImageUploadEditing');
|
|
855
|
+
// Mark uploaded assets with the `ckboxImageId` attribute. Its value represents an ID in CKBox.
|
|
856
|
+
if (shouldInsertDataId) {
|
|
857
|
+
imageUploadEditing.on('uploadComplete', (evt, { imageElement, data })=>{
|
|
858
|
+
editor.model.change((writer)=>{
|
|
859
|
+
writer.setAttribute('ckboxImageId', data.ckboxImageId, imageElement);
|
|
860
|
+
});
|
|
861
|
+
});
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
/**
|
|
866
|
+
* Upload adapter for CKBox.
|
|
867
|
+
*/ class Adapter {
|
|
868
|
+
/**
|
|
869
|
+
* FileLoader instance to use during the upload.
|
|
870
|
+
*/ loader;
|
|
871
|
+
/**
|
|
872
|
+
* CKEditor Cloud Services access token.
|
|
873
|
+
*/ token;
|
|
874
|
+
/**
|
|
875
|
+
* The editor instance.
|
|
876
|
+
*/ editor;
|
|
877
|
+
/**
|
|
878
|
+
* The abort controller for aborting asynchronous processes.
|
|
879
|
+
*/ controller;
|
|
880
|
+
/**
|
|
881
|
+
* The base URL where all requests should be sent.
|
|
882
|
+
*/ serviceOrigin;
|
|
883
|
+
/**
|
|
884
|
+
* The reference to CKBoxUtils plugin.
|
|
885
|
+
*/ ckboxUtils;
|
|
886
|
+
/**
|
|
887
|
+
* Creates a new adapter instance.
|
|
888
|
+
*/ constructor(loader, editor, ckboxUtils){
|
|
889
|
+
this.loader = loader;
|
|
890
|
+
this.token = ckboxUtils.getToken();
|
|
891
|
+
this.ckboxUtils = ckboxUtils;
|
|
892
|
+
this.editor = editor;
|
|
893
|
+
this.controller = new AbortController();
|
|
894
|
+
this.serviceOrigin = editor.config.get('ckbox.serviceOrigin');
|
|
895
|
+
}
|
|
896
|
+
/**
|
|
897
|
+
* Starts the upload process.
|
|
898
|
+
*
|
|
899
|
+
* @see module:upload/filerepository~UploadAdapter#upload
|
|
900
|
+
*/ async upload() {
|
|
901
|
+
const ckboxUtils = this.ckboxUtils;
|
|
902
|
+
const t = this.editor.t;
|
|
903
|
+
const file = await this.loader.file;
|
|
904
|
+
const category = await ckboxUtils.getCategoryIdForFile(file, {
|
|
905
|
+
signal: this.controller.signal
|
|
906
|
+
});
|
|
907
|
+
const uploadUrl = new URL('assets', this.serviceOrigin);
|
|
908
|
+
const formData = new FormData();
|
|
909
|
+
uploadUrl.searchParams.set('workspaceId', await ckboxUtils.getWorkspaceId());
|
|
910
|
+
formData.append('categoryId', category);
|
|
911
|
+
formData.append('file', file);
|
|
912
|
+
const requestConfig = {
|
|
913
|
+
method: 'POST',
|
|
914
|
+
url: uploadUrl,
|
|
915
|
+
data: formData,
|
|
916
|
+
onUploadProgress: (evt)=>{
|
|
917
|
+
/* istanbul ignore else -- @preserve */ if (evt.lengthComputable) {
|
|
918
|
+
this.loader.uploadTotal = evt.total;
|
|
919
|
+
this.loader.uploaded = evt.loaded;
|
|
920
|
+
}
|
|
921
|
+
},
|
|
922
|
+
signal: this.controller.signal,
|
|
923
|
+
authorization: (await this.token).value
|
|
924
|
+
};
|
|
925
|
+
return sendHttpRequest(requestConfig).then(async (data)=>{
|
|
926
|
+
const imageUrls = getImageUrls(data.imageUrls);
|
|
927
|
+
return {
|
|
928
|
+
ckboxImageId: data.id,
|
|
929
|
+
default: imageUrls.imageFallbackUrl,
|
|
930
|
+
sources: imageUrls.imageSources
|
|
931
|
+
};
|
|
932
|
+
}).catch(()=>{
|
|
933
|
+
const genericError = t('Cannot upload file:') + ` ${file.name}.`;
|
|
934
|
+
return Promise.reject(genericError);
|
|
935
|
+
});
|
|
936
|
+
}
|
|
937
|
+
/**
|
|
938
|
+
* Aborts the upload process.
|
|
939
|
+
*
|
|
940
|
+
* @see module:upload/filerepository~UploadAdapter#abort
|
|
941
|
+
*/ abort() {
|
|
942
|
+
this.controller.abort();
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
const COMMAND_FORCE_DISABLE_ID = 'NoPermission';
|
|
947
|
+
/**
|
|
948
|
+
* The CKBox editing feature. It introduces the {@link module:ckbox/ckboxcommand~CKBoxCommand CKBox command} and
|
|
949
|
+
* {@link module:ckbox/ckboxuploadadapter~CKBoxUploadAdapter CKBox upload adapter}.
|
|
950
|
+
*/ class CKBoxEditing extends Plugin {
|
|
951
|
+
/**
|
|
952
|
+
* @inheritDoc
|
|
953
|
+
*/ static get pluginName() {
|
|
954
|
+
return 'CKBoxEditing';
|
|
955
|
+
}
|
|
956
|
+
/**
|
|
957
|
+
* @inheritDoc
|
|
958
|
+
*/ static get isOfficialPlugin() {
|
|
959
|
+
return true;
|
|
960
|
+
}
|
|
961
|
+
/**
|
|
962
|
+
* @inheritDoc
|
|
963
|
+
*/ static get requires() {
|
|
964
|
+
return [
|
|
965
|
+
'LinkEditing',
|
|
966
|
+
'PictureEditing',
|
|
967
|
+
CKBoxUploadAdapter,
|
|
968
|
+
CKBoxUtils
|
|
969
|
+
];
|
|
970
|
+
}
|
|
971
|
+
/**
|
|
972
|
+
* @inheritDoc
|
|
973
|
+
*/ init() {
|
|
974
|
+
const editor = this.editor;
|
|
975
|
+
if (!this._shouldBeInitialised()) {
|
|
976
|
+
return;
|
|
977
|
+
}
|
|
978
|
+
this._checkImagePlugins();
|
|
979
|
+
// Registering the `ckbox` command makes sense only if the CKBox library is loaded, as the `ckbox` command opens the CKBox dialog.
|
|
980
|
+
if (isLibraryLoaded()) {
|
|
981
|
+
editor.commands.add('ckbox', new CKBoxCommand(editor));
|
|
982
|
+
}
|
|
983
|
+
// Promise is not handled intentionally. Errors should be displayed in console if there are so.
|
|
984
|
+
isUploadPermissionGranted(editor).then((isCreateAssetAllowed)=>{
|
|
985
|
+
if (!isCreateAssetAllowed) {
|
|
986
|
+
this._blockImageCommands();
|
|
987
|
+
}
|
|
988
|
+
});
|
|
989
|
+
}
|
|
990
|
+
/**
|
|
991
|
+
* @inheritDoc
|
|
992
|
+
*/ afterInit() {
|
|
993
|
+
const editor = this.editor;
|
|
994
|
+
if (!this._shouldBeInitialised()) {
|
|
995
|
+
return;
|
|
996
|
+
}
|
|
997
|
+
// Extending the schema, registering converters and applying fixers only make sense if the configuration option to assign
|
|
998
|
+
// the assets ID with the model elements is enabled.
|
|
999
|
+
if (!editor.config.get('ckbox.ignoreDataId')) {
|
|
1000
|
+
this._initSchema();
|
|
1001
|
+
this._initConversion();
|
|
1002
|
+
this._initFixers();
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
/**
|
|
1006
|
+
* Returns true only when the integrator intentionally wants to use the plugin, i.e. when the `config.ckbox` exists or
|
|
1007
|
+
* the CKBox JavaScript library is loaded.
|
|
1008
|
+
*/ _shouldBeInitialised() {
|
|
1009
|
+
const editor = this.editor;
|
|
1010
|
+
const hasConfiguration = !!editor.config.get('ckbox');
|
|
1011
|
+
return hasConfiguration || isLibraryLoaded();
|
|
1012
|
+
}
|
|
1013
|
+
/**
|
|
1014
|
+
* Blocks `uploadImage` and `ckboxImageEdit` commands.
|
|
1015
|
+
*/ _blockImageCommands() {
|
|
1016
|
+
const editor = this.editor;
|
|
1017
|
+
const uploadImageCommand = editor.commands.get('uploadImage');
|
|
1018
|
+
const imageEditingCommand = editor.commands.get('ckboxImageEdit');
|
|
1019
|
+
if (uploadImageCommand) {
|
|
1020
|
+
uploadImageCommand.isAccessAllowed = false;
|
|
1021
|
+
uploadImageCommand.forceDisabled(COMMAND_FORCE_DISABLE_ID);
|
|
1022
|
+
}
|
|
1023
|
+
if (imageEditingCommand) {
|
|
1024
|
+
imageEditingCommand.forceDisabled(COMMAND_FORCE_DISABLE_ID);
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1027
|
+
/**
|
|
1028
|
+
* Checks if at least one image plugin is loaded.
|
|
1029
|
+
*/ _checkImagePlugins() {
|
|
1030
|
+
const editor = this.editor;
|
|
1031
|
+
if (!editor.plugins.has('ImageBlockEditing') && !editor.plugins.has('ImageInlineEditing')) {
|
|
1032
|
+
/**
|
|
1033
|
+
* The CKBox feature requires one of the following plugins to be loaded to work correctly:
|
|
1034
|
+
*
|
|
1035
|
+
* * {@link module:image/imageblock~ImageBlock},
|
|
1036
|
+
* * {@link module:image/imageinline~ImageInline},
|
|
1037
|
+
* * {@link module:image/image~Image} (loads both `ImageBlock` and `ImageInline`)
|
|
1038
|
+
*
|
|
1039
|
+
* Please make sure your editor configuration is correct.
|
|
1040
|
+
*
|
|
1041
|
+
* @error ckbox-plugin-image-feature-missing
|
|
1042
|
+
* @param {module:core/editor/editor~Editor} editor
|
|
1043
|
+
*/ logError('ckbox-plugin-image-feature-missing', editor);
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
1046
|
+
/**
|
|
1047
|
+
* Extends the schema to allow the `ckboxImageId` and `ckboxLinkId` attributes for links and images.
|
|
1048
|
+
*/ _initSchema() {
|
|
1049
|
+
const editor = this.editor;
|
|
1050
|
+
const schema = editor.model.schema;
|
|
1051
|
+
schema.extend('$text', {
|
|
1052
|
+
allowAttributes: 'ckboxLinkId'
|
|
1053
|
+
});
|
|
1054
|
+
if (schema.isRegistered('imageBlock')) {
|
|
1055
|
+
schema.extend('imageBlock', {
|
|
1056
|
+
allowAttributes: [
|
|
1057
|
+
'ckboxImageId',
|
|
1058
|
+
'ckboxLinkId'
|
|
1059
|
+
]
|
|
1060
|
+
});
|
|
1061
|
+
}
|
|
1062
|
+
if (schema.isRegistered('imageInline')) {
|
|
1063
|
+
schema.extend('imageInline', {
|
|
1064
|
+
allowAttributes: [
|
|
1065
|
+
'ckboxImageId',
|
|
1066
|
+
'ckboxLinkId'
|
|
1067
|
+
]
|
|
1068
|
+
});
|
|
1069
|
+
}
|
|
1070
|
+
schema.addAttributeCheck((context)=>{
|
|
1071
|
+
// Don't allow `ckboxLinkId` on elements which do not have `linkHref` attribute.
|
|
1072
|
+
if (!context.last.getAttribute('linkHref')) {
|
|
1073
|
+
return false;
|
|
1074
|
+
}
|
|
1075
|
+
}, 'ckboxLinkId');
|
|
1076
|
+
}
|
|
1077
|
+
/**
|
|
1078
|
+
* Configures the upcast and downcast conversions for the `ckboxImageId` and `ckboxLinkId` attributes.
|
|
1079
|
+
*/ _initConversion() {
|
|
1080
|
+
const editor = this.editor;
|
|
1081
|
+
// Convert `ckboxLinkId` => `data-ckbox-resource-id`.
|
|
1082
|
+
editor.conversion.for('downcast').add((dispatcher)=>{
|
|
1083
|
+
// Due to custom converters for linked block images, handle the `ckboxLinkId` attribute manually.
|
|
1084
|
+
dispatcher.on('attribute:ckboxLinkId:imageBlock', (evt, data, conversionApi)=>{
|
|
1085
|
+
const { writer, mapper, consumable } = conversionApi;
|
|
1086
|
+
if (!consumable.consume(data.item, evt.name)) {
|
|
1087
|
+
return;
|
|
1088
|
+
}
|
|
1089
|
+
const viewFigure = mapper.toViewElement(data.item);
|
|
1090
|
+
const linkInImage = [
|
|
1091
|
+
...viewFigure.getChildren()
|
|
1092
|
+
].find((child)=>child.name === 'a');
|
|
1093
|
+
// No link inside an image - no conversion needed.
|
|
1094
|
+
if (!linkInImage) {
|
|
1095
|
+
return;
|
|
1096
|
+
}
|
|
1097
|
+
if (data.item.hasAttribute('ckboxLinkId')) {
|
|
1098
|
+
writer.setAttribute('data-ckbox-resource-id', data.item.getAttribute('ckboxLinkId'), linkInImage);
|
|
1099
|
+
} else {
|
|
1100
|
+
writer.removeAttribute('data-ckbox-resource-id', linkInImage);
|
|
1101
|
+
}
|
|
1102
|
+
}, {
|
|
1103
|
+
priority: 'low'
|
|
1104
|
+
});
|
|
1105
|
+
dispatcher.on('attribute:ckboxLinkId', (evt, data, conversionApi)=>{
|
|
1106
|
+
const { writer, mapper, consumable } = conversionApi;
|
|
1107
|
+
if (!consumable.consume(data.item, evt.name)) {
|
|
1108
|
+
return;
|
|
1109
|
+
}
|
|
1110
|
+
// Remove the previous attribute value if it was applied.
|
|
1111
|
+
if (data.attributeOldValue) {
|
|
1112
|
+
const viewElement = createLinkElement(writer, data.attributeOldValue);
|
|
1113
|
+
writer.unwrap(mapper.toViewRange(data.range), viewElement);
|
|
1114
|
+
}
|
|
1115
|
+
// Add the new attribute value if specified in a model element.
|
|
1116
|
+
if (data.attributeNewValue) {
|
|
1117
|
+
const viewElement = createLinkElement(writer, data.attributeNewValue);
|
|
1118
|
+
if (data.item.is('selection')) {
|
|
1119
|
+
const viewSelection = writer.document.selection;
|
|
1120
|
+
writer.wrap(viewSelection.getFirstRange(), viewElement);
|
|
1121
|
+
} else {
|
|
1122
|
+
writer.wrap(mapper.toViewRange(data.range), viewElement);
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
}, {
|
|
1126
|
+
priority: 'low'
|
|
1127
|
+
});
|
|
1128
|
+
});
|
|
1129
|
+
// Convert `data-ckbox-resource-id` => `ckboxLinkId`.
|
|
1130
|
+
//
|
|
1131
|
+
// The helper conversion does not handle all cases, so take care of the `data-ckbox-resource-id` attribute manually for images
|
|
1132
|
+
// and links.
|
|
1133
|
+
editor.conversion.for('upcast').add((dispatcher)=>{
|
|
1134
|
+
dispatcher.on('element:a', (evt, data, conversionApi)=>{
|
|
1135
|
+
const { writer, consumable } = conversionApi;
|
|
1136
|
+
// Upcast the `data-ckbox-resource-id` attribute only for valid link elements.
|
|
1137
|
+
if (!data.viewItem.getAttribute('href')) {
|
|
1138
|
+
return;
|
|
1139
|
+
}
|
|
1140
|
+
const consumableAttributes = {
|
|
1141
|
+
attributes: [
|
|
1142
|
+
'data-ckbox-resource-id'
|
|
1143
|
+
]
|
|
1144
|
+
};
|
|
1145
|
+
if (!consumable.consume(data.viewItem, consumableAttributes)) {
|
|
1146
|
+
return;
|
|
1147
|
+
}
|
|
1148
|
+
const attributeValue = data.viewItem.getAttribute('data-ckbox-resource-id');
|
|
1149
|
+
// Missing the `data-ckbox-resource-id` attribute.
|
|
1150
|
+
if (!attributeValue) {
|
|
1151
|
+
return;
|
|
1152
|
+
}
|
|
1153
|
+
if (data.modelRange) {
|
|
1154
|
+
// If the `<a>` element contains more than single children (e.g. a linked image), set the `ckboxLinkId` for each
|
|
1155
|
+
// allowed child.
|
|
1156
|
+
for (let item of data.modelRange.getItems()){
|
|
1157
|
+
if (item.is('$textProxy')) {
|
|
1158
|
+
item = item.textNode;
|
|
1159
|
+
}
|
|
1160
|
+
// Do not copy the `ckboxLinkId` attribute when wrapping an element in a block element, e.g. when
|
|
1161
|
+
// auto-paragraphing.
|
|
1162
|
+
if (shouldUpcastAttributeForNode(item)) {
|
|
1163
|
+
writer.setAttribute('ckboxLinkId', attributeValue, item);
|
|
1164
|
+
}
|
|
1165
|
+
}
|
|
1166
|
+
} else {
|
|
1167
|
+
// Otherwise, just set the `ckboxLinkId` for the model element.
|
|
1168
|
+
const modelElement = data.modelCursor.nodeBefore || data.modelCursor.parent;
|
|
1169
|
+
writer.setAttribute('ckboxLinkId', attributeValue, modelElement);
|
|
1170
|
+
}
|
|
1171
|
+
}, {
|
|
1172
|
+
priority: 'low'
|
|
1173
|
+
});
|
|
1174
|
+
});
|
|
1175
|
+
// Convert `ckboxImageId` => `data-ckbox-resource-id`.
|
|
1176
|
+
editor.conversion.for('downcast').attributeToAttribute({
|
|
1177
|
+
model: 'ckboxImageId',
|
|
1178
|
+
view: 'data-ckbox-resource-id'
|
|
1179
|
+
});
|
|
1180
|
+
// Convert `data-ckbox-resource-id` => `ckboxImageId`.
|
|
1181
|
+
editor.conversion.for('upcast').elementToAttribute({
|
|
1182
|
+
model: {
|
|
1183
|
+
key: 'ckboxImageId',
|
|
1184
|
+
value: (viewElement)=>viewElement.getAttribute('data-ckbox-resource-id')
|
|
1185
|
+
},
|
|
1186
|
+
view: {
|
|
1187
|
+
attributes: {
|
|
1188
|
+
'data-ckbox-resource-id': /[\s\S]+/
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
});
|
|
1192
|
+
const replaceImageSourceCommand = editor.commands.get('replaceImageSource');
|
|
1193
|
+
if (replaceImageSourceCommand) {
|
|
1194
|
+
this.listenTo(replaceImageSourceCommand, 'cleanupImage', (_, [writer, image])=>{
|
|
1195
|
+
writer.removeAttribute('ckboxImageId', image);
|
|
1196
|
+
});
|
|
1197
|
+
}
|
|
1198
|
+
}
|
|
1199
|
+
/**
|
|
1200
|
+
* Registers post-fixers that add or remove the `ckboxLinkId` and `ckboxImageId` attributes.
|
|
1201
|
+
*/ _initFixers() {
|
|
1202
|
+
const editor = this.editor;
|
|
1203
|
+
const model = editor.model;
|
|
1204
|
+
const selection = model.document.selection;
|
|
1205
|
+
// Registers the post-fixer to sync the asset ID with the model elements.
|
|
1206
|
+
model.document.registerPostFixer(syncDataIdPostFixer(editor));
|
|
1207
|
+
// Registers the post-fixer to remove the `ckboxLinkId` attribute from the model selection.
|
|
1208
|
+
model.document.registerPostFixer(injectSelectionPostFixer(selection));
|
|
1209
|
+
}
|
|
1210
|
+
}
|
|
1211
|
+
/**
|
|
1212
|
+
* A post-fixer that synchronizes the asset ID with the model element.
|
|
1213
|
+
*/ function syncDataIdPostFixer(editor) {
|
|
1214
|
+
return (writer)=>{
|
|
1215
|
+
let changed = false;
|
|
1216
|
+
const model = editor.model;
|
|
1217
|
+
const ckboxCommand = editor.commands.get('ckbox');
|
|
1218
|
+
// The ID from chosen assets are stored in the `CKBoxCommand#_chosenAssets`. If there is no command, it makes no sense to check
|
|
1219
|
+
// for changes in the model.
|
|
1220
|
+
if (!ckboxCommand) {
|
|
1221
|
+
return changed;
|
|
1222
|
+
}
|
|
1223
|
+
for (const entry of model.document.differ.getChanges()){
|
|
1224
|
+
if (entry.type !== 'insert' && entry.type !== 'attribute') {
|
|
1225
|
+
continue;
|
|
1226
|
+
}
|
|
1227
|
+
const range = entry.type === 'insert' ? new Range(entry.position, entry.position.getShiftedBy(entry.length)) : entry.range;
|
|
1228
|
+
const isLinkHrefAttributeRemoval = entry.type === 'attribute' && entry.attributeKey === 'linkHref' && entry.attributeNewValue === null;
|
|
1229
|
+
for (const item of range.getItems()){
|
|
1230
|
+
// If the `linkHref` attribute has been removed, sync the change with the `ckboxLinkId` attribute.
|
|
1231
|
+
if (isLinkHrefAttributeRemoval && item.hasAttribute('ckboxLinkId')) {
|
|
1232
|
+
writer.removeAttribute('ckboxLinkId', item);
|
|
1233
|
+
changed = true;
|
|
1234
|
+
continue;
|
|
1235
|
+
}
|
|
1236
|
+
// Otherwise, the change concerns either a new model element or an attribute change. Try to find the assets for the modified
|
|
1237
|
+
// model element.
|
|
1238
|
+
const assets = findAssetsForItem(item, ckboxCommand._chosenAssets);
|
|
1239
|
+
for (const asset of assets){
|
|
1240
|
+
const attributeName = asset.type === 'image' ? 'ckboxImageId' : 'ckboxLinkId';
|
|
1241
|
+
if (asset.id === item.getAttribute(attributeName)) {
|
|
1242
|
+
continue;
|
|
1243
|
+
}
|
|
1244
|
+
writer.setAttribute(attributeName, asset.id, item);
|
|
1245
|
+
changed = true;
|
|
1246
|
+
}
|
|
1247
|
+
}
|
|
1248
|
+
}
|
|
1249
|
+
return changed;
|
|
1250
|
+
};
|
|
1251
|
+
}
|
|
1252
|
+
/**
|
|
1253
|
+
* A post-fixer that removes the `ckboxLinkId` from the selection if it does not represent a link anymore.
|
|
1254
|
+
*/ function injectSelectionPostFixer(selection) {
|
|
1255
|
+
return (writer)=>{
|
|
1256
|
+
const shouldRemoveLinkIdAttribute = !selection.hasAttribute('linkHref') && selection.hasAttribute('ckboxLinkId');
|
|
1257
|
+
if (shouldRemoveLinkIdAttribute) {
|
|
1258
|
+
writer.removeSelectionAttribute('ckboxLinkId');
|
|
1259
|
+
return true;
|
|
1260
|
+
}
|
|
1261
|
+
return false;
|
|
1262
|
+
};
|
|
1263
|
+
}
|
|
1264
|
+
/**
|
|
1265
|
+
* Tries to find the asset that is associated with the model element by comparing the attributes:
|
|
1266
|
+
* - the image fallback URL with the `src` attribute for images,
|
|
1267
|
+
* - the link URL with the `href` attribute for links.
|
|
1268
|
+
*
|
|
1269
|
+
* For any model element, zero, one or more than one asset can be found (e.g. a linked image may be associated with the link asset and the
|
|
1270
|
+
* image asset).
|
|
1271
|
+
*/ function findAssetsForItem(item, assets) {
|
|
1272
|
+
const isImageElement = item.is('element', 'imageInline') || item.is('element', 'imageBlock');
|
|
1273
|
+
const isLinkElement = item.hasAttribute('linkHref');
|
|
1274
|
+
return [
|
|
1275
|
+
...assets
|
|
1276
|
+
].filter((asset)=>{
|
|
1277
|
+
if (asset.type === 'image' && isImageElement) {
|
|
1278
|
+
return asset.attributes.imageFallbackUrl === item.getAttribute('src');
|
|
1279
|
+
}
|
|
1280
|
+
if (asset.type === 'link' && isLinkElement) {
|
|
1281
|
+
return asset.attributes.linkHref === item.getAttribute('linkHref');
|
|
1282
|
+
}
|
|
1283
|
+
});
|
|
1284
|
+
}
|
|
1285
|
+
/**
|
|
1286
|
+
* Creates view link element with the requested ID.
|
|
1287
|
+
*/ function createLinkElement(writer, id) {
|
|
1288
|
+
// Priority equal 5 is needed to merge adjacent `<a>` elements together.
|
|
1289
|
+
const viewElement = writer.createAttributeElement('a', {
|
|
1290
|
+
'data-ckbox-resource-id': id
|
|
1291
|
+
}, {
|
|
1292
|
+
priority: 5
|
|
1293
|
+
});
|
|
1294
|
+
writer.setCustomProperty('link', true, viewElement);
|
|
1295
|
+
return viewElement;
|
|
1296
|
+
}
|
|
1297
|
+
/**
|
|
1298
|
+
* Checks if the model element may have the `ckboxLinkId` attribute.
|
|
1299
|
+
*/ function shouldUpcastAttributeForNode(node) {
|
|
1300
|
+
if (node.is('$text')) {
|
|
1301
|
+
return true;
|
|
1302
|
+
}
|
|
1303
|
+
if (node.is('element', 'imageInline') || node.is('element', 'imageBlock')) {
|
|
1304
|
+
return true;
|
|
1305
|
+
}
|
|
1306
|
+
return false;
|
|
1307
|
+
}
|
|
1308
|
+
/**
|
|
1309
|
+
* Returns true if the CKBox library is loaded, false otherwise.
|
|
1310
|
+
*/ function isLibraryLoaded() {
|
|
1311
|
+
return !!window.CKBox;
|
|
1312
|
+
}
|
|
1313
|
+
/**
|
|
1314
|
+
* Checks is access allowed to upload assets.
|
|
1315
|
+
*/ async function isUploadPermissionGranted(editor) {
|
|
1316
|
+
const ckboxUtils = editor.plugins.get(CKBoxUtils);
|
|
1317
|
+
const origin = editor.config.get('ckbox.serviceOrigin');
|
|
1318
|
+
const url = new URL('permissions', origin);
|
|
1319
|
+
const { value } = await ckboxUtils.getToken();
|
|
1320
|
+
const response = await sendHttpRequest({
|
|
1321
|
+
url,
|
|
1322
|
+
authorization: value,
|
|
1323
|
+
signal: new AbortController().signal // Aborting is unnecessary.
|
|
1324
|
+
});
|
|
1325
|
+
return Object.values(response).some((category)=>category['asset:create']);
|
|
1326
|
+
}
|
|
1327
|
+
|
|
1328
|
+
/**
|
|
1329
|
+
* The CKBox feature, a bridge between the CKEditor 5 WYSIWYG editor and the CKBox file manager and uploader.
|
|
1330
|
+
*
|
|
1331
|
+
* This is a "glue" plugin which enables:
|
|
1332
|
+
*
|
|
1333
|
+
* * {@link module:ckbox/ckboxediting~CKBoxEditing},
|
|
1334
|
+
* * {@link module:ckbox/ckboxui~CKBoxUI},
|
|
1335
|
+
*
|
|
1336
|
+
* See the {@glink features/file-management/ckbox CKBox integration} guide to learn how to configure and use this feature.
|
|
1337
|
+
*
|
|
1338
|
+
* Check out the {@glink features/images/image-upload/image-upload Image upload} guide to learn about other ways to upload
|
|
1339
|
+
* images into CKEditor 5.
|
|
1340
|
+
*/ class CKBox extends Plugin {
|
|
1341
|
+
/**
|
|
1342
|
+
* @inheritDoc
|
|
1343
|
+
*/ static get pluginName() {
|
|
1344
|
+
return 'CKBox';
|
|
1345
|
+
}
|
|
1346
|
+
/**
|
|
1347
|
+
* @inheritDoc
|
|
1348
|
+
*/ static get isOfficialPlugin() {
|
|
1349
|
+
return true;
|
|
1350
|
+
}
|
|
1351
|
+
/**
|
|
1352
|
+
* @inheritDoc
|
|
1353
|
+
*/ static get requires() {
|
|
1354
|
+
return [
|
|
1355
|
+
CKBoxEditing,
|
|
1356
|
+
CKBoxUI
|
|
1357
|
+
];
|
|
1358
|
+
}
|
|
1359
|
+
}
|
|
1360
|
+
|
|
1361
|
+
/**
|
|
1362
|
+
* @internal
|
|
1363
|
+
*/ function createEditabilityChecker(allowExternalImagesEditing) {
|
|
1364
|
+
const checkUrl = createUrlChecker(allowExternalImagesEditing);
|
|
1365
|
+
return (element)=>{
|
|
1366
|
+
const isImageElement = element.is('element', 'imageInline') || element.is('element', 'imageBlock');
|
|
1367
|
+
if (!isImageElement) {
|
|
1368
|
+
return false;
|
|
1369
|
+
}
|
|
1370
|
+
if (element.hasAttribute('ckboxImageId')) {
|
|
1371
|
+
return true;
|
|
1372
|
+
}
|
|
1373
|
+
if (element.hasAttribute('src')) {
|
|
1374
|
+
return checkUrl(element.getAttribute('src'));
|
|
1375
|
+
}
|
|
1376
|
+
return false;
|
|
1377
|
+
};
|
|
1378
|
+
}
|
|
1379
|
+
function createUrlChecker(allowExternalImagesEditing) {
|
|
1380
|
+
if (Array.isArray(allowExternalImagesEditing)) {
|
|
1381
|
+
const urlMatchers = allowExternalImagesEditing.map(createUrlChecker);
|
|
1382
|
+
return (src)=>urlMatchers.some((matcher)=>matcher(src));
|
|
1383
|
+
}
|
|
1384
|
+
if (allowExternalImagesEditing == 'origin') {
|
|
1385
|
+
const origin = global.window.location.origin;
|
|
1386
|
+
return (src)=>new URL(src, global.document.baseURI).origin == origin;
|
|
1387
|
+
}
|
|
1388
|
+
if (typeof allowExternalImagesEditing == 'function') {
|
|
1389
|
+
return allowExternalImagesEditing;
|
|
1390
|
+
}
|
|
1391
|
+
if (allowExternalImagesEditing instanceof RegExp) {
|
|
1392
|
+
return (src)=>!!(src.match(allowExternalImagesEditing) || src.replace(/^https?:\/\//, '').match(allowExternalImagesEditing));
|
|
1393
|
+
}
|
|
1394
|
+
return ()=>false;
|
|
1395
|
+
}
|
|
1396
|
+
|
|
1397
|
+
/**
|
|
1398
|
+
* The CKBox edit image command.
|
|
1399
|
+
*
|
|
1400
|
+
* Opens the CKBox dialog for editing the image.
|
|
1401
|
+
*/ class CKBoxImageEditCommand extends Command {
|
|
1402
|
+
/**
|
|
1403
|
+
* The DOM element that acts as a mounting point for the CKBox Edit Image dialog.
|
|
1404
|
+
*/ _wrapper = null;
|
|
1405
|
+
/**
|
|
1406
|
+
* The states of image processing in progress.
|
|
1407
|
+
*/ _processInProgress = new Set();
|
|
1408
|
+
/**
|
|
1409
|
+
* Determines if the element can be edited.
|
|
1410
|
+
*/ _canEdit;
|
|
1411
|
+
/**
|
|
1412
|
+
* A wrapper function to prepare mount options. Ensures that at most one preparation is in-flight.
|
|
1413
|
+
*/ _prepareOptions;
|
|
1414
|
+
/**
|
|
1415
|
+
* CKBox's onClose function runs before the final cleanup, potentially causing
|
|
1416
|
+
* page layout changes after it finishes. To address this, we use a setTimeout hack
|
|
1417
|
+
* to ensure that floating elements on the page maintain their correct position.
|
|
1418
|
+
*
|
|
1419
|
+
* See: https://github.com/ckeditor/ckeditor5/issues/16153.
|
|
1420
|
+
*/ _updateUiDelayed = delay(()=>this.editor.ui.update(), 0);
|
|
1421
|
+
/**
|
|
1422
|
+
* @inheritDoc
|
|
1423
|
+
*/ constructor(editor){
|
|
1424
|
+
super(editor);
|
|
1425
|
+
this.value = false;
|
|
1426
|
+
this._canEdit = createEditabilityChecker(editor.config.get('ckbox.allowExternalImagesEditing'));
|
|
1427
|
+
this._prepareOptions = abortableDebounce((signal, state)=>this._prepareOptionsAbortable(signal, state));
|
|
1428
|
+
this._prepareListeners();
|
|
1429
|
+
}
|
|
1430
|
+
/**
|
|
1431
|
+
* @inheritDoc
|
|
1432
|
+
*/ refresh() {
|
|
1433
|
+
const editor = this.editor;
|
|
1434
|
+
this.value = this._getValue();
|
|
1435
|
+
const selectedElement = editor.model.document.selection.getSelectedElement();
|
|
1436
|
+
this.isEnabled = !!selectedElement && this._canEdit(selectedElement) && !this._checkIfElementIsBeingProcessed(selectedElement);
|
|
1437
|
+
}
|
|
1438
|
+
/**
|
|
1439
|
+
* Opens the CKBox Image Editor dialog for editing the image.
|
|
1440
|
+
*/ execute() {
|
|
1441
|
+
if (this._getValue()) {
|
|
1442
|
+
return;
|
|
1443
|
+
}
|
|
1444
|
+
const wrapper = createElement(document, 'div', {
|
|
1445
|
+
class: 'ck ckbox-wrapper'
|
|
1446
|
+
});
|
|
1447
|
+
this._wrapper = wrapper;
|
|
1448
|
+
this.value = true;
|
|
1449
|
+
document.body.appendChild(this._wrapper);
|
|
1450
|
+
const imageElement = this.editor.model.document.selection.getSelectedElement();
|
|
1451
|
+
const processingState = {
|
|
1452
|
+
element: imageElement,
|
|
1453
|
+
controller: new AbortController()
|
|
1454
|
+
};
|
|
1455
|
+
this._prepareOptions(processingState).then((options)=>window.CKBox.mountImageEditor(wrapper, options), (error)=>{
|
|
1456
|
+
const editor = this.editor;
|
|
1457
|
+
const t = editor.t;
|
|
1458
|
+
const notification = editor.plugins.get(Notification);
|
|
1459
|
+
notification.showWarning(t('Failed to determine category of edited image.'), {
|
|
1460
|
+
namespace: 'ckbox'
|
|
1461
|
+
});
|
|
1462
|
+
console.error(error);
|
|
1463
|
+
this._handleImageEditorClose();
|
|
1464
|
+
});
|
|
1465
|
+
}
|
|
1466
|
+
/**
|
|
1467
|
+
* @inheritDoc
|
|
1468
|
+
*/ destroy() {
|
|
1469
|
+
this._handleImageEditorClose();
|
|
1470
|
+
this._prepareOptions.abort();
|
|
1471
|
+
this._updateUiDelayed.cancel();
|
|
1472
|
+
for (const state of this._processInProgress.values()){
|
|
1473
|
+
state.controller.abort();
|
|
1474
|
+
}
|
|
1475
|
+
super.destroy();
|
|
1476
|
+
}
|
|
1477
|
+
/**
|
|
1478
|
+
* Indicates if the CKBox Image Editor dialog is already opened.
|
|
1479
|
+
*/ _getValue() {
|
|
1480
|
+
return this._wrapper !== null;
|
|
1481
|
+
}
|
|
1482
|
+
/**
|
|
1483
|
+
* Creates the options object for the CKBox Image Editor dialog.
|
|
1484
|
+
*/ async _prepareOptionsAbortable(signal, state) {
|
|
1485
|
+
const editor = this.editor;
|
|
1486
|
+
const ckboxConfig = editor.config.get('ckbox');
|
|
1487
|
+
const ckboxUtils = editor.plugins.get(CKBoxUtils);
|
|
1488
|
+
const { element } = state;
|
|
1489
|
+
let imageMountOptions;
|
|
1490
|
+
const ckboxImageId = element.getAttribute('ckboxImageId');
|
|
1491
|
+
if (ckboxImageId) {
|
|
1492
|
+
imageMountOptions = {
|
|
1493
|
+
assetId: ckboxImageId
|
|
1494
|
+
};
|
|
1495
|
+
} else {
|
|
1496
|
+
const imageUrl = new URL(element.getAttribute('src'), document.baseURI).href;
|
|
1497
|
+
const uploadCategoryId = await ckboxUtils.getCategoryIdForFile(imageUrl, {
|
|
1498
|
+
signal
|
|
1499
|
+
});
|
|
1500
|
+
imageMountOptions = {
|
|
1501
|
+
imageUrl,
|
|
1502
|
+
uploadCategoryId
|
|
1503
|
+
};
|
|
1504
|
+
}
|
|
1505
|
+
return {
|
|
1506
|
+
...imageMountOptions,
|
|
1507
|
+
imageEditing: {
|
|
1508
|
+
allowOverwrite: false
|
|
1509
|
+
},
|
|
1510
|
+
tokenUrl: ckboxConfig.tokenUrl,
|
|
1511
|
+
...ckboxConfig.serviceOrigin && {
|
|
1512
|
+
serviceOrigin: ckboxConfig.serviceOrigin
|
|
1513
|
+
},
|
|
1514
|
+
onClose: ()=>this._handleImageEditorClose(),
|
|
1515
|
+
onSave: (asset)=>this._handleImageEditorSave(state, asset)
|
|
1516
|
+
};
|
|
1517
|
+
}
|
|
1518
|
+
/**
|
|
1519
|
+
* Initializes event lister for an event of removing an image.
|
|
1520
|
+
*/ _prepareListeners() {
|
|
1521
|
+
// Abort editing processing when the image has been removed.
|
|
1522
|
+
this.listenTo(this.editor.model.document, 'change:data', ()=>{
|
|
1523
|
+
const processingStates = this._getProcessingStatesOfDeletedImages();
|
|
1524
|
+
processingStates.forEach((processingState)=>{
|
|
1525
|
+
processingState.controller.abort();
|
|
1526
|
+
});
|
|
1527
|
+
});
|
|
1528
|
+
}
|
|
1529
|
+
/**
|
|
1530
|
+
* Gets processing states of images that have been deleted in the mean time.
|
|
1531
|
+
*/ _getProcessingStatesOfDeletedImages() {
|
|
1532
|
+
const states = [];
|
|
1533
|
+
for (const state of this._processInProgress.values()){
|
|
1534
|
+
if (state.element.root.rootName == '$graveyard') {
|
|
1535
|
+
states.push(state);
|
|
1536
|
+
}
|
|
1537
|
+
}
|
|
1538
|
+
return states;
|
|
1539
|
+
}
|
|
1540
|
+
_checkIfElementIsBeingProcessed(selectedElement) {
|
|
1541
|
+
for (const { element } of this._processInProgress){
|
|
1542
|
+
if (isEqual(element, selectedElement)) {
|
|
1543
|
+
return true;
|
|
1544
|
+
}
|
|
1545
|
+
}
|
|
1546
|
+
return false;
|
|
1547
|
+
}
|
|
1548
|
+
/**
|
|
1549
|
+
* Closes the CKBox Image Editor dialog.
|
|
1550
|
+
*/ _handleImageEditorClose() {
|
|
1551
|
+
if (!this._wrapper) {
|
|
1552
|
+
return;
|
|
1553
|
+
}
|
|
1554
|
+
this._wrapper.remove();
|
|
1555
|
+
this._wrapper = null;
|
|
1556
|
+
this.editor.editing.view.focus();
|
|
1557
|
+
this._updateUiDelayed();
|
|
1558
|
+
this.refresh();
|
|
1559
|
+
}
|
|
1560
|
+
/**
|
|
1561
|
+
* Save edited image. In case server respond with "success" replace with edited image,
|
|
1562
|
+
* otherwise show notification error.
|
|
1563
|
+
*/ _handleImageEditorSave(state, asset) {
|
|
1564
|
+
const t = this.editor.locale.t;
|
|
1565
|
+
const notification = this.editor.plugins.get(Notification);
|
|
1566
|
+
const pendingActions = this.editor.plugins.get(PendingActions);
|
|
1567
|
+
const action = pendingActions.add(t('Processing the edited image.'));
|
|
1568
|
+
this._processInProgress.add(state);
|
|
1569
|
+
this._showImageProcessingIndicator(state.element, asset);
|
|
1570
|
+
this.refresh();
|
|
1571
|
+
this._waitForAssetProcessed(asset.data.id, state.controller.signal).then((asset)=>{
|
|
1572
|
+
this._replaceImage(state.element, asset);
|
|
1573
|
+
}, (error)=>{
|
|
1574
|
+
// Remove processing indicator. It was added only to ViewElement.
|
|
1575
|
+
this.editor.editing.reconvertItem(state.element);
|
|
1576
|
+
if (state.controller.signal.aborted) {
|
|
1577
|
+
return;
|
|
1578
|
+
}
|
|
1579
|
+
if (!error || error instanceof CKEditorError) {
|
|
1580
|
+
notification.showWarning(t('Server failed to process the image.'), {
|
|
1581
|
+
namespace: 'ckbox'
|
|
1582
|
+
});
|
|
1583
|
+
} else {
|
|
1584
|
+
console.error(error);
|
|
1585
|
+
}
|
|
1586
|
+
}).finally(()=>{
|
|
1587
|
+
this._processInProgress.delete(state);
|
|
1588
|
+
pendingActions.remove(action);
|
|
1589
|
+
this.refresh();
|
|
1590
|
+
});
|
|
1591
|
+
}
|
|
1592
|
+
/**
|
|
1593
|
+
* Get asset's status on server. If server responds with "success" status then
|
|
1594
|
+
* image is already proceeded and ready for saving.
|
|
1595
|
+
*/ async _getAssetStatusFromServer(id, signal) {
|
|
1596
|
+
const ckboxUtils = this.editor.plugins.get(CKBoxUtils);
|
|
1597
|
+
const url = new URL('assets/' + id, this.editor.config.get('ckbox.serviceOrigin'));
|
|
1598
|
+
const response = await sendHttpRequest({
|
|
1599
|
+
url,
|
|
1600
|
+
signal,
|
|
1601
|
+
authorization: (await ckboxUtils.getToken()).value
|
|
1602
|
+
});
|
|
1603
|
+
const status = response.metadata.metadataProcessingStatus;
|
|
1604
|
+
if (!status || status == 'queued') {
|
|
1605
|
+
/**
|
|
1606
|
+
* Image has not been processed yet.
|
|
1607
|
+
*
|
|
1608
|
+
* @error ckbox-image-not-processed
|
|
1609
|
+
*/ throw new CKEditorError('ckbox-image-not-processed');
|
|
1610
|
+
}
|
|
1611
|
+
return {
|
|
1612
|
+
data: {
|
|
1613
|
+
...response
|
|
1614
|
+
}
|
|
1615
|
+
};
|
|
1616
|
+
}
|
|
1617
|
+
/**
|
|
1618
|
+
* Waits for an asset to be processed.
|
|
1619
|
+
* It retries retrieving asset status from the server in case of failure.
|
|
1620
|
+
*/ async _waitForAssetProcessed(id, signal) {
|
|
1621
|
+
const result = await retry(()=>this._getAssetStatusFromServer(id, signal), {
|
|
1622
|
+
signal,
|
|
1623
|
+
maxAttempts: 5
|
|
1624
|
+
});
|
|
1625
|
+
if (result.data.metadata.metadataProcessingStatus != 'success') {
|
|
1626
|
+
/**
|
|
1627
|
+
* The image processing failed.
|
|
1628
|
+
*
|
|
1629
|
+
* @error ckbox-image-processing-failed
|
|
1630
|
+
*/ throw new CKEditorError('ckbox-image-processing-failed');
|
|
1631
|
+
}
|
|
1632
|
+
return result;
|
|
1633
|
+
}
|
|
1634
|
+
/**
|
|
1635
|
+
* Shows processing indicator while image is processing.
|
|
1636
|
+
*
|
|
1637
|
+
* @param asset Data about certain asset.
|
|
1638
|
+
*/ _showImageProcessingIndicator(element, asset) {
|
|
1639
|
+
const editor = this.editor;
|
|
1640
|
+
editor.editing.view.change((writer)=>{
|
|
1641
|
+
const imageElementView = editor.editing.mapper.toViewElement(element);
|
|
1642
|
+
const imageUtils = this.editor.plugins.get('ImageUtils');
|
|
1643
|
+
const img = imageUtils.findViewImgElement(imageElementView);
|
|
1644
|
+
writer.removeStyle('aspect-ratio', img);
|
|
1645
|
+
writer.setAttribute('width', asset.data.metadata.width, img);
|
|
1646
|
+
writer.setAttribute('height', asset.data.metadata.height, img);
|
|
1647
|
+
writer.setStyle('width', `${asset.data.metadata.width}px`, img);
|
|
1648
|
+
writer.setStyle('height', `${asset.data.metadata.height}px`, img);
|
|
1649
|
+
writer.addClass('image-processing', imageElementView);
|
|
1650
|
+
});
|
|
1651
|
+
}
|
|
1652
|
+
/**
|
|
1653
|
+
* Replace the edited image with the new one.
|
|
1654
|
+
*/ _replaceImage(element, asset) {
|
|
1655
|
+
const editor = this.editor;
|
|
1656
|
+
const { imageFallbackUrl, imageSources, imageWidth, imageHeight, imagePlaceholder } = prepareImageAssetAttributes(asset);
|
|
1657
|
+
const previousSelectionRanges = Array.from(editor.model.document.selection.getRanges());
|
|
1658
|
+
editor.model.change((writer)=>{
|
|
1659
|
+
writer.setSelection(element, 'on');
|
|
1660
|
+
editor.execute('insertImage', {
|
|
1661
|
+
imageType: element.is('element', 'imageInline') ? 'imageInline' : null,
|
|
1662
|
+
source: {
|
|
1663
|
+
src: imageFallbackUrl,
|
|
1664
|
+
sources: imageSources,
|
|
1665
|
+
width: imageWidth,
|
|
1666
|
+
height: imageHeight,
|
|
1667
|
+
...imagePlaceholder ? {
|
|
1668
|
+
placeholder: imagePlaceholder
|
|
1669
|
+
} : null,
|
|
1670
|
+
...element.hasAttribute('alt') ? {
|
|
1671
|
+
alt: element.getAttribute('alt')
|
|
1672
|
+
} : null
|
|
1673
|
+
}
|
|
1674
|
+
});
|
|
1675
|
+
const previousChildren = element.getChildren();
|
|
1676
|
+
element = editor.model.document.selection.getSelectedElement();
|
|
1677
|
+
for (const child of previousChildren){
|
|
1678
|
+
writer.append(writer.cloneElement(child), element);
|
|
1679
|
+
}
|
|
1680
|
+
writer.setAttribute('ckboxImageId', asset.data.id, element);
|
|
1681
|
+
writer.setSelection(previousSelectionRanges);
|
|
1682
|
+
});
|
|
1683
|
+
}
|
|
1684
|
+
}
|
|
1685
|
+
|
|
1686
|
+
/**
|
|
1687
|
+
* The CKBox image edit editing plugin.
|
|
1688
|
+
*/ class CKBoxImageEditEditing extends Plugin {
|
|
1689
|
+
/**
|
|
1690
|
+
* @inheritDoc
|
|
1691
|
+
*/ static get pluginName() {
|
|
1692
|
+
return 'CKBoxImageEditEditing';
|
|
1693
|
+
}
|
|
1694
|
+
/**
|
|
1695
|
+
* @inheritDoc
|
|
1696
|
+
*/ static get isOfficialPlugin() {
|
|
1697
|
+
return true;
|
|
1698
|
+
}
|
|
1699
|
+
/**
|
|
1700
|
+
* @inheritDoc
|
|
1701
|
+
*/ static get requires() {
|
|
1702
|
+
return [
|
|
1703
|
+
CKBoxEditing,
|
|
1704
|
+
CKBoxUtils,
|
|
1705
|
+
PendingActions,
|
|
1706
|
+
Notification,
|
|
1707
|
+
'ImageUtils',
|
|
1708
|
+
'ImageEditing'
|
|
1709
|
+
];
|
|
1710
|
+
}
|
|
1711
|
+
/**
|
|
1712
|
+
* @inheritDoc
|
|
1713
|
+
*/ init() {
|
|
1714
|
+
const { editor } = this;
|
|
1715
|
+
editor.commands.add('ckboxImageEdit', new CKBoxImageEditCommand(editor));
|
|
1716
|
+
}
|
|
1717
|
+
}
|
|
1718
|
+
|
|
1719
|
+
var ckboxImageEditIcon = "<svg viewBox=\"0 0 20 20\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M1.201 1C.538 1 0 1.47 0 2.1v14.363c0 .64.534 1.037 1.186 1.037H5.06l5.058-5.078L6.617 9.15a.696.696 0 0 0-.957-.033L1.5 13.6V2.5h15v4.354a3.478 3.478 0 0 1 1.5.049V2.1c0-.63-.547-1.1-1.2-1.1H1.202Zm11.713 2.803a2.147 2.147 0 0 0-2.049 1.992 2.14 2.14 0 0 0 1.28 2.096 2.13 2.13 0 0 0 2.642-3.11 2.129 2.129 0 0 0-1.873-.978ZM8.089 17.635v2.388h2.389l7.046-7.046-2.39-2.39-7.045 7.048Zm11.282-6.507a.637.637 0 0 0 .139-.692.603.603 0 0 0-.139-.205l-1.49-1.488a.63.63 0 0 0-.899 0l-1.166 1.163 2.39 2.39 1.165-1.168Z\"/></svg>";
|
|
1720
|
+
|
|
1721
|
+
/**
|
|
1722
|
+
* The UI plugin of the CKBox image edit feature.
|
|
1723
|
+
*
|
|
1724
|
+
* It registers the `'ckboxImageEdit'` UI button in the editor's {@link module:ui/componentfactory~ComponentFactory component factory}
|
|
1725
|
+
* that allows you to open the CKBox dialog and edit the image.
|
|
1726
|
+
*/ class CKBoxImageEditUI extends Plugin {
|
|
1727
|
+
/**
|
|
1728
|
+
* @inheritDoc
|
|
1729
|
+
*/ static get pluginName() {
|
|
1730
|
+
return 'CKBoxImageEditUI';
|
|
1731
|
+
}
|
|
1732
|
+
/**
|
|
1733
|
+
* @inheritDoc
|
|
1734
|
+
*/ static get isOfficialPlugin() {
|
|
1735
|
+
return true;
|
|
1736
|
+
}
|
|
1737
|
+
/**
|
|
1738
|
+
* @inheritDoc
|
|
1739
|
+
*/ init() {
|
|
1740
|
+
const editor = this.editor;
|
|
1741
|
+
editor.ui.componentFactory.add('ckboxImageEdit', (locale)=>{
|
|
1742
|
+
const command = editor.commands.get('ckboxImageEdit');
|
|
1743
|
+
const uploadImageCommand = editor.commands.get('uploadImage');
|
|
1744
|
+
const view = new ButtonView(locale);
|
|
1745
|
+
const t = locale.t;
|
|
1746
|
+
view.set({
|
|
1747
|
+
icon: ckboxImageEditIcon,
|
|
1748
|
+
tooltip: true
|
|
1749
|
+
});
|
|
1750
|
+
view.bind('label').to(uploadImageCommand, 'isAccessAllowed', (isAccessAllowed)=>isAccessAllowed ? t('Edit image') : t('You have no image editing permissions.'));
|
|
1751
|
+
view.bind('isOn').to(command, 'value', command, 'isEnabled', (value, isEnabled)=>value && isEnabled);
|
|
1752
|
+
view.bind('isEnabled').to(command);
|
|
1753
|
+
// Execute the command.
|
|
1754
|
+
this.listenTo(view, 'execute', ()=>{
|
|
1755
|
+
editor.execute('ckboxImageEdit');
|
|
1756
|
+
editor.editing.view.focus();
|
|
1757
|
+
});
|
|
1758
|
+
return view;
|
|
1759
|
+
});
|
|
1760
|
+
}
|
|
1761
|
+
}
|
|
1762
|
+
|
|
1763
|
+
/**
|
|
1764
|
+
* The CKBox image edit feature.
|
|
1765
|
+
*/ class CKBoxImageEdit extends Plugin {
|
|
1766
|
+
/**
|
|
1767
|
+
* @inheritDoc
|
|
1768
|
+
*/ static get pluginName() {
|
|
1769
|
+
return 'CKBoxImageEdit';
|
|
1770
|
+
}
|
|
1771
|
+
/**
|
|
1772
|
+
* @inheritDoc
|
|
1773
|
+
*/ static get isOfficialPlugin() {
|
|
1774
|
+
return true;
|
|
1775
|
+
}
|
|
1776
|
+
/**
|
|
1777
|
+
* @inheritDoc
|
|
1778
|
+
*/ static get requires() {
|
|
1779
|
+
return [
|
|
1780
|
+
CKBoxImageEditEditing,
|
|
1781
|
+
CKBoxImageEditUI
|
|
1782
|
+
];
|
|
1783
|
+
}
|
|
1784
|
+
}
|
|
1785
|
+
|
|
1786
|
+
export { CKBox, CKBoxEditing, CKBoxImageEdit, CKBoxImageEditEditing, CKBoxImageEditUI, CKBoxUI };
|
|
1787
|
+
//# sourceMappingURL=index.js.map
|