slightning-coco-widget
Version:
SLIGHTNING 的 CoCo 控件框架。
111 lines (110 loc) • 4.96 kB
JavaScript
import { capitalize, excludeBoolean } from "../../utils";
import { VoidType } from "../type/void-type";
import { BlockType, BUILD_IN_PROPERTIES, Color, MethodBlockParam } from "../types";
import { traverseTypes } from "./utils";
export function generateBlockForProperties(types, widget) {
const gettersGroup = {
label: "获取",
contents: []
};
const setterGroup = {
label: "设置",
contents: []
};
const propertyBlockGroup = {
label: "属性",
blockOptions: {
color: Color.PINK
},
contents: [gettersGroup, setterGroup]
};
let currentGettersGroup = gettersGroup;
let gettersGroupStack = [];
let currentSettersGroup = setterGroup;
let settersGroupStack = [];
traverseTypes(types, {
PropertyGroup: {
enter(node) {
var _a, _b;
gettersGroupStack.push(currentGettersGroup);
currentGettersGroup = {
label: node.value.label,
blockOptions: excludeBoolean((_a = node.value.blockOptions) === null || _a === void 0 ? void 0 : _a.get),
contents: []
};
settersGroupStack.push(currentSettersGroup);
currentSettersGroup = {
label: node.value.label,
blockOptions: excludeBoolean((_b = node.value.blockOptions) === null || _b === void 0 ? void 0 : _b.set),
contents: []
};
},
exit() {
currentGettersGroup = gettersGroupStack.pop();
currentSettersGroup = settersGroupStack.pop();
}
},
PropertyTypes(node) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
if (BUILD_IN_PROPERTIES.includes(node.value.key)) {
return;
}
if (((_a = node.blockOptions) === null || _a === void 0 ? void 0 : _a.get) != false) {
const propertyGetBlockKey = (_d = (_c = excludeBoolean((_b = node.blockOptions) === null || _b === void 0 ? void 0 : _b.get)) === null || _c === void 0 ? void 0 : _c.key) !== null && _d !== void 0 ? _d : `get${capitalize(node.value.key)}`;
currentGettersGroup.contents.push({
type: BlockType.METHOD,
key: propertyGetBlockKey,
label: `获取 ${node.value.label}`,
block: [
MethodBlockParam.THIS, node.value.label
],
returns: node.value.type,
blockOptions: excludeBoolean((_e = node.value.blockOptions) === null || _e === void 0 ? void 0 : _e.get)
});
if (widget.prototype[propertyGetBlockKey] == null) {
Object.defineProperty(widget.prototype, propertyGetBlockKey, {
value: function () {
return this[node.value.key];
},
writable: true,
enumerable: false,
configurable: true
});
}
}
if (((_f = node.blockOptions) === null || _f === void 0 ? void 0 : _f.set) != false) {
const propertySetBlockKey = (_j = (_h = excludeBoolean((_g = node.blockOptions) === null || _g === void 0 ? void 0 : _g.set)) === null || _h === void 0 ? void 0 : _h.key) !== null && _j !== void 0 ? _j : `set${capitalize(node.value.key)}`;
currentSettersGroup.contents.push({
type: BlockType.METHOD,
key: propertySetBlockKey,
label: `设置 ${node.value.label}`,
block: [
"设置", MethodBlockParam.THIS, "的", node.value.label, "为", {
key: "value",
label: node.value.label,
type: node.value.type
}
],
returns: new VoidType(),
blockOptions: excludeBoolean((_k = node.value.blockOptions) === null || _k === void 0 ? void 0 : _k.set)
});
if (widget.prototype[propertySetBlockKey] == null) {
Object.defineProperty(widget.prototype, propertySetBlockKey, {
value: function (value) {
this[node.value.key] = value;
},
writable: true,
enumerable: false,
configurable: true
});
}
}
node.value.blockOptions = {
get: false,
set: false
};
}
});
types.methods.push(propertyBlockGroup);
return [types, widget];
}