diff --git a/sandbox.html b/sandbox.html index 6f987245..2f771f45 100644 --- a/sandbox.html +++ b/sandbox.html @@ -149,10 +149,12 @@ getData: async data => { const result = await cadenzaClient.getData(data.dataType); if (data.dataType === 'png') { - const canvas = document.getElementById('canvas'); - const ctx = canvas.getContext('2d'); - const imageBitmap = await createImageBitmap(result) - ctx.drawImage(imageBitmap, 0, 0, 300, 150); + const url = URL.createObjectURL(result); + try { + iframe.src = url; + } finally { + URL.revokeObjectURL(url); + } } } }; diff --git a/src/cadenza.js b/src/cadenza.js index f30e0ff1..768e5202 100644 --- a/src/cadenza.js +++ b/src/cadenza.js @@ -524,27 +524,6 @@ export class CadenzaClient { return promise; } - /** - * @template [T=void] - * @param {string} type - * @param {unknown} [detail] - * @returns {Promise} - */ - #postRequest(type, detail) { - /** @type {(() => void)[]} */ - let unsubscribes; - const promise = new Promise((resolve, reject) => { - unsubscribes = [ - this.#on(`${type}:success`, (event) => resolve(event.detail)), - this.#on(`${type}:error`, () => reject()), - ]; - }); - promise.finally(() => unsubscribes.forEach((unsubscribe) => unsubscribe())); - this.#postEvent(type, detail); - return promise; - } - - /** * Subscribe to a `postMessage()` event. * @@ -617,14 +596,14 @@ export class CadenzaClient { */ #postRequest(/** @type string */ type, /** @type unknown */ detail) { const { port1, port2 } = new MessageChannel(); - /** @type {Promise} */ + /** @type {Promise} */ const promise = new Promise((resolve, reject) => { port1.onmessage = ( /** @type MessageEvent> */ event, ) => { const cadenzaEvent = event.data; if (cadenzaEvent.type === `${type}:success`) { - resolve(); + resolve(cadenzaEvent.detail); } else if (cadenzaEvent.type === `${type}:error`) { reject(); } @@ -691,7 +670,7 @@ export class CadenzaClient { return res; } - /** + /** * Get data from the currently shown workbook view. * * Currently, only map views are supported. diff --git a/src/docs.md b/src/docs.md index d365db5d..11c2b141 100644 --- a/src/docs.md +++ b/src/docs.md @@ -308,7 +308,7 @@ button.textContent = 'Download Excel'; button.onclick = () => cadenzaClient.downloadData('{embeddingTargetId}', 'excel'); ``` -### Request Data From a Workbook View via PostMessage +### Request Data From a Workbook View API: [CadenzaClient#getData](./classes/CadenzaClient.html#getData)