@atlaskit/editor-common 93.4.2 → 93.4.4

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.
Files changed (31) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/UNSAFE_do_not_use_editor_context/package.json +15 -0
  3. package/dist/cjs/analytics/types/enums.js +4 -0
  4. package/dist/cjs/analytics/types/mention-events.js +5 -0
  5. package/dist/cjs/monitoring/error.js +1 -1
  6. package/dist/cjs/ui/DropList/index.js +1 -1
  7. package/dist/cjs/ui/EditorContext/index.js +9 -0
  8. package/dist/cjs/with-plugin-state/index.js +273 -18
  9. package/dist/es2019/analytics/types/enums.js +4 -0
  10. package/dist/es2019/analytics/types/mention-events.js +1 -0
  11. package/dist/es2019/monitoring/error.js +1 -1
  12. package/dist/es2019/ui/DropList/index.js +1 -1
  13. package/dist/es2019/ui/EditorContext/index.js +2 -0
  14. package/dist/es2019/with-plugin-state/index.js +217 -5
  15. package/dist/esm/analytics/types/enums.js +4 -0
  16. package/dist/esm/analytics/types/mention-events.js +1 -0
  17. package/dist/esm/monitoring/error.js +1 -1
  18. package/dist/esm/ui/DropList/index.js +1 -1
  19. package/dist/esm/ui/EditorContext/index.js +2 -0
  20. package/dist/esm/with-plugin-state/index.js +272 -17
  21. package/dist/types/analytics/types/enums.d.ts +4 -0
  22. package/dist/types/analytics/types/events.d.ts +2 -1
  23. package/dist/types/analytics/types/mention-events.d.ts +80 -0
  24. package/dist/types/ui/EditorContext/index.d.ts +2 -0
  25. package/dist/types/with-plugin-state/index.d.ts +35 -1
  26. package/dist/types-ts4.5/analytics/types/enums.d.ts +4 -0
  27. package/dist/types-ts4.5/analytics/types/events.d.ts +2 -1
  28. package/dist/types-ts4.5/analytics/types/mention-events.d.ts +80 -0
  29. package/dist/types-ts4.5/ui/EditorContext/index.d.ts +2 -0
  30. package/dist/types-ts4.5/with-plugin-state/index.d.ts +35 -1
  31. package/package.json +3 -2
@@ -1,9 +1,12 @@
1
1
  import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
+ import _extends from "@babel/runtime/helpers/extends";
2
3
  import React from 'react';
3
4
  import PropTypes from 'prop-types';
5
+ import { fg } from '@atlaskit/platform-feature-flags';
4
6
  import { ACTION, ACTION_SUBJECT, EVENT_TYPE } from '../analytics';
5
7
  import { createDispatch } from '../event-dispatcher';
6
8
  import { startMeasure, stopMeasure } from '../performance-measures';
9
+ import { EditorContext } from '../ui/EditorContext';
7
10
  import { analyticsEventKey } from '../utils';
8
11
  const DEFAULT_SAMPLING_RATE = 100;
9
12
  const DEFAULT_SLOW_THRESHOLD = 4;
@@ -50,6 +53,215 @@ const DEFAULT_SLOW_THRESHOLD = 4;
50
53
  *
51
54
  */
