@atlaskit/editor-plugin-selection
Version:
Selection plugin for @atlaskit/editor-core
141 lines • 5.61 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import { getSelectionFragment, getSelectionLocalIds } from './editor-actions';
import { createAutoExpandSelectionRangeOnInlineNodePlugin } from './pm-plugins/auto-expand-selection-range-on-inline-node-main';
import { selectNearNode as _selectNearNode } from './pm-plugins/commands';
import gapCursorKeymapPlugin from './pm-plugins/gap-cursor-keymap';
import gapCursorPlugin from './pm-plugins/gap-cursor-main';
import { gapCursorPluginKey } from './pm-plugins/gap-cursor-plugin-key';
import selectionKeymapPlugin from './pm-plugins/keymap';
import { createMarkBoundaryCursorPlugin } from './pm-plugins/mark-boundary-cursor-main';
import { createPlugin } from './pm-plugins/selection-main';
import { selectionPluginKey } from './types';
var displayGapCursor = function displayGapCursor(toggle) {
return function (_ref) {
var tr = _ref.tr;
return tr.setMeta(gapCursorPluginKey, {
displayGapCursor: toggle
});
};
};
var clearManualSelection = function clearManualSelection() {
return function (_ref2) {
var tr = _ref2.tr;
var currMeta = tr.getMeta(selectionPluginKey);
return tr.setMeta(selectionPluginKey, _objectSpread(_objectSpread({}, currMeta), {}, {
manualSelection: {}
}));
};
};
var setManualSelection = function setManualSelection(anchor, head) {
return function (_ref3) {
var tr = _ref3.tr;
var currMeta = tr.getMeta(selectionPluginKey);
return tr.setMeta(selectionPluginKey, _objectSpread(_objectSpread({}, currMeta), {}, {
manualSelection: {
anchor: anchor,
head: head
}
}));
};
};
var hideCursor = function hideCursor(hide) {
return function (_ref4) {
var tr = _ref4.tr;
var currMeta = tr.getMeta(selectionPluginKey);
return tr.setMeta(selectionPluginKey, _objectSpread(_objectSpread({}, currMeta), {}, {
hideCursor: hide
}));
};
};
var setBlockSelection = function setBlockSelection(selection) {
return function (_ref5) {
var tr = _ref5.tr;
var currMeta = tr.getMeta(selectionPluginKey);
return tr.setMeta(selectionPluginKey, _objectSpread(_objectSpread({}, currMeta), {}, {
setBlockSelection: selection
}));
};
};
var clearBlockSelection = function clearBlockSelection() {
return function (_ref6) {
var tr = _ref6.tr;
var currMeta = tr.getMeta(selectionPluginKey);
return tr.setMeta(selectionPluginKey, _objectSpread(_objectSpread({}, currMeta), {}, {
clearBlockSelection: true
}));
};
};
export var selectionPlugin = function selectionPlugin(_ref7) {
var api = _ref7.api,
options = _ref7.config;
return {
name: 'selection',
commands: {
displayGapCursor: displayGapCursor,
clearManualSelection: clearManualSelection,
hideCursor: hideCursor,
setManualSelection: setManualSelection,
setBlockSelection: setBlockSelection,
clearBlockSelection: clearBlockSelection
},
actions: {
selectNearNode: function selectNearNode(_ref8) {
var selectionRelativeToNode = _ref8.selectionRelativeToNode,
selection = _ref8.selection;
return function (state) {
return _selectNearNode(selectionRelativeToNode, selection)({
tr: state.tr
}) || state.tr;
};
},
getSelectionFragment: getSelectionFragment(api),
getSelectionLocalIds: getSelectionLocalIds(api)
},
getSharedState: function getSharedState(editorState) {
if (!editorState) {
return undefined;
}
var pluginState = selectionPluginKey.getState(editorState);
return {
selectionRelativeToNode: pluginState === null || pluginState === void 0 ? void 0 : pluginState.selectionRelativeToNode,
selection: pluginState === null || pluginState === void 0 ? void 0 : pluginState.selection
};
},
pmPlugins: function pmPlugins() {
return [{
name: 'selection',
plugin: function plugin(_ref9) {
var dispatch = _ref9.dispatch,
dispatchAnalyticsEvent = _ref9.dispatchAnalyticsEvent;
return createPlugin(api, dispatch, dispatchAnalyticsEvent, options);
}
}, {
name: 'selectionKeymap',
plugin: selectionKeymapPlugin
}, {
name: 'gapCursorKeymap',
plugin: function plugin() {
return gapCursorKeymapPlugin();
}
}, {
name: 'gapCursor',
plugin: function plugin() {
return gapCursorPlugin;
}
}, {
name: 'markBoundaryCursor',
plugin: function plugin() {
return createMarkBoundaryCursorPlugin();
}
}, {
name: 'autoExpandSelectionRangeOnInlineNode',
plugin: function plugin() {
return createAutoExpandSelectionRangeOnInlineNodePlugin();
}
}];
}
};
};
export default selectionPlugin;