diff --git a/.changeset/warm-chicken-impress.md b/.changeset/warm-chicken-impress.md new file mode 100644 index 0000000000..9ed222d09a --- /dev/null +++ b/.changeset/warm-chicken-impress.md @@ -0,0 +1,5 @@ +--- +'@sap-ux/cards-editor-middleware': patch +--- + +feat: Ensure UI5 2.x compliance diff --git a/packages/cards-editor-middleware/src/index.ts b/packages/cards-editor-middleware/src/index.ts index 744c7773cd..66ad521bf3 100644 --- a/packages/cards-editor-middleware/src/index.ts +++ b/packages/cards-editor-middleware/src/index.ts @@ -3,7 +3,7 @@ import type { MiddlewareParameters } from '@ui5/server'; import { json, Router as createRouter } from 'express'; import path, { join } from 'path'; import { promises } from 'fs'; -import { getWebappPath, FileName } from '@sap-ux/project-access'; +import { getWebappPath, FileName, type Manifest } from '@sap-ux/project-access'; import { render } from 'ejs'; import * as utils from './utilities'; import os from 'os'; @@ -13,6 +13,32 @@ export const enum ApiRoutes { cardsStore = '/cards/store', i18nStore = '/editor/i18n' } +export const DEFAULT_THEME = 'sap_horizon'; + +const UI5_LIBS = [ + 'sap.apf', + 'sap.base', + 'sap.chart', + 'sap.collaboration', + 'sap.f', + 'sap.fe', + 'sap.fileviewer', + 'sap.gantt', + 'sap.landvisz', + 'sap.m', + 'sap.ndc', + 'sap.ovp', + 'sap.rules', + 'sap.suite', + 'sap.tnt', + 'sap.ui', + 'sap.uiext', + 'sap.ushell', + 'sap.uxap', + 'sap.viz', + 'sap.webanalytics', + 'sap.zen' +]; /** * Check if a file exists. @@ -33,6 +59,29 @@ async function pathExists(path: string) { } } +/** + * Retrieves a comma-separated string of UI5 libraries from the provided manifest. + * It ensures that certain essential libraries are always included. + * + * @param {Partial} manifest - The manifest object containing UI5 library dependencies. + * @returns {string} A comma-separated string of UI5 library names. + */ +function getUI5Libs(manifest: Partial): string { + const libs = manifest['sap.ui5']?.dependencies?.libs ?? {}; + // add libs that should always be preloaded + libs['sap.m'] = {}; + libs['sap.ui.core'] = {}; + libs['sap.ushell'] = {}; + + return Object.keys(libs) + .filter((key) => { + return UI5_LIBS.some((substring) => { + return key === substring || key.startsWith(substring + '.'); + }); + }) + .join(','); +} + module.exports = async ({ resources }: MiddlewareParameters): Promise => { const router = createRouter(); router.use(json()); @@ -47,11 +96,19 @@ module.exports = async ({ resources }: MiddlewareParameters): Promise { const app = JSON.parse(await manifest.getString())['sap.app']; + const ui5libs = getUI5Libs(JSON.parse(await manifest.getString())); + const supportedThemes: string[] = (JSON.parse(await manifest.getString())['sap.ui5'] + ?.supportedThemes as []) ?? [DEFAULT_THEME]; + const ui5Theme = supportedThemes.includes(DEFAULT_THEME) ? DEFAULT_THEME : supportedThemes[0]; res.status(200).send( - render(await promises.readFile(join(__dirname, '../templates/editor.html'), 'utf-8'), { + render(await promises.readFile(join(__dirname, '../templates/flpSandbox.html'), 'utf-8'), { templateModel: { appTitle: app.title || 'Card Editor Preview', - component: app.id + component: app.id, + ui5: { + libs: ui5libs, + theme: ui5Theme + } } }) ); diff --git a/packages/cards-editor-middleware/templates/Init.js b/packages/cards-editor-middleware/templates/Init.js new file mode 100644 index 0000000000..d5b59eb449 --- /dev/null +++ b/packages/cards-editor-middleware/templates/Init.js @@ -0,0 +1,74 @@ +function parseUI5Version(version) { + const versionParts = version.split('.'); + const major = parseInt(versionParts[0], 10); + const minor = parseInt(versionParts[1], 10); + + return { major, minor }; +} + +function isLowerThanMinimalUi5Version(version, minVersion) { + if (version && minVersion) { + const minVersionParsed = { major: 1, minor: 121 }; + const ui5VersionParsed = parseUI5Version(version); + if (!isNaN(ui5VersionParsed.major) && !isNaN(ui5VersionParsed.minor)) { + if (ui5VersionParsed.major < minVersionParsed.major) { + return true; + } + if (ui5VersionParsed.major === minVersionParsed.major && ui5VersionParsed.minor < minVersionParsed.minor) { + return true; + } + } + } + return false; +} + +function addCardGenerationUserAction(oComponentInstance) { + sap.ui.require([ + "sap/cards/ap/generator/CardGenerator", + "sap/ushell/Container" + ], async (CardGenerator, Container) => { + const oRenderer = await Container.getServiceAsync("fiori2"); + if (oRenderer) { + var generateCardBtn = { + controlType: "sap.ushell.ui.launchpad.ActionItem", + bCurrentState: true, + oControlProperties: { + icon: "sap-icon://add", + id: "generate_card", + text: "Generate Card", + tooltip: "Generate Card", + press: function () { + CardGenerator.initializeAsync(oComponentInstance); + } + }, + bIsVisible: true + }; + oRenderer.addUserAction(generateCardBtn); + } + }); +} + +sap.ui.require(["sap/ushell/Container", "sap/m/MessageBox", "sap/ui/VersionInfo"], async (Container, MessageBox, VersionInfo) => { + Container.attachRendererCreatedEvent(function() { + Container.getServiceAsync('AppLifeCycle').then((serviceInstance) => { + serviceInstance.attachAppLoaded(async (event) => { + const sapCoreVersionInfo = await VersionInfo.load({ + library: "sap.ui.core" + }); + const sapCoreVersion = sapCoreVersionInfo?.version; + if (isLowerThanMinimalUi5Version(sapCoreVersion, "1.121")) { + MessageBox.error("Card Generation feature is not supported for the current UI5 version. Please use UI5 version 1.121 or higher."); + return; + } + var oCurrentApplication = serviceInstance.getCurrentApplication(); + var oComponentInstance = oCurrentApplication.componentInstance; + addCardGenerationUserAction(oComponentInstance); + }); + }); + }); + Container.createRendererInternal(undefined, true).then(function(oRenderer){ + oRenderer.placeAt("content"); + }); +}); + + diff --git a/packages/cards-editor-middleware/templates/editor.html b/packages/cards-editor-middleware/templates/editor.html deleted file mode 100644 index 358b6688c4..0000000000 --- a/packages/cards-editor-middleware/templates/editor.html +++ /dev/null @@ -1,136 +0,0 @@ - - - - - - - - - <%- templateModel.appTitle %> - - - - - - - - - - - - - - \ No newline at end of file diff --git a/packages/cards-editor-middleware/templates/flpSandbox.html b/packages/cards-editor-middleware/templates/flpSandbox.html new file mode 100644 index 0000000000..4a14b5cd3f --- /dev/null +++ b/packages/cards-editor-middleware/templates/flpSandbox.html @@ -0,0 +1,63 @@ + + + + + + + + + <%- templateModel.appTitle %> + + + + + + + + + + + + + + \ No newline at end of file