52
55
  class WithPluginState extends React.Component {
56
+ constructor(props) {
57
+ super(props);
58
+ }
59
+ render() {
60
+ if (fg('platform_editor_react18_phase2')) {
61
+ return /*#__PURE__*/React.createElement(WithPluginStateNew, this.props);
62
+ }
63
+ return /*#__PURE__*/React.createElement(WithPluginStateOld, this.props);
64
+ }
65
+ }
66
+ function WithPluginStateNew(props) {
67
+ const context = React.useContext(EditorContext);
68
+ return /*#__PURE__*/React.createElement(WithPluginStateInner, _extends({}, props, {
69
+ editorActions: context === null || context === void 0 ? void 0 : context.editorActions
70
+ }));
71
+ }
72
+ export class WithPluginStateInner extends React.Component {
73
+ constructor(props) {
74
+ super(props);
75
+ _defineProperty(this, "listeners", {});
76
+ _defineProperty(this, "debounce", null);
77
+ _defineProperty(this, "notAppliedState", {});
78
+ _defineProperty(this, "isSubscribed", false);
79
+ _defineProperty(this, "callsCount", 0);
80
+ _defineProperty(this, "handlePluginStateChange", (propName, pluginName, performanceOptions, skipEqualityCheck) => pluginState => {
81
+ // skipEqualityCheck is being used for old plugins since they are mutating plugin state instead of creating a new one
82
+ if (this.state[propName] !== pluginState || skipEqualityCheck) {
83
+ this.updateState({
84
+ stateSubset: {
85
+ [propName]: pluginState
86
+ },
87
+ pluginName,
88
+ performanceOptions
89
+ });
90
+ }
91
+ });
92
+ /**
93
+ * Debounces setState calls in order to reduce number of re-renders caused by several plugin state changes.
94
+ */
95
+ _defineProperty(this, "updateState", ({
96
+ stateSubset,
97
+ pluginName,
98
+ performanceOptions
99
+ }) => {
100
+ this.notAppliedState = {
101
+ ...this.notAppliedState,
102
+ ...stateSubset
103
+ };
104
+ if (this.debounce) {
105
+ window.clearTimeout(this.debounce);
106
+ }
107
+ const debounce = this.props.debounce !== false ? fn => window.setTimeout(fn, 0) : fn => fn();
108
+ this.debounce = debounce(() => {
109
+ const measure = `🦉${pluginName}::WithPluginState`;
110
+ performanceOptions.trackingEnabled && startMeasure(measure);
111
+ this.setState(this.notAppliedState, () => {
112
+ performanceOptions.trackingEnabled && stopMeasure(measure, duration => {
113
+ // Each WithPluginState component will fire analytics event no more than once every `samplingLimit` times
114
+ if (++this.callsCount % performanceOptions.samplingRate === 0 && duration > performanceOptions.slowThreshold) {
115
+ this.dispatchAnalyticsEvent({
116
+ action: ACTION.WITH_PLUGIN_STATE_CALLED,
117
+ actionSubject: ACTION_SUBJECT.EDITOR,
118
+ eventType: EVENT_TYPE.OPERATIONAL,
119
+ attributes: {
120
+ plugin: pluginName,
121
+ duration
122
+ }
123
+ });
124
+ }
125
+ });
126
+ });
127
+ this.debounce = null;
128
+ this.notAppliedState = {};
129
+ });
130
+ });
131
+ _defineProperty(this, "dispatchAnalyticsEvent", payload => {
132
+ const eventDispatcher = this.getEventDispatcher();
133
+ if (eventDispatcher) {
134
+ const dispatch = createDispatch(eventDispatcher);
135
+ dispatch(analyticsEventKey, {
136
+ payload
137
+ });
138
+ }
139
+ });
140
+ this.state = this.getPluginsStates(this.props.plugins, this.getEditorView(props));
141
+ }
142
+ getEditorView(maybeProps) {
143
+ const props = maybeProps || this.props;
144
+ const editorActions = props.editorActions;
145
+ return props.editorView || (editorActions === null || editorActions === void 0 ? void 0 : editorActions._privateGetEditorView());
146
+ }
147
+ getEventDispatcher(maybeProps) {
148
+ var _props$editorActions;
149
+ const props = maybeProps || this.props;
150
+ return props.eventDispatcher || ((_props$editorActions = props.editorActions) === null || _props$editorActions === void 0 ? void 0 : _props$editorActions._privateGetEventDispatcher());
151
+ }
152
+ getPluginsStates(plugins, editorView) {
153
+ if (!editorView || !plugins) {
154
+ return {};
155
+ }
156
+ const keys = Object.keys(plugins);
157
+ return keys.reduce((acc, propName) => {
158
+ const pluginKey = plugins[propName];
159
+ if (!pluginKey) {
160
+ return acc;
161
+ }
162
+ acc[propName] = pluginKey.getState(editorView.state);
163
+ return acc;
164
+ }, {});
165
+ }
166
+ subscribe(props) {
167
+ var _uiTracking$samplingR, _uiTracking$slowThres;
168
+ const plugins = props.plugins;
169
+ const eventDispatcher = this.getEventDispatcher(props);
170
+ const editorView = this.getEditorView(props);
171
+ if (!eventDispatcher || !editorView || this.isSubscribed) {
172
+ return;
173
+ }
174
+
175
+ // TODO: ED-15663
176
+ // Please, do not copy or use this kind of code below
177
+ // @ts-ignore
178
+ const fakePluginKey = {
179
+ key: 'analyticsPlugin$',
180
+ getState: state => {
181
+ return state['analyticsPlugin$'];
182
+ }
183
+ };
184
+ const analyticsPlugin = fakePluginKey.getState(editorView.state);
185
+ const uiTracking = analyticsPlugin && analyticsPlugin.performanceTracking ? analyticsPlugin.performanceTracking.uiTracking || {} : {};
186
+ const trackingEnabled = uiTracking.enabled === true;
187
+ const samplingRate = (_uiTracking$samplingR = uiTracking.samplingRate) !== null && _uiTracking$samplingR !== void 0 ? _uiTracking$samplingR : DEFAULT_SAMPLING_RATE;
188
+ const slowThreshold = (_uiTracking$slowThres = uiTracking.slowThreshold) !== null && _uiTracking$slowThres !== void 0 ? _uiTracking$slowThres : DEFAULT_SLOW_THRESHOLD;
189
+ this.isSubscribed = true;
190
+ const pluginsStates = this.getPluginsStates(plugins, editorView);
191
+ this.setState(pluginsStates);
192
+ Object.keys(plugins).forEach(propName => {
193
+ const pluginKey = plugins[propName];
194
+ if (!pluginKey) {
195
+ return;
196
+ }
197
+ const pluginName = pluginKey.key;
198
+ const pluginState = pluginsStates[propName];
199
+ const isPluginWithSubscribe = pluginState && pluginState.subscribe;
200
+ const handler = this.handlePluginStateChange(propName, pluginName, {
201
+ samplingRate,
202
+ slowThreshold,
203
+ trackingEnabled
204
+ }, isPluginWithSubscribe);
205
+ if (isPluginWithSubscribe) {
206
+ pluginState.subscribe(handler);
207
+ } else {
208
+ eventDispatcher.on(pluginKey.key, handler);
209
+ }
210
+ this.listeners[pluginKey.key] = {
211
+ handler,
212
+ pluginKey
213
+ };
214
+ });
215
+ }
216
+ unsubscribe() {
217
+ const eventDispatcher = this.getEventDispatcher();
218
+ const editorView = this.getEditorView();
219
+ if (!eventDispatcher || !editorView || !this.isSubscribed) {
220
+ return;
221
+ }
222
+ Object.keys(this.listeners).forEach(key => {
223
+ const pluginState = this.listeners[key].pluginKey.getState(editorView.state);
224
+ if (pluginState && pluginState.unsubscribe) {
225
+ pluginState.unsubscribe(this.listeners[key].handler);
226
+ } else {
227
+ eventDispatcher.off(key, this.listeners[key].handler);
228
+ }
229
+ });
230
+ this.listeners = [];
231
+ }
232
+ subscribeToContextUpdates() {
233
+ var _this$props$editorAct;
234
+ (_this$props$editorAct = this.props.editorActions) === null || _this$props$editorAct === void 0 ? void 0 : _this$props$editorAct._privateSubscribe(() => this.subscribe(this.props));
235
+ }
236
+ unsubscribeFromContextUpdates() {
237
+ var _this$props$editorAct2;
238
+ (_this$props$editorAct2 = this.props.editorActions) === null || _this$props$editorAct2 === void 0 ? void 0 : _this$props$editorAct2._privateUnsubscribe(() => this.subscribe(this.props));
239
+ }
240
+ componentDidMount() {
241
+ this.subscribe(this.props);
242
+ this.subscribeToContextUpdates();
243
+ }
244
+ UNSAFE_componentWillReceiveProps(nextProps) {
245
+ if (!this.isSubscribed) {
246
+ this.subscribe(nextProps);
247
+ }
248
+ }
249
+ componentWillUnmount() {
250
+ if (this.debounce) {
251
+ window.clearTimeout(this.debounce);
252
+ }
253
+ this.unsubscribeFromContextUpdates();
254
+ this.unsubscribe();
255
+ }
256
+ render() {
257
+ const {
258
+ render
259
+ } = this.props;
260
+ return render(this.state);
261
+ }
262
+ }
263
+ _defineProperty(WithPluginStateInner, "displayName", 'WithPluginState');
264
+ export class WithPluginStateOld extends React.Component {
53
265
  constructor(props, context) {
54
266
  super(props, context);
55
267
  _defineProperty(this, "listeners", {});
@@ -146,7 +358,7 @@ class WithPluginState extends React.Component {
146
358
  }, {});
147
359
  }
