@sap-ux/preview-middleware 0.10.5 → 0.10.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -39,6 +39,7 @@ sap.ui.define([
39
39
  this.selectionService = selectionService;
40
40
  }
41
41
  async init(sendAction, subscribe) {
42
+ this.sendAction = sendAction;
42
43
  subscribe(async action => {
43
44
  if (changeProperty.match(action)) {
44
45
  try {
@@ -63,6 +64,18 @@ sap.ui.define([
63
64
  await this.deleteChange(action.payload.controlId, action.payload.propertyName, action.payload.fileName);
64
65
  }
65
66
  });
67
+ await this.fetchSavedChanges();
68
+ this.updateStack();
69
+ this.options.rta.attachUndoRedoStackModified(this.createOnStackChangeHandler());
70
+ }
71
+ updateStack() {
72
+ let pendingChanges = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
73
+ this.sendAction(changeStackModified({
74
+ saved: this.savedChanges,
75
+ pending: pendingChanges
76
+ }));
77
+ }
78
+ async fetchSavedChanges() {
66
79
  const savedChangesResponse = await fetch(FlexChangesEndPoints.changes + `?_=${ Date.now() }`);
67
80
  const savedChanges = await savedChangesResponse.json();
68
81
  const changes = Object.keys(savedChanges ?? {}).map(key => {
@@ -106,11 +119,6 @@ sap.ui.define([
106
119
  }
107
120
  }).filter(change => !!change).sort((a, b) => b.timestamp - a.timestamp);
108
121
  this.savedChanges = changes;
109
- sendAction(changeStackModified({
110
- saved: changes,
111
- pending: []
112
- }));
113
- this.options.rta.attachUndoRedoStackModified(this.createOnStackChangeHandler(sendAction));
114
122
  }
115
123
  async deleteChange(controlId, propertyName, fileName) {
116
124
  const filesToDelete = this.savedChanges.filter(change => fileName ? fileName === change.fileName : change.controlId === controlId && change.propertyName === propertyName).map(change => fetch(FlexChangesEndPoints.changes, {
@@ -119,9 +127,11 @@ sap.ui.define([
119
127
  body: JSON.stringify({ fileName: change.fileName })
120
128
  }));
121
129
  await Promise.all(filesToDelete).catch(error => Log.error(error));
130
+ await this.fetchSavedChanges();
131
+ this.updateStack();
122
132
  }
123
- createOnStackChangeHandler(sendAction) {
124
- return () => {
133
+ createOnStackChangeHandler() {
134
+ return async () => {
125
135
  const stack = this.options.rta.getCommandStack();
126
136
  const allCommands = stack.getCommands();
127
137
  const executedCommands = stack.getAllExecutedCommands();
@@ -142,10 +152,10 @@ sap.ui.define([
142
152
  }
143
153
  });
144
154
  activeChanges = activeChanges.filter(change => !!change);
145
- sendAction(changeStackModified({
146
- saved: this.savedChanges,
147
- pending: activeChanges
148
- }));
155
+ if (Array.isArray(allCommands) && allCommands.length === 0) {
156
+ await this.fetchSavedChanges();
157
+ }
158
+ this.updateStack(activeChanges);
149
159
  };
150
160
  }
151
161
  prepareChangeType(command, inactiveCommandCount, index) {
@@ -86,6 +86,7 @@ function modifyRTAErrorMessage(errorMessage: string, id: string, type: string):
86
86
  */
87
87
  export class ChangeService {
88
88
  private savedChanges: SavedPropertyChange[] = [];
89
+ private sendAction: (action: ExternalAction) => void;
89
90
  /**
90
91
  *
91
92
  * @param options ui5 adaptation options.
@@ -95,11 +96,13 @@ export class ChangeService {
95
96
  constructor(private readonly options: UI5AdaptationOptions, private readonly selectionService: SelectionService) {}
96
97
 
97
98
  /**
99
+ * Initializes change service.
98
100
  *
99
101
  * @param sendAction action sender function
100
102
  * @param subscribe subscriber function
101
103
  */
102
104
  public async init(sendAction: ActionSenderFunction, subscribe: SubscribeFunction): Promise<void> {
105
+ this.sendAction = sendAction;
103
106
  subscribe(async (action): Promise<void> => {
104
107
  if (changeProperty.match(action)) {
105
108
  try {
@@ -127,6 +130,30 @@ export class ChangeService {
127
130
  }
128
131
  });
129
132
 
133
+ await this.fetchSavedChanges();
134
+ this.updateStack();
135
+
136
+ this.options.rta.attachUndoRedoStackModified(this.createOnStackChangeHandler());
137
+ }
138
+
139
+ /**
140
+ * Send update to the editor with modified stack.
141
+ *
142
+ * @param pendingChanges Changes that are waiting to be saved
143
+ */
144
+ private updateStack(pendingChanges: PendingChange[] = []) {
145
+ this.sendAction(
146
+ changeStackModified({
147
+ saved: this.savedChanges,
148
+ pending: pendingChanges
149
+ })
150
+ );
151
+ }
152
+
153
+ /**
154
+ * Fetches saved changes from the workspace and sorts them.
155
+ */
156
+ private async fetchSavedChanges(): Promise<void> {
130
157
  const savedChangesResponse = await fetch(FlexChangesEndPoints.changes + `?_=${Date.now()}`);
131
158
  const savedChanges = await savedChangesResponse.json();
132
159
  const changes = (
@@ -176,13 +203,6 @@ export class ChangeService {
176
203
  .filter((change) => !!change) as SavedPropertyChange[]
177
204
  ).sort((a, b) => b.timestamp - a.timestamp);
178
205
  this.savedChanges = changes;
179
- sendAction(
180
- changeStackModified({
181
- saved: changes,
182
- pending: []
183
- })
184
- );
185
- this.options.rta.attachUndoRedoStackModified(this.createOnStackChangeHandler(sendAction));
186
206
  }
187
207
 
188
208
  /**
@@ -209,16 +229,19 @@ export class ChangeService {
209
229
  );
210
230
 
211
231
  await Promise.all(filesToDelete).catch((error) => Log.error(error));
232
+
233
+ await this.fetchSavedChanges();
234
+ this.updateStack();
212
235
  }
213
236
 
214
237
  /**
215
238
  * Handler for undo/redo stack change.
216
239
  *
217
240
  * @param sendAction send action method
218
- * @returns (event: sap.ui.base.Event) => void
241
+ * @returns (event: sap.ui.base.Event) => Promise<void>
219
242
  */
220
- private createOnStackChangeHandler(sendAction: (action: ExternalAction) => void): (event: Event) => void {
221
- return (): void => {
243
+ private createOnStackChangeHandler(): (event: Event) => Promise<void> {
244
+ return async (): Promise<void> => {
222
245
  const stack = this.options.rta.getCommandStack();
223
246
  const allCommands = stack.getCommands();
224
247
  const executedCommands = stack.getAllExecutedCommands();
@@ -240,12 +263,12 @@ export class ChangeService {
240
263
  });
241
264
 
242
265
  activeChanges = activeChanges.filter((change): boolean => !!change);
243
- sendAction(
244
- changeStackModified({
245
- saved: this.savedChanges,
246
- pending: activeChanges
247
- })
248
- );
266
+
267
+ if (Array.isArray(allCommands) && allCommands.length === 0) {
268
+ await this.fetchSavedChanges();
269
+ }
270
+
271
+ this.updateStack(activeChanges);
249
272
  };
250
273
  }
251
274
 
package/package.json CHANGED
@@ -9,7 +9,7 @@
9
9
  "bugs": {
10
10
  "url": "https://github.com/SAP/open-ux-tools/issues?q=is%3Aopen+is%3Aissue+label%3Abug+label%3Apreview-middleware"
11
11
  },
12
- "version": "0.10.5",
12
+ "version": "0.10.7",
13
13
  "license": "Apache-2.0",
14
14
  "author": "@SAP/ux-tools-team",
15
15
  "main": "dist/index.js",
@@ -24,7 +24,7 @@
24
24
  "dependencies": {
25
25
  "ejs": "3.1.9",
26
26
  "@sap-ux/logger": "0.4.0",
27
- "@sap-ux/adp-tooling": "0.5.4",
27
+ "@sap-ux/adp-tooling": "0.5.5",
28
28
  "@sap-ux/control-property-editor-sources": "npm:@sap-ux/control-property-editor@0.4.2"
29
29
  },
30
30
  "devDependencies": {
@@ -36,9 +36,9 @@
36
36
  "npm-run-all": "4.1.5",
37
37
  "nock": "13.2.1",
38
38
  "supertest": "6.3.3",
39
- "@private/preview-middleware-client": "npm:@sap-ux-private/preview-middleware-client@0.7.3",
40
- "@sap-ux/store": "0.4.0",
41
- "@sap-ux/project-access": "1.15.1"
39
+ "@private/preview-middleware-client": "npm:@sap-ux-private/preview-middleware-client@0.7.4",
40
+ "@sap-ux/project-access": "1.15.1",
41
+ "@sap-ux/store": "0.4.0"
42
42
  },
43
43
  "peerDependencies": {
44
44
  "express": "4"