@atlaskit/editor-plugin-block-menu
Version:
BlockMenu plugin for @atlaskit/editor-core
56 lines (52 loc) • 2.17 kB
JavaScript
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) {
const list = {};
const copyLinkToBlockCommand = (state, dispatch) => {
var _api$blockControls, _api$blockControls$sh, _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)
const selection = api === null || api === void 0 ? void 0 : (_api$blockControls = api.blockControls) === null || _api$blockControls === void 0 ? void 0 : (_api$blockControls$sh = _api$blockControls.sharedState.currentState()) === null || _api$blockControls$sh === void 0 ? void 0 : _api$blockControls$sh.preservedSelection;
if (!selection) {
return false;
}
// Check if the selection has a valid block range with localId
const blockRange = expandSelectionToBlockRange(selection);
if (!blockRange) {
return false;
}
const 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
const {
getLinkPath,
blockLinkHashPrefix
} = config || {};
copyLink({
getLinkPath,
blockLinkHashPrefix,
selection
}).then(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);
}