UNPKG

@atlaskit/editor-plugin-selection-extension

Version:

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

46 lines (45 loc) 2.44 kB
import { Node } from '@atlaskit/editor-prosemirror/model'; import { fg } from '@atlaskit/platform-feature-flags'; import { SelectionExtensionActionTypes } from '../../types'; import { selectionExtensionPluginKey } from '../main'; export var replaceWithAdf = function replaceWithAdf(nodeAdf, api) { return function (state, dispatch) { var _selectionExtensionPl; var tr = state.tr, schema = state.schema; // we need to track if any changes were made since user click the toolbar button var 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; var selectedNode = (_selectionExtensionPl2 = selectionExtensionPluginKey.getState(state)) === null || _selectionExtensionPl2 === void 0 ? void 0 : _selectionExtensionPl2.selectedNode; var 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'); } var endPos = selectedNode.type.name === 'doc' && fg('platform_editor_selection_extension_improvement') ? nodePos + selectedNode.content.size : nodePos + selectedNode.nodeSize; var modifiedNode = Node.fromJSON(schema, nodeAdf); modifiedNode.check(); var updatedTr = tr.replaceWith(nodePos, endPos, modifiedNode).scrollIntoView(); // Allow this transaction in view mode and apply changes dispatch((api === null || api === void 0 || (_api$editorViewModeEf = api.editorViewModeEffects) === null || _api$editorViewModeEf === void 0 ? void 0 : _api$editorViewModeEf.actions.allowViewModeTransaction(updatedTr)) || updatedTr); return { status: 'success' }; } catch (_unused) { dispatch(tr); return { status: 'failed-to-replace' }; } }; };