UNPKG

slightning-coco-widget

Version:

SLIGHTNING 的 CoCo 控件框架。

149 lines (148 loc) 5.14 kB
import { BooleanType, NumberType, StringEnumType, StringType } from "../type"; import { BlockType } from "../types"; export function standardizeTypes(types) { return Object.assign(Object.assign({}, types), { properties: standardizeProperties(types.properties), methods: [ ...types.events.map(standardizeEvent), { space: 40 }, ...standardizeMethods(types.methods) ], events: [] }); } export function standardizeProperties(properties) { var _a; const result = []; for (const property of properties) { if ("contents" in property) { result.push(Object.assign(Object.assign({}, property), { contents: standardizeProperties(property.contents) })); continue; } if (Array.isArray(property)) { result.push({ key: property[0], label: property[1], type: inferType(property[2]), blockOptions: (_a = property[3]) === null || _a === void 0 ? void 0 : _a.blockOptions }); } else { result.push(property); } } return result; } export function standardizeMethods(methods) { const result = []; for (const method of methods) { if ("contents" in method) { result.push(Object.assign(Object.assign({}, method), { contents: standardizeMethods(method.contents) })); } else if (Array.isArray(method)) { if (!(method[0] instanceof BlockType)) { method.unshift(BlockType.METHOD); } switch (method[0]) { case BlockType.METHOD: const standardized = Object.assign({ type: BlockType.METHOD, key: method[1], label: method[2], block: standardizeMethodBlock(method[3]), returns: inferType(method[4]) }, method[5]); result.push(standardized); break; case BlockType.EVENT: result.push(standardizeEvent(method)); break; } } else if ("key" in method) { if (method.type == null || method.type == BlockType.METHOD) { result.push(Object.assign(Object.assign({}, method), { type: BlockType.METHOD, block: standardizeMethodBlock(method.block) })); } else { result.push(standardizeEvent(method)); } } else { result.push(method); } } return result; } export function standardizeMethodBlock(block) { const result = []; for (const item of block) { if (typeof item == "string") { result.push(item); } else if (Array.isArray(item)) { result.push({ key: item[0], label: item[1], type: inferType(item[2]) }); } else { result.push(item); } } return result; } export function standardizeEvent(event) { var _a; if (Array.isArray(event)) { if (event[0] instanceof BlockType) { event = event.slice(1); } if (Array.isArray(event[3])) { return Object.assign({ type: BlockType.EVENT, key: event[0], label: event[1], subTypes: event[2].map(standardizeEventSubType), params: event[3].map(standardizeEventParam) }, event[4]); } else { return Object.assign({ type: BlockType.EVENT, key: event[0], label: event[1], params: event[2].map(standardizeEventParam) }, event[3]); } } else { return Object.assign(Object.assign({}, event), { type: BlockType.EVENT, subTypes: (_a = event.subTypes) === null || _a === void 0 ? void 0 : _a.map(standardizeEventSubType), params: event.params.map(standardizeEventParam) }); } } export function standardizeEventSubType(subType) { if (Array.isArray(subType)) { return { key: subType[0], dropdown: subType[1].map(standardizeDropdownItem) }; } return Object.assign(Object.assign({}, subType), { dropdown: subType.dropdown.map(standardizeDropdownItem) }); } export function standardizeEventParam(param) { if (Array.isArray(param)) { return { key: param[0], label: param[1], type: inferType(param[2]) }; } else { return param; } } export function standardizeDropdownItem(item) { if (Array.isArray(item)) { return { label: item[0], value: item[1] }; } return item; } function inferType(typeOrDefaultValue) { switch (typeof typeOrDefaultValue) { case "string": return new StringType(typeOrDefaultValue); case "number": return new NumberType(typeOrDefaultValue); case "boolean": return new BooleanType(typeOrDefaultValue); default: if (Array.isArray(typeOrDefaultValue)) { return new StringEnumType(typeOrDefaultValue); } return typeOrDefaultValue; } }