@atlaskit/editor-common 101.1.2 → 102.0.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 CHANGED
@@ -1,5 +1,14 @@
1
1
  # @atlaskit/editor-common
2
2
 
3
+ ## 102.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - [#123345](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/pull-requests/123345)
8
+ [`31b02e82858e5`](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/commits/31b02e82858e5) -
9
+ NO-ISSUE: Make selection utils more generic by changing parameters from PMNode to
10
+ ContentNodeWithPos as only the actual node is being used in the utils.
11
+
3
12
  ## 101.1.2
4
13
 
5
14
  ### Patch Changes
@@ -17,7 +17,7 @@ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return
17
17
  function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
18
18
  var SENTRY_DSN = 'https://0b10c8e02fb44d8796c047b102c9bee8@o55978.ingest.sentry.io/4505129224110080';
19
19
  var packageName = 'editor-common'; // Sentry doesn't accept '/' in its releases https://docs.sentry.io/platforms/javascript/configuration/releases/
20
- var packageVersion = "101.1.2";
20
+ var packageVersion = "102.0.0";
21
21
  var sanitiseSentryEvents = function sanitiseSentryEvents(data, _hint) {
22
22
  // Remove URL as it has UGC
23
23
  // TODO: Sanitise the URL instead of just removing it
@@ -19,7 +19,7 @@ var isSelectionAtStartOfNode = exports.isSelectionAtStartOfNode = function isSel
19
19
  }
