UNPKG

@atlaskit/editor-core

Version:

A package contains Atlassian editor core functionality

212 lines (209 loc) • 12.4 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = exports.ErrorBoundaryWithEditorViewWithAnalyticsReactContext = exports.ErrorBoundaryWithEditorView = void 0; var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")); var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")); var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _react = _interopRequireDefault(require("react")); var _uuid = _interopRequireDefault(require("uuid")); var _analyticsNextStableReactContext = _interopRequireDefault(require("@atlaskit/analytics-next-stable-react-context")); var _analytics = require("@atlaskit/editor-common/analytics"); var _coreUtils = require("@atlaskit/editor-common/core-utils"); var _intlErrorBoundary = require("@atlaskit/editor-common/intl-error-boundary"); var _monitoring = require("@atlaskit/editor-common/monitoring"); var _platformFeatureFlags = require("@atlaskit/platform-feature-flags"); var _platformFeatureFlagsReact = require("@atlaskit/platform-feature-flags-react"); var _outdatedBrowsers = require("../utils/outdatedBrowsers"); var _WithEditorView = require("./WithEditorView"); 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; } 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; } function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2.default)(o), (0, _possibleConstructorReturn2.default)(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2.default)(t).constructor) : o.apply(t, e)); } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } // eslint-disable-next-line @atlaskit/platform/prefer-crypto-random-uuid -- Use crypto.randomUUID instead // Ignored via go/ees005 // eslint-disable-next-line @repo/internal/react/no-class-components var ErrorBoundaryWithEditorView = exports.ErrorBoundaryWithEditorView = /*#__PURE__*/function (_React$Component) { function ErrorBoundaryWithEditorView(props) { var _this; (0, _classCallCheck2.default)(this, ErrorBoundaryWithEditorView); _this = _callSuper(this, ErrorBoundaryWithEditorView, [props]); (0, _defineProperty2.default)(_this, "browserExtensions", undefined); (0, _defineProperty2.default)(_this, "state", { error: undefined }); (0, _defineProperty2.default)(_this, "sendErrorData", /*#__PURE__*/function () { var _ref = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(analyticsErrorPayload) { var _window; var product, error, errorInfo, sharedId, browserInfo, attributes; return _regenerator.default.wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: _context.next = 2; return _this.getProductName(); case 2: product = _context.sent; error = analyticsErrorPayload.error, errorInfo = analyticsErrorPayload.errorInfo; // eslint-disable-next-line @atlaskit/platform/prefer-crypto-random-uuid -- Use crypto.randomUUID instead sharedId = (0, _uuid.default)(); browserInfo = ((_window = window) === null || _window === void 0 || (_window = _window.navigator) === null || _window === void 0 ? void 0 : _window.userAgent) || 'unknown'; attributes = { product: product, browserInfo: browserInfo, error: error.toString(), errorInfo: errorInfo, errorId: sharedId, browserExtensions: _this.browserExtensions, docStructure: _this.featureFlags.errorBoundaryDocStructure && _this.props.editorView ? (0, _coreUtils.getDocStructure)(_this.props.editorView.state.doc, { compact: true }) : undefined, outdatedBrowser: (0, _outdatedBrowsers.isOutdatedBrowser)(browserInfo) }; _this.fireAnalyticsEvent({ action: _analytics.ACTION.EDITOR_CRASHED, actionSubject: _analytics.ACTION_SUBJECT.EDITOR, eventType: _analytics.EVENT_TYPE.OPERATIONAL, attributes: attributes }); _this.fireAnalyticsEvent({ action: _analytics.ACTION.EDITOR_CRASHED_ADDITIONAL_INFORMATION, actionSubject: _analytics.ACTION_SUBJECT.EDITOR, eventType: _analytics.EVENT_TYPE.OPERATIONAL, attributes: { errorId: sharedId } }); _this.logException(error, { location: 'editor-core/create-editor', product: product }); case 10: case "end": return _context.stop(); } }, _callee); })); return function (_x) { return _ref.apply(this, arguments); }; }()); (0, _defineProperty2.default)(_this, "getProductName", /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2() { var contextIdentifierProvider, context; return _regenerator.default.wrap(function _callee2$(_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: contextIdentifierProvider = _this.props.contextIdentifierProvider; if (!contextIdentifierProvider) { _context2.next = 7; break; } _context2.next = 4; return contextIdentifierProvider; case 4: context = _context2.sent; if (!context.product) { _context2.next = 7; break; } return _context2.abrupt("return", context.product); case 7: return _context2.abrupt("return", 'atlaskit'); case 8: case "end": return _context2.stop(); } }, _callee2); }))); (0, _defineProperty2.default)(_this, "fireAnalyticsEvent", function (event) { var _this$props$createAna, _this$props; (_this$props$createAna = (_this$props = _this.props).createAnalyticsEvent) === null || _this$props$createAna === void 0 || _this$props$createAna.call(_this$props, event).fire(_analytics.editorAnalyticsChannel); }); (0, _defineProperty2.default)(_this, "logException", function (error, tags) { (0, _monitoring.logException)(error, tags); }); _this.featureFlags = props.featureFlags; return _this; } (0, _inherits2.default)(ErrorBoundaryWithEditorView, _React$Component); return (0, _createClass2.default)(ErrorBoundaryWithEditorView, [{ key: "componentDidCatch", value: function componentDidCatch(error, errorInfo) { var _this2 = this; // Only report and re-render once, to avoid over-reporting errors and infinite rerendering if (this.state.error) { return; } if (this.props.errorTracking) { this.sendErrorData({ error: error, errorInfo: errorInfo, errorStack: error.stack }); } // Update state to allow a re-render to attempt graceful recovery (in the event that // the error was caused by a race condition or is intermittent) this.setState({ error: error }, function () { if (_this2.props.rethrow) { // Now that a re-render has occurred, we re-throw to allow product error boundaries // to catch and handle the error too. // // Note that when rethrowing inside a error boundary, the stack trace // from a higher error boundary's componentDidCatch.info param will reset // to this component, instead of the original component which threw it. throw error; } }); } // Ignored via go/ees007 // eslint-disable-next-line @atlaskit/editor/enforce-todo-comment-format // FIXME: This is causing more problems then it's solving. The async check to sniff the browser extensions is block some // react unit tests. Essentially jest never completes and just hangs. This was code was added 3yrs ago so that errors // would detail if the browser had grammarly extension installed or not. I'm not sure if anyone has every inspecting this // as it doesn't look like any dashboards exist for it. // You can see the open handles that are block tests if you run unit tests with --detectOpenHandles // async componentDidMount() { // this.browserExtensions = await sniffUserBrowserExtensions({ // extensions: ['grammarly'], // async: true, // asyncTimeoutMs: 20000, // }); // } }, { key: "render", value: function render() { return /*#__PURE__*/_react.default.createElement(_intlErrorBoundary.IntlErrorBoundary, null, this.props.children); } }]); }(_react.default.Component); (0, _defineProperty2.default)(ErrorBoundaryWithEditorView, "defaultProps", { rethrow: true, errorTracking: true }); var ErrorBoundaryWithEditorViewWithAnalyticsReactContext = exports.ErrorBoundaryWithEditorViewWithAnalyticsReactContext = /*#__PURE__*/function (_ErrorBoundaryWithEdi) { function ErrorBoundaryWithEditorViewWithAnalyticsReactContext(props) { var _this3; (0, _classCallCheck2.default)(this, ErrorBoundaryWithEditorViewWithAnalyticsReactContext); _this3 = _callSuper(this, ErrorBoundaryWithEditorViewWithAnalyticsReactContext, [props]); (0, _defineProperty2.default)(_this3, "logException", function (error, tags) { var _this3$context; var extraTags = {}; extraTags.editorSessionId = (_this3$context = _this3.context) === null || _this3$context === void 0 || (_this3$context = _this3$context.getAtlaskitAnalyticsContext()) === null || _this3$context === void 0 || (_this3$context = _this3$context[0]) === null || _this3$context === void 0 || (_this3$context = _this3$context.fabricEditorCtx) === null || _this3$context === void 0 ? void 0 : _this3$context.editorSessionId; (0, _monitoring.logException)(error, _objectSpread(_objectSpread({}, tags), extraTags)); }); return _this3; } (0, _inherits2.default)(ErrorBoundaryWithEditorViewWithAnalyticsReactContext, _ErrorBoundaryWithEdi); return (0, _createClass2.default)(ErrorBoundaryWithEditorViewWithAnalyticsReactContext); }(ErrorBoundaryWithEditorView); // eslint-disable-next-line @typescript-eslint/ban-types (0, _defineProperty2.default)(ErrorBoundaryWithEditorViewWithAnalyticsReactContext, "contextType", _analyticsNextStableReactContext.default); var _default_1 = (0, _platformFeatureFlagsReact.componentWithCondition)(function () { return (0, _platformFeatureFlags.fg)('platform_editor_sentry_breadcrumbs'); }, (0, _WithEditorView.WithEditorView)(ErrorBoundaryWithEditorViewWithAnalyticsReactContext), (0, _WithEditorView.WithEditorView)(ErrorBoundaryWithEditorView)); var _default = exports.default = _default_1;