UNPKG

@atlaskit/editor-plugin-selection-extension

Version:

editor-plugin-selection-extension plugin for @atlaskit/editor-core

46 lines (45 loc) 2.33 kB
import { Node } from '@atlaskit/editor-prosemirror/model'; import { fg } from '@atlaskit/platform-feature-flags'; import { SelectionExtensionActionTypes } from '../../types'; import { selectionExtensionPluginKey } from '../main'; export const replaceWithAdf = (nodeAdf, api) => (state, dispatch) => { var _selectionExtensionPl; const { tr, schema } = state; // we need to track if any changes were made since user click the toolbar button const docChangedAfterClick = ((_selectionExtensionPl = selectionExtensionPluginKey.getState(state)) === null || _selectionExtensionPl === void 0 ? void 0 : _selectionExtensionPl.docChangedAfterClick) || false; tr.setMeta(selectionExtensionPluginKey, { type: SelectionExtensionActionTypes.START_TRACK_CHANGES, startTrackChanges: false // Reset the flag when starting to track changes }); if (docChangedAfterClick) { dispatch(tr); return { status: 'document-changed' }; } try { var _selectionExtensionPl2, _selectionExtensionPl3, _api$editorViewModeEf; const selectedNode = (_selectionExtensionPl2 = selectionExtensionPluginKey.getState(state)) === null || _selectionExtensionPl2 === void 0 ? void 0 : _selectionExtensionPl2.selectedNode; const nodePos = (_selectionExtensionPl3 = selectionExtensionPluginKey.getState(state)) === null || _selectionExtensionPl3 === void 0 ? void 0 : _selectionExtensionPl3.nodePos; if (!selectedNode || nodePos === undefined) { throw new Error('No selected node or node position found'); } const endPos = selectedNode.type.name === 'doc' && fg('platform_editor_selection_extension_improvement') ? nodePos + selectedNode.content.size : nodePos + selectedNode.nodeSize; const modifiedNode = Node.fromJSON(schema, nodeAdf); modifiedNode.check(); const updatedTr = tr.replaceWith(nodePos, endPos, modifiedNode).scrollIntoView(); // Allow this transaction in view mode and apply changes dispatch((api === null || api === void 0 ? void 0 : (_api$editorViewModeEf = api.editorViewModeEffects) === null || _api$editorViewModeEf === void 0 ? void 0 : _api$editorViewModeEf.actions.allowViewModeTransaction(updatedTr)) || updatedTr); return { status: 'success' }; } catch { dispatch(tr); return { status: 'failed-to-replace' }; } };