slightning-coco-widget
Version:
SLIGHTNING 的 CoCo 控件框架。
137 lines (136 loc) • 4.34 kB
JavaScript
import * as stringify from "@slightning/anything-to-string";
import { betterToString, XMLEscape } from "../../utils";
import { TypeValidateError } from "./type-validate-error";
import { inlineTypeToString, typeToString, validate } from "./utils";
export class ArrayType {
constructor({ itemType, defaultValue } = {}) {
this.itemType = itemType;
this.defaultValue = defaultValue !== null && defaultValue !== void 0 ? defaultValue : inlineTypeToString(this);
}
validate(value) {
if (!Array.isArray(value)) {
throw new TypeValidateError(`不能将 ${betterToString(value)} 分配给 ${typeToString(this)}`, value, this);
}
if (this.itemType != null) {
const errors = [];
for (const [index, item] of Object.entries(errors)) {
try {
validate(`第 ${index} 项`, item, this.itemType);
}
catch (error) {
if (!(error instanceof TypeValidateError)) {
throw error;
}
errors.push(error);
}
}
if (errors.length != 0) {
throw new TypeValidateError(`不能将 ${betterToString(value)} 分配给 ${typeToString(this)}:\n` +
errors.map((error) => error.message
.split("\n")
.map((line) => ` ${line}`)
.join("\n")).join("\n"), value, this);
}
}
return true;
}
getSameDirectionChildren() {
return this.itemType == null ? [] : [{
key: "__slightning_coco_widget_array_item__",
label: "数组项",
type: this.itemType
}];
}
getReverseDirectionChildren() {
return [];
}
isVoid() {
return false;
}
typeToStringPrepare(config, context) {
if (this.itemType != null) {
new stringify.AnythingRule().prepare(this.itemType, config, context);
}
}
typeToString(config, context) {
let result = "列表";
if (this.itemType != null) {
result += `<${new stringify.AnythingRule().toString(this.itemType, config, context)}>`;
}
return result;
}
inlineTypeToStringPrepare(config, context) {
this.typeToStringPrepare(config, context);
}
inlineTypeToString(config, context) {
return this.typeToString(config, context);
}
toCoCoPropertyValueTypes() {
return {
valueType: ["string", "array"],
checkType: "string",
defaultValue: XMLEscape(typeof this.defaultValue == "string" ? this.defaultValue : JSON.stringify(this.defaultValue))
};
}
toCoCoMethodParamValueTypes() {
return {
valueType: ["string", "array"],
checkType: "string",
defaultValue: XMLEscape(typeof this.defaultValue == "string" ? this.defaultValue : JSON.stringify(this.defaultValue))
};
}
toCoCoMethodValueTypes() {
return {
valueType: "array"
};
}
toCoCoEventParamValueTypes() {
return {
valueType: "array"
};
}
toCreationProject1PropValueTypes() {
return {
valueType: "array",
defaultValue: this.defaultValue
};
}
toCreationProject1MethodParamValueTypes() {
return {
valueType: "array",
defaultValue: this.defaultValue
};
}
toCreationProject1MethodValueTypes() {
return {
valueType: "array"
};
}
toCreationProject1EmitParamValueTypes() {
return {
valueType: "array"
};
}
toCreationProject2PropValueTypes() {
return {
valueType: "array",
defaultValue: this.defaultValue
};
}
toCreationProject2MethodParamValueTypes() {
return {
valueType: "array",
defaultValue: this.defaultValue
};
}
toCreationProject2MethodValueTypes() {
return {
valueType: "array"
};
}
toCreationProject2EmitParamValueTypes() {
return {
valueType: "array"
};
}
}