@atlaskit/editor-plugin-user-preferences 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ # @atlaskit/editor-plugin-user-preferences
2
+
3
+ ## 0.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#150264](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/pull-requests/150264)
8
+ [`f714a99eb7641`](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/commits/f714a99eb7641) -
9
+ ED-27284 add user-preferences plugin
10
+
11
+ ### Patch Changes
12
+
13
+ - Updated dependencies
package/LICENSE.md ADDED
@@ -0,0 +1,11 @@
1
+ Copyright 2019 Atlassian Pty Ltd
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
4
+ compliance with the License. You may obtain a copy of the License at
5
+
6
+ http://www.apache.org/licenses/LICENSE-2.0
7
+
8
+ Unless required by applicable law or agreed to in writing, software distributed under the License is
9
+ distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
10
+ implied. See the License for the specific language governing permissions and limitations under the
11
+ License.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # Editor plugin user preferences
@@ -0,0 +1,30 @@
1
+ {
2
+ "extends": "../../../../tsconfig.entry-points.confluence.json",
3
+ "compilerOptions": {
4
+ "target": "es5",
5
+ "composite": true,
6
+ "outDir": "../dist",
7
+ "rootDir": "../"
8
+ },
9
+ "include": [
10
+ "../src/**/*.ts",
11
+ "../src/**/*.tsx"
12
+ ],
13
+ "exclude": [
14
+ "../src/**/__tests__/*",
15
+ "../src/**/*.test.*",
16
+ "../src/**/test.*",
17
+ "../src/**/examples.*"
18
+ ],
19
+ "references": [
20
+ {
21
+ "path": "../../editor-common/afm-cc/tsconfig.json"
22
+ },
23
+ {
24
+ "path": "../../editor-plugin-analytics/afm-cc/tsconfig.json"
25
+ },
26
+ {
27
+ "path": "../../../platform/feature-flags/afm-cc/tsconfig.json"
28
+ }
29
+ ]
30
+ }
@@ -0,0 +1,17 @@
1
+ {
2
+ "extends": "../tsconfig",
3
+ "compilerOptions": {
4
+ "target": "es5",
5
+ "paths": {}
6
+ },
7
+ "include": [
8
+ "../src/**/*.ts",
9
+ "../src/**/*.tsx"
10
+ ],
11
+ "exclude": [
12
+ "../src/**/__tests__/*",
13
+ "../src/**/*.test.*",
14
+ "../src/**/test.*",
15
+ "../src/**/examples.*"
16
+ ]
17
+ }
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "userPreferencesPlugin", {
7
+ enumerable: true,
8
+ get: function get() {
9
+ return _userPreferencesPlugin.userPreferencesPlugin;
10
+ }
11
+ });
12
+ var _userPreferencesPlugin = require("./userPreferencesPlugin");
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.updateToolbarDockingPosition = void 0;
8
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
9
+ var _analytics = require("@atlaskit/editor-common/analytics");
10
+ var _monitoring = require("@atlaskit/editor-common/monitoring");
11
+ var _main = require("./main");
12
+ var updateToolbarDockingPosition = exports.updateToolbarDockingPosition = function updateToolbarDockingPosition(_ref) {
13
+ var key = _ref.key,
14
+ value = _ref.value,
15
+ userPreferencesProvider = _ref.userPreferencesProvider,
16
+ editorAnalyticsApi = _ref.editorAnalyticsApi;
17
+ return function (_ref2) {
18
+ var tr = _ref2.tr;
19
+ try {
20
+ userPreferencesProvider.updatePreference(key, value);
21
+ } catch (error) {
22
+ (0, _monitoring.logException)(error, {
23
+ location: 'editor-plugin-user-preferences/userPreferencesPlugin'
24
+ });
25
+ }
26
+ tr.setMeta(_main.userPreferencesPluginKey, {
27
+ preferences: (0, _defineProperty2.default)({}, key, value)
28
+ });
29
+ if (key === 'toolbarDockingPosition') {
30
+ editorAnalyticsApi === null || editorAnalyticsApi === void 0 || editorAnalyticsApi.attachAnalyticsEvent({
31
+ action: _analytics.ACTION.UPDATED,
32
+ actionSubject: _analytics.ACTION_SUBJECT.USER_PREFERENCES,
33
+ actionSubjectId: _analytics.ACTION_SUBJECT_ID.SELECTION_TOOLBAR_PREFERENCES,
34
+ attributes: {
35
+ toolbarDocking: value
36
+ },
37
+ eventType: _analytics.EVENT_TYPE.TRACK
38
+ })(tr);
39
+ }
40
+ return tr;
41
+ };
42
+ };
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.userPreferencesPluginKey = exports.createPlugin = void 0;
8
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
9
+ var _safePlugin = require("@atlaskit/editor-common/safe-plugin");
10
+ var _state = require("@atlaskit/editor-prosemirror/state");
11
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
12
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
13
+ var userPreferencesPluginKey = exports.userPreferencesPluginKey = new _state.PluginKey('userPreferencesPlugin');
14
+ var createPlugin = exports.createPlugin = function createPlugin(pluginOptions, _api) {
15
+ var userPreferencesProvider = pluginOptions.userPreferencesProvider;
16
+ return new _safePlugin.SafePlugin({
17
+ key: userPreferencesPluginKey,
18
+ state: {
19
+ init: function init() {
20
+ return {
21
+ preferences: userPreferencesProvider.getPreferences()
22
+ };
23
+ },
24
+ apply: function apply(tr, currentPluginState) {
25
+ var meta = tr.getMeta(userPreferencesPluginKey);
26
+ if (meta !== null && meta !== void 0 && meta.preferences) {
27
+ return _objectSpread(_objectSpread({}, currentPluginState), {}, {
28
+ preferences: _objectSpread(_objectSpread({}, currentPluginState.preferences), meta.preferences)
29
+ });
30
+ }
31
+ return currentPluginState;
32
+ }
33
+ }
34
+ });
35
+ };
@@ -0,0 +1,118 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.userPreferencesPlugin = void 0;
8
+ var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
9
+ var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
10
+ var _react = require("react");
11
+ var _bindEventListener = require("bind-event-listener");
12
+ var _analytics = require("@atlaskit/editor-common/analytics");
13
+ var _monitoring = require("@atlaskit/editor-common/monitoring");
14
+ var _userPreferences = require("@atlaskit/editor-common/user-preferences");
15
+ var _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
16
+ var _commands = require("./pm-plugins/commands");
17
+ var _main = require("./pm-plugins/main");
18
+ var userPreferencesPlugin = exports.userPreferencesPlugin = function userPreferencesPlugin(_ref) {
19
+ var config = _ref.config,
20
+ api = _ref.api;
21
+ var userPreferencesProvider = config.userPreferencesProvider;
22
+ return {
23
+ name: 'userPreferences',
24
+ pmPlugins: function pmPlugins() {
25
+ return [{
26
+ name: 'userPreferencesPlugin',
27
+ plugin: function plugin() {
28
+ return (0, _main.createPlugin)(config, api);
29
+ }
30
+ }];
31
+ },
32
+ actions: {
33
+ updateUserPreference: function updateUserPreference(key, value) {
34
+ var _api$analytics;
35
+ return (0, _commands.updateToolbarDockingPosition)({
36
+ key: key,
37
+ value: value,
38
+ userPreferencesProvider: userPreferencesProvider,
39
+ editorAnalyticsApi: api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions
40
+ });
41
+ },
42
+ setDefaultPreferences: function setDefaultPreferences(preferences) {
43
+ userPreferencesProvider.setDefaultPreferences(preferences);
44
+ }
45
+ },
46
+ getSharedState: function getSharedState(editorState) {
47
+ if (!editorState) {
48
+ return null;
49
+ }
50
+ return _main.userPreferencesPluginKey.getState(editorState);
51
+ },
52
+ usePluginHook: function usePluginHook(_ref2) {
53
+ var editorView = _ref2.editorView;
54
+ var _useResolvedUserPrefe = (0, _userPreferences.useResolvedUserPreferences)(userPreferencesProvider),
55
+ resolvedUserPreferences = _useResolvedUserPrefe.resolvedUserPreferences;
56
+ var isInitialized = (0, _react.useRef)(false);
57
+ (0, _react.useEffect)(function () {
58
+ if ((0, _platformFeatureFlags.fg)('platform_editor_use_preferences_plugin')) {
59
+ if (userPreferencesProvider.isInitialized && !isInitialized.current) {
60
+ var _api$analytics2;
61
+ isInitialized.current = true;
62
+ api === null || api === void 0 || (_api$analytics2 = api.analytics) === null || _api$analytics2 === void 0 || _api$analytics2.actions.fireAnalyticsEvent({
63
+ action: _analytics.ACTION.INITIALISED,
64
+ actionSubject: _analytics.ACTION_SUBJECT.USER_PREFERENCES,
65
+ actionSubjectId: _analytics.ACTION_SUBJECT_ID.SELECTION_TOOLBAR_PREFERENCES,
66
+ attributes: {
67
+ toolbarDocking: resolvedUserPreferences.toolbarDockingPosition
68
+ },
69
+ eventType: _analytics.EVENT_TYPE.OPERATIONAL
70
+ });
71
+ }
72
+ editorView.dispatch(editorView.state.tr.setMeta(_main.userPreferencesPluginKey, {
73
+ preferences: resolvedUserPreferences
74
+ }));
75
+ }
76
+ }, [resolvedUserPreferences, editorView]);
77
+ (0, _react.useEffect)(function () {
78
+ if ((0, _platformFeatureFlags.fg)('platform_editor_use_preferences_plugin')) {
79
+ var refreshPrefrerence = /*#__PURE__*/function () {
80
+ var _ref3 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
81
+ return _regenerator.default.wrap(function _callee$(_context) {
82
+ while (1) switch (_context.prev = _context.next) {
83
+ case 0:
84
+ if (!(document.visibilityState === 'visible')) {
85
+ _context.next = 9;
86
+ break;
87
+ }
88
+ _context.prev = 1;
89
+ _context.next = 4;
90
+ return userPreferencesProvider.loadPreferences();
91
+ case 4:
92
+ _context.next = 9;
93
+ break;
94
+ case 6:
95
+ _context.prev = 6;
96
+ _context.t0 = _context["catch"](1);
97
+ (0, _monitoring.logException)(_context.t0, {
98
+ location: 'editor-plugin-user-preferences/userPreferencesPlugin'
99
+ });
100
+ case 9:
101
+ case "end":
102
+ return _context.stop();
103
+ }
104
+ }, _callee, null, [[1, 6]]);
105
+ }));
106
+ return function refreshPrefrerence() {
107
+ return _ref3.apply(this, arguments);
108
+ };
109
+ }();
110
+ return (0, _bindEventListener.bind)(document, {
111
+ type: 'visibilitychange',
112
+ listener: refreshPrefrerence
113
+ });
114
+ }
115
+ }, []);
116
+ }
117
+ };
118
+ };
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
@@ -0,0 +1,3 @@
1
+ /* eslint-disable @atlaskit/editor/no-re-export */
2
+ // Entry file in package.json
3
+ export { userPreferencesPlugin } from './userPreferencesPlugin';
@@ -0,0 +1,36 @@
1
+ import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE } from '@atlaskit/editor-common/analytics';
2
+ import { logException } from '@atlaskit/editor-common/monitoring';
3
+ import { userPreferencesPluginKey } from './main';
4
+ export const updateToolbarDockingPosition = ({
5
+ key,
6
+ value,
7
+ userPreferencesProvider,
8
+ editorAnalyticsApi
9
+ }) => ({
10
+ tr
11
+ }) => {
12
+ try {
13
+ userPreferencesProvider.updatePreference(key, value);
14
+ } catch (error) {
15
+ logException(error, {
16
+ location: 'editor-plugin-user-preferences/userPreferencesPlugin'
17
+ });
18
+ }
19
+ tr.setMeta(userPreferencesPluginKey, {
20
+ preferences: {
21
+ [key]: value
22
+ }
23
+ });
24
+ if (key === 'toolbarDockingPosition') {
25
+ editorAnalyticsApi === null || editorAnalyticsApi === void 0 ? void 0 : editorAnalyticsApi.attachAnalyticsEvent({
26
+ action: ACTION.UPDATED,
27
+ actionSubject: ACTION_SUBJECT.USER_PREFERENCES,
28
+ actionSubjectId: ACTION_SUBJECT_ID.SELECTION_TOOLBAR_PREFERENCES,
29
+ attributes: {
30
+ toolbarDocking: value
31
+ },
32
+ eventType: EVENT_TYPE.TRACK
33
+ })(tr);
34
+ }
35
+ return tr;
36
+ };
@@ -0,0 +1,31 @@
1
+ import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
2
+ import { PluginKey } from '@atlaskit/editor-prosemirror/state';
3
+ export const userPreferencesPluginKey = new PluginKey('userPreferencesPlugin');
4
+ export const createPlugin = (pluginOptions, _api) => {
5
+ const {
6
+ userPreferencesProvider
7
+ } = pluginOptions;
8
+ return new SafePlugin({
9
+ key: userPreferencesPluginKey,
10
+ state: {
11
+ init() {
12
+ return {
13
+ preferences: userPreferencesProvider.getPreferences()
14
+ };
15
+ },
16
+ apply: (tr, currentPluginState) => {
17
+ const meta = tr.getMeta(userPreferencesPluginKey);
18
+ if (meta !== null && meta !== void 0 && meta.preferences) {
19
+ return {
20
+ ...currentPluginState,
21
+ preferences: {
22
+ ...currentPluginState.preferences,
23
+ ...meta.preferences
24
+ }
25
+ };
26
+ }
27
+ return currentPluginState;
28
+ }
29
+ }
30
+ });
31
+ };
@@ -0,0 +1,94 @@
1
+ import { useEffect, useRef } from 'react';
2
+ import { bind } from 'bind-event-listener';
3
+ import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE } from '@atlaskit/editor-common/analytics';
4
+ import { logException } from '@atlaskit/editor-common/monitoring';
5
+ import { useResolvedUserPreferences } from '@atlaskit/editor-common/user-preferences';
6
+ import { fg } from '@atlaskit/platform-feature-flags';
7
+ import { updateToolbarDockingPosition } from './pm-plugins/commands';
8
+ import { createPlugin, userPreferencesPluginKey } from './pm-plugins/main';
9
+ export const userPreferencesPlugin = ({
10
+ config,
11
+ api
12
+ }) => {
13
+ const {
14
+ userPreferencesProvider
15
+ } = config;
16
+ return {
17
+ name: 'userPreferences',
18
+ pmPlugins() {
19
+ return [{
20
+ name: 'userPreferencesPlugin',
21
+ plugin: () => {
22
+ return createPlugin(config, api);
23
+ }
24
+ }];
25
+ },
26
+ actions: {
27
+ updateUserPreference: (key, value) => {
28
+ var _api$analytics;
29
+ return updateToolbarDockingPosition({
30
+ key,
31
+ value,
32
+ userPreferencesProvider,
33
+ editorAnalyticsApi: api === null || api === void 0 ? void 0 : (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions
34
+ });
35
+ },
36
+ setDefaultPreferences: preferences => {
37
+ userPreferencesProvider.setDefaultPreferences(preferences);
38
+ }
39
+ },
40
+ getSharedState(editorState) {
41
+ if (!editorState) {
42
+ return null;
43
+ }
44
+ return userPreferencesPluginKey.getState(editorState);
45
+ },
46
+ usePluginHook({
47
+ editorView
48
+ }) {
49
+ const {
50
+ resolvedUserPreferences
51
+ } = useResolvedUserPreferences(userPreferencesProvider);
52
+ const isInitialized = useRef(false);
53
+ useEffect(() => {
54
+ if (fg('platform_editor_use_preferences_plugin')) {
55
+ if (userPreferencesProvider.isInitialized && !isInitialized.current) {
56
+ var _api$analytics2;
57
+ isInitialized.current = true;
58
+ api === null || api === void 0 ? void 0 : (_api$analytics2 = api.analytics) === null || _api$analytics2 === void 0 ? void 0 : _api$analytics2.actions.fireAnalyticsEvent({
59
+ action: ACTION.INITIALISED,
60
+ actionSubject: ACTION_SUBJECT.USER_PREFERENCES,
61
+ actionSubjectId: ACTION_SUBJECT_ID.SELECTION_TOOLBAR_PREFERENCES,
62
+ attributes: {
63
+ toolbarDocking: resolvedUserPreferences.toolbarDockingPosition
64
+ },
65
+ eventType: EVENT_TYPE.OPERATIONAL
66
+ });
67
+ }
68
+ editorView.dispatch(editorView.state.tr.setMeta(userPreferencesPluginKey, {
69
+ preferences: resolvedUserPreferences
70
+ }));
71
+ }
72
+ }, [resolvedUserPreferences, editorView]);
73
+ useEffect(() => {
74
+ if (fg('platform_editor_use_preferences_plugin')) {
75
+ const refreshPrefrerence = async () => {
76
+ if (document.visibilityState === 'visible') {
77
+ try {
78
+ await userPreferencesProvider.loadPreferences();
79
+ } catch (error) {
80
+ logException(error, {
81
+ location: 'editor-plugin-user-preferences/userPreferencesPlugin'
82
+ });
83
+ }
84
+ }
85
+ };
86
+ return bind(document, {
87
+ type: 'visibilitychange',
88
+ listener: refreshPrefrerence
89
+ });
90
+ }
91
+ }, []);
92
+ }
93
+ };
94
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ /* eslint-disable @atlaskit/editor/no-re-export */
2
+ // Entry file in package.json
3
+ export { userPreferencesPlugin } from './userPreferencesPlugin';
@@ -0,0 +1,35 @@
1
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
+ import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE } from '@atlaskit/editor-common/analytics';
3
+ import { logException } from '@atlaskit/editor-common/monitoring';
4
+ import { userPreferencesPluginKey } from './main';
5
+ export var updateToolbarDockingPosition = function updateToolbarDockingPosition(_ref) {
6
+ var key = _ref.key,
7
+ value = _ref.value,
8
+ userPreferencesProvider = _ref.userPreferencesProvider,
9
+ editorAnalyticsApi = _ref.editorAnalyticsApi;
10
+ return function (_ref2) {
11
+ var tr = _ref2.tr;
12
+ try {
13
+ userPreferencesProvider.updatePreference(key, value);
14
+ } catch (error) {
15
+ logException(error, {
16
+ location: 'editor-plugin-user-preferences/userPreferencesPlugin'
17
+ });
18
+ }
19
+ tr.setMeta(userPreferencesPluginKey, {
20
+ preferences: _defineProperty({}, key, value)
21
+ });
22
+ if (key === 'toolbarDockingPosition') {
23
+ editorAnalyticsApi === null || editorAnalyticsApi === void 0 || editorAnalyticsApi.attachAnalyticsEvent({
24
+ action: ACTION.UPDATED,
25
+ actionSubject: ACTION_SUBJECT.USER_PREFERENCES,
26
+ actionSubjectId: ACTION_SUBJECT_ID.SELECTION_TOOLBAR_PREFERENCES,
27
+ attributes: {
28
+ toolbarDocking: value
29
+ },
30
+ eventType: EVENT_TYPE.TRACK
31
+ })(tr);
32
+ }
33
+ return tr;
34
+ };
35
+ };
@@ -0,0 +1,28 @@
1
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
3
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
4
+ import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
5
+ import { PluginKey } from '@atlaskit/editor-prosemirror/state';
6
+ export var userPreferencesPluginKey = new PluginKey('userPreferencesPlugin');
7
+ export var createPlugin = function createPlugin(pluginOptions, _api) {
8
+ var userPreferencesProvider = pluginOptions.userPreferencesProvider;
9
+ return new SafePlugin({
10
+ key: userPreferencesPluginKey,
11
+ state: {
12
+ init: function init() {
13
+ return {
14
+ preferences: userPreferencesProvider.getPreferences()
15
+ };
16
+ },
17
+ apply: function apply(tr, currentPluginState) {
18
+ var meta = tr.getMeta(userPreferencesPluginKey);
19
+ if (meta !== null && meta !== void 0 && meta.preferences) {
20
+ return _objectSpread(_objectSpread({}, currentPluginState), {}, {
21
+ preferences: _objectSpread(_objectSpread({}, currentPluginState.preferences), meta.preferences)
22
+ });
23
+ }
24
+ return currentPluginState;
25
+ }
26
+ }
27
+ });
28
+ };
@@ -0,0 +1,111 @@
1
+ import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
2
+ import _regeneratorRuntime from "@babel/runtime/regenerator";
3
+ import { useEffect, useRef } from 'react';
4
+ import { bind } from 'bind-event-listener';
5
+ import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE } from '@atlaskit/editor-common/analytics';
6
+ import { logException } from '@atlaskit/editor-common/monitoring';
7
+ import { useResolvedUserPreferences } from '@atlaskit/editor-common/user-preferences';
8
+ import { fg } from '@atlaskit/platform-feature-flags';
9
+ import { updateToolbarDockingPosition } from './pm-plugins/commands';
10
+ import { createPlugin, userPreferencesPluginKey } from './pm-plugins/main';
11
+ export var userPreferencesPlugin = function userPreferencesPlugin(_ref) {
12
+ var config = _ref.config,
13
+ api = _ref.api;
14
+ var userPreferencesProvider = config.userPreferencesProvider;
15
+ return {
16
+ name: 'userPreferences',
17
+ pmPlugins: function pmPlugins() {
18
+ return [{
19
+ name: 'userPreferencesPlugin',
20
+ plugin: function plugin() {
21
+ return createPlugin(config, api);
22
+ }
23
+ }];
24
+ },
25
+ actions: {
26
+ updateUserPreference: function updateUserPreference(key, value) {
27
+ var _api$analytics;
28
+ return updateToolbarDockingPosition({
29
+ key: key,
30
+ value: value,
31
+ userPreferencesProvider: userPreferencesProvider,
32
+ editorAnalyticsApi: api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions
33
+ });
34
+ },
35
+ setDefaultPreferences: function setDefaultPreferences(preferences) {
36
+ userPreferencesProvider.setDefaultPreferences(preferences);
37
+ }
38
+ },
39
+ getSharedState: function getSharedState(editorState) {
40
+ if (!editorState) {
41
+ return null;
42
+ }
43
+ return userPreferencesPluginKey.getState(editorState);
44
+ },
45
+ usePluginHook: function usePluginHook(_ref2) {
46
+ var editorView = _ref2.editorView;
47
+ var _useResolvedUserPrefe = useResolvedUserPreferences(userPreferencesProvider),
48
+ resolvedUserPreferences = _useResolvedUserPrefe.resolvedUserPreferences;
49
+ var isInitialized = useRef(false);
50
+ useEffect(function () {
51
+ if (fg('platform_editor_use_preferences_plugin')) {
52
+ if (userPreferencesProvider.isInitialized && !isInitialized.current) {
53
+ var _api$analytics2;
54
+ isInitialized.current = true;
55
+ api === null || api === void 0 || (_api$analytics2 = api.analytics) === null || _api$analytics2 === void 0 || _api$analytics2.actions.fireAnalyticsEvent({
56
+ action: ACTION.INITIALISED,
57
+ actionSubject: ACTION_SUBJECT.USER_PREFERENCES,
58
+ actionSubjectId: ACTION_SUBJECT_ID.SELECTION_TOOLBAR_PREFERENCES,
59
+ attributes: {
60
+ toolbarDocking: resolvedUserPreferences.toolbarDockingPosition
61
+ },
62
+ eventType: EVENT_TYPE.OPERATIONAL
63
+ });
64
+ }
65
+ editorView.dispatch(editorView.state.tr.setMeta(userPreferencesPluginKey, {
66
+ preferences: resolvedUserPreferences
67
+ }));
68
+ }
69
+ }, [resolvedUserPreferences, editorView]);
70
+ useEffect(function () {
71
+ if (fg('platform_editor_use_preferences_plugin')) {
72
+ var refreshPrefrerence = /*#__PURE__*/function () {
73
+ var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
74
+ return _regeneratorRuntime.wrap(function _callee$(_context) {
75
+ while (1) switch (_context.prev = _context.next) {
76
+ case 0:
77
+ if (!(document.visibilityState === 'visible')) {
78
+ _context.next = 9;
79
+ break;
80
+ }
81
+ _context.prev = 1;
82
+ _context.next = 4;
83
+ return userPreferencesProvider.loadPreferences();
84
+ case 4:
85
+ _context.next = 9;
86
+ break;
87
+ case 6:
88
+ _context.prev = 6;
89
+ _context.t0 = _context["catch"](1);
90
+ logException(_context.t0, {
91
+ location: 'editor-plugin-user-preferences/userPreferencesPlugin'
92
+ });
93
+ case 9:
94
+ case "end":
95
+ return _context.stop();
96
+ }
97
+ }, _callee, null, [[1, 6]]);
98
+ }));
99
+ return function refreshPrefrerence() {
100
+ return _ref3.apply(this, arguments);
101
+ };
102
+ }();
103
+ return bind(document, {
104
+ type: 'visibilitychange',
105
+ listener: refreshPrefrerence
106
+ });
107
+ }
108
+ }, []);
109
+ }
110
+ };
111
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ export { userPreferencesPlugin } from './userPreferencesPlugin';
2
+ export type { UserPreferencesPlugin } from './userPreferencesPluginType';
@@ -0,0 +1,10 @@
1
+ import { EditorAnalyticsAPI } from '@atlaskit/editor-common/analytics';
2
+ import type { EditorCommand } from '@atlaskit/editor-common/types';
3
+ import { ResolvedUserPreferences, UserPreferencesProvider } from '@atlaskit/editor-common/user-preferences';
4
+ import { PrefKey } from '../userPreferencesPluginType';
5
+ export declare const updateToolbarDockingPosition: ({ key, value, userPreferencesProvider, editorAnalyticsApi, }: {
6
+ key: PrefKey;
7
+ value: ResolvedUserPreferences[PrefKey];
8
+ userPreferencesProvider: UserPreferencesProvider;
9
+ editorAnalyticsApi?: EditorAnalyticsAPI | undefined;
10
+ }) => EditorCommand;
@@ -0,0 +1,11 @@
1
+ import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
2
+ import { ExtractInjectionAPI } from '@atlaskit/editor-common/types';
3
+ import { ResolvedUserPreferences } from '@atlaskit/editor-common/user-preferences';
4
+ import { PluginKey } from '@atlaskit/editor-prosemirror/state';
5
+ import { Config, UserPreferencesPlugin } from '../userPreferencesPluginType';
6
+ export declare const userPreferencesPluginKey: PluginKey<any>;
7
+ type UserPreferencesPluginState = {
8
+ preferences: ResolvedUserPreferences;
9
+ };
10
+ export declare const createPlugin: (pluginOptions: Config, _api: ExtractInjectionAPI<UserPreferencesPlugin> | undefined) => SafePlugin<UserPreferencesPluginState>;
11
+ export {};
@@ -0,0 +1,2 @@
1
+ import type { UserPreferencesPlugin } from './userPreferencesPluginType';
2
+ export declare const userPreferencesPlugin: UserPreferencesPlugin;
@@ -0,0 +1,20 @@
1
+ import type { EditorCommand, NextEditorPlugin, OptionalPlugin } from '@atlaskit/editor-common/types';
2
+ import { ResolvedUserPreferences, UserPreferences, UserPreferencesProvider } from '@atlaskit/editor-common/user-preferences';
3
+ import type { AnalyticsPlugin } from '@atlaskit/editor-plugin-analytics';
4
+ export type Config = {
5
+ userPreferencesProvider: UserPreferencesProvider;
6
+ };
7
+ export type PrefKey = keyof UserPreferences;
8
+ export type ResolvedPrefKey = keyof ResolvedUserPreferences;
9
+ export type UserPreferencesSharedState = {
10
+ preferences: ResolvedUserPreferences;
11
+ };
12
+ export type UserPreferencesPlugin = NextEditorPlugin<'userPreferences', {
13
+ pluginConfiguration: Config;
14
+ actions: {
15
+ updateUserPreference: (key: PrefKey, value: ResolvedUserPreferences[PrefKey]) => EditorCommand;
16
+ setDefaultPreferences: (preferences: ResolvedUserPreferences) => void;
17
+ };
18
+ sharedState: UserPreferencesSharedState;
19
+ dependencies: [OptionalPlugin<AnalyticsPlugin>];
20
+ }>;
@@ -0,0 +1,2 @@
1
+ export { userPreferencesPlugin } from './userPreferencesPlugin';
2
+ export type { UserPreferencesPlugin } from './userPreferencesPluginType';
@@ -0,0 +1,10 @@
1
+ import { EditorAnalyticsAPI } from '@atlaskit/editor-common/analytics';
2
+ import type { EditorCommand } from '@atlaskit/editor-common/types';
3
+ import { ResolvedUserPreferences, UserPreferencesProvider } from '@atlaskit/editor-common/user-preferences';
4
+ import { PrefKey } from '../userPreferencesPluginType';
5
+ export declare const updateToolbarDockingPosition: ({ key, value, userPreferencesProvider, editorAnalyticsApi, }: {
6
+ key: PrefKey;
7
+ value: ResolvedUserPreferences[PrefKey];
8
+ userPreferencesProvider: UserPreferencesProvider;
9
+ editorAnalyticsApi?: EditorAnalyticsAPI | undefined;
10
+ }) => EditorCommand;
@@ -0,0 +1,11 @@
1
+ import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
2
+ import { ExtractInjectionAPI } from '@atlaskit/editor-common/types';
3
+ import { ResolvedUserPreferences } from '@atlaskit/editor-common/user-preferences';
4
+ import { PluginKey } from '@atlaskit/editor-prosemirror/state';
5
+ import { Config, UserPreferencesPlugin } from '../userPreferencesPluginType';
6
+ export declare const userPreferencesPluginKey: PluginKey<any>;
7
+ type UserPreferencesPluginState = {
8
+ preferences: ResolvedUserPreferences;
9
+ };
10
+ export declare const createPlugin: (pluginOptions: Config, _api: ExtractInjectionAPI<UserPreferencesPlugin> | undefined) => SafePlugin<UserPreferencesPluginState>;
11
+ export {};
@@ -0,0 +1,2 @@
1
+ import type { UserPreferencesPlugin } from './userPreferencesPluginType';
2
+ export declare const userPreferencesPlugin: UserPreferencesPlugin;
@@ -0,0 +1,22 @@
1
+ import type { EditorCommand, NextEditorPlugin, OptionalPlugin } from '@atlaskit/editor-common/types';
2
+ import { ResolvedUserPreferences, UserPreferences, UserPreferencesProvider } from '@atlaskit/editor-common/user-preferences';
3
+ import type { AnalyticsPlugin } from '@atlaskit/editor-plugin-analytics';
4
+ export type Config = {
5
+ userPreferencesProvider: UserPreferencesProvider;
6
+ };
7
+ export type PrefKey = keyof UserPreferences;
8
+ export type ResolvedPrefKey = keyof ResolvedUserPreferences;
9
+ export type UserPreferencesSharedState = {
10
+ preferences: ResolvedUserPreferences;
11
+ };
12
+ export type UserPreferencesPlugin = NextEditorPlugin<'userPreferences', {
13
+ pluginConfiguration: Config;
14
+ actions: {
15
+ updateUserPreference: (key: PrefKey, value: ResolvedUserPreferences[PrefKey]) => EditorCommand;
16
+ setDefaultPreferences: (preferences: ResolvedUserPreferences) => void;
17
+ };
18
+ sharedState: UserPreferencesSharedState;
19
+ dependencies: [
20
+ OptionalPlugin<AnalyticsPlugin>
21
+ ];
22
+ }>;
@@ -0,0 +1,43 @@
1
+ import React from 'react';
2
+
3
+ import { AtlassianInternalWarning, code, md } from '@atlaskit/docs';
4
+ // eslint-disable-next-line @atlassian/tangerine/import/entry-points
5
+ import { createEditorUseOnlyNotice } from '@atlaskit/editor-common/doc-utils';
6
+ import { token } from '@atlaskit/tokens';
7
+
8
+ export default md`
9
+
10
+ ${createEditorUseOnlyNotice('Editor Plugin User Preferences', [
11
+ { name: 'Editor Core', link: '/packages/editor/editor-core' },
12
+ ])}
13
+
14
+
15
+ ${
16
+ (
17
+ // eslint-disable-next-line @atlaskit/ui-styling-standard/enforce-style-prop -- Ignored via go/DSP-18766
18
+ <div style={{ marginTop: token('space.100', '8px') }}>
19
+ <AtlassianInternalWarning />
20
+ </div>
21
+ )
22
+ }
23
+
24
+ This package includes the user preferences plugin used by \`@atlaskit/editor-core\`.
25
+
26
+ ## Usage
27
+ ---
28
+
29
+ The \`dependencies\`, \`configuration\`, \`state\`, \`actions\`, and \`commands\` of the plugin are defined
30
+ below:
31
+
32
+ ${code`
33
+ type UserPreferencesPlugin = NextEditorPlugin<'userPreferences'>
34
+ `}
35
+
36
+
37
+ ## Support
38
+ ---
39
+ For internal Atlassian, visit the slack channel [#help-editor](https://atlassian.slack.com/archives/CFG3PSQ9E) for support or visit [go/editor-help](https://go/editor-help) to submit a bug.
40
+ ## License
41
+ ---
42
+ Please see [Atlassian Frontend - License](https://hello.atlassian.net/wiki/spaces/AF/pages/2589099144/Documentation#License) for more licensing information.
43
+ `;
package/package.json ADDED
@@ -0,0 +1,89 @@
1
+ {
2
+ "name": "@atlaskit/editor-plugin-user-preferences",
3
+ "version": "0.1.0",
4
+ "description": "UserPreferences plugin for @atlaskit/editor-core",
5
+ "author": "Atlassian Pty Ltd",
6
+ "license": "Apache-2.0",
7
+ "publishConfig": {
8
+ "registry": "https://registry.npmjs.org/"
9
+ },
10
+ "atlassian": {
11
+ "team": "Editor: Lego",
12
+ "releaseModel": "continuous",
13
+ "singleton": true,
14
+ "runReact18": true
15
+ },
16
+ "repository": "https://bitbucket.org/atlassian/atlassian-frontend-mirror",
17
+ "main": "dist/cjs/index.js",
18
+ "module": "dist/esm/index.js",
19
+ "module:es2019": "dist/es2019/index.js",
20
+ "types": "dist/types/index.d.ts",
21
+ "typesVersions": {
22
+ ">=4.5 <4.9": {
23
+ "*": [
24
+ "dist/types-ts4.5/*",
25
+ "dist/types-ts4.5/index.d.ts"
26
+ ]
27
+ }
28
+ },
29
+ "sideEffects": false,
30
+ "atlaskit:src": "src/index.ts",
31
+ "af:exports": {
32
+ ".": "./src/index.ts"
33
+ },
34
+ "dependencies": {
35
+ "@atlaskit/editor-common": "^105.2.0",
36
+ "@atlaskit/editor-plugin-analytics": "^2.3.0",
37
+ "@atlaskit/editor-prosemirror": "7.0.0",
38
+ "@atlaskit/platform-feature-flags": "^1.1.0",
39
+ "@babel/runtime": "^7.0.0",
40
+ "bind-event-listener": "^3.0.0"
41
+ },
42
+ "peerDependencies": {
43
+ "react": "^18.2.0"
44
+ },
45
+ "devDependencies": {
46
+ "typescript": "~5.4.2"
47
+ },
48
+ "techstack": {
49
+ "@atlassian/frontend": {
50
+ "code-structure": [
51
+ "editor-plugin"
52
+ ],
53
+ "import-structure": [
54
+ "atlassian-conventions"
55
+ ],
56
+ "circular-dependencies": [
57
+ "file-and-folder-level"
58
+ ]
59
+ },
60
+ "@repo/internal": {
61
+ "dom-events": "use-bind-event-listener",
62
+ "analytics": [
63
+ "analytics-next"
64
+ ],
65
+ "design-tokens": [
66
+ "color"
67
+ ],
68
+ "theming": [
69
+ "react-context"
70
+ ],
71
+ "ui-components": [
72
+ "lite-mode"
73
+ ],
74
+ "deprecation": "no-deprecated-imports",
75
+ "styling": [
76
+ "emotion",
77
+ "emotion"
78
+ ],
79
+ "imports": [
80
+ "import-no-extraneous-disable-for-examples-and-docs"
81
+ ]
82
+ }
83
+ },
84
+ "platform-feature-flags": {
85
+ "platform_editor_use_preferences_plugin": {
86
+ "type": "boolean"
87
+ }
88
+ }
89
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "../../../tsconfig.json",
3
+ "include": [
4
+ "src/**/*.ts",
5
+ "src/**/*.tsx"
6
+ ],
7
+ "compilerOptions": {}
8
+ }