@refore-ai/copy-to-design-sdk
Version:
Copy the HTML source and import it into the design platform via the plugin.
63 lines (61 loc) • 2.14 kB
JavaScript
import CryptoJS from "crypto-js";
import { nanoid } from "nanoid";
import { createFetch } from "ofetch";
//#region src/index.ts
let PlatformType = /* @__PURE__ */ function(PlatformType$1) {
PlatformType$1["Figma"] = "figma";
PlatformType$1["MasterGo"] = "mastergo";
PlatformType$1["JSDesign"] = "jsdesign";
PlatformType$1["PixsoChina"] = "pixso-china";
return PlatformType$1;
}({});
const $fetch = createFetch({ defaults: { async onResponse(context) {
if (context.error) return;
const data = context.response?._data;
if (!data || typeof data !== "object") return;
if (data.error) context.error = data.error;
else if ("data" in data && context.response?._data) context.response._data = data.data;
} } });
const DEFAULT_GET_ENDPOINT = (platform) => platform === PlatformType.Figma ? "https://api.demoway.com" : "https://api.demoway.cn";
var CopyToDesign = class {
constructor(options) {
this.options = options;
}
getEndpointByPlatform(platform) {
const getEndPoint = this.options?._endpoint ?? DEFAULT_GET_ENDPOINT;
return getEndPoint(platform);
}
get headers() {
const url = window.location.href;
return { Authorization: `Basic ${btoa(JSON.stringify({
url,
key: this.options.key
}))}` };
}
async copyToClipboardFromHTML(html, options) {
const { height, width, platform } = options;
const endpoint = this.getEndpointByPlatform(platform);
const source = JSON.stringify({
type: "html",
html,
height,
width
});
const secret = nanoid(32);
const encrypted = CryptoJS.AES.encrypt(source, secret);
const res = await $fetch("/api/refore/copy-to-design/save-copy-info", {
baseURL: endpoint,
method: "POST",
headers: this.headers,
body: { secret }
});
const div = document.createElement("refore-copy-to-design");
div.setAttribute("data-copy-id", res.copyId);
div.setAttribute("data-copy-endpoint", endpoint);
div.setAttribute("data-copy-content", encrypted.toString());
const data = new ClipboardItem({ "text/html": new Blob([div.outerHTML], { type: "text/html" }) });
await navigator.clipboard.write([data]);
}
};
//#endregion
export { CopyToDesign, PlatformType };