From affdbb516aae28d3823c4a8c6f78edb79aaf3f27 Mon Sep 17 00:00:00 2001 From: alexander-kotov-dx Date: Thu, 10 Feb 2022 12:24:01 +0300 Subject: [PATCH 1/7] add blob url format --- formats/image.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/formats/image.js b/formats/image.js index bc19fdaaae..fad1206f41 100644 --- a/formats/image.js +++ b/formats/image.js @@ -36,7 +36,7 @@ class Image extends EmbedBlot { } static sanitize(url) { - return sanitize(url, ['http', 'https', 'data']) ? url : '//:0'; + return sanitize(url, ['http', 'https', 'data', 'blob']) ? url : '//:0'; } static value(domNode) { From 40ecb9d2db9b794fa371ca738ee7f6af0c6f55aa Mon Sep 17 00:00:00 2001 From: "CORP\\kotov.alexander" Date: Fri, 1 Apr 2022 17:10:51 +0400 Subject: [PATCH 2/7] Revert "add blob url format" This reverts commit affdbb516aae28d3823c4a8c6f78edb79aaf3f27. --- formats/image.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/formats/image.js b/formats/image.js index fad1206f41..bc19fdaaae 100644 --- a/formats/image.js +++ b/formats/image.js @@ -36,7 +36,7 @@ class Image extends EmbedBlot { } static sanitize(url) { - return sanitize(url, ['http', 'https', 'data', 'blob']) ? url : '//:0'; + return sanitize(url, ['http', 'https', 'data']) ? url : '//:0'; } static value(domNode) { From 62c7438afe3bf4cb3070d06b34403d94f6dfa86f Mon Sep 17 00:00:00 2001 From: "CORP\\kotov.alexander" Date: Wed, 13 Apr 2022 10:54:21 +0400 Subject: [PATCH 3/7] support prevent image uploading --- modules/uploader.js | 73 ++++++++++++++++++++++++++++----------------- 1 file changed, 46 insertions(+), 27 deletions(-) diff --git a/modules/uploader.js b/modules/uploader.js index 07d79c3de4..0637f5de97 100644 --- a/modules/uploader.js +++ b/modules/uploader.js @@ -7,6 +7,7 @@ class Uploader extends Module { constructor(quill, options) { super(quill, options); + this.preventImageUploading(false); this.addDragOverHandler(); this.addDropHandler(); } @@ -28,38 +29,56 @@ class Uploader extends Module { } addDropHandler() { - this.quill.root.addEventListener('drop', e => { - const noFiles = e.dataTransfer.files.length === 0; - const { onDrop } = this.options; + this.bindedDropHandler = this.dropHandler.bind(this); + this.quill.root.addEventListener('drop', this.bindedDropHandler); + } - if (onDrop && typeof onDrop === 'function') { - onDrop(e); - } + dropHandler(e) { + const noFiles = e.dataTransfer.files.length === 0; + const { onDrop } = this.options; - if (noFiles) { - return; - } + if (onDrop && typeof onDrop === 'function') { + onDrop(e); + } - e.preventDefault(); - let native; - - if (document.caretRangeFromPoint) { - native = document.caretRangeFromPoint(e.clientX, e.clientY); - } else if (document.caretPositionFromPoint) { - const position = document.caretPositionFromPoint(e.clientX, e.clientY); - native = document.createRange(); - native.setStart(position.offsetNode, position.offset); - native.setEnd(position.offsetNode, position.offset); - } else { - return; - } - const normalized = this.quill.selection.normalizeNative(native); - const range = this.quill.selection.normalizedToRange(normalized); - this.upload(range, e.dataTransfer.files); - }); + if (noFiles || this.preventImageUpload) { + return; + } + + e.preventDefault(); + let native; + + if (document.caretRangeFromPoint) { + native = document.caretRangeFromPoint(e.clientX, e.clientY); + } else if (document.caretPositionFromPoint) { + const position = document.caretPositionFromPoint(e.clientX, e.clientY); + native = document.createRange(); + native.setStart(position.offsetNode, position.offset); + native.setEnd(position.offsetNode, position.offset); + } else { + return; + } + const normalized = this.quill.selection.normalizeNative(native); + const range = this.quill.selection.normalizedToRange(normalized); + this.upload(range, e.dataTransfer.files); + } + + removeDropHandler() { + this.quill.root.removeEventListener('drop', this.bindedDropHandler); + } + + preventImageUploading(value) { + if (typeof value !== 'undefined') { + this.preventImageUpload = value; + } + return this.preventImageUpload; } - upload(range, files) { + upload(range, files, force) { + if (this.preventImageUpload && !force) { + return; + } + const uploads = []; Array.from(files).forEach(file => { if (file && this.options.mimetypes.indexOf(file.type) !== -1) { From 597fb6cf2b11ca76d62b1771da1eab8ddbad7144 Mon Sep 17 00:00:00 2001 From: "CORP\\kotov.alexander" Date: Thu, 14 Apr 2022 11:15:33 +0400 Subject: [PATCH 4/7] remove unnecessary changes --- modules/uploader.js | 59 ++++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 33 deletions(-) diff --git a/modules/uploader.js b/modules/uploader.js index 0637f5de97..5338db570f 100644 --- a/modules/uploader.js +++ b/modules/uploader.js @@ -29,42 +29,35 @@ class Uploader extends Module { } addDropHandler() { - this.bindedDropHandler = this.dropHandler.bind(this); - this.quill.root.addEventListener('drop', this.bindedDropHandler); - } - - dropHandler(e) { - const noFiles = e.dataTransfer.files.length === 0; - const { onDrop } = this.options; - - if (onDrop && typeof onDrop === 'function') { - onDrop(e); - } - - if (noFiles || this.preventImageUpload) { - return; - } + this.quill.root.addEventListener('drop', e => { + const noFiles = e.dataTransfer.files.length === 0; + const { onDrop } = this.options; - e.preventDefault(); - let native; + if (onDrop && typeof onDrop === 'function') { + onDrop(e); + } - if (document.caretRangeFromPoint) { - native = document.caretRangeFromPoint(e.clientX, e.clientY); - } else if (document.caretPositionFromPoint) { - const position = document.caretPositionFromPoint(e.clientX, e.clientY); - native = document.createRange(); - native.setStart(position.offsetNode, position.offset); - native.setEnd(position.offsetNode, position.offset); - } else { - return; - } - const normalized = this.quill.selection.normalizeNative(native); - const range = this.quill.selection.normalizedToRange(normalized); - this.upload(range, e.dataTransfer.files); - } + if (noFiles || this.preventImageUpload) { + return; + } - removeDropHandler() { - this.quill.root.removeEventListener('drop', this.bindedDropHandler); + e.preventDefault(); + let native; + + if (document.caretRangeFromPoint) { + native = document.caretRangeFromPoint(e.clientX, e.clientY); + } else if (document.caretPositionFromPoint) { + const position = document.caretPositionFromPoint(e.clientX, e.clientY); + native = document.createRange(); + native.setStart(position.offsetNode, position.offset); + native.setEnd(position.offsetNode, position.offset); + } else { + return; + } + const normalized = this.quill.selection.normalizeNative(native); + const range = this.quill.selection.normalizedToRange(normalized); + this.upload(range, e.dataTransfer.files); + }); } preventImageUploading(value) { From ad83c9119a92f3f37d25327fba5479001168c06e Mon Sep 17 00:00:00 2001 From: "CORP\\kotov.alexander" Date: Thu, 14 Apr 2022 12:48:30 +0400 Subject: [PATCH 5/7] add uploader tests --- test/unit/modules/uploader.js | 59 +++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/test/unit/modules/uploader.js b/test/unit/modules/uploader.js index 6e5da375d3..071e19e74e 100644 --- a/test/unit/modules/uploader.js +++ b/test/unit/modules/uploader.js @@ -108,5 +108,64 @@ describe('Uploader', function() { expect(dropEvent.defaultPrevented).toBeTrue(); }); + + it('check preventImageUploading on', function() { + const testRange = new Range(0); + const file = { + name: 'test.png', + type: 'image/png', + }; + let uploads = []; + + const quillMock = { + root: { + addEventListener: () => {}, + }, + }; + + const uploaderInstance = new Uploader(quillMock, { + mimetypes: Uploader.DEFAULTS.mimetypes, + handler: (range, files) => { + uploads = files; + }, + }); + + uploaderInstance.preventImageUploading(true); + + uploaderInstance.upload(testRange, [file]); + + expect(uploaderInstance.preventImageUploading()).toBeTrue(); + expect(uploads.length).toEqual(0); + }); + + it('check preventImageUploading off', function() { + const testRange = new Range(0); + const file = { + name: 'test.png', + type: 'image/png', + }; + let uploads = []; + + const quillMock = { + root: { + addEventListener: () => {}, + }, + }; + + const uploaderInstance = new Uploader(quillMock, { + mimetypes: Uploader.DEFAULTS.mimetypes, + handler: (range, files) => { + uploads = files; + }, + }); + + uploaderInstance.preventImageUploading(true); + uploaderInstance.preventImageUploading(false); + + uploaderInstance.upload(testRange, [file]); + + expect(uploaderInstance.preventImageUploading()).toBeFalse(); + expect(uploads.length).toEqual(1); + }); }); }); From 0ff53cccd47347437745d1e5d350cf260436b9a3 Mon Sep 17 00:00:00 2001 From: "CORP\\kotov.alexander" Date: Thu, 14 Apr 2022 12:54:21 +0400 Subject: [PATCH 6/7] tests refactoring --- test/unit/modules/uploader.js | 76 ++++++++++++----------------------- 1 file changed, 25 insertions(+), 51 deletions(-) diff --git a/test/unit/modules/uploader.js b/test/unit/modules/uploader.js index 071e19e74e..fa44084e3f 100644 --- a/test/unit/modules/uploader.js +++ b/test/unit/modules/uploader.js @@ -109,63 +109,37 @@ describe('Uploader', function() { expect(dropEvent.defaultPrevented).toBeTrue(); }); - it('check preventImageUploading on', function() { - const testRange = new Range(0); - const file = { - name: 'test.png', - type: 'image/png', - }; - let uploads = []; - - const quillMock = { - root: { - addEventListener: () => {}, - }, - }; - - const uploaderInstance = new Uploader(quillMock, { - mimetypes: Uploader.DEFAULTS.mimetypes, - handler: (range, files) => { - uploads = files; - }, - }); - - uploaderInstance.preventImageUploading(true); + [true, false].forEach(preventValue => { + it(`check preventImageUploading ${preventValue}`, function() { + const testRange = new Range(0); + const file = { + name: 'test.png', + type: 'image/png', + }; + const expectedUploadsCount = preventValue ? 0 : 1; + let uploads = []; - uploaderInstance.upload(testRange, [file]); + const quillMock = { + root: { + addEventListener: () => {}, + }, + }; - expect(uploaderInstance.preventImageUploading()).toBeTrue(); - expect(uploads.length).toEqual(0); - }); + const uploaderInstance = new Uploader(quillMock, { + mimetypes: Uploader.DEFAULTS.mimetypes, + handler: (range, files) => { + uploads = files; + }, + }); - it('check preventImageUploading off', function() { - const testRange = new Range(0); - const file = { - name: 'test.png', - type: 'image/png', - }; - let uploads = []; + uploaderInstance.preventImageUploading(!preventValue); + uploaderInstance.preventImageUploading(preventValue); - const quillMock = { - root: { - addEventListener: () => {}, - }, - }; + uploaderInstance.upload(testRange, [file]); - const uploaderInstance = new Uploader(quillMock, { - mimetypes: Uploader.DEFAULTS.mimetypes, - handler: (range, files) => { - uploads = files; - }, + expect(uploaderInstance.preventImageUploading()).toEqual(preventValue); + expect(uploads.length).toEqual(expectedUploadsCount); }); - - uploaderInstance.preventImageUploading(true); - uploaderInstance.preventImageUploading(false); - - uploaderInstance.upload(testRange, [file]); - - expect(uploaderInstance.preventImageUploading()).toBeFalse(); - expect(uploads.length).toEqual(1); }); }); }); From 99b64b80a2abc3aff30b488e84e60866b8b48ee9 Mon Sep 17 00:00:00 2001 From: "CORP\\kotov.alexander" Date: Thu, 14 Apr 2022 13:03:13 +0400 Subject: [PATCH 7/7] test force upload --- test/unit/modules/uploader.js | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/test/unit/modules/uploader.js b/test/unit/modules/uploader.js index fa44084e3f..d575e790b6 100644 --- a/test/unit/modules/uploader.js +++ b/test/unit/modules/uploader.js @@ -109,14 +109,26 @@ describe('Uploader', function() { expect(dropEvent.defaultPrevented).toBeTrue(); }); - [true, false].forEach(preventValue => { - it(`check preventImageUploading ${preventValue}`, function() { + [ + { + preventValue: true, + }, + { + preventValue: false, + }, + { + preventValue: false, + forceUpload: true, + }, + ].forEach(data => { + it(`check preventImageUploading ${data.preventValue}`, function() { const testRange = new Range(0); const file = { name: 'test.png', type: 'image/png', }; - const expectedUploadsCount = preventValue ? 0 : 1; + const expectedUploadsCount = + data.preventValue && !data.forceUpload ? 0 : 1; let uploads = []; const quillMock = { @@ -132,12 +144,14 @@ describe('Uploader', function() { }, }); - uploaderInstance.preventImageUploading(!preventValue); - uploaderInstance.preventImageUploading(preventValue); + uploaderInstance.preventImageUploading(!data.preventValue); + uploaderInstance.preventImageUploading(data.preventValue); - uploaderInstance.upload(testRange, [file]); + uploaderInstance.upload(testRange, [file], data.forceUpload); - expect(uploaderInstance.preventImageUploading()).toEqual(preventValue); + expect(uploaderInstance.preventImageUploading()).toEqual( + data.preventValue, + ); expect(uploads.length).toEqual(expectedUploadsCount); }); });