UNPKG

@docus/admin

Version:
40 lines (39 loc) 1.22 kB
import { components } from "nuxt-component-meta"; import { defineNuxtPlugin } from "#app"; export default defineNuxtPlugin((nuxt) => { const router = nuxt.nuxt2Context.app.router; const trustedOrigins = ["http://localhost:3000", "https://dev.docus.com", "https://docus.com"]; const actions = { refresh: () => { window.$nuxt.refresh(); }, push: (path) => { router?.push(path); }, slots: (...args) => { window.$nuxt.$emit("docus:components:slots", JSON.parse(args.join(":"))); }, props: (...args) => { window.$nuxt.$emit("docus:components:props", JSON.parse(args.join(":"))); } }; window.addEventListener("message", (event) => { if (!trustedOrigins.includes(event.origin)) { return; } if (typeof event.data !== "string") return; const [action, ...args] = event.data.split(":"); if (typeof actions[action] === "function") { actions[action](...args); } }, false); if (window !== window.top) { setTimeout(() => { window.parent.postMessage(`components:${JSON.stringify(components)}`, "*"); }, 100); router?.afterEach((to) => { window.parent.postMessage(`push:${to.path}`, "*"); }); } });