diff --git a/js/src/plotly.ts b/js/src/plotly.ts index 9f81060..79f7cbc 100644 --- a/js/src/plotly.ts +++ b/js/src/plotly.ts @@ -34,6 +34,12 @@ export async function waitForPlotlyReadyToReveal( } function waitForPlotlyAfterPlot(plotEl: HTMLElement): Promise { + // If Plotly has already completed its render, _fullLayout will be populated. + // This avoids waiting for an event that already fired before we subscribed. + if ((plotEl as any)._fullLayout) { + return Promise.resolve(); + } + return new Promise((resolve) => { const handler = () => { cleanup(); diff --git a/shinywidgets/static/output.js b/shinywidgets/static/output.js index 02701e3..830baa0 100644 --- a/shinywidgets/static/output.js +++ b/shinywidgets/static/output.js @@ -46,7 +46,7 @@ eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var _jup \***********************/ /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { -eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ \"findPlotlyGraphDiv\": () => (/* binding */ findPlotlyGraphDiv),\n/* harmony export */ \"waitForPlotlyReadyToReveal\": () => (/* binding */ waitForPlotlyReadyToReveal)\n/* harmony export */ });\nconst plotlyAfterPlotTimeoutMs = 5000;\nfunction findPlotlyGraphDiv(root) {\n const plotlyEl = root.matches(\".js-plotly-plot\")\n ? root\n : root.querySelector(\".js-plotly-plot\");\n return plotlyEl instanceof HTMLElement ? plotlyEl : null;\n}\nasync function waitForPlotlyReadyToReveal(plotEl, dispatchResize) {\n var _a;\n const plotly = window.Plotly;\n await waitForPlotlyAfterPlot(plotEl);\n if ((_a = plotly === null || plotly === void 0 ? void 0 : plotly.Plots) === null || _a === void 0 ? void 0 : _a.resize) {\n try {\n const afterResize = waitForPlotlyAfterPlot(plotEl);\n await plotly.Plots.resize(plotEl);\n await afterResize;\n }\n catch (err) {\n console.error(\"Error resizing Plotly widget before reveal:\", err);\n }\n }\n dispatchResize();\n}\nfunction waitForPlotlyAfterPlot(plotEl) {\n return new Promise((resolve) => {\n const handler = () => {\n cleanup();\n resolve();\n };\n const cleanup = () => {\n if (hasMethod(plotEl, \"removeListener\")) {\n plotEl.removeListener(\"plotly_afterplot\", handler);\n }\n window.clearTimeout(timeoutId);\n };\n const timeoutId = window.setTimeout(() => {\n cleanup();\n resolve();\n }, plotlyAfterPlotTimeoutMs);\n if (hasMethod(plotEl, \"once\")) {\n plotEl.once(\"plotly_afterplot\", handler);\n return;\n }\n if (hasMethod(plotEl, \"on\")) {\n plotEl.on(\"plotly_afterplot\", handler);\n return;\n }\n cleanup();\n resolve();\n });\n}\nfunction hasMethod(x, key) {\n return !!x && typeof x[key] === \"function\";\n}\n\n\n//# sourceURL=webpack://@jupyter-widgets/shiny-embed-manager/./src/plotly.ts?"); +eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ \"findPlotlyGraphDiv\": () => (/* binding */ findPlotlyGraphDiv),\n/* harmony export */ \"waitForPlotlyReadyToReveal\": () => (/* binding */ waitForPlotlyReadyToReveal)\n/* harmony export */ });\nconst plotlyAfterPlotTimeoutMs = 5000;\nfunction findPlotlyGraphDiv(root) {\n const plotlyEl = root.matches(\".js-plotly-plot\")\n ? root\n : root.querySelector(\".js-plotly-plot\");\n return plotlyEl instanceof HTMLElement ? plotlyEl : null;\n}\nasync function waitForPlotlyReadyToReveal(plotEl, dispatchResize) {\n var _a;\n const plotly = window.Plotly;\n await waitForPlotlyAfterPlot(plotEl);\n if ((_a = plotly === null || plotly === void 0 ? void 0 : plotly.Plots) === null || _a === void 0 ? void 0 : _a.resize) {\n try {\n const afterResize = waitForPlotlyAfterPlot(plotEl);\n await plotly.Plots.resize(plotEl);\n await afterResize;\n }\n catch (err) {\n console.error(\"Error resizing Plotly widget before reveal:\", err);\n }\n }\n dispatchResize();\n}\nfunction waitForPlotlyAfterPlot(plotEl) {\n // If Plotly has already completed its render, _fullLayout will be populated.\n // This avoids waiting for an event that already fired before we subscribed.\n if (plotEl._fullLayout) {\n return Promise.resolve();\n }\n return new Promise((resolve) => {\n const handler = () => {\n cleanup();\n resolve();\n };\n const cleanup = () => {\n if (hasMethod(plotEl, \"removeListener\")) {\n plotEl.removeListener(\"plotly_afterplot\", handler);\n }\n window.clearTimeout(timeoutId);\n };\n const timeoutId = window.setTimeout(() => {\n cleanup();\n resolve();\n }, plotlyAfterPlotTimeoutMs);\n if (hasMethod(plotEl, \"once\")) {\n plotEl.once(\"plotly_afterplot\", handler);\n return;\n }\n if (hasMethod(plotEl, \"on\")) {\n plotEl.on(\"plotly_afterplot\", handler);\n return;\n }\n cleanup();\n resolve();\n });\n}\nfunction hasMethod(x, key) {\n return !!x && typeof x[key] === \"function\";\n}\n\n\n//# sourceURL=webpack://@jupyter-widgets/shiny-embed-manager/./src/plotly.ts?"); /***/ }),