-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
ckboxuploadadapter.ts
196 lines (164 loc) · 5.34 KB
/
ckboxuploadadapter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/* globals AbortController, FormData, URL, window */
/**
* @module ckbox/ckboxuploadadapter
*/
import { Plugin, type Editor } from 'ckeditor5/src/core.js';
import {
FileRepository,
type FileLoader,
type UploadAdapter,
type UploadResponse
} from 'ckeditor5/src/upload.js';
import type { InitializedToken } from '@ckeditor/ckeditor5-cloud-services';
import type { ImageUploadCompleteEvent, ImageUploadEditing } from '@ckeditor/ckeditor5-image';
import CKBoxEditing from './ckboxediting.js';
import {
getImageUrls,
sendHttpRequest
} from './utils.js';
import CKBoxUtils from './ckboxutils.js';
/**
* A plugin that enables file uploads in CKEditor 5 using the CKBox server–side connector.
* See the {@glink features/file-management/ckbox CKBox file manager integration} guide to learn how to configure
* and use this feature as well as find out more about the full integration with the file manager
* provided by the {@link module:ckbox/ckbox~CKBox} plugin.
*
* Check out the {@glink features/images/image-upload/image-upload Image upload overview} guide to learn about
* other ways to upload images into CKEditor 5.
*/
export default class CKBoxUploadAdapter extends Plugin {
/**
* @inheritDoc
*/
public static get requires() {
return [ 'ImageUploadEditing', 'ImageUploadProgress', FileRepository, CKBoxEditing ] as const;
}
/**
* @inheritDoc
*/
public static get pluginName() {
return 'CKBoxUploadAdapter' as const;
}
/**
* @inheritDoc
*/
public async afterInit(): Promise<void> {
const editor = this.editor;
const hasConfiguration = !!editor.config.get( 'ckbox' );
const isLibraryLoaded = !!window.CKBox;
// Editor supports only one upload adapter. Register the CKBox upload adapter (and potentially overwrite other one) only when the
// integrator intentionally wants to use the CKBox plugin, i.e. when the `config.ckbox` exists or the CKBox JavaScript library is
// loaded.
if ( !hasConfiguration && !isLibraryLoaded ) {
return;
}
const fileRepository = editor.plugins.get( FileRepository );
const ckboxUtils = editor.plugins.get( CKBoxUtils );
fileRepository.createUploadAdapter = loader => new Adapter( loader, editor, ckboxUtils );
const shouldInsertDataId = !editor.config.get( 'ckbox.ignoreDataId' );
const imageUploadEditing: ImageUploadEditing = editor.plugins.get( 'ImageUploadEditing' );
// Mark uploaded assets with the `ckboxImageId` attribute. Its value represents an ID in CKBox.
if ( shouldInsertDataId ) {
imageUploadEditing.on<ImageUploadCompleteEvent>( 'uploadComplete', ( evt, { imageElement, data } ) => {
editor.model.change( writer => {
writer.setAttribute( 'ckboxImageId', data.ckboxImageId, imageElement );
} );
} );
}
}
}
/**
* Upload adapter for CKBox.
*/
class Adapter implements UploadAdapter {
/**
* FileLoader instance to use during the upload.
*/
public loader: FileLoader;
/**
* CKEditor Cloud Services access token.
*/
public token: InitializedToken;
/**
* The editor instance.
*/
public editor: Editor;
/**
* The abort controller for aborting asynchronous processes.
*/
public controller: AbortController;
/**
* The base URL where all requests should be sent.
*/
public serviceOrigin: string;
/**
* The reference to CKBoxUtils plugin.
*/
public ckboxUtils: CKBoxUtils;
/**
* Creates a new adapter instance.
*/
constructor( loader: FileLoader, editor: Editor, ckboxUtils: CKBoxUtils ) {
this.loader = loader;
this.token = ckboxUtils.getToken();
this.ckboxUtils = ckboxUtils;
this.editor = editor;
this.controller = new AbortController();
this.serviceOrigin = editor.config.get( 'ckbox.serviceOrigin' )!;
}
/**
* Starts the upload process.
*
* @see module:upload/filerepository~UploadAdapter#upload
*/
public async upload(): Promise<UploadResponse> {
const ckboxUtils = this.ckboxUtils;
const t = this.editor.t;
const file = ( await this.loader.file )!;
const category = await ckboxUtils.getCategoryIdForFile( file, { signal: this.controller.signal } );
const uploadUrl = new URL( 'assets', this.serviceOrigin );
const formData = new FormData();
uploadUrl.searchParams.set( 'workspaceId', ckboxUtils.getWorkspaceId() );
formData.append( 'categoryId', category );
formData.append( 'file', file );
const requestConfig = {
method: 'POST',
url: uploadUrl,
data: formData,
onUploadProgress: ( evt: ProgressEvent ) => {
/* istanbul ignore else -- @preserve */
if ( evt.lengthComputable ) {
this.loader.uploadTotal = evt.total;
this.loader.uploaded = evt.loaded;
}
},
signal: this.controller.signal,
authorization: this.token.value
} as const;
return sendHttpRequest( requestConfig )
.then( async data => {
const imageUrls = getImageUrls( data.imageUrls );
return {
ckboxImageId: data.id,
default: imageUrls.imageFallbackUrl,
sources: imageUrls.imageSources
};
} )
.catch( () => {
const genericError = t( 'Cannot upload file:' ) + ` ${ file.name }.`;
return Promise.reject( genericError );
} );
}
/**
* Aborts the upload process.
*
* @see module:upload/filerepository~UploadAdapter#abort
*/
public abort(): void {
this.controller.abort();
}
}