diff --git a/modules/uploader.js b/modules/uploader.js index 07d79c3de4..5338db570f 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(); } @@ -36,7 +37,7 @@ class Uploader extends Module { onDrop(e); } - if (noFiles) { + if (noFiles || this.preventImageUpload) { return; } @@ -59,7 +60,18 @@ class Uploader extends Module { }); } - upload(range, files) { + preventImageUploading(value) { + if (typeof value !== 'undefined') { + this.preventImageUpload = value; + } + return this.preventImageUpload; + } + + 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) { diff --git a/test/unit/modules/uploader.js b/test/unit/modules/uploader.js index 6e5da375d3..d575e790b6 100644 --- a/test/unit/modules/uploader.js +++ b/test/unit/modules/uploader.js @@ -108,5 +108,52 @@ describe('Uploader', function() { expect(dropEvent.defaultPrevented).toBeTrue(); }); + + [ + { + 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 = + data.preventValue && !data.forceUpload ? 0 : 1; + let uploads = []; + + const quillMock = { + root: { + addEventListener: () => {}, + }, + }; + + const uploaderInstance = new Uploader(quillMock, { + mimetypes: Uploader.DEFAULTS.mimetypes, + handler: (range, files) => { + uploads = files; + }, + }); + + uploaderInstance.preventImageUploading(!data.preventValue); + uploaderInstance.preventImageUploading(data.preventValue); + + uploaderInstance.upload(testRange, [file], data.forceUpload); + + expect(uploaderInstance.preventImageUploading()).toEqual( + data.preventValue, + ); + expect(uploads.length).toEqual(expectedUploadsCount); + }); + }); }); });