@atlaskit/editor-plugin-block-menu
Version:
BlockMenu plugin for @atlaskit/editor-core
61 lines (56 loc) • 2.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.keymapPlugin = keymapPlugin;
var _keymaps = require("@atlaskit/editor-common/keymaps");
var _selection = require("@atlaskit/editor-common/selection");
var _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
var _blockMenuPluginType = require("../blockMenuPluginType");
var _main = require("../pm-plugins/main");
var _copyLink = require("../ui/utils/copyLink");
function keymapPlugin(api, config) {
var list = {};
var copyLinkToBlockCommand = function copyLinkToBlockCommand(state, dispatch) {
var _api$blockControls, _node$attrs;
// Check if feature flag is enabled
if (!(0, _platformFeatureFlags.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 = (0, _selection.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;
(0, _copyLink.copyLink)({
getLinkPath: getLinkPath,
blockLinkHashPrefix: blockLinkHashPrefix,
selection: selection
}).then(function (success) {
if (success && dispatch) {
dispatch(state.tr.setMeta(_main.blockMenuPluginKey, {
showFlag: _blockMenuPluginType.FLAG_ID.LINK_COPIED_TO_CLIPBOARD
}));
}
});
return true;
};
// Ignored via go/ees005
(0, _keymaps.bindKeymapWithCommand)(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
_keymaps.copyLinkToBlock.common, copyLinkToBlockCommand, list);
return (0, _keymaps.keymap)(list);
}