frayerjj-frontend 0.1.41 → 0.1.42
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/package.json +1 -1
- package/src/ckeupload.js +45 -36
package/package.json
CHANGED
package/src/ckeupload.js
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import { message } from "./message";
|
|
2
2
|
|
|
3
3
|
export const ckeupload = {
|
|
4
|
-
|
|
5
4
|
init: () => {
|
|
5
|
+
// Ensure CKEditor is available
|
|
6
|
+
if (typeof window === 'undefined' || !window.ClassicEditor) {
|
|
7
|
+
message.warn('CKEditor not loaded.');
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
6
10
|
|
|
7
11
|
class CkeUploadAdapter {
|
|
8
12
|
constructor(loader, uri, token) {
|
|
@@ -11,6 +15,7 @@ export const ckeupload = {
|
|
|
11
15
|
this.token = token;
|
|
12
16
|
message.verbose('CKEditor Upload Adapter Initialized');
|
|
13
17
|
}
|
|
18
|
+
|
|
14
19
|
upload() {
|
|
15
20
|
return this.loader.file
|
|
16
21
|
.then(file => new Promise((resolve, reject) => {
|
|
@@ -19,26 +24,33 @@ export const ckeupload = {
|
|
|
19
24
|
this._sendRequest(file);
|
|
20
25
|
}));
|
|
21
26
|
}
|
|
27
|
+
|
|
22
28
|
abort() {
|
|
23
29
|
if (this.xhr) this.xhr.abort();
|
|
24
30
|
}
|
|
31
|
+
|
|
25
32
|
_initRequest() {
|
|
26
33
|
const xhr = this.xhr = new XMLHttpRequest();
|
|
27
34
|
xhr.open('POST', this.uri, true);
|
|
28
35
|
xhr.setRequestHeader('X-CSRFToken', this.token);
|
|
29
36
|
xhr.responseType = 'json';
|
|
30
37
|
}
|
|
38
|
+
|
|
31
39
|
_initListeners(resolve, reject, file) {
|
|
32
40
|
const xhr = this.xhr;
|
|
33
41
|
const loader = this.loader;
|
|
34
|
-
const genericErrorText = `Couldn't upload file: ${
|
|
35
|
-
|
|
36
|
-
xhr.addEventListener(
|
|
37
|
-
xhr.addEventListener(
|
|
42
|
+
const genericErrorText = `Couldn't upload file: ${file.name}.`;
|
|
43
|
+
|
|
44
|
+
xhr.addEventListener('error', () => reject(genericErrorText));
|
|
45
|
+
xhr.addEventListener('abort', () => reject());
|
|
46
|
+
xhr.addEventListener('load', () => {
|
|
38
47
|
const response = xhr.response;
|
|
39
|
-
if (!response || response.error)
|
|
48
|
+
if (!response || response.error) {
|
|
49
|
+
return reject(response && response.error ? response.error.message : genericErrorText);
|
|
50
|
+
}
|
|
40
51
|
resolve({ default: response.url });
|
|
41
52
|
});
|
|
53
|
+
|
|
42
54
|
if (xhr.upload) {
|
|
43
55
|
xhr.upload.addEventListener('progress', evt => {
|
|
44
56
|
if (evt.lengthComputable) {
|
|
@@ -48,47 +60,44 @@ export const ckeupload = {
|
|
|
48
60
|
});
|
|
49
61
|
}
|
|
50
62
|
}
|
|
63
|
+
|
|
51
64
|
_sendRequest(file) {
|
|
52
65
|
const data = new FormData();
|
|
53
66
|
data.append('file', file);
|
|
54
67
|
this.xhr.send(data);
|
|
55
68
|
}
|
|
56
|
-
}
|
|
69
|
+
}
|
|
57
70
|
|
|
58
|
-
|
|
71
|
+
// Register the upload adapter plugin
|
|
72
|
+
window.CkeUploadAdapterPlugin = function (editor) {
|
|
59
73
|
editor.plugins.get('FileRepository').createUploadAdapter = (loader) => {
|
|
60
|
-
|
|
74
|
+
const uri = document.querySelector('meta[name="asset-upload"]').getAttribute('content');
|
|
75
|
+
const token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
|
|
76
|
+
return new CkeUploadAdapter(loader, uri, token);
|
|
61
77
|
};
|
|
62
78
|
};
|
|
63
79
|
|
|
64
|
-
|
|
65
|
-
|
|
80
|
+
// Initialize editors
|
|
66
81
|
document.querySelectorAll('.wysiwyg').forEach(el => {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
editors[el.id] = editor;
|
|
87
|
-
}).catch( error => {
|
|
88
|
-
console.error( 'There was a problem initializing the editor.', error );
|
|
89
|
-
});
|
|
90
|
-
} else message.warn('CKEditor not loaded.');
|
|
82
|
+
ClassicEditor.create(el, {
|
|
83
|
+
licenseKey: 'GPL',
|
|
84
|
+
htmlSupport: {
|
|
85
|
+
allow: [
|
|
86
|
+
{
|
|
87
|
+
name: /.*/,
|
|
88
|
+
attributes: true,
|
|
89
|
+
classes: true,
|
|
90
|
+
styles: true
|
|
91
|
+
}
|
|
92
|
+
]
|
|
93
|
+
},
|
|
94
|
+
extraPlugins: [window.CkeUploadAdapterPlugin]
|
|
95
|
+
}).then(editor => {
|
|
96
|
+
if (!window.editors) window.editors = [];
|
|
97
|
+
window.editors[el.id] = editor;
|
|
98
|
+
}).catch(error => {
|
|
99
|
+
console.error('There was a problem initializing the editor.', error);
|
|
100
|
+
});
|
|
91
101
|
});
|
|
92
|
-
|
|
93
102
|
}
|
|
94
103
|
};
|