slightning-coco-widget
Version:
SLIGHTNING 的 CoCo 控件框架。
129 lines (128 loc) • 5.35 kB
JavaScript
import { standardizeMethodBlock } from "../convert/standardize-types";
import { MethodBlockParam } from "../types";
import { ArrayType } from "./array-type";
import { ObjectType } from "./object-type";
export class MutatorType extends ArrayType {
constructor({ block, separators, separator, min, max, defaultNumber, transformMin, transformMax }) {
var _a;
const standardBlock = standardizeMethodBlock(block);
const propertiesType = {};
for (const item of standardBlock) {
if (typeof item == "object") {
propertiesType[item.key] = item.type;
}
}
const itemType = new ObjectType({ propertiesType });
super({ itemType });
this.block = standardBlock;
this.separators = separators !== null && separators !== void 0 ? separators : (typeof separator == "string" ? [separator] : []);
this.separator = (_a = separator !== null && separator !== void 0 ? separator : separators === null || separators === void 0 ? void 0 : separators.join(" ")) !== null && _a !== void 0 ? _a : "";
this.min = min !== null && min !== void 0 ? min : 2;
this.max = max !== null && max !== void 0 ? max : Infinity;
this.defaultNumber = defaultNumber !== null && defaultNumber !== void 0 ? defaultNumber : this.min;
this.transformMin = transformMin !== null && transformMin !== void 0 ? transformMin : this.min;
this.transformMax = transformMax !== null && transformMax !== void 0 ? transformMax : Math.min(this.min + 5, this.max);
}
toCreationProject1MethodParamValueTypes() {
const mutator = [];
let mutatorLastParam = null;
let mutatorLabelsAfterLastParam = [];
function mutatorAddText(text) {
if (mutatorLastParam == null) {
mutatorLabelsAfterLastParam.push(text);
}
else {
if (mutatorLastParam.label == null) {
mutatorLastParam.label = text;
}
else if (mutatorLastParam.label.endsWith("\n") ||
text.startsWith("\n")) {
mutatorLastParam.label += text;
}
else {
mutatorLastParam.label += " " + text;
}
}
}
const block = [...this.block, ...this.separators];
let i = block.length - 1;
for (; i >= 0; i--) {
const mutatorPart = block[i];
if (mutatorPart == undefined) {
continue;
}
if (mutatorPart == MethodBlockParam.BREAK_LINE) {
mutatorAddText("\n");
}
else if (typeof mutatorPart == "string") {
mutatorAddText(mutatorPart);
}
else {
mutatorLastParam = Object.assign({ key: mutatorPart.key }, mutatorPart.type.toCreationProject1MethodParamValueTypes());
if (mutatorLabelsAfterLastParam.length > 0) {
mutatorLastParam.labelAfter = mutatorLabelsAfterLastParam.join(" ");
mutatorLabelsAfterLastParam = [];
}
mutator.unshift(mutatorLastParam);
}
}
return {
valueType: "mutator",
mutator,
mutatorMin: this.min,
mutatorMax: this.max,
mutatorItem: this.defaultNumber
};
}
toCreationProject2MethodParamValueTypes() {
const mutator = [];
let mutatorLastParam = null;
let mutatorLabelsAfterLastParam = [];
function mutatorAddText(text) {
if (mutatorLastParam == null) {
mutatorLabelsAfterLastParam.push(text);
}
else {
if (mutatorLastParam.label == null) {
mutatorLastParam.label = text;
}
else if (mutatorLastParam.label.endsWith("\n") ||
text.startsWith("\n")) {
mutatorLastParam.label += text;
}
else {
mutatorLastParam.label += " " + text;
}
}
}
const block = [...this.block, ...this.separators];
let i = block.length - 1;
for (; i >= 0; i--) {
const mutatorPart = block[i];
if (mutatorPart == undefined) {
continue;
}
if (mutatorPart == MethodBlockParam.BREAK_LINE) {
mutatorAddText("\n");
}
else if (typeof mutatorPart == "string") {
mutatorAddText(mutatorPart);
}
else {
mutatorLastParam = Object.assign({ key: mutatorPart.key }, mutatorPart.type.toCreationProject2MethodParamValueTypes());
if (mutatorLabelsAfterLastParam.length > 0) {
mutatorLastParam.labelAfter = mutatorLabelsAfterLastParam.join(" ");
mutatorLabelsAfterLastParam = [];
}
mutator.unshift(mutatorLastParam);
}
}
return {
valueType: "mutator",
mutator,
mutatorMin: this.min,
mutatorMax: this.max,
mutatorItem: this.defaultNumber
};
}
}