UNPKG

@atlaskit/editor-plugin-user-preferences

Version:

UserPreferences plugin for @atlaskit/editor-core

94 lines 3.43 kB
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 const userPreferencesPlugin = ({ config, api }) => { const { userPreferencesProvider } = config; return { name: 'userPreferences', pmPlugins() { return [{ name: 'userPreferencesPlugin', plugin: () => { return createPlugin(config, api); } }]; }, actions: { updateUserPreference: (key, value) => { var _api$analytics; return updateToolbarDockingPosition({ key, value, userPreferencesProvider, editorAnalyticsApi: api === null || api === void 0 ? void 0 : (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions }); }, setDefaultPreferences: preferences => { userPreferencesProvider.setDefaultPreferences(preferences); } }, getSharedState(editorState) { if (!editorState) { return null; } return userPreferencesPluginKey.getState(editorState); }, usePluginHook({ editorView }) { const { resolvedUserPreferences } = useResolvedUserPreferences(userPreferencesProvider); const isInitialized = useRef(false); useEffect(() => { if (fg('platform_editor_use_preferences_plugin')) { if (userPreferencesProvider.isInitialized && !isInitialized.current) { var _api$analytics2; isInitialized.current = true; api === null || api === void 0 ? void 0 : (_api$analytics2 = api.analytics) === null || _api$analytics2 === void 0 ? 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(() => { if (fg('platform_editor_use_preferences_plugin')) { const refreshPrefrerence = async () => { if (document.visibilityState === 'visible') { try { await userPreferencesProvider.loadPreferences(); } catch (error) { logException(error, { location: 'editor-plugin-user-preferences/userPreferencesPlugin' }); } } }; return bind(document, { type: 'visibilitychange', listener: refreshPrefrerence }); } }, []); } }; };