20
20
  for (var i = $pos.depth + 1; i > 0; i--) {
21
21
  var node = $pos.node(i);
22
- if (node && node.eq(parentNode.node)) {
22
+ if (node && node.eq(parentNode)) {
23
23
  break;
24
24
  }
25
25
  if (i > 1 && $pos.before(i) !== $pos.before(i - 1) + 1) {
@@ -34,7 +34,7 @@ var isSelectionAtEndOfNode = exports.isSelectionAtEndOfNode = function isSelecti
34
34
  }
35
35
  for (var i = $pos.depth + 1; i > 0; i--) {
36
36
  var node = $pos.node(i);
37
- if (node && node.eq(parentNode.node)) {
37
+ if (node && node.eq(parentNode)) {
38
38
  break;
39
39
  }
40
40
  if (i > 1 && $pos.after(i) !== $pos.after(i - 1) - 1) {
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ Object.defineProperty(exports, "editorUGCToken", {
8
+ enumerable: true,
9
+ get: function get() {
10
+ return _getEditorUgcToken.default;
11
+ }
12
+ });
13
+ var _getEditorUgcToken = _interopRequireDefault(require("./get-editor-ugc-token"));
@@ -23,7 +23,7 @@ function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.
23
23
  * @jsx jsx
24
24
  */ // eslint-disable-next-line @atlaskit/ui-styling-standard/use-compiled -- Ignored via go/DSP-18766
25
25
  var packageName = "@atlaskit/editor-common";
26
- var packageVersion = "101.1.2";
26
+ var packageVersion = "102.0.0";
27
27
  var halfFocusRing = 1;
28
28
  var dropOffset = '0, 8';
29
29
  // Ignored via go/ees005
@@ -4,7 +4,7 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
4
4
  Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
- exports.WithPluginStateOld = exports.WithPluginStateInner = exports.WithPluginState = void 0;
7
+ exports.WithPluginStateInner = exports.WithPluginState = void 0;
8
8
  var _readOnlyError2 = _interopRequireDefault(require("@babel/runtime/helpers/readOnlyError"));
9
9
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
10
10
  var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
@@ -14,8 +14,6 @@ var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime
14
14
  var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
15
15
  var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
16
16
  var _react = _interopRequireDefault(require("react"));
17
- var _propTypes = _interopRequireDefault(require("prop-types"));
18
- var _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
19
17
  var _analytics = require("../analytics");
20
18
  var _eventDispatcher = require("../event-dispatcher");
21
19
  var _performanceMeasures = require("../performance-measures");
@@ -79,18 +77,13 @@ var WithPluginState = exports.WithPluginState = /*#__PURE__*/function (_React$Co
79
77
  return (0, _createClass2.default)(WithPluginState, [{
80
78
  key: "render",
81
79
  value: function render() {
82
- if ((0, _platformFeatureFlags.fg)('platform_editor_react18_phase2_v2')) {
83
- // Ignored via go/ees005
84
- // eslint-disable-next-line react/jsx-props-no-spreading
85
- return /*#__PURE__*/_react.default.createElement(WithPluginStateNew, this.props);
86
- }
87
80
  // Ignored via go/ees005
88
81
  // eslint-disable-next-line react/jsx-props-no-spreading
89
- return /*#__PURE__*/_react.default.createElement(WithPluginStateOld, this.props);
82
+ return /*#__PURE__*/_react.default.createElement(WithPluginStateEditionActionsWrapper, this.props);
90
83
  }
91
84
  }]);
92
85
  }(_react.default.Component);
93
- function WithPluginStateNew(props) {
86
+ function WithPluginStateEditionActionsWrapper(props) {
94
87
  var context = _react.default.useContext(_EditorContext.EditorContext);
95
88
  return /*#__PURE__*/_react.default.createElement(WithPluginStateInner
96
89
  // Ignored via go/ees005
@@ -354,263 +347,5 @@ var WithPluginStateInner = exports.WithPluginStateInner = /*#__PURE__*/function
354
347
  return render(this.state);
355
348
  }
356
349
  }]);
357
- }(_react.default.Component); // Ignored via go/ees005
358
- // eslint-disable-next-line @repo/internal/react/no-class-components
359
- (0, _defineProperty2.default)(WithPluginStateInner, "displayName", 'WithPluginState');
360
- var WithPluginStateOld = exports.WithPluginStateOld = /*#__PURE__*/function (_React$Component3) {
361
- function WithPluginStateOld(props, context) {
362
- var _this6;
363
- (0, _classCallCheck2.default)(this, WithPluginStateOld);
364
- _this6 = _callSuper(this, WithPluginStateOld, [props, context]);
365
- (0, _defineProperty2.default)(_this6, "listeners", {});
366
- (0, _defineProperty2.default)(_this6, "debounce", null);
367
- (0, _defineProperty2.default)(_this6, "notAppliedState", {});
368
- (0, _defineProperty2.default)(_this6, "isSubscribed", false);
369
- (0, _defineProperty2.default)(_this6, "callsCount", 0);
370
- (0, _defineProperty2.default)(_this6, "handlePluginStateChange", function (propName, pluginName, performanceOptions, skipEqualityCheck) {
371
- return (
372
- // Ignored via go/ees005
373
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
374
- function (pluginState) {
375
- // skipEqualityCheck is being used for old plugins since they are mutating plugin state instead of creating a new one
376
- // Ignored via go/ees005
377
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
378
- if (_this6.state[propName] !== pluginState || skipEqualityCheck) {
379
- _this6.updateState({
380
- stateSubset: (0, _defineProperty2.default)({}, propName, pluginState),
381
- pluginName: pluginName,
382
- performanceOptions: performanceOptions
383
- });
384
- }
385
- }
386
- );
387
- });
388
- /**
389
- * Debounces setState calls in order to reduce number of re-renders caused by several plugin state changes.
390
- */
391
- (0, _defineProperty2.default)(_this6, "updateState", function (_ref2) {
392
- var stateSubset = _ref2.stateSubset,
393
- pluginName = _ref2.pluginName,
394
- performanceOptions = _ref2.performanceOptions;
395
- _this6.notAppliedState = _objectSpread(_objectSpread({}, _this6.notAppliedState), stateSubset);
396
- if (_this6.debounce) {
397
- window.clearTimeout(_this6.debounce);
398
- }
399
- var debounce = _this6.props.debounce !== false ? function (fn) {
400
- return window.setTimeout(fn, 0);
401
- } : function (fn) {
402
- return fn();
403
- };
404
- _this6.debounce = debounce(function () {
405
- var measure = "\uD83E\uDD89".concat(pluginName, "::WithPluginState");
406
- performanceOptions.trackingEnabled && (0, _performanceMeasures.startMeasure)(measure);
407
- _this6.setState(_this6.notAppliedState, function () {
408
- performanceOptions.trackingEnabled && (0, _performanceMeasures.stopMeasure)(measure, function (duration) {
409
- // Each WithPluginState component will fire analytics event no more than once every `samplingLimit` times
410
- if (++_this6.callsCount % performanceOptions.samplingRate === 0 && duration > performanceOptions.slowThreshold) {
411
- _this6.dispatchAnalyticsEvent({
412
- action: _analytics.ACTION.WITH_PLUGIN_STATE_CALLED,
413
- actionSubject: _analytics.ACTION_SUBJECT.EDITOR,
414
- eventType: _analytics.EVENT_TYPE.OPERATIONAL,
415
- attributes: {
416
- plugin: pluginName,
417
- duration: duration
418
- }
419
- });
420
- }
421
- });
422
- });
423
- _this6.debounce = null;
424
- _this6.notAppliedState = {};
425
- });
426
- });
427
- (0, _defineProperty2.default)(_this6, "dispatchAnalyticsEvent", function (payload) {
428
- var eventDispatcher = _this6.getEventDispatcher();
429
- if (eventDispatcher) {
430
- var dispatch = (0, _eventDispatcher.createDispatch)(eventDispatcher);
431
- dispatch(_utils.analyticsEventKey, {
432
- payload: payload
433
- });
434
- }
435
- });
436
- (0, _defineProperty2.default)(_this6, "onContextUpdate", function () {
437
- _this6.subscribe(_this6.props);
438
- });
439
- _this6.state = _this6.getPluginsStates(_this6.props.plugins, _this6.getEditorView(props, context));
440
- return _this6;
441
- }
442
- (0, _inherits2.default)(WithPluginStateOld, _React$Component3);
443
- return (0, _createClass2.default)(WithPluginStateOld, [{
444
- key: "getEditorView",
445
- value: function getEditorView(maybeProps, maybeContext) {
446
- var props = maybeProps || this.props;
447
- var context = maybeContext || this.context;
448
- return props.editorView || context && context.editorActions && context.editorActions._privateGetEditorView() || context && context.editorSharedConfig && context.editorSharedConfig.editorView;
449
- }
450
- }, {
451
- key: "getEventDispatcher",
452
- value: function getEventDispatcher(maybeProps) {
453
- var props = maybeProps || this.props;
454
- return props.eventDispatcher || this.context && this.context.editorActions && this.context.editorActions._privateGetEventDispatcher() || this.context && this.context.editorSharedConfig && this.context.editorSharedConfig.eventDispatcher;
455
- }
456
- }, {
457
- key: "getPluginsStates",
458
- value: function getPluginsStates(plugins, editorView) {
459
- if (!editorView || !plugins) {
460
- return {};
461
- }
462
- var keys = Object.keys(plugins);
463
- return keys.reduce(function (acc, propName) {
464
- var pluginKey = plugins[propName];
465
- if (!pluginKey) {
466
- return acc;
467
- }
468
- acc[propName] = pluginKey.getState(editorView.state);
469
- return acc;
470
- }, {});
471
- }
472
- }, {
473
- key: "subscribe",
474
- value: function subscribe(props) {
475
- var _uiTracking$samplingR2,
476
- _uiTracking$slowThres2,
477
- _this7 = this;
478
- var plugins = props.plugins;
479
- var eventDispatcher = this.getEventDispatcher(props);
480
- var editorView = this.getEditorView(props);
481
- if (!eventDispatcher || !editorView || this.isSubscribed) {
482
- return;
483
- }
484
-
485
- // TODO: ED-15663
486
- // Please, do not copy or use this kind of code below
487
- // @ts-ignore
488
- var fakePluginKey = {
489
- key: 'analyticsPlugin$',
490
- getState: function getState(state) {
491
- // Ignored via go/ees005
492
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
493
- return state['analyticsPlugin$'];
494
- }
495
- };
496
- var analyticsPlugin = fakePluginKey.getState(editorView.state);
497
- var uiTracking = analyticsPlugin && analyticsPlugin.performanceTracking ? analyticsPlugin.performanceTracking.uiTracking || {} : {};
498
- var trackingEnabled = uiTracking.enabled === true;
499
- var samplingRate = (_uiTracking$samplingR2 = uiTracking.samplingRate) !== null && _uiTracking$samplingR2 !== void 0 ? _uiTracking$samplingR2 : DEFAULT_SAMPLING_RATE;
500
- var slowThreshold = (_uiTracking$slowThres2 = uiTracking.slowThreshold) !== null && _uiTracking$slowThres2 !== void 0 ? _uiTracking$slowThres2 : DEFAULT_SLOW_THRESHOLD;
501
- this.isSubscribed = true;
502
- var pluginsStates = this.getPluginsStates(plugins, editorView);
503
- this.setState(pluginsStates);
504
- Object.keys(plugins).forEach(function (propName) {
505
- var pluginKey = plugins[propName];
506
- if (!pluginKey) {
507
- return;
508
- }
509
-
510
- // Ignored via go/ees005
511
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
512
- var pluginName = pluginKey.key;
513
- // Ignored via go/ees005
514
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
515
- var pluginState = pluginsStates[propName];
516
- var isPluginWithSubscribe = pluginState && pluginState.subscribe;
517
- var handler = _this7.handlePluginStateChange(propName, pluginName, {
518
- samplingRate: samplingRate,
519
- slowThreshold: slowThreshold,
520
- trackingEnabled: trackingEnabled
521
- }, isPluginWithSubscribe);
522
- if (isPluginWithSubscribe) {
523
- pluginState.subscribe(handler);
524
- } else {
525
- // Ignored via go/ees005
526
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
527
- eventDispatcher.on(pluginKey.key, handler);
528
- }
529
-
530
- // Ignored via go/ees005
531
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
532
- // Ignored via go/ees005
533
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
534
- _this7.listeners[pluginKey.key] = {
535
- handler: handler,
536
- pluginKey: pluginKey
537
- };
538
- });
539
- }
540
- }, {
541
- key: "unsubscribe",
542
- value: function unsubscribe() {
543
- var _this8 = this;
544
- var eventDispatcher = this.getEventDispatcher();
545
- var editorView = this.getEditorView();
546
- if (!eventDispatcher || !editorView || !this.isSubscribed) {
547
- return;
548
- }
549
- Object.keys(this.listeners).forEach(function (key) {
550
- // Ignored via go/ees005
551
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
552
- var pluginState = _this8.listeners[key].pluginKey.getState(editorView.state);
553
- if (pluginState && pluginState.unsubscribe) {
554
- // Ignored via go/ees005
555
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
556
- pluginState.unsubscribe(_this8.listeners[key].handler);
557
- } else {
558
- // Ignored via go/ees005
559
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
560
- eventDispatcher.off(key, _this8.listeners[key].handler);
561
- }
562
- });
563
- this.listeners = [];
564
- }
565
- }, {
566
- key: "subscribeToContextUpdates",
567
- value: function subscribeToContextUpdates(context) {
568
- if (context && context.editorActions) {
569
- context.editorActions._privateSubscribe(this.onContextUpdate);
570
- }
571
- }
572
- }, {
573
- key: "unsubscribeFromContextUpdates",
574
- value: function unsubscribeFromContextUpdates(context) {
575
- if (context && context.editorActions) {
576
- context.editorActions._privateUnsubscribe(this.onContextUpdate);
577
- }
578
- }
579
- }, {
580
- key: "componentDidMount",
581
- value: function componentDidMount() {
582
- this.subscribe(this.props);
583
- this.subscribeToContextUpdates(this.context);
584
- }
585
-
586
- // Ignored via go/ees005
587
- // eslint-disable-next-line react/no-unsafe
588
- }, {
589
- key: "UNSAFE_componentWillReceiveProps",
590
- value: function UNSAFE_componentWillReceiveProps(nextProps) {
591
- if (!this.isSubscribed) {
592
- this.subscribe(nextProps);
593
- }
594
- }
595
- }, {
596
- key: "componentWillUnmount",
597
- value: function componentWillUnmount() {
598
- if (this.debounce) {
599
- window.clearTimeout(this.debounce);
600
- }
601
- this.unsubscribeFromContextUpdates(this.context);
602
- this.unsubscribe();
603
- }
604
- }, {
605
- key: "render",
606
- value: function render() {
607
- var render = this.props.render;
608
- return render(this.state);
609
- }
610
- }]);
611
350
  }(_react.default.Component);
612
- (0, _defineProperty2.default)(WithPluginStateOld, "displayName", 'WithPluginState');
613
- (0, _defineProperty2.default)(WithPluginStateOld, "contextTypes", {
614
- editorActions: _propTypes.default.object,
615
- editorSharedConfig: _propTypes.default.object
616
- });
351
+ (0, _defineProperty2.default)(WithPluginStateInner, "displayName", 'WithPluginState');
@@ -1,7 +1,7 @@
1
1
  import { isFedRamp } from './environment';
2
2
  const SENTRY_DSN = 'https://0b10c8e02fb44d8796c047b102c9bee8@o55978.ingest.sentry.io/4505129224110080';
3
3
  const packageName = 'editor-common'; // Sentry doesn't accept '/' in its releases https://docs.sentry.io/platforms/javascript/configuration/releases/
4
- const packageVersion = "101.1.2";
4
+ const packageVersion = "102.0.0";
5
5
  const sanitiseSentryEvents = (data, _hint) => {
6
6
  // Remove URL as it has UGC
7
7
  // TODO: Sanitise the URL instead of just removing it
@@ -7,7 +7,7 @@ export const isSelectionAtStartOfNode = ($pos, parentNode) => {
7
7
  }
8
8
  for (let i = $pos.depth + 1; i > 0; i--) {
9
9
  const node = $pos.node(i);
10
- if (node && node.eq(parentNode.node)) {
10
+ if (node && node.eq(parentNode)) {
11
11
  break;
12
12
  }
13
13
  if (i > 1 && $pos.before(i) !== $pos.before(i - 1) + 1) {
@@ -22,7 +22,7 @@ export const isSelectionAtEndOfNode = ($pos, parentNode) => {
22
22
  }
23
23
  for (let i = $pos.depth + 1; i > 0; i--) {
24
24
  const node = $pos.node(i);
25
- if (node && node.eq(parentNode.node)) {
25
+ if (node && node.eq(parentNode)) {
26
26
  break;
27
27
  }
28
28
  if (i > 1 && $pos.after(i) !== $pos.after(i - 1) - 1) {
@@ -0,0 +1,3 @@
1
+ /* eslint-disable @atlaskit/editor/no-re-export */
2
+ import editorUGCToken from './get-editor-ugc-token';
3
+ export { editorUGCToken };
@@ -13,7 +13,7 @@ import withAnalyticsContext from '@atlaskit/analytics-next/withAnalyticsContext'
13
13
  import withAnalyticsEvents from '@atlaskit/analytics-next/withAnalyticsEvents';
14
14
  import Layer from '../Layer';
15
15
  const packageName = "@atlaskit/editor-common";
16
- const packageVersion = "101.1.2";
16
+ const packageVersion = "102.0.0";
17
17
  const halfFocusRing = 1;
18
18
  const dropOffset = '0, 8';
19
19
  // Ignored via go/ees005
@@ -1,8 +1,6 @@
1
1
  import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
2
  import _extends from "@babel/runtime/helpers/extends";
3
3
  import React from 'react';
4
- import PropTypes from 'prop-types';
5
- import { fg } from '@atlaskit/platform-feature-flags';
6
4
  import { ACTION, ACTION_SUBJECT, EVENT_TYPE } from '../analytics';
7
5
  import { createDispatch } from '../event-dispatcher';
8
6
  import { startMeasure, stopMeasure } from '../performance-measures';
@@ -59,17 +57,12 @@ class WithPluginState extends React.Component {
59
57
  super(props);
60
58
  }
61
59
  render() {
62
- if (fg('platform_editor_react18_phase2_v2')) {
63
- // Ignored via go/ees005
64
- // eslint-disable-next-line react/jsx-props-no-spreading
65
- return /*#__PURE__*/React.createElement(WithPluginStateNew, this.props);
66
- }
67
60
  // Ignored via go/ees005
68
61
  // eslint-disable-next-line react/jsx-props-no-spreading
69
- return /*#__PURE__*/React.createElement(WithPluginStateOld, this.props);
62
+ return /*#__PURE__*/React.createElement(WithPluginStateEditionActionsWrapper, this.props);
70
63
  }
71
64
  }
72
- function WithPluginStateNew(props) {
65
+ function WithPluginStateEditionActionsWrapper(props) {
73
66
  const context = React.useContext(EditorContext);
74
67
  return /*#__PURE__*/React.createElement(WithPluginStateInner
75
68
  // Ignored via go/ees005
@@ -299,236 +292,5 @@ export class WithPluginStateInner extends React.Component {
299
292
  return render(this.state);
300
293
  }
301
294
  }
302
-
303
- // Ignored via go/ees005
304
- // eslint-disable-next-line @repo/internal/react/no-class-components
305
295
  _defineProperty(WithPluginStateInner, "displayName", 'WithPluginState');
306
- export class WithPluginStateOld extends React.Component {
307
- constructor(props, context) {
308
- super(props, context);
309
- _defineProperty(this, "listeners", {});
310
- _defineProperty(this, "debounce", null);
311
- _defineProperty(this, "notAppliedState", {});
312
- _defineProperty(this, "isSubscribed", false);
313
- _defineProperty(this, "callsCount", 0);
314
- _defineProperty(this, "handlePluginStateChange", (propName, pluginName, performanceOptions, skipEqualityCheck) =>
315
- // Ignored via go/ees005
316
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
317
- pluginState => {
318
- // skipEqualityCheck is being used for old plugins since they are mutating plugin state instead of creating a new one
319
- // Ignored via go/ees005
320
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
321
- if (this.state[propName] !== pluginState || skipEqualityCheck) {
322
- this.updateState({
323
- stateSubset: {
324
- [propName]: pluginState
325
- },
326
- pluginName,
327
- performanceOptions
328
- });
329
- }
330
- });
331
- /**
332
- * Debounces setState calls in order to reduce number of re-renders caused by several plugin state changes.
333
- */
334
- _defineProperty(this, "updateState", ({
335
- stateSubset,
336
- pluginName,
337
- performanceOptions
338
- }) => {
339
- this.notAppliedState = {
340
- ...this.notAppliedState,
341
- ...stateSubset
342
- };
343
- if (this.debounce) {
344
- window.clearTimeout(this.debounce);
345
- }
346
- const debounce = this.props.debounce !== false ? fn => window.setTimeout(fn, 0) : fn => fn();
347
- this.debounce = debounce(() => {
348
- const measure = `🦉${pluginName}::WithPluginState`;
349
- performanceOptions.trackingEnabled && startMeasure(measure);
350
- this.setState(this.notAppliedState, () => {
351
- performanceOptions.trackingEnabled && stopMeasure(measure, duration => {
352
- // Each WithPluginState component will fire analytics event no more than once every `samplingLimit` times
353
- if (++this.callsCount % performanceOptions.samplingRate === 0 && duration > performanceOptions.slowThreshold) {
354
- this.dispatchAnalyticsEvent({
355
- action: ACTION.WITH_PLUGIN_STATE_CALLED,
356
- actionSubject: ACTION_SUBJECT.EDITOR,
357
- eventType: EVENT_TYPE.OPERATIONAL,
358
- attributes: {
359
- plugin: pluginName,
360
- duration
361
- }
362
- });
363
- }
364
- });
365
- });
366
- this.debounce = null;
367
- this.notAppliedState = {};
368
- });
369
- });
370
- _defineProperty(this, "dispatchAnalyticsEvent", payload => {
371
- const eventDispatcher = this.getEventDispatcher();
372
- if (eventDispatcher) {
373
- const dispatch = createDispatch(eventDispatcher);
374
- dispatch(analyticsEventKey, {
375
- payload
376
- });
377
- }
378
- });
379
- _defineProperty(this, "onContextUpdate", () => {
380
- this.subscribe(this.props);
381
- });
382
- this.state = this.getPluginsStates(this.props.plugins, this.getEditorView(props, context));
383
- }
384
- getEditorView(maybeProps, maybeContext) {
385
- const props = maybeProps || this.props;
386
- const context = maybeContext || this.context;
387
- return props.editorView || context && context.editorActions && context.editorActions._privateGetEditorView() || context && context.editorSharedConfig && context.editorSharedConfig.editorView;
388
- }
389
- getEventDispatcher(maybeProps) {
390
- const props = maybeProps || this.props;
391
- return props.eventDispatcher || this.context && this.context.editorActions && this.context.editorActions._privateGetEventDispatcher() || this.context && this.context.editorSharedConfig && this.context.editorSharedConfig.eventDispatcher;
392
- }
393
- getPluginsStates(plugins, editorView) {
394
- if (!editorView || !plugins) {
395
- return {};
396
- }
397
- const keys = Object.keys(plugins);
398
- return keys.reduce((acc, propName) => {
399
- const pluginKey = plugins[propName];
400
- if (!pluginKey) {
401
- return acc;
402
- }
403
- acc[propName] = pluginKey.getState(editorView.state);
404
- return acc;
405
- }, {});
406
- }
407
- subscribe(props) {
408
- var _uiTracking$samplingR2, _uiTracking$slowThres2;
409
- const plugins = props.plugins;
410
- const eventDispatcher = this.getEventDispatcher(props);
411
- const editorView = this.getEditorView(props);
412
- if (!eventDispatcher || !editorView || this.isSubscribed) {
413
- return;
414
- }
415
-
416
- // TODO: ED-15663
417
- // Please, do not copy or use this kind of code below
418
- // @ts-ignore
419
- const fakePluginKey = {
420
- key: 'analyticsPlugin$',
421
- getState: state => {
422
- // Ignored via go/ees005
423
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
424
- return state['analyticsPlugin$'];
425
- }
426
- };
427
- const analyticsPlugin = fakePluginKey.getState(editorView.state);
428
- const uiTracking = analyticsPlugin && analyticsPlugin.performanceTracking ? analyticsPlugin.performanceTracking.uiTracking || {} : {};
429
- const trackingEnabled = uiTracking.enabled === true;
430
- const samplingRate = (_uiTracking$samplingR2 = uiTracking.samplingRate) !== null && _uiTracking$samplingR2 !== void 0 ? _uiTracking$samplingR2 : DEFAULT_SAMPLING_RATE;
431
- const slowThreshold = (_uiTracking$slowThres2 = uiTracking.slowThreshold) !== null && _uiTracking$slowThres2 !== void 0 ? _uiTracking$slowThres2 : DEFAULT_SLOW_THRESHOLD;
432
- this.isSubscribed = true;
433
- const pluginsStates = this.getPluginsStates(plugins, editorView);
434
- this.setState(pluginsStates);
435
- Object.keys(plugins).forEach(propName => {
436
- const pluginKey = plugins[propName];
437
- if (!pluginKey) {
438
- return;
439
- }
440
-
441
- // Ignored via go/ees005
442
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
443
- const pluginName = pluginKey.key;
444
- // Ignored via go/ees005
445
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
446
- const pluginState = pluginsStates[propName];
447
- const isPluginWithSubscribe = pluginState && pluginState.subscribe;
448
- const handler = this.handlePluginStateChange(propName, pluginName, {
449
- samplingRate,
450
- slowThreshold,
451
- trackingEnabled
452
- }, isPluginWithSubscribe);
453
- if (isPluginWithSubscribe) {
454
- pluginState.subscribe(handler);
455
- } else {
456
- // Ignored via go/ees005
457
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
458
- eventDispatcher.on(pluginKey.key, handler);
459
- }
460
-
461
- // Ignored via go/ees005
462
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
463
- // Ignored via go/ees005
464
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
465
- this.listeners[pluginKey.key] = {
466
- handler,
467
- pluginKey
468
- };
469
- });
470
- }
471
- unsubscribe() {
472
- const eventDispatcher = this.getEventDispatcher();
473
- const editorView = this.getEditorView();
474
- if (!eventDispatcher || !editorView || !this.isSubscribed) {
475
- return;
476
- }
477
- Object.keys(this.listeners).forEach(key => {
478
- // Ignored via go/ees005
479
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
480
- const pluginState = this.listeners[key].pluginKey.getState(editorView.state);
481
- if (pluginState && pluginState.unsubscribe) {
482
- // Ignored via go/ees005
483
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
484
- pluginState.unsubscribe(this.listeners[key].handler);
485
- } else {
486
- // Ignored via go/ees005
487
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
488
- eventDispatcher.off(key, this.listeners[key].handler);
489
- }
490
- });
491
- this.listeners = [];
492
- }
493
- subscribeToContextUpdates(context) {
494
- if (context && context.editorActions) {
495
- context.editorActions._privateSubscribe(this.onContextUpdate);
496
- }
497
- }
498
- unsubscribeFromContextUpdates(context) {
499
- if (context && context.editorActions) {
500
- context.editorActions._privateUnsubscribe(this.onContextUpdate);
501
- }
502
- }
503
- componentDidMount() {
504
- this.subscribe(this.props);
505
- this.subscribeToContextUpdates(this.context);
506
- }
507
-
508
- // Ignored via go/ees005
509
- // eslint-disable-next-line react/no-unsafe
510
- UNSAFE_componentWillReceiveProps(nextProps) {
511
- if (!this.isSubscribed) {
512
- this.subscribe(nextProps);
513
- }
514
- }
515
- componentWillUnmount() {
516
- if (this.debounce) {
517
- window.clearTimeout(this.debounce);
518
- }
519
- this.unsubscribeFromContextUpdates(this.context);
520
- this.unsubscribe();
521
- }
522
- render() {
523
- const {
524
- render
525
- } = this.props;
526
- return render(this.state);
527
- }
528
- }
529
- _defineProperty(WithPluginStateOld, "displayName", 'WithPluginState');
530
- _defineProperty(WithPluginStateOld, "contextTypes", {
531
- editorActions: PropTypes.object,
532
- editorSharedConfig: PropTypes.object
533
- });
534
296
  export { WithPluginState };