@lcap/nasl
Version:
NetEase Application Specific Language
393 lines • 12.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatSub = exports.formatItem = exports.formatTableSlots = exports.formatBasicCompose = exports.formatCustomSetter = exports.formatPropsMap = exports.attrKebab2Camel = exports.kebab2Camel = void 0;
/**
* 中划线格式 -转-> 驼峰格式
* @param name 原名称
* @return 转换后的名称
*/
const kebab2Camel = (name) => name.replace(/(?:^|-)([a-zA-Z0-9])/g, (m, $1) => $1.toUpperCase());
exports.kebab2Camel = kebab2Camel;
/**
* 中划线格式 -转-> 驼峰格式
* @param name 原名称
* @return 转换后的名称
*/
const attrKebab2Camel = (name) => name.replace(/(?:-)([a-zA-Z0-9])/g, (m, $1) => $1.toUpperCase());
exports.attrKebab2Camel = attrKebab2Camel;
// 可读属性转成map
const formatPropsMap = (jsonSchema, propKey, options) => {
const { frameworkKind } = options || {};
const excludeProps = {
'props': (prop) => {
if (!prop?.settable) {
return true;
}
if ([
'dataSource',
'dataSchema',
'sorting',
'linkType',
'hrefAndTo',
'target',
'source', // 穿梭框
'rules',
].includes(prop?.name)) {
return true;
}
if (/^{.*}$/.test(prop?.tsType)) {
return true;
}
if (/^\(\s*item\s*:\s*T\s*\)\s*=>\s*.+/.test(prop?.tsType)) {
return true;
}
return false;
},
'readableProps': () => false,
};
// FIXME 只支持Vue
const extraProps = frameworkKind?.startsWith('vue') ? {
props: [
{
name: '_if',
prop: '_if',
title: '显示条件',
settable: true,
},
],
readableProps: [
{
name: '_if',
prop: '_if',
title: '显示条件',
},
],
} : {};
const props = [
...(jsonSchema[propKey] || []),
...(extraProps[propKey] || []),
];
if (Array.isArray(props) && props.length > 0) {
for (let i = 0; i < props.length; i++) {
const prop = props[i];
// 一些需要过滤的prop
if (excludeProps[propKey](prop)) {
continue;
}
if (!jsonSchema[`${propKey}Map`]) {
jsonSchema[`${propKey}Map`] = {};
}
jsonSchema[`${propKey}Map`][prop.name] = prop;
if (!jsonSchema[`${propKey}Map`][prop.name].prop) {
jsonSchema[`${propKey}Map`][prop.name].prop = prop.name;
}
}
}
};
exports.formatPropsMap = formatPropsMap;
const formatCustomSetter = (attr) => {
if (attr.setter) {
return attr;
}
if (attr.options && !attr.display) {
attr.setter = {
concept: 'EnumSelectSetter',
options: attr.options,
};
}
else if (attr.display === 'capsules') {
attr.setter = {
concept: 'CapsulesSetter',
options: attr.options,
};
}
else if (attr.display === 'number' || attr.type === 'number') {
attr.setter = {
concept: 'NumberInputSetter',
placeholder: attr.place,
};
if (attr.min !== undefined) {
attr.setter.min = attr.min;
}
if (attr.max !== undefined) {
attr.setter.max = attr.max;
}
if (attr.precision !== undefined) {
attr.setter.precision = attr.precision;
}
if (attr.step !== undefined) {
attr.setter.step = attr.step;
}
}
else if (attr.display === 'property-select') {
attr.setter = {
concept: 'PropertySelectSetter',
};
}
else if (['icon', 'file-icon'].includes(attr.type)) {
attr.setter = {
concept: 'IconSetter',
};
}
else if (attr.type === 'boolean' && !attr.place) {
attr.setter = {
concept: 'SwitchSetter',
};
}
else if (!attr.display && attr.place) {
attr.setter = {
concept: 'InputSetter',
placeholder: attr.place,
};
}
else if (attr.type === 'image' || attr.type === 'link') {
attr.setter = {
concept: 'ImageSetter',
};
}
return attr;
};
exports.formatCustomSetter = formatCustomSetter;
function getMapItem(map, tag) {
const key = Object.keys(map).find((k) => tag?.endsWith(k));
if (!key) {
return null;
}
return map[key];
}
/**
* 组件库的api.ts没有定义compose字段,这里先补充下,待后续定义后优化
* 新的代码请不要这么写
* @param {*} attr 属性
* @param {*} newItem 组件
*/
const formatBasicCompose = (attr, newItem) => {
const otherPropsDataMap = {
'u-number-input': {
decimalPlaces: {
compose: [
{
key: 'places',
type: 'number',
min: 0,
},
{
key: 'omit',
type: 'switch',
suffix: '隐藏末尾0',
},
],
default: "{ places: '', omit: true }",
},
unit: {
compose: [
{
key: 'type',
type: 'select',
options: [
{
value: 'prefix',
title: '前缀',
},
{
value: 'suffix',
title: '后缀',
},
],
},
{
key: 'value',
type: 'text',
},
],
default: "{ type: 'prefix', value: '' }",
},
},
'van-stepper-new': {
decimalPlaces: {
compose: [
{
key: 'places',
type: 'number',
min: 0,
},
{
key: 'omit',
type: 'switch',
suffix: '隐藏末尾0',
},
],
default: "{ places: '', omit: true }",
},
unit: {
compose: [
{
key: 'type',
type: 'select',
options: [
{
value: 'prefix',
title: '前缀',
},
{
value: 'suffix',
title: '后缀',
},
],
},
{
key: 'value',
type: 'text',
},
],
default: "{ type: 'prefix', value: '' }",
},
},
};
const mapItem = getMapItem(otherPropsDataMap, newItem.name);
if (mapItem && mapItem[attr.name]) {
Object.assign(attr, mapItem[attr.name]);
}
};
exports.formatBasicCompose = formatBasicCompose;
/**
* 添加table的plus-empty
* 新的代码请不要这么写
* @param {*} slot
*/
const formatTableSlots = (attr) => {
const slotDataMap = {
'u-table-view-column': {
cell: {
'plus-empty': true,
},
editcell: {
'plus-empty': true,
'plus-empty-tag': 'editcell-plus-empty',
},
expandContent: {
'plus-empty': true,
'plus-empty-tag': 'expand-content-plus-empty',
},
expander: {
'plus-empty': true,
},
},
'u-table-view-column-dynamic': {
cell: {
'plus-empty': true,
},
},
};
const mapItem = getMapItem(slotDataMap, attr.kebabName);
if (mapItem) {
const slotData = mapItem;
(attr.slots || []).forEach((slot) => {
slotData[slot.name] && Object.assign(slot, slotData[slot.name]);
});
}
};
exports.formatTableSlots = formatTableSlots;
const formatItem = ({ jsonSchema, symbol, name, icon, scenes, majorVersion, downloadMethod, minorVersion, curMajorVersion, curMinorVersion, repoAddr, depDescription, activeVersion, }, map) => {
if (!curMajorVersion || !curMinorVersion) {
const versionList = activeVersion.split('.');
curMajorVersion = versionList[0];
majorVersion = versionList[0];
curMinorVersion = `${versionList[1]}.${versionList[2]}`;
minorVersion = `${versionList[1]}.${versionList[2]}`;
}
const schema = JSON.parse(jsonSchema || '{}');
const newItem = {
name: symbol,
title: name,
icon,
category: scenes,
majorVersion,
downloadMethod,
minorVersion,
curMajorVersion,
curMinorVersion,
version: `${curMajorVersion}.${curMinorVersion}`,
registry: repoAddr,
vusionDependencies: depDescription,
};
newItem.tagName = newItem.name.replace(/^.+?\//, '').replace(/\.vue$/, '');
newItem.componentName = (0, exports.kebab2Camel)(newItem.tagName);
try {
newItem.screenshots = (schema.screenShot || '').split(',').filter((i) => i);
}
catch (error) {
newItem.screenshots = [];
}
try {
newItem.drawings = (schema.drawings || '').split(',').filter((i) => i);
}
catch (error) {
newItem.drawings = [];
}
try {
const blocks = typeof schema.blocks === 'string' ? JSON.parse(schema.blocks || '{}') : schema.blocks;
newItem.blocks = schema.blocks
? blocks
: [
{
title: '默认',
code: `<template><${newItem.tagName}></${newItem.tagName}></template>`,
},
];
}
catch (error) {
newItem.blocks = [];
}
// 新usage.json中将图片放到了blocks中,扩展组件做兼容
if (newItem.drawings.length) {
newItem.blocks.forEach((block, index) => {
block.drawing = block.drawing || newItem.drawings[index];
});
}
if (newItem.screenshots.length) {
newItem.blocks.forEach((block, index) => {
block.screenshot = block.screenshot || newItem.screenshots[index];
});
}
newItem.props = schema.props || schema.attrs || [];
newItem.props.forEach((prop) => {
if (prop['designer-value']) {
prop.designerValue = prop['designer-value'];
delete prop['designer-value'];
}
(0, exports.formatCustomSetter)(prop);
});
map[newItem.name] = newItem;
return newItem;
};
exports.formatItem = formatItem;
const formatSub = (item, map, useName = (item) => item.kebabName || item.name) => {
item.name = useName(item);
const newItem = {
...item,
};
newItem.tagName = newItem.name.replace(/^.+?\//, '').replace(/\.vue$/, '');
newItem.componentName = newItem.kebabName
? newItem.name : (0, exports.kebab2Camel)(newItem.tagName);
newItem.props = newItem.props || newItem.attrs || [];
// 扩展组件也用了formatSub,所以这里需要转一下
newItem.props.forEach((prop) => {
prop.name = (0, exports.attrKebab2Camel)(prop.name);
if (prop['designer-value']) {
prop.designerValue = prop['designer-value'];
delete prop['designer-value'];
}
(0, exports.formatCustomSetter)(prop);
// 暂时代码,待后续组件库定义后优化
(0, exports.formatBasicCompose)(prop, newItem);
});
(0, exports.formatTableSlots)(newItem);
// FIXME 判断是否React item.name以大写字母开头的是react
const frameworkKind = item.name[0] === item.name[0].toUpperCase() ? 'react' : 'vue2';
(0, exports.formatPropsMap)(newItem, 'readableProps', { frameworkKind: frameworkKind });
(0, exports.formatPropsMap)(newItem, 'props', { frameworkKind: frameworkKind });
map[newItem.name] = newItem;
};
exports.formatSub = formatSub;
//# sourceMappingURL=format.js.map