MCPcopy
hub / github.com/codex-team/editor.js / processDataTransfer

Method processDataTransfer

src/components/modules/paste.ts:167–225  ·  view source on GitHub ↗

* Handle pasted or dropped data transfer object * * @param {DataTransfer} dataTransfer - pasted or dropped data transfer object * @param {boolean} isDragNDrop - true if data transfer comes from drag'n'drop events

(dataTransfer: DataTransfer, isDragNDrop = false)

Source from the content-addressed store, hash-verified

165 * @param {boolean} isDragNDrop - true if data transfer comes from drag'n'drop events
166 */
167 public async processDataTransfer(dataTransfer: DataTransfer, isDragNDrop = false): Promise<void> {
168 const { Tools } = this.Editor;
169 const types = dataTransfer.types;
170
171 /**
172 * In Microsoft Edge types is DOMStringList. So 'contains' is used to check if 'Files' type included
173 */
174 // eslint-disable-next-line @typescript-eslint/no-explicit-any
175 const includesFiles = types.includes ? types.includes('Files') : (types as any).contains('Files');
176
177 if (includesFiles && !_.isEmpty(this.toolsFiles)) {
178 await this.processFiles(dataTransfer.files);
179
180 return;
181 }
182
183 const editorJSData = dataTransfer.getData(this.MIME_TYPE);
184 const plainData = dataTransfer.getData('text/plain');
185 let htmlData = dataTransfer.getData('text/html');
186
187 /**
188 * If EditorJS json is passed, insert it
189 */
190 if (editorJSData) {
191 try {
192 this.insertEditorJSData(JSON.parse(editorJSData));
193
194 return;
195 } catch (e) { } // Do nothing and continue execution as usual if error appears
196 }
197
198 /**
199 * If text was drag'n'dropped, wrap content with P tag to insert it as the new Block
200 */
201 if (isDragNDrop && plainData.trim() && htmlData.trim()) {
202 htmlData = '<p>' + (htmlData.trim() ? htmlData : plainData) + '</p>';
203 }
204
205 /** Add all tags that can be substituted to sanitizer configuration */
206 const toolsTags = Object.keys(this.toolsTags).reduce((result, tag) => {
207 /**
208 * If Tool explicitly specifies sanitizer configuration for the tag, use it.
209 * Otherwise, remove all attributes
210 */
211 result[tag.toLowerCase()] = this.toolsTags[tag].sanitizationConfig ?? {};
212
213 return result;
214 }, {});
215
216 const customConfig = Object.assign({}, toolsTags, Tools.getAllInlineToolsSanitizeConfig(), { br: {} });
217 const cleanData = clean(htmlData, customConfig);
218
219 /** If there is no HTML or HTML string is equal to plain one, process it as plain text */
220 if (!cleanData.trim() || cleanData.trim() === plainData || !$.isHTMLString(cleanData)) {
221 await this.processText(plainData);
222 } else {
223 await this.processText(cleanData, true);
224 }

Callers 2

PasteClass · 0.95
processDropMethod · 0.80

Calls 8

processFilesMethod · 0.95
insertEditorJSDataMethod · 0.95
processTextMethod · 0.95
cleanFunction · 0.90
keysMethod · 0.80
isHTMLStringMethod · 0.80
isEmptyMethod · 0.45

Tested by

no test coverage detected