UNPKG

@lcap/nasl

Version:

NetEase Application Specific Language

125 lines 4.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.viewMergeBlock = exports.genLogics = exports.genView = void 0; const nasl_concepts_1 = require("@lcap/nasl-concepts"); const transformBlock2Nasl_1 = require("./transformBlock2Nasl"); const nasl_utils_1 = require("@lcap/nasl-utils"); function genView(code, frameworkKind, parentElement) { const naslCode = (0, transformBlock2Nasl_1.tryTransformBlock2Nasl)(code); const blockView = naslCode.view; if (frameworkKind === 'vue2' || frameworkKind === 'vue3') { (0, nasl_utils_1.traverse)(({ node }) => { if (node.concept === 'ViewElement') { node.tag = (0, nasl_utils_1.Camel2kebab)(node.tag); } }, { node: blockView.elements[0] }, { mode: 'anyObject' }); } const node = nasl_concepts_1.ViewElement.from({ ...blockView.elements[0], }, parentElement, 'elements'); return { blockView, naslCode, node }; } exports.genView = genView; function genLogics(blockView, view, naslCode, app) { // 页面入参 blockView.params?.forEach((paramsOption) => { const param = nasl_concepts_1.Param.from({ ...paramsOption, }, view, 'params'); view.addParam(param); }); // 页面变量 blockView.variables?.forEach((variableOption) => { const variable = nasl_concepts_1.Variable.from({ ...variableOption, }, view, 'variables'); view.addVariable(variable); }); // 页面生命周期函数 blockView.bindEvents?.forEach((eventOption) => { const bindEvent = view.bindEvents.find((currentEventItem) => currentEventItem.name === eventOption.name); if (bindEvent) { eventOption.logics.forEach((logic) => { bindEvent.addLogic(logic); }); } else { const event = nasl_concepts_1.BindEvent.from({ ...eventOption, calleeName: '', logics: [...eventOption.logics], }, view, 'bindEvents'); view.addBindEvent(event); } }); // 页面逻辑 blockView.logics?.forEach((logicOption) => { const logic = nasl_concepts_1.Logic.from({ ...logicOption, }, view, 'logics'); view.addLogic(logic); }); // 全局逻辑 const logics = naslCode.json?.logics || []; logics.forEach((logicOption) => { const logic = nasl_concepts_1.Logic.from({ ...logicOption, }, app, 'logics'); app.addLogic(logic); // logic.traverseStrictChildren((logicItem: SyntaxNode) => { // if (isStrictCallQueryComponent(logicItem)) { // logicItem.saveStructure(); // } // }); }); // 全局数据结构 const structures = naslCode.json?.structures || []; if (Array.isArray(structures)) { structures.forEach((structureOption) => { const structure = nasl_concepts_1.Structure.from({ ...structureOption, }, app, 'structures'); app.addStructure(structure); }); } } exports.genLogics = genLogics; function viewMergeBlock({ app, view, code, nodePath, position, cb, }) { let targetNode = app.findNodeByPath(nodePath); let parentNode = position === 'append' ? targetNode : targetNode.parentNode; if (parentNode.concept === 'View') { parentNode = targetNode; if (position === 'insertBefore') { targetNode = parentNode.children[0]; } else if (position === 'insertAfter') { const len = parentNode.children.length; targetNode = parentNode.children[len - 1]; } } // 页面元素 if (!parentNode) { return; } // 页面元素 const frontendType = view?.getAncestor('FrontendType'); const { blockView, node, naslCode } = genView(code, frontendType?.frameworkKind, view); if (position === 'append') { parentNode.addViewElement(node); cb && cb(parentNode, node); } else { const index = targetNode.getIndexOfParent(); if (position === 'insertBefore') { parentNode.insertViewElementAt(node, index); } else if (position === 'insertAfter') { parentNode.insertViewElementAt(node, index + 1); } } // 页面变量、页面生命周期函数、页面逻辑、后端逻辑等 genLogics(blockView, view, naslCode, app); return node; } exports.viewMergeBlock = viewMergeBlock; //# sourceMappingURL=viewMergeBlock.js.map