import type View from 'sap/ui/core/mvc/View'; import type UI5Element from 'sap/ui/core/Element'; import type RuntimeAuthoring from 'sap/ui/rta/RuntimeAuthoring'; import AddXMLAtExtensionPoint from 'sap/ui/rta/plugin/AddXMLAtExtensionPoint'; import CommandFactory from 'sap/ui/rta/command/CommandFactory'; import type { ExternalAction } from '@sap-ux-private/control-property-editor-common'; import { addExtensionPoint } from '@sap-ux-private/control-property-editor-common'; import type { Deferred } from './utils.js'; import { createDeferred } from './utils.js'; import { DialogFactory, DialogNames } from './dialog-factory.js'; import { CommunicationService } from '../cpe/communication-service.js'; type ActionService = { execute: (controlId: string, actionId: string) => void; }; type DeferredExtPointData = { fragmentPath: string; extensionPointName: string | undefined; }; export interface ExtensionPointInfo { name: string; index?: number; view?: View; createdControls: string[]; fragmentId?: string; aggregation?: string[]; aggregationName?: string; defaultContent: UI5Element[]; targetControl?: UI5Element; } export interface ExtensionPointData { name: string; deferred: Deferred; info: ExtensionPointInfo[]; } export default class ExtensionPointService { private readonly actionId = 'CTX_ADDXML_AT_EXTENSIONPOINT'; private selectedExtensionPointName: string; /** * @param rta Runtime Authoring */ constructor(private readonly rta: RuntimeAuthoring) {} /** * Initializes communication with CPE, and the extension point plugin. * */ public init() { this.initPlugin(); CommunicationService.subscribe(async (action: ExternalAction): Promise => { if (addExtensionPoint.match(action)) { try { const { controlId, name } = action.payload; const suffix = `--${name}`; const baseControlId = controlId.endsWith(suffix) ? controlId.slice(0, -suffix.length) : controlId; const service = await this.rta.getService('action'); service.execute(baseControlId, this.actionId); this.selectedExtensionPointName = name; } catch { throw new Error(`Failed to execute service with actionId: ${this.actionId}`); } } }); } /** * Initializes Add XML at Extension Point plugin and adds it to the default RTA plugins. */ public initPlugin() { const flexSettings = this.rta.getFlexSettings(); const commandFactory = new CommandFactory({ flexSettings }); const plugin = new AddXMLAtExtensionPoint({ commandFactory, fragmentHandler: async (overlay: UI5Element, info: ExtensionPointInfo[]) => await this.fragmentHandler(overlay, info) }); const plugins = this.rta.getPlugins(); plugins.addXMLAtExtensionPoint = plugin; this.rta.setPlugins(plugins); } /** * Handler function for AddXMLAtExtensionPoint plugin. * * @param overlay UI5 Element overlay * @param info Extension point data from the plugin * @returns Deferred extension point data that is provided to the plugin */ public async fragmentHandler(overlay: UI5Element, info: ExtensionPointInfo[]): Promise { const deferred = createDeferred(); const name = this.selectedExtensionPointName; await DialogFactory.createDialog(overlay, this.rta, DialogNames.ADD_FRAGMENT_AT_EXTENSION_POINT, { name, info, deferred }); this.selectedExtensionPointName = ''; return deferred.promise; } }