UNPKG

@atlaskit/editor-plugin-user-preferences

Version:

UserPreferences plugin for @atlaskit/editor-core

118 lines (117 loc) 5.23 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.userPreferencesPlugin = void 0; var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); var _react = require("react"); var _bindEventListener = require("bind-event-listener"); var _analytics = require("@atlaskit/editor-common/analytics"); var _monitoring = require("@atlaskit/editor-common/monitoring"); var _userPreferences = require("@atlaskit/editor-common/user-preferences"); var _platformFeatureFlags = require("@atlaskit/platform-feature-flags"); var _commands = require("./pm-plugins/commands"); var _main = require("./pm-plugins/main"); var userPreferencesPlugin = exports.userPreferencesPlugin = function userPreferencesPlugin(_ref) { var config = _ref.config, api = _ref.api; var userPreferencesProvider = config.userPreferencesProvider; return { name: 'userPreferences', pmPlugins: function pmPlugins() { return [{ name: 'userPreferencesPlugin', plugin: function plugin() { return (0, _main.createPlugin)(config, api); } }]; }, actions: { updateUserPreference: function updateUserPreference(key, value) { var _api$analytics; return (0, _commands.updateToolbarDockingPosition)({ key: key, value: value, userPreferencesProvider: userPreferencesProvider, editorAnalyticsApi: api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions }); }, setDefaultPreferences: function setDefaultPreferences(preferences) { userPreferencesProvider.setDefaultPreferences(preferences); } }, getSharedState: function getSharedState(editorState) { if (!editorState) { return null; } return _main.userPreferencesPluginKey.getState(editorState); }, usePluginHook: function usePluginHook(_ref2) { var editorView = _ref2.editorView; var _useResolvedUserPrefe = (0, _userPreferences.useResolvedUserPreferences)(userPreferencesProvider), resolvedUserPreferences = _useResolvedUserPrefe.resolvedUserPreferences; var isInitialized = (0, _react.useRef)(false); (0, _react.useEffect)(function () { if ((0, _platformFeatureFlags.fg)('platform_editor_use_preferences_plugin')) { if (userPreferencesProvider.isInitialized && !isInitialized.current) { var _api$analytics2; isInitialized.current = true; api === null || api === void 0 || (_api$analytics2 = api.analytics) === null || _api$analytics2 === void 0 || _api$analytics2.actions.fireAnalyticsEvent({ action: _analytics.ACTION.INITIALISED, actionSubject: _analytics.ACTION_SUBJECT.USER_PREFERENCES, actionSubjectId: _analytics.ACTION_SUBJECT_ID.SELECTION_TOOLBAR_PREFERENCES, attributes: { toolbarDocking: resolvedUserPreferences.toolbarDockingPosition }, eventType: _analytics.EVENT_TYPE.OPERATIONAL }); } editorView.dispatch(editorView.state.tr.setMeta(_main.userPreferencesPluginKey, { preferences: resolvedUserPreferences })); } }, [resolvedUserPreferences, editorView]); (0, _react.useEffect)(function () { if ((0, _platformFeatureFlags.fg)('platform_editor_use_preferences_plugin')) { var refreshPrefrerence = /*#__PURE__*/function () { var _ref3 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() { return _regenerator.default.wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: if (!(document.visibilityState === 'visible')) { _context.next = 9; break; } _context.prev = 1; _context.next = 4; return userPreferencesProvider.loadPreferences(); case 4: _context.next = 9; break; case 6: _context.prev = 6; _context.t0 = _context["catch"](1); (0, _monitoring.logException)(_context.t0, { location: 'editor-plugin-user-preferences/userPreferencesPlugin' }); case 9: case "end": return _context.stop(); } }, _callee, null, [[1, 6]]); })); return function refreshPrefrerence() { return _ref3.apply(this, arguments); }; }(); return (0, _bindEventListener.bind)(document, { type: 'visibilitychange', listener: refreshPrefrerence }); } }, []); } }; };