|
| 1 | +import * as react from "react"; |
| 2 | +import * as reactDOM from "react-dom"; |
| 3 | +import htm from "htm"; |
| 4 | + |
| 5 | +import serializeEvent from "./event-to-object"; |
| 6 | + |
| 7 | +import { applyPatchInplace, joinUrl } from "./utils"; |
| 8 | + |
| 9 | +const html = htm.bind(react.createElement); |
| 10 | +const LayoutConfigContext = react.createContext({ |
| 11 | + sendEvent: undefined, |
| 12 | + loadImportSource: undefined, |
| 13 | +}); |
| 14 | + |
| 15 | +export function Layout({ saveUpdateHook, sendEvent, loadImportSource }) { |
| 16 | + const [model, patchModel] = useInplaceJsonPatch({}); |
| 17 | + |
| 18 | + react.useEffect(() => saveUpdateHook(patchModel), [patchModel]); |
| 19 | + |
| 20 | + if (model.tagName) { |
| 21 | + return html` |
| 22 | + <${LayoutConfigContext.Provider} value=${{ sendEvent, loadImportSource }}> |
| 23 | + <${Element} model=${model} /> |
| 24 | + <//> |
| 25 | + `; |
| 26 | + } else { |
| 27 | + return html`<div />`; |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +function Element({ model }) { |
| 32 | + if (model.importSource) { |
| 33 | + return html`<${ImportedElement} model=${model} />`; |
| 34 | + } else { |
| 35 | + return html`<${StandardElement} model=${model} />`; |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +function ImportedElement({ model }) { |
| 40 | + const config = react.useContext(LayoutConfigContext); |
| 41 | + const mountPoint = react.useRef(null); |
| 42 | + |
| 43 | + react.useEffect(() => { |
| 44 | + config.loadImportSource(model.importSource.source).then((module) => { |
| 45 | + mountImportSource(mountPoint.current, module, model, config); |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + return html`<div ref=${mountPoint} />`; |
| 50 | +} |
| 51 | + |
| 52 | +function StandardElement({ model }) { |
| 53 | + const config = react.useContext(LayoutConfigContext); |
| 54 | + const children = elementChildren(model); |
| 55 | + const attributes = elementAttributes(model, config.sendEvent); |
| 56 | + if (model.children && model.children.length) { |
| 57 | + return html`<${model.tagName} ...${attributes}>${children}<//>`; |
| 58 | + } else { |
| 59 | + return html`<${model.tagName} ...${attributes} />`; |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +function elementChildren(model) { |
| 64 | + if (!model.children) { |
| 65 | + return []; |
| 66 | + } else { |
| 67 | + return model.children.map((child) => { |
| 68 | + switch (typeof child) { |
| 69 | + case "object": |
| 70 | + return html`<${Element} key=${child.key} model=${child} />`; |
| 71 | + case "string": |
| 72 | + return child; |
| 73 | + } |
| 74 | + }); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +function elementAttributes(model, sendEvent) { |
| 79 | + const attributes = Object.assign({}, model.attributes); |
| 80 | + |
| 81 | + if (model.eventHandlers) { |
| 82 | + Object.keys(model.eventHandlers).forEach((eventName) => { |
| 83 | + const eventSpec = model.eventHandlers[eventName]; |
| 84 | + attributes[eventName] = eventHandler(sendEvent, eventSpec); |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + return attributes; |
| 89 | +} |
| 90 | + |
| 91 | +function eventHandler(sendEvent, eventSpec) { |
| 92 | + return function () { |
| 93 | + const data = Array.from(arguments).map((value) => { |
| 94 | + if (typeof value === "object" && value.nativeEvent) { |
| 95 | + if (eventSpec["preventDefault"]) { |
| 96 | + value.preventDefault(); |
| 97 | + } |
| 98 | + if (eventSpec["stopPropagation"]) { |
| 99 | + value.stopPropagation(); |
| 100 | + } |
| 101 | + return serializeEvent(value); |
| 102 | + } else { |
| 103 | + return value; |
| 104 | + } |
| 105 | + }); |
| 106 | + const sentEvent = new Promise((resolve, reject) => { |
| 107 | + const msg = { |
| 108 | + data: data, |
| 109 | + target: eventSpec["target"], |
| 110 | + }; |
| 111 | + sendEvent(msg); |
| 112 | + resolve(msg); |
| 113 | + }); |
| 114 | + }; |
| 115 | +} |
| 116 | + |
| 117 | +function mountImportSource(element, module, model, config) { |
| 118 | + if (model.importSource.exportsMount) { |
| 119 | + const props = elementAttributes(model, config.sendEvent); |
| 120 | + if (model.children) { |
| 121 | + props.children = model.children; |
| 122 | + } |
| 123 | + module.mount(element, module[model.tagName], props); |
| 124 | + } else { |
| 125 | + reactDOM.render( |
| 126 | + react.createElement( |
| 127 | + module[model.tagName], |
| 128 | + elementAttributes(model, config.sendEvent), |
| 129 | + ...elementChildren(model) |
| 130 | + ), |
| 131 | + element |
| 132 | + ); |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +function useInplaceJsonPatch(doc) { |
| 137 | + const ref = react.useRef(doc); |
| 138 | + const forceUpdate = useForceUpdate(); |
| 139 | + |
| 140 | + const applyPatch = react.useCallback( |
| 141 | + (path, patch) => { |
| 142 | + applyPatchInplace(ref.current, path, patch); |
| 143 | + forceUpdate(); |
| 144 | + }, |
| 145 | + [ref, forceUpdate] |
| 146 | + ); |
| 147 | + |
| 148 | + return [ref.current, applyPatch]; |
| 149 | +} |
| 150 | + |
| 151 | +function useForceUpdate() { |
| 152 | + const [, updateState] = react.useState(); |
| 153 | + return react.useCallback(() => updateState({}), []); |
| 154 | +} |
0 commit comments