UNPKG

@alilc/build-plugin-alt

Version:

build-scripts plugin template for developers

130 lines (129 loc) 5.01 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getPageSchema = exports.resetSchema = exports.saveSchema = exports.preview = exports.SAVE_KEY = void 0; const lowcode_engine_1 = require("@alilc/lowcode-engine"); const next_1 = require("@alifd/next"); exports.SAVE_KEY = `projectSchema_${PACKAGE_NAME}`; console.log(exports.SAVE_KEY); const preview = () => { (0, exports.saveSchema)(); setTimeout(() => { window.open('./preview.html'); }, 500); }; exports.preview = preview; const saveSchema = () => { window.localStorage.setItem(exports.SAVE_KEY, JSON.stringify(lowcode_engine_1.project.exportSchema())); const packages = lowcode_engine_1.material.getAssets().packages; window.localStorage.setItem('packages', JSON.stringify(packages)); next_1.Message.success('成功保存到本地'); }; exports.saveSchema = saveSchema; const resetSchema = () => __awaiter(void 0, void 0, void 0, function* () { var _a; const projectSchema = JSON.parse(window.localStorage.getItem(exports.SAVE_KEY) || `{ "componentName": "Page" }`); projectSchema.componentsTree = [{ componentName: 'Page' }]; window.localStorage.setItem(exports.SAVE_KEY, JSON.stringify(projectSchema)); (_a = lowcode_engine_1.project.getCurrentDocument()) === null || _a === void 0 ? void 0 : _a.importSchema({ componentName: 'Page', fileName: 'sample', }); next_1.Message.success('成功重置页面'); }); exports.resetSchema = resetSchema; const getPageSchema = (type) => __awaiter(void 0, void 0, void 0, function* () { var _b; const schema = JSON.parse(window.localStorage.getItem(exports.SAVE_KEY) || '{}'); const pageSchema = (_b = schema === null || schema === void 0 ? void 0 : schema.componentsTree) === null || _b === void 0 ? void 0 : _b[0]; if (pageSchema) { return pageSchema; } const initialSchema = yield request('./schema.json'); if (type === 'setter') { initialSchema.children.push({ "componentName": "BuiltInComp", }); } return initialSchema; }); exports.getPageSchema = getPageSchema; function request(dataAPI, method = 'GET', data, headers, otherProps) { return new Promise((resolve, reject) => { if (otherProps && otherProps.timeout) { setTimeout(() => { reject(new Error('timeout')); }, otherProps.timeout); } fetch(dataAPI, Object.assign({ method, credentials: 'include', headers, body: data }, otherProps)) .then((response) => { switch (response.status) { case 200: case 201: case 202: return response.json(); case 204: if (method === 'DELETE') { return { success: true, }; } else { return { __success: false, code: response.status, }; } case 400: case 401: case 403: case 404: case 406: case 410: case 422: case 500: return response .json() .then((res) => { return { __success: false, code: response.status, data: res, }; }) .catch(() => { return { __success: false, code: response.status, }; }); default: return null; } }) .then((json) => { if (json && json.__success !== false) { resolve(json); } else { delete json.__success; reject(json); } }) .catch((err) => { reject(err); }); }); }