Skip to content

Commit

Permalink
Support preventing of internal image uploading (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-kotov-dx authored Apr 14, 2022
1 parent 4239db6 commit cd26ea8
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
16 changes: 14 additions & 2 deletions modules/uploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Uploader extends Module {
constructor(quill, options) {
super(quill, options);

this.preventImageUploading(false);
this.addDragOverHandler();
this.addDropHandler();
}
Expand Down Expand Up @@ -36,7 +37,7 @@ class Uploader extends Module {
onDrop(e);
}

if (noFiles) {
if (noFiles || this.preventImageUpload) {
return;
}

Expand All @@ -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) {
Expand Down
47 changes: 47 additions & 0 deletions test/unit/modules/uploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
});

0 comments on commit cd26ea8

Please sign in to comment.