@atlaskit/editor-plugin-synced-block
Version:
SyncedBlock plugin for @atlaskit/editor-core
78 lines (77 loc) • 3.27 kB
JavaScript
import { pmHistoryPluginKey } from '@atlaskit/editor-common/utils';
import { FLAG_ID } from '../../types';
import { syncedBlockPluginKey } from '../main';
const onRetry = (api, syncBlockStore) => () => {
var _api$core;
api === null || api === void 0 ? void 0 : (_api$core = api.core) === null || _api$core === void 0 ? void 0 : _api$core.actions.execute(({
tr
}) => {
return tr.setMeta(syncedBlockPluginKey, {
bodiedSyncBlockDeletionStatus: 'processing',
activeFlag: false
});
});
syncBlockStore.sourceManager.retryDeletion();
};
const onDismissed = syncBlockStore => tr => {
syncBlockStore.sourceManager.clearPendingDeletion();
return tr.setMeta(syncedBlockPluginKey, {
bodiedSyncBlockDeletionStatus: 'none'
});
};
export const 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(node => node.attrs), deletionReason, () => {
var _api$core2;
const confirmationTransaction = confirmationTransactionRef.current;
if (!confirmationTransaction) {
return;
}
api === null || api === void 0 ? void 0 : (_api$core2 = api.core) === null || _api$core2 === void 0 ? void 0 : _api$core2.actions.execute(() => {
const 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;
});
}, success => {
var _api$core3;
api === null || api === void 0 ? void 0 : (_api$core3 = api.core) === null || _api$core3 === void 0 ? void 0 : _api$core3.actions.execute(({
tr
}) => {
let newState;
if (!success) {
newState = {
activeFlag: {
id: FLAG_ID.FAIL_TO_DELETE,
onRetry: onRetry(api, syncBlockStore),
onDismissed: onDismissed(syncBlockStore)
}
};
} else {
newState = {
activeFlag: false
};
}
newState = {
...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);
});
}, () => {
confirmationTransactionRef.current = undefined;
});
return false;
};