UNPKG

slightning-coco-widget

Version:

SLIGHTNING 的 CoCo 控件框架。

263 lines (262 loc) 12.2 kB
import * as CreationProject2 from "../../creation-project-2"; import { convertToCreationProject2 } from "../convert/to-creation-project-2"; import { decorate } from "../export"; function isEditorWindow(window) { return /^https:\/\/(test-)?cp\.cocotais\.cn\/(#|\?.*)?$/.test(window.location.href); } function getEditorWindow() { if (isEditorWindow(window)) { return window; } try { if (parent != window && isEditorWindow(parent)) { return parent; } } catch (__ignore) { } try { if (opener instanceof Window && isEditorWindow(opener)) { return opener; } } catch (__ignore) { } return null; } function getEditorWorkName() { var _a, _b, _c, _d; return (_d = (_c = (_b = (_a = getEditorRunButton()) === null || _a === void 0 ? void 0 : _a.parentElement) === null || _b === void 0 ? void 0 : _b.querySelector("input[type=\"text\"]")) === null || _c === void 0 ? void 0 : _c.value) !== null && _d !== void 0 ? _d : null; } function getEditorRunButton() { var _a, _b, _c; return (_c = Array.from((_b = (_a = getEditorWindow()) === null || _a === void 0 ? void 0 : _a.document.querySelectorAll("button")) !== null && _b !== void 0 ? _b : []).find((button) => { var _a; return /^[\s]*(运[\s]*行|停[\s]*止)[\s]*$/.test((_a = button.textContent) !== null && _a !== void 0 ? _a : ""); })) !== null && _c !== void 0 ? _c : null; } function editorIsRunningWork() { var _a, _b; return /^[\s]*停[\s]*止[\s]*$/.test((_b = (_a = getEditorRunButton()) === null || _a === void 0 ? void 0 : _a.textContent) !== null && _b !== void 0 ? _b : ""); } export const CreationProject2Adapter = { getSuperWidget(types) { const propertiesSet = new Set(); function addProperties(properties) { for (const property of properties) { if ("contents" in property) { addProperties(property.contents); continue; } propertiesSet.add(Array.isArray(property) ? property[0] : property.key); } } addProperties(types.properties); return class extends CreationProject2.widgetClass { constructor() { super(); return new Proxy(this, { defineProperty(target, property, attributes) { if (typeof property == "string" && propertiesSet.has(property)) { if (target.props == null) { return true; } return Reflect.defineProperty(target.props, property, attributes); } return Reflect.defineProperty(target, property, attributes); }, deleteProperty(target, p) { if (typeof p == "string" && propertiesSet.has(p)) { return Reflect.deleteProperty(target.props, p); } return Reflect.deleteProperty(target, p); }, get(target, p, receiver) { if (typeof p == "string" && propertiesSet.has(p)) { return Reflect.get(target.props, p, receiver); } return Reflect.get(target, p, receiver); }, getOwnPropertyDescriptor(target, p) { if (typeof p == "string" && propertiesSet.has(p)) { return Reflect.getOwnPropertyDescriptor(target.props, p); } return Reflect.getOwnPropertyDescriptor(target, p); }, has(target, p) { if (typeof p == "string" && propertiesSet.has(p)) { return Reflect.has(target.props, p); } return Reflect.has(target, p); }, ownKeys(target) { return Reflect.ownKeys(target).concat(Reflect.ownKeys(target.props)); }, set(target, p, newValue, receiver) { if (typeof p == "string" && propertiesSet.has(p)) { if (types.options.visible) { CreationProject2.widgetClass.prototype.setProp.call(receiver, p, newValue); return true; } else { return Reflect.set(target.props, p, newValue, receiver); } } return Reflect.set(target, p, newValue, receiver); } }); } }; }, exportWidget(types, widget, config) { [types, widget] = decorate(types, widget, config, ["CreationProject", "CreationProject2"]); CreationProject2.exportWidget(...convertToCreationProject2(types, widget)); }, Logger: class CreationProjectLogger { constructor(__types, widget) { this.widget = widget; } log(messages) { CreationProject2.widgetClass.prototype.widgetLog.call(this.widget, messages); } info(messages) { CreationProject2.widgetClass.prototype.widgetLog.call(this.widget, `[信息] ${messages}`); } warn(messages) { CreationProject2.widgetClass.prototype.widgetWarn.call(this.widget, messages); } error(messages) { CreationProject2.widgetClass.prototype.widgetError.call(this.widget, messages); } }, emit(key, ...args) { CreationProject2.widgetClass.prototype.emit.call(this, key, ...args); }, utils: { inNative() { return location.protocol == "file:"; }, inEditor() { return isEditorWindow(window); }, inEditorWindow: function () { return /^https:\/\/(test-)?cp\.cocotais\.cn\/(pptui)?$/.test(location.href); }, inEditorPlayer: function () { return /^https:\/\/(test-)?cp\.cocotais\.cn\/player(#|\?.*)?$/.test(location.href); }, getImageURLByFileName(__fileName) { return null; }, getAudioURLByFileName(__fileName) { return null; }, getVideoURLByFileName(__fileName) { return null; }, editor: { getWindow: getEditorWindow, getWorkName: getEditorWorkName, getRunButton: getEditorRunButton, isRunningWork: editorIsRunningWork, runWork() { var _a; if (!editorIsRunningWork()) { (_a = getEditorRunButton()) === null || _a === void 0 ? void 0 : _a.click(); } }, stopWork() { var _a; if (editorIsRunningWork()) { (_a = getEditorRunButton()) === null || _a === void 0 ? void 0 : _a.click(); } }, saveWork() { return new Promise((resolve, reject) => { var _a; const editorWindow = getEditorWindow(); if (editorWindow == null) { reject(new Error("找不到编辑器窗口")); return; } const saveButton = Array.from((_a = editorWindow.document.querySelectorAll("button")) !== null && _a !== void 0 ? _a : []).find((button) => { var _a; return /^[\s]*保[\s]*存[\s]*$/.test((_a = button.textContent) !== null && _a !== void 0 ? _a : ""); }); if (saveButton == null) { reject(new Error("找不到保存按钮")); return; } saveButton.click(); const handle = setInterval(() => { if (saveButton.children.length == 1) { resolve(); clearInterval(handle); } }, 100); }); }, reopenWork() { const workName = getEditorWorkName(); if (workName == null) { throw new Error("找不到作品名称"); } const editorWindow = getEditorWindow(); if (editorWindow == null) { throw new Error("找不到编辑器窗口"); } const openButton = editorWindow.document.querySelector("[data-menu-id=\"open_cloud\"]"); if (openButton == null) { throw new Error("找不到打开按钮"); } openButton.click(); const handle = setInterval(() => { var _a; const worksElement = Array.from((_a = editorWindow.document.querySelectorAll(".ant-card")) !== null && _a !== void 0 ? _a : []); const workElements = worksElement.find((element) => { var _a; return ((_a = element.querySelector(".ant-card-meta-title")) === null || _a === void 0 ? void 0 : _a.textContent) == workName; }); if (workElements != null) { workElements.click(); clearInterval(handle); } }, 100); }, importWidget(widget) { return new Promise((resolve, reject) => { if (typeof widget == "string") { widget = new Blob([widget], { type: "text/javascript" }); } if (widget instanceof Blob) { widget = new File([widget], "widget.js", { type: "text/javascript" }); } if (!(widget instanceof File)) { reject(new Error("参数错误,widget 必须是 File 或 Blob 或字符串")); return; } const widgetFile = widget; const editorWindow = getEditorWindow(); if (editorWindow == null) { throw new Error("找不到编辑器窗口"); } const importButton = editorWindow.document.querySelector("[data-menu-id=\"import_extension\"]"); if (importButton == null) { reject(new Error("找不到自定义控件导入按钮")); return; } const originalCreateElement = editorWindow.document.createElement; editorWindow.document.createElement = function createElement(tagName, options) { const element = originalCreateElement.call(this, tagName, options); if (element instanceof HTMLInputElement) { element.addEventListener("click", (event) => { event.preventDefault(); }); editorWindow.document.createElement = originalCreateElement; setTimeout(() => { const dataTransfer = new DataTransfer(); dataTransfer.items.add(widgetFile); element.files = dataTransfer.files; element.dispatchEvent(new Event("change", { bubbles: true })); setTimeout(() => { resolve(); }, 150); }, 0); } return element; }; importButton.click(); }); }, } } };