Skip to content

Commit

Permalink
fix(office): Pass viewer options to loaders (#1243)
Browse files Browse the repository at this point in the history
* fix(office): Pass viewer options to loaders

* fix(office): Address comments

* fix(office): Address comments
  • Loading branch information
Mingze authored Aug 21, 2020
1 parent 7d20a81 commit 1e63d6c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
6 changes: 4 additions & 2 deletions src/lib/Preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ class Preview extends EventEmitter {
try {
file = getCachedFile(this.cache, { fileId, fileVersionId });
loader = file ? this.getLoader(file) : null;
viewer = loader ? loader.determineViewer(file) : null;
viewer = loader ? loader.determineViewer(file, undefined, this.options.viewers) : null;
if (!viewer) {
return;
}
Expand Down Expand Up @@ -1844,7 +1844,9 @@ class Preview extends EventEmitter {
* @return {Object|null} Matching loader
*/
getLoader(file) {
return this.loaders.find(loader => loader.canLoad(file, Object.keys(this.disabledViewers)));
return this.loaders.find(loader =>
loader.canLoad(file, Object.keys(this.disabledViewers), this.options.viewers),
);
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/lib/__tests__/Preview-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,8 +542,9 @@ describe('lib/Preview', () => {
sandbox
.mock(loader)
.expects('determineViewer')
.withArgs(someFile)
.withArgs(someFile, undefined, { viewer: {} })
.returns(viewer);
preview.options.viewers = { viewer: {} };
preview.prefetch({ fileId, token, sharedLink, sharedLinkPassword });
});

Expand Down Expand Up @@ -2750,9 +2751,13 @@ describe('lib/Preview', () => {
canLoad: sandbox.stub().returns(true),
},
];
preview.options.viewers = { viewer: {} };

const loader = preview.getLoader('file');
expect(loader.name).to.equal('csv');
preview.loaders.forEach(loaderMock => {
expect(loaderMock.canLoad).to.be.calledWith('file', ['Office'], { viewer: {} });
});
});
});

Expand Down
9 changes: 6 additions & 3 deletions src/lib/viewers/AssetLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ class AssetLoader {
*
* @param {Object} file - Box file
* @param {Array} [disabledViewers] - List of disabled viewers
* @param {Object} viewerOptions - Custom options for viewers
* @return {boolean} Is file supported
*/
canLoad(file, disabledViewers = []) {
return !!this.determineViewer(file, disabledViewers);
canLoad(file, disabledViewers = [], viewerOptions = {}) {
return !!this.determineViewer(file, disabledViewers, viewerOptions);
}

/**
Expand All @@ -24,9 +25,11 @@ class AssetLoader {
*
* @param {Object} file - Box file
* @param {Array} [disabledViewers] - List of disabled viewers
* @param {Object} viewerOptions - Custom options for viewers
* @return {Object} The viewer to use
*/
determineViewer(file, disabledViewers = []) {
// eslint-disable-next-line no-unused-vars
determineViewer(file, disabledViewers = [], viewerOptions = {}) {
return this.viewers.find(viewer => {
if (disabledViewers.indexOf(viewer.NAME) > -1) {
return false;
Expand Down
8 changes: 4 additions & 4 deletions src/lib/viewers/__tests__/AssetLoader-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ describe('lib/viewers/AssetLoader', () => {
it('should return true if loader can find a viewer to match the file', () => {
sandbox.stub(loader, 'determineViewer').returns({});

expect(loader.canLoad({})).to.be.true;
expect(loader.determineViewer).to.be.called;
expect(loader.canLoad({}, [], { viewer: {} })).to.be.true;
expect(loader.determineViewer).to.be.calledWith({}, [], { viewer: {} });
});

it("should return false if loader can't find a viewer to match the file", () => {
sandbox.stub(loader, 'determineViewer').returns(null);

expect(loader.canLoad({})).to.be.false;
expect(loader.determineViewer).to.be.called;
expect(loader.canLoad({}, [], { viewer: {} })).to.be.false;
expect(loader.determineViewer).to.be.calledWith({}, [], { viewer: {} });
});
});

Expand Down

0 comments on commit 1e63d6c

Please sign in to comment.