Skip to content

Commit

Permalink
fix(cli-serve): export image (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
miralemd committed Dec 11, 2019
1 parent cf717a5 commit efefa0d
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 44 deletions.
14 changes: 14 additions & 0 deletions apis/nucleus/src/__tests__/viz.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@ describe('viz', () => {
let setSnOptions;
let setSnContext;
let takeSnapshot;
let exportImage;
before(() => {
sandbox = sinon.createSandbox();
unmount = sandbox.spy();
setSnOptions = sandbox.spy();
setSnContext = sandbox.spy();
takeSnapshot = sandbox.spy();
exportImage = sandbox.spy();
cellRef = {
current: {
setSnOptions,
setSnContext,
takeSnapshot,
exportImage,
},
};
glue = sandbox.stub().returns([unmount, cellRef]);
Expand Down Expand Up @@ -61,6 +64,10 @@ describe('viz', () => {
it('should have a setTemporaryProperties method', () => {
expect(api.setTemporaryProperties).to.be.a('function');
});

it('should have an exportImage method', () => {
expect(api.exportImage).to.be.a('function');
});
});

describe('mounting', () => {
Expand Down Expand Up @@ -133,4 +140,11 @@ describe('viz', () => {
expect(cellRef.current.takeSnapshot).to.have.been.calledWithExactly();
});
});

describe('export', () => {
it('should export image', async () => {
api.exportImage();
expect(cellRef.current.exportImage).to.have.been.calledWithExactly();
});
});
});
39 changes: 15 additions & 24 deletions apis/nucleus/src/components/Cell.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -233,29 +233,15 @@ const Cell = forwardRef(({ nebulaContext, model, initialSnContext, initialSnOpti
setSnContext,
setSnOptions,
async takeSnapshot() {
const snapshot = {
...layout,
snapshotData: {
object: {
size: {
w: contentRect.width,
h: contentRect.height,
},
},
},
};
const { width, height } = cellRef.current.getBoundingClientRect();

// clone layout to avoid mutation
let clonedLayout = JSON.parse(JSON.stringify(layout));
if (typeof state.sn.component.setSnapshotData === 'function') {
return (await state.sn.component.setSnapshotData(snapshot)) || snapshot;
clonedLayout = (await state.sn.component.setSnapshotData(clonedLayout)) || clonedLayout;
}
return snapshot;
},
async exportImage() {
if (!nebulaContext.snapshot.capture) {
throw new Error('Nebula has not been configured with snapshot.capture');
}
const lyt = await this.takeSnapshot(); // eslint-disable-line
const { width, height } = cellRef.current.getBoundingClientRect();
const s = {
return {
key: String(+Date.now()),
meta: {
language: translator.language(),
theme: theme.name,
Expand All @@ -265,10 +251,15 @@ const Cell = forwardRef(({ nebulaContext, model, initialSnContext, initialSnOpti
height: Math.round(height),
},
},
layout: lyt,
layout: clonedLayout,
};

return nebulaContext.snapshot.capture(s);
},
async exportImage() {
if (!nebulaContext.snapshot.capture) {
throw new Error('Nebula has not been configured with snapshot.capture');
}
const snapshot = await this.takeSnapshot(); // eslint-disable-line
return nebulaContext.snapshot.capture(snapshot);
},
}),
[state.sn, contentRect, layout, theme.name]
Expand Down
20 changes: 16 additions & 4 deletions apis/nucleus/src/components/__tests__/cell.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe('<Cell />', () => {
await act(async () => {
renderer = create(
<ThemeProvider theme={theme}>
<LocaleContext.Provider value={{ get: s => s }}>
<LocaleContext.Provider value={{ get: s => s, language: () => 'sv' }}>
<Cell
ref={cellRef}
nebulaContext={nebulaContext}
Expand Down Expand Up @@ -413,9 +413,21 @@ describe('<Cell />', () => {
});
global.window.addEventListener.callArg(1);
const snapshot = await cellRef.current.takeSnapshot();
const { key } = snapshot;
delete snapshot.key;
expect(key).to.be.a('string');
expect(snapshot).to.deep.equal({
visualization: 'sn',
snapshotData: { object: { size: { w: 300, h: 400 } } },
layout: {
visualization: 'sn',
},
meta: {
language: 'sv',
theme: 'dark',
size: {
height: 400,
width: 300,
},
},
});
});

Expand Down Expand Up @@ -460,7 +472,7 @@ describe('<Cell />', () => {
});
global.window.addEventListener.callArg(1);
const snapshot = await cellRef.current.takeSnapshot();
expect(snapshot).to.deep.equal({
expect(snapshot.layout).to.deep.equal({
foo: 'bar',
});
});
Expand Down
10 changes: 6 additions & 4 deletions apis/nucleus/src/viz.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,15 @@ export default function viz({ model, context: nebulaContext } = {}) {
setSnContext(ctx);
return api;
},
exportImage() {
return cellRef.current.exportImage();
},

// DEBUG MODE ?
// TODO - decide if this method is useful as part of public API
takeSnapshot() {
// TODO - decide if this method is useful at all
return cellRef.current.takeSnapshot();
},
exportImage(settings) {
return cellRef.current.exportImage(settings);
},

// QVisualization API
// close() {},
Expand Down
22 changes: 13 additions & 9 deletions commands/serve/web/components/Cell.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,19 @@ export default function({ id, expandable, minHeight }) {
});
} else {
const containerSize = vizRef.current.el.getBoundingClientRect();
vizRef.current.viz.exportImage().then(res => {
if (res && res.url) {
const key = /\/([A-z0-9_]+)$/.exec(res.url)[1];
window.open(
`/render?snapshot=${key}`,
'snapshot',
`height=${Math.round(containerSize.height)},width=${Math.round(containerSize.width)}`
);
}
vizRef.current.viz.takeSnapshot().then(snapshot => {
fetch('/njs/snapshot', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(snapshot),
});
window.open(
`/render?snapshot=${snapshot.key}`,
'snapshot',
`height=${Math.round(containerSize.height)},width=${Math.round(containerSize.width)}`
);
});
}
},
Expand Down
9 changes: 6 additions & 3 deletions commands/serve/web/eRender.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,12 @@ async function renderSnapshot() {
},
],
});
snapshooter({
nucleus: n,
element,

window.onHotChange(supernova.name, async () => {
snapshooter({
nucleus: n,
element,
});
});
}

Expand Down

0 comments on commit efefa0d

Please sign in to comment.