Skip to content

Commit

Permalink
Apply middleware changes on autosave and push them to the editor, uni…
Browse files Browse the repository at this point in the history
…fy common calls (#4671)
  • Loading branch information
thecalcc committed Oct 22, 2024
1 parent c9ae32d commit e74c71d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ export function AuthoringDirective(
}
});

$scope.autosave(item);
$scope.autosave(item, 0);
};

$scope.firstLineConfig = appConfig?.ui?.authoring?.firstLine ?? {};
Expand All @@ -858,6 +858,7 @@ export function AuthoringDirective(
$scope.item,
$scope.origItem,
timeout,
$scope.$applyAsync,
).then(
() => {
$scope.$applyAsync(() => {
Expand Down
37 changes: 24 additions & 13 deletions scripts/apps/authoring/authoring/services/AutosaveService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as helpers from 'apps/authoring/authoring/helpers';
import {AUTOSAVE_TIMEOUT} from 'core/constants';
import {IArticle} from 'superdesk-api';
import {runBeforeUpdateMiddlware, runAfterUpdateEvent} from './authoring-helpers';
import {authoringApiCommon} from 'apps/authoring-bridge/authoring-api-common';

const RESOURCE = 'archive_autosave';

Expand Down Expand Up @@ -60,33 +60,44 @@ export class AutosaveService {
/**
* Auto-saves an item
*/
save(item: IArticle, orig: IArticle, timeout: number = AUTOSAVE_TIMEOUT, callback) {
save(
item: IArticle,
orig: IArticle,
timeout: number = AUTOSAVE_TIMEOUT,
callback, applyAsync?: () => Promise<void>,
) {
if (!item._editable || !item._locked) {
return $q.reject('item not ' + item._editable ? 'locked' : 'editable');
}

this.stop(item);

let id = item._id;
const id = item._id;

this.timeouts[id] = $timeout(() => {
runBeforeUpdateMiddlware(item, orig)
authoringApiCommon.saveBefore(item, orig)
.then((itemLatest: IArticle) => {
var diff = helpers.extendItem({_id: id}, itemLatest);
const diff = helpers.extendItem({_id: id}, itemLatest);

helpers.filterDefaultValues(diff, orig);
helpers.extendItem(item, diff); // update item for changes to get picked up by the editor

return api.save(RESOURCE, {}, diff).then((_autosave: IArticle) => {
runAfterUpdateEvent(orig, _autosave);
const saveAndRunMiddleware = () => {
return api.save(RESOURCE, {}, diff).then((_autosave: IArticle) => {
authoringApiCommon.saveAfter(item, orig);
orig._autosave = _autosave;
callback?.();

orig._autosave = _autosave;
return _autosave;
});
};

if (typeof callback === 'function') {
callback();
}
if (applyAsync) {
// update authoring view
return applyAsync().then(() => saveAndRunMiddleware());
}

return _autosave;
});
return saveAndRunMiddleware();
});
}, timeout, false);

Expand Down

0 comments on commit e74c71d

Please sign in to comment.