@atlaskit/editor-plugin-selection-marker
Version:
Selection marker plugin for @atlaskit/editor-core.
98 lines (97 loc) • 4.6 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createPlugin = exports.applyNextPluginState = void 0;
exports.dispatchShouldHideDecorations = dispatchShouldHideDecorations;
exports.key = void 0;
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _debounce = _interopRequireDefault(require("lodash/debounce"));
var _browser = require("@atlaskit/editor-common/browser");
var _safePlugin = require("@atlaskit/editor-common/safe-plugin");
var _utils = require("@atlaskit/editor-common/utils");
var _state = require("@atlaskit/editor-prosemirror/state");
var _view = require("@atlaskit/editor-prosemirror/view");
var _selectionDecoration = require("../ui/selection-decoration");
var _widgetDecoration = require("../ui/widget-decoration");
var key = exports.key = new _state.PluginKey('selectionMarker');
function getDecorations(tr, type) {
var selection = tr.selection;
switch (type) {
case 'none':
return _view.DecorationSet.empty;
case 'highlight':
return _view.DecorationSet.create(tr.doc, [].concat((0, _toConsumableArray2.default)((0, _widgetDecoration.createWidgetDecoration)(selection.$anchor, 'anchor', selection, true)), (0, _toConsumableArray2.default)((0, _selectionDecoration.selectionDecoration)(tr.doc, selection, true)), (0, _toConsumableArray2.default)((0, _widgetDecoration.createWidgetDecoration)(selection.$head, 'head', selection, true))));
case 'blur':
return _view.DecorationSet.create(tr.doc, [].concat((0, _toConsumableArray2.default)((0, _widgetDecoration.createWidgetDecoration)(selection.$anchor, 'anchor', selection, false)), (0, _toConsumableArray2.default)((0, _selectionDecoration.selectionDecoration)(tr.doc, selection, false))));
}
}
function getDecorationType(tr, shouldHideDecorations) {
if (shouldHideDecorations || (0, _utils.isEmptyDocument)(tr.doc)) {
return 'none';
}
// TODO: ED-26961 - implement "highlight" for AI features
return 'blur';
}
var applyNextPluginState = exports.applyNextPluginState = function applyNextPluginState(tr, currentState, oldEditorState) {
var _meta$forceHide, _meta$shouldHideDecor;
var meta = tr.getMeta(key);
if (!meta && !tr.selectionSet) {
return currentState;
}
var forceHide = (_meta$forceHide = meta === null || meta === void 0 ? void 0 : meta.forceHide) !== null && _meta$forceHide !== void 0 ? _meta$forceHide : currentState.forceHide;
var shouldHideDecorations = (_meta$shouldHideDecor = meta === null || meta === void 0 ? void 0 : meta.shouldHideDecorations) !== null && _meta$shouldHideDecor !== void 0 ? _meta$shouldHideDecor : currentState.shouldHideDecorations;
var type = getDecorationType(tr, shouldHideDecorations);
var nextDecorations = currentState.decorations;
var hasSelectionChangedToRange = oldEditorState.selection.empty && !tr.selection.empty;
if (hasSelectionChangedToRange || currentState.decorationType !== type) {
nextDecorations = getDecorations(tr, type);
} else {
nextDecorations = nextDecorations.map(tr.mapping, tr.doc, {});
}
return {
decorations: nextDecorations,
shouldHideDecorations: shouldHideDecorations,
forceHide: forceHide,
decorationType: type
};
};
var debouncedDecorations = (0, _debounce.default)(function (state) {
var _key$getState;
return (_key$getState = key.getState(state)) === null || _key$getState === void 0 ? void 0 : _key$getState.decorations;
}, 25);
var createPlugin = exports.createPlugin = function createPlugin(api) {
return new _safePlugin.SafePlugin({
key: key,
state: {
init: function init() {
return {
decorations: _view.DecorationSet.empty,
shouldHideDecorations: true,
forceHide: false,
decorationType: 'none'
};
},
apply: applyNextPluginState
},
props: {
decorations: function decorations(state) {
var browser = (0, _browser.getBrowserInfo)();
if (browser.ie) {
return debouncedDecorations(state);
} else {
var _key$getState2;
return (_key$getState2 = key.getState(state)) === null || _key$getState2 === void 0 ? void 0 : _key$getState2.decorations;
}
}
}
});
};
function dispatchShouldHideDecorations(editorView, shouldHideDecorations) {
var dispatch = editorView.dispatch,
state = editorView.state;
dispatch(state.tr.setMeta(key, {
shouldHideDecorations: shouldHideDecorations
}));
}