@atlaskit/editor-plugin-selection-toolbar
Version:
@atlaskit/editor-plugin-selection-toolbar for @atlaskit/editor-core
68 lines (67 loc) • 2.87 kB
JavaScript
import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE } from '@atlaskit/editor-common/analytics';
import { TextSelection } from '@atlaskit/editor-prosemirror/state';
import { selectionToolbarPluginKey } from './plugin-key';
export var toggleToolbar = function toggleToolbar(_ref) {
var hide = _ref.hide;
return function (_ref2) {
var tr = _ref2.tr;
tr.setMeta(selectionToolbarPluginKey, {
hide: hide
});
return tr;
};
};
export var updateToolbarDocking = function updateToolbarDocking(_ref3) {
var toolbarDocking = _ref3.toolbarDocking;
return function (_ref4) {
var tr = _ref4.tr;
tr.setMeta(selectionToolbarPluginKey, {
toolbarDocking: toolbarDocking
});
return tr;
};
};
export var setToolbarDocking = function setToolbarDocking(_ref5) {
var toolbarDocking = _ref5.toolbarDocking,
userPreferencesProvider = _ref5.userPreferencesProvider,
editorAnalyticsApi = _ref5.editorAnalyticsApi;
return function (_ref6) {
var tr = _ref6.tr;
// We currently ignore any update failures, need to confirm this is the desired behaviour
userPreferencesProvider === null || userPreferencesProvider === void 0 || userPreferencesProvider.updatePreference('toolbarDockingInitialPosition', toolbarDocking);
tr.setMeta(selectionToolbarPluginKey, {
toolbarDocking: toolbarDocking
});
if (toolbarDocking === 'top') {
// Remove the selection if the toolbar is docked to the top
tr.setSelection(TextSelection.create(tr.doc, tr.selection.head));
}
editorAnalyticsApi === null || editorAnalyticsApi === void 0 || editorAnalyticsApi.attachAnalyticsEvent({
action: ACTION.UPDATED,
actionSubject: ACTION_SUBJECT.USER_PREFERENCES,
actionSubjectId: ACTION_SUBJECT_ID.SELECTION_TOOLBAR_PREFERENCES,
attributes: {
toolbarDocking: toolbarDocking
},
eventType: EVENT_TYPE.TRACK
})(tr);
return tr;
};
};
// Performs similarly to `setToolbarDocking` with a couple of differences.
// 1) It does not fire any analytics.
// 2) It does not make any changes to the selection.
// This was required due to issues with the Confluence Legacy Content Extension which needs to manipulate the scrollbar position when editor controls are enabled
// but relies on the selection remaining stable.
export var forceToolbarDockingWithoutAnalytics = function forceToolbarDockingWithoutAnalytics(_ref7) {
var toolbarDocking = _ref7.toolbarDocking,
userPreferencesProvider = _ref7.userPreferencesProvider;
return function (_ref8) {
var tr = _ref8.tr;
userPreferencesProvider === null || userPreferencesProvider === void 0 || userPreferencesProvider.updatePreference('toolbarDockingInitialPosition', toolbarDocking);
tr.setMeta(selectionToolbarPluginKey, {
toolbarDocking: toolbarDocking
});
return tr;
};
};