148
360
  subscribe(props) {
149
- var _uiTracking$samplingR, _uiTracking$slowThres;
361
+ var _uiTracking$samplingR2, _uiTracking$slowThres2;
150
362
  const plugins = props.plugins;
151
363
  const eventDispatcher = this.getEventDispatcher(props);
152
364
  const editorView = this.getEditorView(props);
@@ -166,8 +378,8 @@ class WithPluginState extends React.Component {
166
378
  const analyticsPlugin = fakePluginKey.getState(editorView.state);
167
379
  const uiTracking = analyticsPlugin && analyticsPlugin.performanceTracking ? analyticsPlugin.performanceTracking.uiTracking || {} : {};
168
380
  const trackingEnabled = uiTracking.enabled === true;
169
- const samplingRate = (_uiTracking$samplingR = uiTracking.samplingRate) !== null && _uiTracking$samplingR !== void 0 ? _uiTracking$samplingR : DEFAULT_SAMPLING_RATE;
170
- const slowThreshold = (_uiTracking$slowThres = uiTracking.slowThreshold) !== null && _uiTracking$slowThres !== void 0 ? _uiTracking$slowThres : DEFAULT_SLOW_THRESHOLD;
381
+ const samplingRate = (_uiTracking$samplingR2 = uiTracking.samplingRate) !== null && _uiTracking$samplingR2 !== void 0 ? _uiTracking$samplingR2 : DEFAULT_SAMPLING_RATE;
382
+ const slowThreshold = (_uiTracking$slowThres2 = uiTracking.slowThreshold) !== null && _uiTracking$slowThres2 !== void 0 ? _uiTracking$slowThres2 : DEFAULT_SLOW_THRESHOLD;
171
383
  this.isSubscribed = true;
172
384
  const pluginsStates = this.getPluginsStates(plugins, editorView);
173
385
  this.setState(pluginsStates);
@@ -244,8 +456,8 @@ class WithPluginState extends React.Component {
244
456
  return render(this.state);
245
457
  }
246
458
  }
247
- _defineProperty(WithPluginState, "displayName", 'WithPluginState');
248
- _defineProperty(WithPluginState, "contextTypes", {
459
+ _defineProperty(WithPluginStateOld, "displayName", 'WithPluginState');
460
+ _defineProperty(WithPluginStateOld, "contextTypes", {
249
461
  editorActions: PropTypes.object,
250
462
  editorSharedConfig: PropTypes.object
251
463
  });
@@ -77,6 +77,7 @@ export var ACTION = /*#__PURE__*/function (ACTION) {
77
77
  ACTION["PASTED"] = "pasted";
78
78
  ACTION["PASTED_AS_PLAIN"] = "pastedAsPlain";
79
79
  ACTION["PASTED_TIMED"] = "pastedTimed";
80
+ ACTION["PRESSED"] = "pressed";
80
81
  ACTION["PROSEMIRROR_RENDERED"] = "proseMirrorRendered";
81
82
  ACTION["REACT_NODEVIEW_RENDERED"] = "reactNodeViewRendered";
82
83
  ACTION["REPLACED_ALL"] = "replacedAll";
@@ -215,11 +216,13 @@ export var ACTION_SUBJECT = /*#__PURE__*/function (ACTION_SUBJECT) {
215
216
  ACTION_SUBJECT["FLOATING_CONTEXTUAL_BUTTON"] = "floatingContextualButton";
216
217
  ACTION_SUBJECT["FLOATING_TOOLBAR_PLUGIN"] = "floatingToolbarPlugin";
217
218
  ACTION_SUBJECT["HELP"] = "help";
219
+ ACTION_SUBJECT["INVITE_ITEM"] = "inviteItem";
218
220
  ACTION_SUBJECT["LAYOUT"] = "layout";
219
221
  ACTION_SUBJECT["LIST"] = "list";
220
222
  ACTION_SUBJECT["MEDIA"] = "media";
221
223
  ACTION_SUBJECT["MEDIA_SINGLE"] = "mediaSingle";
222
224
  ACTION_SUBJECT["MENTION"] = "mention";
225
+ ACTION_SUBJECT["MENTION_TYPEAHEAD"] = "mentionTypeahead";
223
226
  ACTION_SUBJECT["NESTED_EXPAND"] = "nestedExpand";
224
227
  ACTION_SUBJECT["PANEL"] = "panel";
225
228
  ACTION_SUBJECT["PICKER"] = "picker";
@@ -232,6 +235,7 @@ export var ACTION_SUBJECT = /*#__PURE__*/function (ACTION_SUBJECT) {
232
235
  ACTION_SUBJECT["HYPERLINK"] = "hyperlink";
233
236
  ACTION_SUBJECT["TABLE"] = "table";
234
237
  ACTION_SUBJECT["TABLES_PLUGIN"] = "tablesPlugin";
238
+ ACTION_SUBJECT["TEAM_MENTION_TYPEAHEAD"] = "teamMentionTypeahead";
235
239
  ACTION_SUBJECT["TEXT"] = "text";
236
240
  ACTION_SUBJECT["TOOLBAR"] = "toolbar";
237
241
  ACTION_SUBJECT["TYPEAHEAD"] = "typeAhead";
@@ -0,0 +1 @@
1
+ export {};
@@ -7,7 +7,7 @@ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t =
7
7
  import { isFedRamp } from './environment';
8
8
  var SENTRY_DSN = 'https://0b10c8e02fb44d8796c047b102c9bee8@o55978.ingest.sentry.io/4505129224110080';
9
9
  var packageName = 'editor-common'; // Sentry doesn't accept '/' in its releases https://docs.sentry.io/platforms/javascript/configuration/releases/
10
- var packageVersion = "93.4.2";
10
+ var packageVersion = "93.4.4";
11
11
  var sanitiseSentryEvents = function sanitiseSentryEvents(data, _hint) {
12
12
  // Remove URL as it has UGC
13
13
  // TODO: Sanitise the URL instead of just removing it
@@ -21,7 +21,7 @@ import withAnalyticsContext from '@atlaskit/analytics-next/withAnalyticsContext'
21
21
  import withAnalyticsEvents from '@atlaskit/analytics-next/withAnalyticsEvents';
22
22
  import Layer from '../Layer';
23
23
  var packageName = "@atlaskit/editor-common";
24
- var packageVersion = "93.4.2";
24
+ var packageVersion = "93.4.4";
25
25
  var halfFocusRing = 1;
26
26
  var dropOffset = '0, 8';
27
27
  var DropList = /*#__PURE__*/function (_Component) {
@@ -0,0 +1,2 @@
1
+ import React from "react";
2
+ export var EditorContext = /*#__PURE__*/React.createContext({});
@@ -1,19 +1,22 @@
1
+ import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
2
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
3
+ import _extends from "@babel/runtime/helpers/extends";
1
4
  import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
2
5
  import _createClass from "@babel/runtime/helpers/createClass";
3
- import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
4
6
  import _inherits from "@babel/runtime/helpers/inherits";
5
7
  import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
6
8
  import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
7
- import _defineProperty from "@babel/runtime/helpers/defineProperty";
8
9
  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; }
9
10
  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; }
10
11
  function _createSuper(t) { var r = _isNativeReflectConstruct(); return function () { var e, o = _getPrototypeOf(t); if (r) { var s = _getPrototypeOf(this).constructor; e = Reflect.construct(o, arguments, s); } else e = o.apply(this, arguments); return _possibleConstructorReturn(this, e); }; }
11
12
  function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
12
13
  import React from 'react';
13
14
  import PropTypes from 'prop-types';
15
+ import { fg } from '@atlaskit/platform-feature-flags';
14
16
  import { ACTION, ACTION_SUBJECT, EVENT_TYPE } from '../analytics';
15
17
  import { createDispatch } from '../event-dispatcher';
16
18
  import { startMeasure, stopMeasure } from '../performance-measures';
19
+ import { EditorContext } from '../ui/EditorContext';
17
20
  import { analyticsEventKey } from '../utils';
18
21
  var DEFAULT_SAMPLING_RATE = 100;
19
22
  var DEFAULT_SLOW_THRESHOLD = 4;
@@ -61,10 +64,34 @@ var DEFAULT_SLOW_THRESHOLD = 4;
61
64
  var WithPluginState = /*#__PURE__*/function (_React$Component) {
62
65
  _inherits(WithPluginState, _React$Component);
63
66
  var _super = _createSuper(WithPluginState);
64
- function WithPluginState(props, context) {
65
- var _this;
67
+ function WithPluginState(props) {
66
68
  _classCallCheck(this, WithPluginState);
67
- _this = _super.call(this, props, context);
69
+ return _super.call(this, props);
70
+ }
71
+ _createClass(WithPluginState, [{
72
+ key: "render",
73
+ value: function render() {
74
+ if (fg('platform_editor_react18_phase2')) {
75
+ return /*#__PURE__*/React.createElement(WithPluginStateNew, this.props);
76
+ }
77
+ return /*#__PURE__*/React.createElement(WithPluginStateOld, this.props);
78
+ }
79
+ }]);
80
+ return WithPluginState;
81
+ }(React.Component);
82
+ function WithPluginStateNew(props) {
83
+ var context = React.useContext(EditorContext);
84
+ return /*#__PURE__*/React.createElement(WithPluginStateInner, _extends({}, props, {
85
+ editorActions: context === null || context === void 0 ? void 0 : context.editorActions
86
+ }));
87
+ }
88
+ export var WithPluginStateInner = /*#__PURE__*/function (_React$Component2) {
89
+ _inherits(WithPluginStateInner, _React$Component2);
90
+ var _super2 = _createSuper(WithPluginStateInner);
91
+ function WithPluginStateInner(props) {
92
+ var _this;
93
+ _classCallCheck(this, WithPluginStateInner);
94
+ _this = _super2.call(this, props);
68
95
  _defineProperty(_assertThisInitialized(_this), "listeners", {});
69
96
  _defineProperty(_assertThisInitialized(_this), "debounce", null);
70
97
  _defineProperty(_assertThisInitialized(_this), "notAppliedState", {});
@@ -130,24 +157,22 @@ var WithPluginState = /*#__PURE__*/function (_React$Component) {
130
157
  });
131
158
  }
132
159
  });
133
- _defineProperty(_assertThisInitialized(_this), "onContextUpdate", function () {
134
- _this.subscribe(_this.props);
135
- });
136
- _this.state = _this.getPluginsStates(_this.props.plugins, _this.getEditorView(props, context));
160
+ _this.state = _this.getPluginsStates(_this.props.plugins, _this.getEditorView(props));
137
161
  return _this;
138
162
  }
139
- _createClass(WithPluginState, [{
163
+ _createClass(WithPluginStateInner, [{
140
164
  key: "getEditorView",
141
- value: function getEditorView(maybeProps, maybeContext) {
165
+ value: function getEditorView(maybeProps) {
142
166
  var props = maybeProps || this.props;
143
- var context = maybeContext || this.context;
144
- return props.editorView || context && context.editorActions && context.editorActions._privateGetEditorView() || context && context.editorSharedConfig && context.editorSharedConfig.editorView;
167
+ var editorActions = props.editorActions;
168
+ return props.editorView || (editorActions === null || editorActions === void 0 ? void 0 : editorActions._privateGetEditorView());
145
169
  }
146
170
  }, {
147
171
  key: "getEventDispatcher",
148
172
  value: function getEventDispatcher(maybeProps) {
173
+ var _props$editorActions;
149
174
  var props = maybeProps || this.props;
150
- return props.eventDispatcher || this.context && this.context.editorActions && this.context.editorActions._privateGetEventDispatcher() || this.context && this.context.editorSharedConfig && this.context.editorSharedConfig.eventDispatcher;
175
+ return props.eventDispatcher || ((_props$editorActions = props.editorActions) === null || _props$editorActions === void 0 ? void 0 : _props$editorActions._privateGetEventDispatcher());
151
176
  }
152
177
  }, {
153
178
  key: "getPluginsStates",
@@ -238,6 +263,236 @@ var WithPluginState = /*#__PURE__*/function (_React$Component) {
238
263
  });
239
264
  this.listeners = [];
240
265
  }
266
+ }, {
267
+ key: "subscribeToContextUpdates",
268
+ value: function subscribeToContextUpdates() {
269
+ var _this$props$editorAct,
270
+ _this4 = this;
271
+ (_this$props$editorAct = this.props.editorActions) === null || _this$props$editorAct === void 0 || _this$props$editorAct._privateSubscribe(function () {
272
+ return _this4.subscribe(_this4.props);
273
+ });
274
+ }
275
+ }, {
276
+ key: "unsubscribeFromContextUpdates",
277
+ value: function unsubscribeFromContextUpdates() {
278
+ var _this$props$editorAct2,
279
+ _this5 = this;
280
+ (_this$props$editorAct2 = this.props.editorActions) === null || _this$props$editorAct2 === void 0 || _this$props$editorAct2._privateUnsubscribe(function () {
281
+ return _this5.subscribe(_this5.props);
282
+ });
283
+ }
284
+ }, {
285
+ key: "componentDidMount",
286
+ value: function componentDidMount() {
287
+ this.subscribe(this.props);
288
+ this.subscribeToContextUpdates();
289
+ }
290
+ }, {
291
+ key: "UNSAFE_componentWillReceiveProps",
292
+ value: function UNSAFE_componentWillReceiveProps(nextProps) {
293
+ if (!this.isSubscribed) {
294
+ this.subscribe(nextProps);
295
+ }
296
+ }
297
+ }, {
298
+ key: "componentWillUnmount",
299
+ value: function componentWillUnmount() {
300
+ if (this.debounce) {
301
+ window.clearTimeout(this.debounce);
302
+ }
303
+ this.unsubscribeFromContextUpdates();
304
+ this.unsubscribe();
305
+ }
306
+ }, {
307
+ key: "render",
308
+ value: function render() {
309
+ var render = this.props.render;
310
+ return render(this.state);
311
+ }
312
+ }]);
313
+ return WithPluginStateInner;
314
+ }(React.Component);
315
+ _defineProperty(WithPluginStateInner, "displayName", 'WithPluginState');
316
+ export var WithPluginStateOld = /*#__PURE__*/function (_React$Component3) {
317
+ _inherits(WithPluginStateOld, _React$Component3);
318
+ var _super3 = _createSuper(WithPluginStateOld);
319
+ function WithPluginStateOld(props, context) {
320
+ var _this6;
321
+ _classCallCheck(this, WithPluginStateOld);
322
+ _this6 = _super3.call(this, props, context);
323
+ _defineProperty(_assertThisInitialized(_this6), "listeners", {});
324
+ _defineProperty(_assertThisInitialized(_this6), "debounce", null);
325
+ _defineProperty(_assertThisInitialized(_this6), "notAppliedState", {});
326
+ _defineProperty(_assertThisInitialized(_this6), "isSubscribed", false);
327
+ _defineProperty(_assertThisInitialized(_this6), "callsCount", 0);
328
+ _defineProperty(_assertThisInitialized(_this6), "handlePluginStateChange", function (propName, pluginName, performanceOptions, skipEqualityCheck) {
329
+ return function (pluginState) {
330
+ // skipEqualityCheck is being used for old plugins since they are mutating plugin state instead of creating a new one
331
+ if (_this6.state[propName] !== pluginState || skipEqualityCheck) {
332
+ _this6.updateState({
333
+ stateSubset: _defineProperty({}, propName, pluginState),
334
+ pluginName: pluginName,
335
+ performanceOptions: performanceOptions
336
+ });
337
+ }
338
+ };
339
+ });
340
+ /**
341
+ * Debounces setState calls in order to reduce number of re-renders caused by several plugin state changes.
342
+ */
343
+ _defineProperty(_assertThisInitialized(_this6), "updateState", function (_ref2) {
344
+ var stateSubset = _ref2.stateSubset,
345
+ pluginName = _ref2.pluginName,
346
+ performanceOptions = _ref2.performanceOptions;
347
+ _this6.notAppliedState = _objectSpread(_objectSpread({}, _this6.notAppliedState), stateSubset);
348
+ if (_this6.debounce) {
349
+ window.clearTimeout(_this6.debounce);
350
+ }
351
+ var debounce = _this6.props.debounce !== false ? function (fn) {
352
+ return window.setTimeout(fn, 0);
353
+ } : function (fn) {
354
+ return fn();
355
+ };
356
+ _this6.debounce = debounce(function () {
357
+ var measure = "\uD83E\uDD89".concat(pluginName, "::WithPluginState");
358
+ performanceOptions.trackingEnabled && startMeasure(measure);
359
+ _this6.setState(_this6.notAppliedState, function () {
360
+ performanceOptions.trackingEnabled && stopMeasure(measure, function (duration) {
361
+ // Each WithPluginState component will fire analytics event no more than once every `samplingLimit` times
362
+ if (++_this6.callsCount % performanceOptions.samplingRate === 0 && duration > performanceOptions.slowThreshold) {
363
+ _this6.dispatchAnalyticsEvent({
364
+ action: ACTION.WITH_PLUGIN_STATE_CALLED,
365
+ actionSubject: ACTION_SUBJECT.EDITOR,
366
+ eventType: EVENT_TYPE.OPERATIONAL,
367
+ attributes: {
368
+ plugin: pluginName,
369
+ duration: duration
370
+ }
371
+ });
372
+ }
373
+ });
374
+ });
375
+ _this6.debounce = null;
376
+ _this6.notAppliedState = {};
377
+ });
378
+ });
379
+ _defineProperty(_assertThisInitialized(_this6), "dispatchAnalyticsEvent", function (payload) {
380
+ var eventDispatcher = _this6.getEventDispatcher();
381
+ if (eventDispatcher) {
382
+ var dispatch = createDispatch(eventDispatcher);
383
+ dispatch(analyticsEventKey, {
384
+ payload: payload
385
+ });
386
+ }
387
+ });
388
+ _defineProperty(_assertThisInitialized(_this6), "onContextUpdate", function () {
389
+ _this6.subscribe(_this6.props);
390
+ });
391
+ _this6.state = _this6.getPluginsStates(_this6.props.plugins, _this6.getEditorView(props, context));
392
+ return _this6;
393
+ }
394
+ _createClass(WithPluginStateOld, [{
395
+ key: "getEditorView",
396
+ value: function getEditorView(maybeProps, maybeContext) {
397
+ var props = maybeProps || this.props;
398
+ var context = maybeContext || this.context;
399
+ return props.editorView || context && context.editorActions && context.editorActions._privateGetEditorView() || context && context.editorSharedConfig && context.editorSharedConfig.editorView;
400
+ }
401
+ }, {
402
+ key: "getEventDispatcher",
403
+ value: function getEventDispatcher(maybeProps) {
404
+ var props = maybeProps || this.props;
405
+ return props.eventDispatcher || this.context && this.context.editorActions && this.context.editorActions._privateGetEventDispatcher() || this.context && this.context.editorSharedConfig && this.context.editorSharedConfig.eventDispatcher;
406
+ }
407
+ }, {
408
+ key: "getPluginsStates",
409
+ value: function getPluginsStates(plugins, editorView) {
410
+ if (!editorView || !plugins) {
411
+ return {};
412
+ }
413
+ var keys = Object.keys(plugins);
414
+ return keys.reduce(function (acc, propName) {
415
+ var pluginKey = plugins[propName];
416
+ if (!pluginKey) {
417
+ return acc;
418
+ }
419
+ acc[propName] = pluginKey.getState(editorView.state);
420
+ return acc;
421
+ }, {});
422
+ }
423
+ }, {
424
+ key: "subscribe",
425
+ value: function subscribe(props) {
426
+ var _uiTracking$samplingR2,
427
+ _uiTracking$slowThres2,
428
+ _this7 = this;
429
+ var plugins = props.plugins;
430
+ var eventDispatcher = this.getEventDispatcher(props);
431
+ var editorView = this.getEditorView(props);
432
+ if (!eventDispatcher || !editorView || this.isSubscribed) {
433
+ return;
434
+ }
435
+
436
+ // TODO: ED-15663
437
+ // Please, do not copy or use this kind of code below
438
+ // @ts-ignore
439
+ var fakePluginKey = {
440
+ key: 'analyticsPlugin$',
441
+ getState: function getState(state) {
442
+ return state['analyticsPlugin$'];
443
+ }
444
+ };
445
+ var analyticsPlugin = fakePluginKey.getState(editorView.state);
446
+ var uiTracking = analyticsPlugin && analyticsPlugin.performanceTracking ? analyticsPlugin.performanceTracking.uiTracking || {} : {};
447
+ var trackingEnabled = uiTracking.enabled === true;
448
+ var samplingRate = (_uiTracking$samplingR2 = uiTracking.samplingRate) !== null && _uiTracking$samplingR2 !== void 0 ? _uiTracking$samplingR2 : DEFAULT_SAMPLING_RATE;
449
+ var slowThreshold = (_uiTracking$slowThres2 = uiTracking.slowThreshold) !== null && _uiTracking$slowThres2 !== void 0 ? _uiTracking$slowThres2 : DEFAULT_SLOW_THRESHOLD;
450
+ this.isSubscribed = true;
451
+ var pluginsStates = this.getPluginsStates(plugins, editorView);
452
+ this.setState(pluginsStates);
453
+ Object.keys(plugins).forEach(function (propName) {
454
+ var pluginKey = plugins[propName];
455
+ if (!pluginKey) {
456
+ return;
457
+ }
458
+ var pluginName = pluginKey.key;
459
+ var pluginState = pluginsStates[propName];
460
+ var isPluginWithSubscribe = pluginState && pluginState.subscribe;
461
+ var handler = _this7.handlePluginStateChange(propName, pluginName, {
462
+ samplingRate: samplingRate,
463
+ slowThreshold: slowThreshold,
464
+ trackingEnabled: trackingEnabled
465
+ }, isPluginWithSubscribe);
466
+ if (isPluginWithSubscribe) {
467
+ pluginState.subscribe(handler);
468
+ } else {
469
+ eventDispatcher.on(pluginKey.key, handler);
470
+ }
471
+ _this7.listeners[pluginKey.key] = {
472
+ handler: handler,
473
+ pluginKey: pluginKey
474
+ };
475
+ });
476
+ }
477
+ }, {
478
+ key: "unsubscribe",
479
+ value: function unsubscribe() {
480
+ var _this8 = this;
481
+ var eventDispatcher = this.getEventDispatcher();
482
+ var editorView = this.getEditorView();
483
+ if (!eventDispatcher || !editorView || !this.isSubscribed) {
484
+ return;
485
+ }
486
+ Object.keys(this.listeners).forEach(function (key) {
487
+ var pluginState = _this8.listeners[key].pluginKey.getState(editorView.state);
488
+ if (pluginState && pluginState.unsubscribe) {
489
+ pluginState.unsubscribe(_this8.listeners[key].handler);
490
+ } else {
491
+ eventDispatcher.off(key, _this8.listeners[key].handler);
492
+ }
493
+ });
494
+ this.listeners = [];
495
+ }
241
496
  }, {
242
497
  key: "subscribeToContextUpdates",
243
498
  value: function subscribeToContextUpdates(context) {
@@ -281,10 +536,10 @@ var WithPluginState = /*#__PURE__*/function (_React$Component) {
281
536
  return render(this.state);
282
537
  }
283
538
  }]);
284
- return WithPluginState;
539
+ return WithPluginStateOld;
285
540
  }(React.Component);
286
- _defineProperty(WithPluginState, "displayName", 'WithPluginState');
287
- _defineProperty(WithPluginState, "contextTypes", {
541
+ _defineProperty(WithPluginStateOld, "displayName", 'WithPluginState');
542
+ _defineProperty(WithPluginStateOld, "contextTypes", {
288
543
  editorActions: PropTypes.object,
289
544
  editorSharedConfig: PropTypes.object
290
545
  });