import type { ConfigurationValue, ExternalAction, PendingChange, PendingGenericChange, SavedChange, SavedControlChange, SavedGenericChange, UnknownSavedChange } from '@sap-ux-private/control-property-editor-common'; import { changeProperty, changeStackModified, deletePropertyChanges, FlexChangesEndPoints, GENERIC_CHANGE_KIND, MessageBarType, PENDING_CHANGE_TYPE, propertyChangeFailed, reloadApplication, save, setApplicationRequiresReload, UNKNOWN_CHANGE_KIND } from '@sap-ux-private/control-property-editor-common'; import Log from 'sap/base/Log'; import type Event from 'sap/ui/base/Event'; import UI5Element from 'sap/ui/core/Element'; import Change, { ChangeDefinition } from 'sap/ui/fl/Change'; import type FlexCommand from 'sap/ui/rta/command/FlexCommand'; import { getTextBundle } from '../../i18n.js'; import { setAdditionalChangeInfo } from '../../utils/additional-change-info.js'; import { getChangeDefinition, getFlexChangeList, getFlexXMLChangeList } from '../../utils/changes.js'; import { getControlById, isA } from '../../utils/core.js'; import { getError } from '../../utils/error.js'; import { sendInfoCenterMessage } from '../../utils/info-center-message.js'; import { modeAndStackChangeHandler } from '../rta-service.js'; import type { ActionSenderFunction, SubscribeFunction, UI5AdaptationOptions } from '../types.js'; import { applyChange } from './flex-change.js'; import { ChangeHandler, ChangeType, ConfigChange, GENERIC_CHANGE_HANDLER, getControlIdByChange, getFlexObject, type GenericChange } from './generic-change.js'; const TITLE_MAP: { [key: string]: string } = { appdescr_app_addAnnotationsToOData: 'Add New Annotation File' }; export const STACK_CHANGE_EVENT = 'STACK_CHANGED'; export interface StackChangedEventDetail { controls: UI5Element[]; } type SavedChangesResponse = Record; interface ChangeContent { property: string; newValue: string; newBinding: string; } /** * Modify rta message. * * @param errorMessage error message to be replaced * @param id - control id * @param type - control type * @returns string */ function modifyRTAErrorMessage(errorMessage: string, id: string, type: string): string { return errorMessage.replace('Error: Applying property changes failed:', '').replace(`${type}#${id}`, ''); } /** * A Class of ChangeService */ export class ChangeService extends EventTarget { private savedChanges: SavedChange[] = []; private changesRequiringReload = 0; private sendAction: (action: ExternalAction) => void; private pendingChanges: PendingChange[] = []; private changedFiles: Record = {}; private readonly eventStack: object[] = []; private pendingConfigChangeMap: Map = new Map(); private configPropertyControlIdMap: Map = new Map(); private readonly configPropertyPath: Set = new Set(); /** * * @param options ui5 adaptation options. * @param ui5 facade for ui5 framework methods. * @param selectionService selection service instance. */ constructor(private readonly options: UI5AdaptationOptions) { super(); } /** * Initializes change service. * * @param sendAction action sender function * @param subscribe subscriber function */ public async init(sendAction: ActionSenderFunction, subscribe: SubscribeFunction): Promise { this.sendAction = sendAction; subscribe(async (action): Promise => { if (changeProperty.match(action)) { try { await applyChange(this.options, action.payload); } catch (exception) { // send error information let name = ''; const id = action.payload.controlId || ''; const control = sap.ui.getCore().byId(id); if (control) { name = control.getMetadata().getName(); } const error = getError(exception); const modifiedMessage = modifyRTAErrorMessage(error.toString(), id, name); const errorMessage = modifiedMessage || `RTA Exception applying expression "${action.payload.value}"`; await sendInfoCenterMessage({ title: { key: 'CHANGE_CREATION_FAILED_TITLE' }, description: errorMessage, type: MessageBarType.error }); const propertyChangeFailedAction = propertyChangeFailed({ ...action.payload, errorMessage }); sendAction(propertyChangeFailedAction); } } else if (deletePropertyChanges.match(action)) { await this.deleteChange(action.payload.controlId, action.payload.propertyName, action.payload.fileName); } else if (reloadApplication.match(action)) { this.sendAction(setApplicationRequiresReload(false)); } else if (save.match(action)) { this.changesRequiringReload = 0; this.sendAction(setApplicationRequiresReload(false)); } }); await this.fetchSavedChanges(); this.updateStack(); this.options.rta.attachUndoRedoStackModified(this.createOnStackChangeHandler()); } /** * Send update to the editor with modified stack. * * @param pendingChanges Changes that are waiting to be saved */ private updateStack() { this.sendAction( changeStackModified({ saved: this.savedChanges ?? [], pending: this.pendingChanges ?? [] }) ); } private isGenericChange(change: GenericChange): change is GenericChange { return ( change.changeType === 'appdescr_app_addAnnotationsToOData' || change.changeType === 'rename' || change.changeType === 'moveControls' || change.changeType === 'addXML' || change.changeType === 'propertyChange' || change.changeType === 'propertyBindingChange' || change.changeType === 'appdescr_fe_changePageConfiguration' || change.changeType === 'appdescr_ui_generic_app_changePageConfiguration' ); } /** * Fetches saved changes from the workspace and sorts them. */ private async fetchSavedChanges(): Promise { this.changedFiles = {}; const savedChangesResponse = await fetch(FlexChangesEndPoints.changes + `?_=${Date.now()}`); const savedChanges = (await savedChangesResponse.json()) as SavedChangesResponse; const textBundle = await getTextBundle(); const changes = ( await Promise.all( Object.keys(savedChanges ?? {}).map( async (key): Promise => { const change = savedChanges[key]; try { const handler = GENERIC_CHANGE_HANDLER[ change.changeType ] as unknown as ChangeHandler; if (this.isGenericChange(change)) { const { properties, changeTitle, controlId, changeType: type, subtitle } = await handler(change, { textBundle, appComponent: this.options.rta.getRootControlInstance(), configPropertyControlIdMap: this.configPropertyControlIdMap }); this.changedFiles[change.fileName] = change; return { kind: GENERIC_CHANGE_KIND, type: 'saved', fileName: change.fileName, ...(subtitle && { subtitle }), changeType: type ?? change.changeType, timestamp: new Date(change.creation).getTime(), ...(controlId && { controlId }), properties, title: textBundle.getText(changeTitle) }; } throw new Error('Unknown change type'); } catch { // Gracefully handle change files with invalid content const flexObject = await getFlexObject(change); const selectorId = await getControlIdByChange( flexObject, this.options.rta.getRootControlInstance() ); if (change.fileName) { this.changedFiles[change.fileName] = change; const unknownChange: UnknownSavedChange = { type: 'saved', kind: 'unknown', changeType: change.changeType, fileName: change.fileName, timestamp: new Date(change.creation).getTime() }; if (change.creation) { unknownChange.timestamp = new Date(change.creation).getTime(); } if (selectorId) { const controlChange: SavedControlChange = { ...unknownChange, kind: 'control', controlId: selectorId }; return controlChange; } return unknownChange; } return undefined; } } ) ) ) .filter((change) => !!change) .sort((a, b) => b.timestamp - a.timestamp); this.savedChanges = changes; } /** * * @param controlId unique identifier for a control * @param propertyName name of the property change to be deleted * @param fileName name of the file. */ public async deleteChange(controlId: string, propertyName: string, fileName?: string): Promise { const filesToDelete = this.savedChanges .filter((change) => { if (fileName) { return fileName === change.fileName; } if (change.kind === 'control') { return change.controlId === controlId; } return false; }) .map((change) => fetch(FlexChangesEndPoints.changes, { method: 'DELETE', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ fileName: change.fileName }) }) ); await Promise.all(filesToDelete).catch((error) => Log.error(getError(error).message)); await this.fetchSavedChanges(); this.updateStack(); } /** * Handler for undo/redo stack change. * * @param sendAction send action method * @returns (event: sap.ui.base.Event) => Promise */ private createOnStackChangeHandler(): (event: Event) => Promise { const handleStackChange = modeAndStackChangeHandler(this.sendAction, this.options.rta); return async (event): Promise => { const pendingChanges: PendingChange[] = []; this.eventStack.push(event); const stack = this.options.rta.getCommandStack(); const allCommands = stack.getCommands(); const executedCommands = stack.getAllExecutedCommands(); const allCommandsFlattened = allCommands.flatMap((command: FlexCommand) => typeof command.getCommands === 'function' ? command.getCommands() : [command] ); const activeCommandCount = allCommandsFlattened.length - executedCommands.length; this.pendingConfigChangeMap = new Map(); let i = 0; for (const command of allCommands) { try { if (typeof command.getCommands === 'function') { const subCommands = command.getCommands(); for (const subCommand of subCommands) { await this.handleCommand(subCommand, activeCommandCount, i, pendingChanges); i++; } } else { await this.handleCommand(command, activeCommandCount, i, pendingChanges); i++; } } catch (error) { Log.error('CPE: Change creation Failed', getError(error)); } } const eventIndex = this.eventStack.indexOf(event); if (this.eventStack.length - 1 === eventIndex) { this.pendingChanges = pendingChanges.filter((change): boolean => !!change); const changesRequiringReload = this.pendingChanges.reduce( (sum, change) => (isGenericConfigChange(change) ? sum + 1 : sum), 0 ); if (changesRequiringReload > this.changesRequiringReload) { await sendInfoCenterMessage({ title: { key: 'CHANGES_VISIBLE_AFTER_SAVE_AND_RELOAD_TITLE' }, description: { key: 'CHANGES_VISIBLE_AFTER_SAVE_AND_RELOAD_DESCRIPTION' }, type: MessageBarType.info }); this.sendAction(setApplicationRequiresReload(changesRequiringReload > 0)); } this.changesRequiringReload = changesRequiringReload; } this.eventStack.splice(eventIndex, 1); if (Array.isArray(allCommands) && allCommands.length === 0) { this.pendingChanges = []; this.pendingConfigChangeMap = new Map(); await this.fetchSavedChanges(); } // Notify to update the ui for configuration changes. const configurationChanges: PendingGenericChange[] = this.pendingChanges?.filter(isGenericConfigChange); if (configurationChanges.length) { const stackChangeEvent = new CustomEvent(STACK_CHANGE_EVENT, { detail: { controls: configurationChanges.reduce((acc: UI5Element[], item) => { const controls = ([...(item.controlId ?? [])] as string[]) .map((id: string) => { return getControlById(id); }) .filter((ui5Element) => isA('sap.ui.core.Element', ui5Element)); acc.push(...controls); return acc; }, []) } }); this.dispatchEvent(stackChangeEvent); } this.updateStack(); handleStackChange(); }; } /** * Cached configuration commands to set reset value during stack change event. * * @param controlId - control id of the config property. * @param propertyName - config property name. * @returns Configuration property value. */ public getConfigurationPropertyValue(controlId: string, propertyName: string): ConfigurationValue | undefined { const pendingChanges = this.pendingConfigChangeMap?.get(controlId); return (pendingChanges || []).find((item) => item.isActive && item.properties[0].label === propertyName) ?.properties[0].value; } /** * Get all pending configuration changes. * * @returns array of pending generic changes */ public getAllPendingConfigPropertyPath(): Set { return this.configPropertyPath ?? new Set(); } /** * Update config changes with associated controls. * * @param {Map} configPropertyControlIdMap - config property path control id map. */ public async updateConfigurationProps(configPropertyControlIdMap: Map): Promise { this.configPropertyControlIdMap = configPropertyControlIdMap; await this.fetchSavedChanges(); this.updateStack(); } /** * Handles a command by preparing a pending change and adding it to the list of pending changes. * * @param {FlexCommand} command - The command to process. * @param {number} inactiveCommandCount - The number of inactive commands. * @param {number} index - The index of the current command being processed. * @param {PendingChange[]} pendingChanges - The list of pending changes to update. * @returns {Promise} A promise that resolves when the command is handled. */ private async handleCommand( command: FlexCommand, inactiveCommandCount: number, index: number, pendingChanges: PendingChange[] ): Promise { const flexXMLChanges = getFlexXMLChangeList(command); const rootController = this.options.rta.getRootControlInstance(); setAdditionalChangeInfo(flexXMLChanges, rootController); const changes = await this.buildPendingChanges(command, inactiveCommandCount, index); pendingChanges.push(...changes); } private trackPendingConfigChanges(result: PendingGenericChange): void { for (const id of result?.controlId ?? []) { if (!this.pendingConfigChangeMap.get(id)) { this.pendingConfigChangeMap.set(id, []); } const pendingChanges = this.pendingConfigChangeMap.get(id); pendingChanges?.push(result); } } /** * Build a PendingGenericChange and track config change path if applicable. * * @param changeDefinition - change definition object * @param changeType - the change type string * @param handler - the generic change handler * @param isActive - whether the change is currently active * @param textBundle - i18n text bundle * @returns Promise resolving to PendingGenericChange */ private async buildGenericChange( changeDefinition: ChangeDefinition, changeType: string, handler: ChangeHandler, isActive: boolean, textBundle: Awaited> ): Promise { const fileName = changeDefinition.fileName; const { properties, changeTitle, controlId, changeType: type, subtitle } = await handler(changeDefinition as unknown as GenericChange, { textBundle, appComponent: this.options.rta.getRootControlInstance(), configPropertyControlIdMap: this.configPropertyControlIdMap }); const genericChange: PendingGenericChange = { kind: GENERIC_CHANGE_KIND, type: 'pending', changeType: type ?? changeType, ...(subtitle && { subtitle }), isActive, title: textBundle.getText(changeTitle), fileName, ...(controlId && { controlId }), properties }; if (changeType === 'appdescr_fe_changePageConfiguration') { const configChangePath = (changeDefinition as ConfigChange).content.entityPropertyChange.propertyPath; if (genericChange.isActive) { this.configPropertyPath.add(configChangePath); } else { // remove value from set if change is undone this.configPropertyPath.delete(configChangePath); } this.trackPendingConfigChanges(genericChange); } return genericChange; } /** * Build a fallback PendingChange for changes without a registered handler. * * @param changeType - the change type string * @param selectorId - optional selector/control ID * @param isActive - whether the change is currently active * @param fileName - file name of the change * @returns PendingChange */ private buildFallbackChange( changeType: string, selectorId: string | undefined, isActive: boolean, fileName: string ): PendingChange { const title = TITLE_MAP[changeType] ?? ''; let result: PendingChange = { type: PENDING_CHANGE_TYPE, kind: UNKNOWN_CHANGE_KIND, ...(title && { title }), changeType, isActive, fileName }; if (selectorId) { result = { ...result, kind: 'control', controlId: selectorId }; } return result; } /** * Build the list of pending changes based on the command and other parameters. * * @param {FlexCommand} command - The command to process. * @param {number} inactiveCommandCount - The number of inactive commands. * @param {number} index - The index of the current command being processed. * @returns {Promise} - A promise that resolves to a `PendingChange` list. */ private async buildPendingChanges( command: FlexCommand, inactiveCommandCount: number, index: number ): Promise { const textBundle = await getTextBundle(); const rootController = this.options.rta.getRootControlInstance(); return Promise.all( getFlexChangeList(command).map(async (change) => { const selectorId = typeof change?.getSelector === 'function' ? await getControlIdByChange(change as Change, rootController) : this.getCommandSelectorId(command); const changeType = change.getChangeType?.() ?? command.getChangeType?.(); const changeDefinition = getChangeDefinition(change); const { fileName } = changeDefinition; const handler = GENERIC_CHANGE_HANDLER[ changeType as ChangeType ] as unknown as ChangeHandler; const isActive = index >= inactiveCommandCount; return handler ? this.buildGenericChange(changeDefinition, changeType, handler, isActive, textBundle) : this.buildFallbackChange(changeType, selectorId, isActive, fileName); }) ).then((pendingChanges) => pendingChanges.filter((change) => !!change)); } /** * Retry operations. * * @param operations to be executed * @returns first successfull operation result or undefined */ private retryOperations(operations: Array<() => T>): T | undefined { for (const operation of operations) { try { const result = operation(); if (!result) { continue; } return result; } catch { continue; } } Log.error('All retry operations failed'); return undefined; } /** * Get command selector id. * * @param command to be executed for creating change * @returns command selector id or undefined */ private getCommandSelectorId(command: FlexCommand): string | undefined { return this.retryOperations([ () => command.getSelector().id, // eslint-disable-next-line @typescript-eslint/no-unsafe-return () => command.getElement().getProperty('persistencyKey'), () => command.getElement().getId(), () => command.getParent()?.getElement().getId() ]) as string | undefined; } /** * Sync outline changes to place modification markers when outline is changed. * * @returns void */ public async syncOutlineChanges(): Promise { for (const change of this.savedChanges) { if (change.kind !== 'unknown' && change.changeType !== 'configuration') { const flexObject = await getFlexObject(this.changedFiles[change.fileName]); change.controlId = (await getControlIdByChange(flexObject, this.options.rta.getRootControlInstance())) ?? ''; } } this.updateStack(); } public onStackChange(handler: (event: CustomEvent) => void | Promise): void { this.addEventListener(STACK_CHANGE_EVENT, handler as EventListener); } } function isGenericConfigChange(change: PendingChange): change is PendingGenericChange { return change.kind === GENERIC_CHANGE_KIND && change.changeType === 'configuration'; }