@atlaskit/editor-plugin-synced-block
Version:
SyncedBlock plugin for @atlaskit/editor-core
84 lines (83 loc) • 4.23 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import { pmHistoryPluginKey } from '@atlaskit/editor-common/utils';
import { FLAG_ID } from '../../types';
import { syncedBlockPluginKey } from '../main';
var onRetry = function onRetry(api, syncBlockStore) {
return function () {
var _api$core;
api === null || api === void 0 || (_api$core = api.core) === null || _api$core === void 0 || _api$core.actions.execute(function (_ref) {
var tr = _ref.tr;
return tr.setMeta(syncedBlockPluginKey, {
bodiedSyncBlockDeletionStatus: 'processing',
activeFlag: false
});
});
syncBlockStore.sourceManager.retryDeletion();
};
};
var onDismissed = function onDismissed(syncBlockStore) {
return function (tr) {
syncBlockStore.sourceManager.clearPendingDeletion();
return tr.setMeta(syncedBlockPluginKey, {
bodiedSyncBlockDeletionStatus: 'none'
});
};
};
export var handleBodiedSyncBlockRemoval = function handleBodiedSyncBlockRemoval(bodiedSyncBlockRemoved, syncBlockStore, api, confirmationTransactionRef, deletionReason) {
// Clear potential old pending deletion to retreat the deletion as first attempt
syncBlockStore.sourceManager.clearPendingDeletion();
// If there are source sync blocks being removed, and we need to confirm with user before deleting,
// we block the transaction here, and wait for user confirmation to proceed with deletion.
// See editor-common/src/sync-block/sync-block-store-manager.ts for how we handle user confirmation and
// proceed with deletion.
syncBlockStore.sourceManager.deleteSyncBlocksWithConfirmation(bodiedSyncBlockRemoved.map(function (node) {
return node.attrs;
}), deletionReason, function () {
var _api$core2;
var confirmationTransaction = confirmationTransactionRef.current;
if (!confirmationTransaction) {
return;
}
api === null || api === void 0 || (_api$core2 = api.core) === null || _api$core2 === void 0 || _api$core2.actions.execute(function () {
var trToDispatch = confirmationTransaction.setMeta('isConfirmedSyncBlockDeletion', true);
if (!trToDispatch.getMeta(pmHistoryPluginKey)) {
// bodiedSyncBlock deletion is expected to be permanent (cannot undo)
// For a normal deletion (not triggered by undo), remove it from history so that it cannot be undone
trToDispatch.setMeta('addToHistory', false);
}
return trToDispatch;
});
}, function (success) {
var _api$core3;
api === null || api === void 0 || (_api$core3 = api.core) === null || _api$core3 === void 0 || _api$core3.actions.execute(function (_ref2) {
var tr = _ref2.tr;
var newState;
if (!success) {
newState = {
activeFlag: {
id: FLAG_ID.FAIL_TO_DELETE,
onRetry: onRetry(api, syncBlockStore),
onDismissed: onDismissed(syncBlockStore)
}
};
} else {
newState = {
activeFlag: false
};
}
newState = _objectSpread(_objectSpread({}, newState), {}, {
bodiedSyncBlockDeletionStatus: syncBlockStore.sourceManager.isRetryingDeletion() ?
// For retry, reset to none directly to clean up the status
'none' :
// For the first attempt, set to completed for deletion modal can close the modal
'completed'
});
return tr.setMeta(syncedBlockPluginKey, newState);
});
}, function () {
confirmationTransactionRef.current = undefined;
});
return false;
};