@atlaskit/editor-plugin-selection
Version:
Selection plugin for @atlaskit/editor-core
148 lines (147 loc) • 6.14 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.selectionPlugin = exports.default = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _editorActions = require("./editor-actions");
var _autoExpandSelectionRangeOnInlineNodeMain = require("./pm-plugins/auto-expand-selection-range-on-inline-node-main");
var _commands = require("./pm-plugins/commands");
var _gapCursorKeymap = _interopRequireDefault(require("./pm-plugins/gap-cursor-keymap"));
var _gapCursorMain = _interopRequireDefault(require("./pm-plugins/gap-cursor-main"));
var _gapCursorPluginKey = require("./pm-plugins/gap-cursor-plugin-key");
var _keymap = _interopRequireDefault(require("./pm-plugins/keymap"));
var _markBoundaryCursorMain = require("./pm-plugins/mark-boundary-cursor-main");
var _selectionMain = require("./pm-plugins/selection-main");
var _types = require("./types");
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) { (0, _defineProperty2.default)(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; }
var displayGapCursor = function displayGapCursor(toggle) {
return function (_ref) {
var tr = _ref.tr;
return tr.setMeta(_gapCursorPluginKey.gapCursorPluginKey, {
displayGapCursor: toggle
});
};
};
var clearManualSelection = function clearManualSelection() {
return function (_ref2) {
var tr = _ref2.tr;
var currMeta = tr.getMeta(_types.selectionPluginKey);
return tr.setMeta(_types.selectionPluginKey, _objectSpread(_objectSpread({}, currMeta), {}, {
manualSelection: {}
}));
};
};
var setManualSelection = function setManualSelection(anchor, head) {
return function (_ref3) {
var tr = _ref3.tr;
var currMeta = tr.getMeta(_types.selectionPluginKey);
return tr.setMeta(_types.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(_types.selectionPluginKey);
return tr.setMeta(_types.selectionPluginKey, _objectSpread(_objectSpread({}, currMeta), {}, {
hideCursor: hide
}));
};
};
var setBlockSelection = function setBlockSelection(selection) {
return function (_ref5) {
var tr = _ref5.tr;
var currMeta = tr.getMeta(_types.selectionPluginKey);
return tr.setMeta(_types.selectionPluginKey, _objectSpread(_objectSpread({}, currMeta), {}, {
setBlockSelection: selection
}));
};
};
var clearBlockSelection = function clearBlockSelection() {
return function (_ref6) {
var tr = _ref6.tr;
var currMeta = tr.getMeta(_types.selectionPluginKey);
return tr.setMeta(_types.selectionPluginKey, _objectSpread(_objectSpread({}, currMeta), {}, {
clearBlockSelection: true
}));
};
};
var selectionPlugin = exports.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 (0, _commands.selectNearNode)(selectionRelativeToNode, selection)({
tr: state.tr
}) || state.tr;
};
},
getSelectionFragment: (0, _editorActions.getSelectionFragment)(api),
getSelectionLocalIds: (0, _editorActions.getSelectionLocalIds)(api)
},
getSharedState: function getSharedState(editorState) {
if (!editorState) {
return undefined;
}
var pluginState = _types.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 (0, _selectionMain.createPlugin)(api, dispatch, dispatchAnalyticsEvent, options);
}
}, {
name: 'selectionKeymap',
plugin: _keymap.default
}, {
name: 'gapCursorKeymap',
plugin: function plugin() {
return (0, _gapCursorKeymap.default)();
}
}, {
name: 'gapCursor',
plugin: function plugin() {
return _gapCursorMain.default;
}
}, {
name: 'markBoundaryCursor',
plugin: function plugin() {
return (0, _markBoundaryCursorMain.createMarkBoundaryCursorPlugin)();
}
}, {
name: 'autoExpandSelectionRangeOnInlineNode',
plugin: function plugin() {
return (0, _autoExpandSelectionRangeOnInlineNodeMain.createAutoExpandSelectionRangeOnInlineNodePlugin)();
}
}];
}
};
};
var _default = exports.default = selectionPlugin;