UNPKG

@silexlabs/grapesjs-data-source

Version:
115 lines 5.06 kB
"use strict"; /* * Silex website builder, free/libre no-code tool for makers. * Copyright (c) 2023 lexoyo and Silex Labs foundation * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.getPageQuery = getPageQuery; exports.buildPageQueries = buildPageQueries; const ExpressionTree_1 = require("./ExpressionTree"); const dataSourceManager_1 = require("./dataSourceManager"); const dataSourceRegistry_1 = require("./dataSourceRegistry"); const utils_1 = require("../utils"); const expressionEvaluator_1 = require("./expressionEvaluator"); /** * Get page query for both preview and production use * Used by preview system and 11ty site generation * @param page - The page to get queries for * @param editor - The GrapesJS editor instance * @param dataTree - The DataTree instance to use for query generation */ function getPageQuery(page, editor) { const manager = (0, dataSourceManager_1.getManager)(); const expressions = (0, ExpressionTree_1.getPageExpressions)(manager, page); const dataSources = (0, dataSourceRegistry_1.getAllDataSources)(); return dataSources .map(ds => { if (!ds.isConnected()) { console.error('The data source is not yet connected, the value for this page can not be loaded'); return { dataSourceId: ds.id.toString(), query: '', }; } const dsExpressions = expressions // Resolve all states .map((componentExpression) => ({ component: componentExpression.component, expression: componentExpression.expression.flatMap((token) => { switch (token.type) { case 'property': case 'filter': return token; case 'state': { const resolved = (0, expressionEvaluator_1.resolveStateExpression)(token, componentExpression.component, []); if (!resolved) { editor.runCommand('notifications:add', { type: 'error', group: utils_1.NOTIFICATION_GROUP, message: `Unable to resolve state ${JSON.stringify(token)}. State defined on component ${(0, utils_1.getComponentDebug)(componentExpression.component)}`, componentId: componentExpression.component.getId(), }); throw new Error(`Unable to resolve state ${JSON.stringify(token)}. State defined on component ${(0, utils_1.getComponentDebug)(componentExpression.component)}`); } return resolved; } } }), })) // Keep only the expressions for the current data source .filter((componentExpression) => { const e = componentExpression.expression; if (e.length === 0) return false; // We resolved all states // An expression can not start with a filter // So this is a property const first = e[0]; // Keep only the expressions for the current data source return (first === null || first === void 0 ? void 0 : first.dataSourceId) === ds.id; }); const trees = (0, ExpressionTree_1.toTrees)(manager, dsExpressions, ds.id); if (trees.length === 0) { return { dataSourceId: ds.id.toString(), query: '', }; } const query = ds.getQuery(trees); return { dataSourceId: ds.id.toString(), query, }; }) .filter(obj => !!obj.query) .reduce((acc, { dataSourceId, query }) => { acc[dataSourceId] = query; return acc; }, {}); } /** * Build queries for multiple pages * Useful for batch operations like site generation * @param pages - Array of pages to build queries for * @param editor - The GrapesJS editor instance * @param dataTree - The DataTree instance to use for query generation */ function buildPageQueries(pages, editor) { return pages.reduce((acc, page) => { acc[page.getId()] = getPageQuery(page, editor); return acc; }, {}); } //# sourceMappingURL=queryBuilder.js.map