UNPKG

slightning-coco-widget

Version:

SLIGHTNING 的 CoCo 控件框架。

242 lines (241 loc) 11.8 kB
import { traverseTypes } from "../decorators"; import { IntegerType, MutatorType, NumberType } from "../type"; import { Color, MethodBlockParam } from "../types"; export function convertToCreationProject1(types, widget) { return [typesToCreationProject1(types), widget]; } export function typesToCreationProject1(types) { var _a; const defaultPropertiesValues = {}; const result = { type: types.type, label: types.info.title, icon: types.info.icon, category: (_a = types.info.category) !== null && _a !== void 0 ? _a : undefined, visibleWidget: types.options.visible, global: types.options.global, default: defaultPropertiesValues, props: [], methods: [], emits: [] }; const labels = []; let showLine = false; let addSpace = false; traverseTypes(types, { PropertyGroup: { enter(node) { if (node.value.label != null) { labels.push(node.value.label); showLine = true; } addSpace = true; }, exit(node) { if (node.value.label != null) { labels.pop(); showLine = true; } addSpace = true; } }, PropertyTypes(node) { if (node.value.key == "__width" && (node.value.type instanceof IntegerType || node.value.type instanceof NumberType)) { defaultPropertiesValues.width = node.value.type.defaultValue; } else if (node.value.key == "__height" && (node.value.type instanceof IntegerType || node.value.type instanceof NumberType)) { defaultPropertiesValues.height = node.value.type.defaultValue; } else { result.props.push(Object.assign(Object.assign({ key: node.value.key, label: node.value.label }, node.value.type.toCreationProject1PropValueTypes()), { noBlock: (() => { var _a, _b, _c, _d, _e; if (((_b = (_a = node.value.blockOptions) === null || _a === void 0 ? void 0 : _a.get) !== null && _b !== void 0 ? _b : true == true) && ((_d = (_c = node.value.blockOptions) === null || _c === void 0 ? void 0 : _c.set) !== null && _d !== void 0 ? _d : true == true)) { return false; } else if (((_e = node.value.blockOptions) === null || _e === void 0 ? void 0 : _e.get) == false && node.value.blockOptions.set == false) { return true; } else { throw new Error(`无法将属性 ${node.value.label} 转为 Creation Project 类型`); } })() })); } }, MethodGroup: { enter(node) { if (node.value.label != null) { labels.push(node.value.label); showLine = true; } addSpace = true; }, exit(node) { if (node.value.label != null) { labels.pop(); showLine = true; } addSpace = true; } }, MethodTypes(node) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l; const { value: method } = node; if (method.throws != null && !(method.throws.isVoid())) { throw new Error(`无法将方法 ${method.label} 的抛出类型转为 Creation Project 类型`); } const deprecated = (_b = (_a = method.deprecated) !== null && _a !== void 0 ? _a : node.blockOptions.deprecated) !== null && _b !== void 0 ? _b : false; const transformed = Object.assign(Object.assign({ key: method.key, tipBefore: "", tipAfter: "", params: [], noPs: !((_c = node.blockOptions.previousStatement) !== null && _c !== void 0 ? _c : true), noNs: !((_d = node.blockOptions.nextStatement) !== null && _d !== void 0 ? _d : true) }, (_e = method.returns) === null || _e === void 0 ? void 0 : _e.toCreationProject1MethodValueTypes()), { tooltip: (_f = method.tooltip) !== null && _f !== void 0 ? _f : undefined, color: deprecated ? Color.GREY : (_g = node.blockOptions.color) !== null && _g !== void 0 ? _g : undefined, flyoutOptions: { line: typeof showLine == "string" ? showLine : showLine ? labels.join("·") : undefined } }); if (typeof deprecated == "string") { if (transformed.tooltip == null) { transformed.tooltip = `${deprecated}`; } else { transformed.tooltip = `${deprecated}\n\n${transformed.tooltip}`; } } else if (deprecated) { if (transformed.tooltip == null) { transformed.tooltip = `该方法已弃用,并且可能在未来版本中移除,请尽快迁移到其他方法`; } else { transformed.tooltip = `该方法已弃用,并且可能在未来版本中移除,请尽快迁移到其他方法\n\n${transformed.tooltip}`; } } const inline = (_h = node.blockOptions.inline) !== null && _h !== void 0 ? _h : true; if (inline || method.block.includes(MethodBlockParam.BREAK_LINE)) { let lastParam = null; let labelsAfterLastParam = []; function addText(text) { if (lastParam == null) { labelsAfterLastParam.unshift(text); } else { if (lastParam.label == null) { lastParam.label = text; } else if (text.endsWith("\n") || lastParam.label.startsWith("\n")) { lastParam.label = text + lastParam.label; } else { lastParam.label = text + " " + lastParam.label; } } } let i = method.block.length - 1; for (; i >= 0; i--) { const part = method.block[i]; if (part == undefined) { continue; } if (part == MethodBlockParam.THIS) { break; } else if (part == MethodBlockParam.METHOD) { addText(method.label); } else if (part == MethodBlockParam.BREAK_LINE) { addText(inline ? "" : "\n"); } else if (typeof part == "string") { addText(part); } else { lastParam = Object.assign({ key: part.key }, part.type.toCreationProject1MethodParamValueTypes()); if (labelsAfterLastParam.length > 0) { lastParam.labelAfter = labelsAfterLastParam.join(" "); labelsAfterLastParam = []; } transformed.params.unshift(lastParam); } } if (method.block[i] != MethodBlockParam.THIS) { throw new Error(`方法 ${method.label} 缺少 this 参数`); } if (labelsAfterLastParam.length > 0) { transformed.label = labelsAfterLastParam.join(" "); } i--; let labelsBeforeThis = []; for (; i >= 0; i--) { const part = method.block[i]; if (part == undefined) { continue; } if (part == MethodBlockParam.THIS) { throw new Error(`方法只能有一个 this 参数,而方法 ${method.label} 有多个 this 参数`); } else if (part == MethodBlockParam.METHOD) { labelsBeforeThis.unshift(method.label); } else if (part == MethodBlockParam.BREAK_LINE) { labelsBeforeThis.unshift(inline ? "" : "\n"); } else if (typeof part == "string") { labelsBeforeThis.unshift(part); } else { throw new Error(`方法 ${method.label} 的积木 this 参数前存在他参数,不能将其转为 Creation Project 类型`); } } if (deprecated != false) { labelsBeforeThis.unshift("[已弃用]"); } transformed.tipBefore = labelsBeforeThis.join(" ").replace(/( )?\n( )?/g, "\n"); } else { if (deprecated != false) { transformed.tipBefore = "[已弃用]"; } transformed.label = method.label; for (const part of method.block) { if (typeof part != "object") { continue; } if (part.type instanceof MutatorType) { const mutatorValueType = part.type.toCreationProject1MethodParamValueTypes(); for (const mutatorParam of (_j = mutatorValueType.mutator) !== null && _j !== void 0 ? _j : []) { mutatorParam.label = `\n${(_k = mutatorParam.label) !== null && _k !== void 0 ? _k : ""}`; } transformed.params.push(Object.assign({ key: part.key }, mutatorValueType)); } else { transformed.params.push(Object.assign({ key: part.key, label: `\n${part.label}` }, part.type.toCreationProject1MethodParamValueTypes())); } } } if (addSpace != false) { (_l = transformed.flyoutOptions) !== null && _l !== void 0 ? _l : (transformed.flyoutOptions = {}); transformed.flyoutOptions.gap = typeof addSpace == "number" ? addSpace : 40; addSpace = false; } showLine = false; result.methods.push(transformed); }, EventTypes(node) { var _a, _b; if (node.value.subTypes != null) { throw new Error("无法将事件的子类型转为 Creation Project 类型"); } const deprecated = (_b = (_a = node.value.deprecated) !== null && _a !== void 0 ? _a : node.blockOptions.deprecated) !== null && _b !== void 0 ? _b : false; result.emits.push({ key: node.value.key, label: deprecated != false ? `[已弃用] ${node.value.label}` : node.value.label, params: node.value.params.map((param) => { return Object.assign({ key: param.key, label: param.label }, param.type.toCreationProject1EmitParamValueTypes()); }) }); }, BlockBoxOptions(node) { if (node.value.space != null) { addSpace = node.value.space; } if (node.value.line != null) { showLine = node.value.line; } } }); return result; }