|
| 1 | +import { |
| 2 | + createApp, |
| 3 | + customRef, |
| 4 | + defineComponent, |
| 5 | + h, |
| 6 | + inject, |
| 7 | + onMounted, |
| 8 | + onUnmounted, |
| 9 | + provide, |
| 10 | + toValue, |
| 11 | + unref, |
| 12 | +} from "vue"; |
| 13 | + |
| 14 | +/** |
| 15 | + * @template {Record<string, any>} T |
| 16 | + * @typedef RenderContext |
| 17 | + * @property {import("@anywidget/types").AnyModel<T>} model |
| 18 | + * @property {import("@anywidget/types").Experimental} experimental |
| 19 | + */ |
| 20 | + |
| 21 | +/** |
| 22 | + * @type {import("vue").InjectionKey<RenderContext<any>>} |
| 23 | + */ |
| 24 | +const RENDER_CONTEXT_KEY = Symbol("anywidget.RenderContext"); |
| 25 | + |
| 26 | +/** |
| 27 | + * @returns {RenderContext<any>} |
| 28 | + */ |
| 29 | +function useRenderContext() { |
| 30 | + let ctx = inject(RENDER_CONTEXT_KEY); |
| 31 | + if (!ctx) throw new Error("anywidget.RenderContext is not provided."); |
| 32 | + return ctx; |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * @template {Record<string, any>} T |
| 37 | + * @returns {import("@anywidget/types").AnyModel<T>} |
| 38 | + */ |
| 39 | +export function useModel() { |
| 40 | + let ctx = useRenderContext(); |
| 41 | + return ctx.model; |
| 42 | +} |
| 43 | + |
| 44 | +/** @returns {import("@anywidget/types").Experimental} */ |
| 45 | +export function useExperimental() { |
| 46 | + let ctx = useRenderContext(); |
| 47 | + return ctx.experimental; |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * A Vue Composable to use model-backed state in a component. |
| 52 | + * |
| 53 | + * Returns a ShallowRef that synchronizes its value with |
| 54 | + * the underlying model provided by an anywidget host. |
| 55 | + * |
| 56 | + * @example |
| 57 | + * ```ts |
| 58 | + * import { useModelState } from "@anywidget/vue"; |
| 59 | + * |
| 60 | + * function Counter() { |
| 61 | + * const value = useModelState<number>("value"); |
| 62 | + * |
| 63 | + * return ( |
| 64 | + * <button onClick={() => value++}> |
| 65 | + * Count: {value} |
| 66 | + * </button> |
| 67 | + * ); |
| 68 | + * } |
| 69 | + * ``` |
| 70 | + * |
| 71 | + * @template S |
| 72 | + * @param {import("vue").MaybeRef<string>} key - The name of the model field to use |
| 73 | + * @returns {import("vue").ShallowRef<S>} |
| 74 | + */ |
| 75 | +export function useModelState(key) { |
| 76 | + const model = useModel(); |
| 77 | + |
| 78 | + /** |
| 79 | + * @type {VoidFunction} |
| 80 | + */ |
| 81 | + let trigger; |
| 82 | + |
| 83 | + /** |
| 84 | + * @type {import("vue").Ref<S>} |
| 85 | + */ |
| 86 | + const value = customRef((_track, _trigger) => { |
| 87 | + trigger = _trigger; |
| 88 | + return { |
| 89 | + get() { |
| 90 | + _track(); |
| 91 | + return model.get(unref(key)); |
| 92 | + }, |
| 93 | + set(newValue) { |
| 94 | + model.set(unref(key), toValue(newValue)); |
| 95 | + model.save_changes(); |
| 96 | + }, |
| 97 | + }; |
| 98 | + }); |
| 99 | + |
| 100 | + const update = () => { |
| 101 | + value.value = model.get(unref(key)); |
| 102 | + trigger(); |
| 103 | + }; |
| 104 | + |
| 105 | + onMounted(() => { |
| 106 | + model.on(`change:${key}`, update); |
| 107 | + }); |
| 108 | + |
| 109 | + onUnmounted(() => { |
| 110 | + model.off(`change:${key}`, update); |
| 111 | + }); |
| 112 | + |
| 113 | + return value; |
| 114 | +} |
| 115 | + |
| 116 | +/** |
| 117 | + * @type {import("vue").DefineSetupFnComponent<RenderContext<any>>} |
| 118 | + */ |
| 119 | +const WidgetWrapper = defineComponent( |
| 120 | + ({ model, experimental }, ctx) => { |
| 121 | + provide(RENDER_CONTEXT_KEY, { model, experimental }); |
| 122 | + return () => ctx.slots?.default?.(); |
| 123 | + }, |
| 124 | + { |
| 125 | + props: ["model", "experimental"], |
| 126 | + name: "WidgetWrapper", |
| 127 | + }, |
| 128 | +); |
| 129 | + |
| 130 | +/** |
| 131 | + * @param {import("vue").Component} Widget |
| 132 | + * @returns {import("@anywidget/types").Render} |
| 133 | + */ |
| 134 | +export function createRender(Widget) { |
| 135 | + return ({ el, model, experimental }) => { |
| 136 | + const app = createApp(h(WidgetWrapper, { model, experimental }, h(Widget))); |
| 137 | + app.mount(el); |
| 138 | + |
| 139 | + return () => app.unmount(); |
| 140 | + }; |
| 141 | +} |
0 commit comments