UNPKG

slightning-coco-widget

Version:

SLIGHTNING 的 CoCo 控件框架。

122 lines (121 loc) 3.63 kB
import { betterToString } from "../../utils"; import { TypeValidateError } from "./type-validate-error"; import { typeToString } from "./utils"; export var StringEnumInputType; (function (StringEnumInputType) { StringEnumInputType["DROPDOWN"] = "INLINE"; StringEnumInputType["OPTION_SWITCH"] = "OPTION_SWITCH"; })(StringEnumInputType || (StringEnumInputType = {})); export class StringEnumType { constructor(props) { var _a; if (Array.isArray(props)) { props = { entries: props }; } this.entries = props.entries.map((entry) => { if (typeof entry == "string") { return { label: entry, value: entry }; } else if (Array.isArray(entry)) { return { label: entry[0], value: entry[1] }; } else { return entry; } }); this.inputType = (_a = props.inputType) !== null && _a !== void 0 ? _a : StringEnumInputType.DROPDOWN; // @ts-ignore this.valueToLabelMap = {}; for (const entry of this.entries) { this.valueToLabelMap[entry.value] = entry.label; } this.values = this.entries.map((entry) => entry.value); } validate(value) { // @ts-ignore if (typeof value != "string" || !this.values.includes(value)) { throw new TypeValidateError(`不能将 ${betterToString(value)} 分配给 ${typeToString(this)}`, value, this); } return true; } getSameDirectionChildren() { return []; } getReverseDirectionChildren() { return []; } isVoid() { return this.entries.length != 0; } typeToString() { return this.values.length == 0 ? "空" : `(${this.values.map((value) => JSON.stringify(value)).join(" | ")})`; } inlineTypeToString() { return this.typeToString(); } toCoCoPropertyValueTypes() { return { editorType: this.inputType == StringEnumInputType.DROPDOWN ? undefined : "OptionSwitch", dropdown: this.entries }; } toCoCoMethodParamValueTypes() { return { dropdown: this.entries }; } toCoCoMethodValueTypes() { return { valueType: "string" }; } toCoCoEventParamValueTypes() { return { valueType: "string" }; } toCreationProject1PropValueTypes() { return { valueType: "dropdown", dropdown: this.entries.map((entry) => [entry.label, entry.value]) }; } toCreationProject1MethodParamValueTypes() { return { valueType: "dropdown", dropdown: this.entries.map((entry) => [entry.label, entry.value]) }; } toCreationProject1MethodValueTypes() { return { valueType: "string" }; } toCreationProject1EmitParamValueTypes() { return { valueType: "string" }; } toCreationProject2PropValueTypes() { return { valueType: "dropdown", dropdown: this.entries.map((entry) => [entry.label, entry.value]) }; } toCreationProject2MethodParamValueTypes() { return { valueType: "dropdown", dropdown: this.entries.map((entry) => [entry.label, entry.value]) }; } toCreationProject2MethodValueTypes() { return { valueType: "string" }; } toCreationProject2EmitParamValueTypes() { return { valueType: "string" }; } }