|
| 1 | +import React from "react"; |
| 2 | +import ReactDOM from "react-dom/client"; |
| 3 | +import ShinyRow from "./ShinyRow"; |
| 4 | +import { |
| 5 | + addElementToCache, |
| 6 | + getCachedElement, |
| 7 | + registerFn, |
| 8 | +} from "./plugin-helpers"; |
| 9 | +import pluginInfo from "./plugin-manifest.json"; |
| 10 | + |
| 11 | +const updateApp = (root, data) => { |
| 12 | + root.render(<ShinyRow data={data} />); |
| 13 | +}; |
| 14 | + |
| 15 | +const initApp = (div, data) => { |
| 16 | + const root = ReactDOM.createRoot(div); |
| 17 | + updateApp(root, data); |
| 18 | + return root; |
| 19 | +}; |
| 20 | + |
| 21 | +registerFn(pluginInfo, (handler) => { |
| 22 | + handler.on( |
| 23 | + "flotiq.grid.cell::render", |
| 24 | + ({ data, accessor, contentTypeName, contentObject }) => { |
| 25 | + if (accessor !== "title") return null; |
| 26 | + const key = `${contentTypeName}-${contentObject.id}-${accessor}`; |
| 27 | + const cachedApp = getCachedElement(key); |
| 28 | + if (cachedApp) { |
| 29 | + updateApp(cachedApp.root, data); |
| 30 | + return cachedApp.element; |
| 31 | + } |
| 32 | + |
| 33 | + const div = document.createElement("div"); |
| 34 | + addElementToCache(div, initApp(div, data), key); |
| 35 | + return div; |
| 36 | + } |
| 37 | + ); |
| 38 | +}); |
| 39 | + |
| 40 | +const puginPreviewRoot = document.getElementById("plugin-preview-root"); |
| 41 | + |
| 42 | +if (puginPreviewRoot) initApp(puginPreviewRoot, "Hello World!"); |
0 commit comments