UNPKG

@scalar/api-reference

Version:

Generate beautiful API references from OpenAPI documents

91 lines (90 loc) 3.52 kB
import { defineComponent, mergeModels, useModel, createElementBlock, openBlock, Fragment, createVNode, unref, createSlots, withCtx, createTextVNode } from "vue"; import { useLoadingState, ScalarTextInput, ScalarIconButton, ScalarButton } from "@scalar/components"; import { ScalarIconCopy } from "@scalar/icons"; import { useClipboard } from "@scalar/use-hooks/useClipboard"; import { useToasts } from "@scalar/use-toasts"; import { REGISTRY_SHARE_URL } from "../../consts/urls.js"; import ApiReferenceToolbarBlurb from "./ApiReferenceToolbarBlurb.vue.js"; import { uploadTempDocument } from "../../helpers/upload-temp-document.js"; const _sfc_main = /* @__PURE__ */ defineComponent({ __name: "ApiReferenceToolbarShareTemporary", props: /* @__PURE__ */ mergeModels({ workspace: {} }, { "url": {}, "urlModifiers": {} }), emits: ["update:url"], setup(__props) { const { toast } = useToasts(); const loader = useLoadingState(); const tempDocUrl = useModel(__props, "url"); async function generateTemporaryLink() { if (loader.isLoading || !__props.workspace || !!tempDocUrl.value) { return; } loader.start(); const document = __props.workspace.exportActiveDocument("json"); if (!document) { toast("Unable to export active document", "error"); await loader.invalidate(); return; } try { tempDocUrl.value = await uploadTempDocument(document); await copyToClipboard(tempDocUrl.value); await loader.validate(); } catch (error) { const message = error instanceof Error ? error.message : "An unknown error occurred"; toast(message, "error"); await loader.invalidate(); } } const { copyToClipboard } = useClipboard(); return (_ctx, _cache) => { return openBlock(), createElementBlock(Fragment, null, [ createVNode(unref(ScalarTextInput), { modelValue: tempDocUrl.value, placeholder: `${unref(REGISTRY_SHARE_URL)}/apis/...`, readonly: "", onClick: _cache[1] || (_cache[1] = ($event) => tempDocUrl.value && unref(copyToClipboard)(tempDocUrl.value)) }, createSlots({ _: 2 }, [ tempDocUrl.value ? { name: "aside", fn: withCtx(() => [ createVNode(unref(ScalarIconButton), { class: "-m-1.5 -ml-1", icon: unref(ScalarIconCopy), label: "Copy link to clipboard", size: "sm", onClick: _cache[0] || (_cache[0] = ($event) => unref(copyToClipboard)(tempDocUrl.value)) }, null, 8, ["icon"]) ]), key: "0" } : void 0 ]), 1032, ["modelValue", "placeholder"]), createVNode(unref(ScalarButton), { class: "h-auto p-2.5", disabled: !!tempDocUrl.value, loader: unref(loader), variant: "outlined", onClick: generateTemporaryLink }, { default: withCtx(() => [..._cache[2] || (_cache[2] = [ createTextVNode(" Generate ", -1) ])]), _: 1 }, 8, ["disabled", "loader"]), createVNode(ApiReferenceToolbarBlurb, { class: "-mt-1" }, { default: withCtx(() => [..._cache[3] || (_cache[3] = [ createTextVNode(" Shared documents will automatically be deleted after 7 days. ", -1) ])]), _: 1 }) ], 64); }; } }); export { _sfc_main as default };