UNPKG

@atlaskit/editor-plugin-user-preferences

Version:

UserPreferences plugin for @atlaskit/editor-core

111 lines 4.79 kB
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; import _regeneratorRuntime from "@babel/runtime/regenerator"; import { useEffect, useRef } from 'react'; import { bind } from 'bind-event-listener'; import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE } from '@atlaskit/editor-common/analytics'; import { logException } from '@atlaskit/editor-common/monitoring'; import { useResolvedUserPreferences } from '@atlaskit/editor-common/user-preferences'; import { fg } from '@atlaskit/platform-feature-flags'; import { updateToolbarDockingPosition } from './pm-plugins/commands'; import { createPlugin, userPreferencesPluginKey } from './pm-plugins/main'; export var 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 createPlugin(config, api); } }]; }, actions: { updateUserPreference: function updateUserPreference(key, value) { var _api$analytics; return 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 userPreferencesPluginKey.getState(editorState); }, usePluginHook: function usePluginHook(_ref2) { var editorView = _ref2.editorView; var _useResolvedUserPrefe = useResolvedUserPreferences(userPreferencesProvider), resolvedUserPreferences = _useResolvedUserPrefe.resolvedUserPreferences; var isInitialized = useRef(false); useEffect(function () { if (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: ACTION.INITIALISED, actionSubject: ACTION_SUBJECT.USER_PREFERENCES, actionSubjectId: ACTION_SUBJECT_ID.SELECTION_TOOLBAR_PREFERENCES, attributes: { toolbarDocking: resolvedUserPreferences.toolbarDockingPosition }, eventType: EVENT_TYPE.OPERATIONAL }); } editorView.dispatch(editorView.state.tr.setMeta(userPreferencesPluginKey, { preferences: resolvedUserPreferences })); } }, [resolvedUserPreferences, editorView]); useEffect(function () { if (fg('platform_editor_use_preferences_plugin')) { var refreshPrefrerence = /*#__PURE__*/function () { var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() { return _regeneratorRuntime.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); 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 bind(document, { type: 'visibilitychange', listener: refreshPrefrerence }); } }, []); } }; };