import type { ExternalAction } from '@sap-ux-private/control-property-editor-common'; import { reloadApplication, storageFileChanged } from '@sap-ux-private/control-property-editor-common'; import type { ActionSenderFunction, SubscribeFunction } from './types.js'; import { getUi5Version, isLowerThanMinimalUi5Version } from '../utils/version.js'; import { AdditionalChangeInfo } from '../utils/additional-change-info.js'; /** * A Class of WorkspaceConnectorService */ export class WorkspaceConnectorService { /** * When save and reload is triggered, we do not need special handling for changes that are not directly visible in preview. */ private isReloadPending = false; private sendAction: (action: ExternalAction) => void; /** * Initializes connector service. * * @param sendAction action sender function * @param subscribe subscriber function */ public async init(sendAction: ActionSenderFunction, subscribe: SubscribeFunction): Promise { this.sendAction = sendAction; subscribe((action): void => { if (reloadApplication.match(action)) { this.isReloadPending = true; } }); const ui5Version = await getUi5Version(); if (isLowerThanMinimalUi5Version(ui5Version, { major: 1, minor: 84 })) { if (ui5Version.isCdn) { const FakeLrepConnector = (await import('sap/ui/fl/FakeLrepConnector')).default; FakeLrepConnector.fileChangeRequestNotifier = this.onChangeSaved.bind(this); } else { // For UI5 versions below 1.84 served from npmjs, we do not support any connector service return; } } else { const connector = (await import('open/ux/preview/client/flp/WorkspaceConnector')).default; // hook the file deletion listener to the UI5 workspace connector connector.storage.fileChangeRequestNotifier = this.onChangeSaved.bind(this); } } private onChangeSaved( fileName: string, kind: 'delete' | 'create', change: unknown = {}, additionalChangeInfo: unknown = {} ): void { const { changeType, content } = change as { changeType?: string; content?: { fragmentPath?: string; codeRef?: string; }; }; if ( (changeType && !['appdescr_fe_changePageConfiguration', 'appdescr_ui_generic_app_changePageConfiguration'].includes( changeType )) || kind === 'delete' || this.isReloadPending ) { this.sendAction(storageFileChanged(fileName?.replace('sap.ui.fl.', ''))); } if (changeType === 'addXML' && (additionalChangeInfo as AdditionalChangeInfo )?.templateName && content?.fragmentPath) { // If there is template available, then we save and reload right away, // so we should ignore the first file change event that comes for the fragment. // (We don't want to show "Reload" button) this.sendAction(storageFileChanged(content.fragmentPath)); } } }