* 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)
| 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 | } |
no test coverage detected