UNPKG

@atlaskit/editor-plugin-block-menu

Version:

BlockMenu plugin for @atlaskit/editor-core

55 lines (51 loc) 2.23 kB
import { bindKeymapWithCommand, copyLinkToBlock, keymap } from '@atlaskit/editor-common/keymaps'; import { expandSelectionToBlockRange } from '@atlaskit/editor-common/selection'; import { fg } from '@atlaskit/platform-feature-flags'; import { FLAG_ID } from '../blockMenuPluginType'; import { blockMenuPluginKey } from '../pm-plugins/main'; import { copyLink } from '../ui/utils/copyLink'; export function keymapPlugin(api, config) { var list = {}; var copyLinkToBlockCommand = function copyLinkToBlockCommand(state, dispatch) { var _api$blockControls, _node$attrs; // Check if feature flag is enabled if (!fg('platform_editor_adf_with_localid')) { return false; } // Get the preserved selection (only works when block menu is open and selection is preserved) var selection = api === null || api === void 0 || (_api$blockControls = api.blockControls) === null || _api$blockControls === void 0 || (_api$blockControls = _api$blockControls.sharedState.currentState()) === null || _api$blockControls === void 0 ? void 0 : _api$blockControls.preservedSelection; if (!selection) { return false; } // Check if the selection has a valid block range with localId var blockRange = expandSelectionToBlockRange(selection); if (!blockRange) { return false; } var node = blockRange.$from.nodeAfter; if (!(node !== null && node !== void 0 && (_node$attrs = node.attrs) !== null && _node$attrs !== void 0 && _node$attrs.localId)) { return false; } // Execute the copy link action var _ref = config || {}, getLinkPath = _ref.getLinkPath, blockLinkHashPrefix = _ref.blockLinkHashPrefix; copyLink({ getLinkPath: getLinkPath, blockLinkHashPrefix: blockLinkHashPrefix, selection: selection }).then(function (success) { if (success && dispatch) { dispatch(state.tr.setMeta(blockMenuPluginKey, { showFlag: FLAG_ID.LINK_COPIED_TO_CLIPBOARD })); } }); return true; }; // Ignored via go/ees005 bindKeymapWithCommand( // eslint-disable-next-line @typescript-eslint/no-non-null-assertion copyLinkToBlock.common, copyLinkToBlockCommand, list); return keymap(list); }