UNPKG

@atlaskit/editor-core

Version:

A package contains Atlassian editor core functionality

208 lines (205 loc) • 11.1 kB
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _inherits from "@babel/runtime/helpers/inherits"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; 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) { _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; } import _regeneratorRuntime from "@babel/runtime/regenerator"; function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(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; })(); } import React from 'react'; // eslint-disable-next-line @atlaskit/platform/prefer-crypto-random-uuid -- Use crypto.randomUUID instead import uuid from 'uuid'; import { default as AnalyticsReactContext } from '@atlaskit/analytics-next-stable-react-context'; import { ACTION, ACTION_SUBJECT, editorAnalyticsChannel, EVENT_TYPE } from '@atlaskit/editor-common/analytics'; import { getDocStructure } from '@atlaskit/editor-common/core-utils'; import { IntlErrorBoundary } from '@atlaskit/editor-common/intl-error-boundary'; import { logException } from '@atlaskit/editor-common/monitoring'; import { fg } from '@atlaskit/platform-feature-flags'; import { componentWithCondition } from '@atlaskit/platform-feature-flags-react'; import { isOutdatedBrowser } from '../utils/outdatedBrowsers'; import { WithEditorView } from './WithEditorView'; // Ignored via go/ees005 // eslint-disable-next-line @repo/internal/react/no-class-components export var ErrorBoundaryWithEditorView = /*#__PURE__*/function (_React$Component) { function ErrorBoundaryWithEditorView(props) { var _this; _classCallCheck(this, ErrorBoundaryWithEditorView); _this = _callSuper(this, ErrorBoundaryWithEditorView, [props]); _defineProperty(_this, "browserExtensions", undefined); _defineProperty(_this, "state", { error: undefined }); _defineProperty(_this, "sendErrorData", /*#__PURE__*/function () { var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(analyticsErrorPayload) { var _window; var product, error, errorInfo, sharedId, browserInfo, attributes; return _regeneratorRuntime.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 = uuid(); 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 ? getDocStructure(_this.props.editorView.state.doc, { compact: true }) : undefined, outdatedBrowser: isOutdatedBrowser(browserInfo) }; _this.fireAnalyticsEvent({ action: ACTION.EDITOR_CRASHED, actionSubject: ACTION_SUBJECT.EDITOR, eventType: EVENT_TYPE.OPERATIONAL, attributes: attributes }); _this.fireAnalyticsEvent({ action: ACTION.EDITOR_CRASHED_ADDITIONAL_INFORMATION, actionSubject: ACTION_SUBJECT.EDITOR, eventType: 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); }; }()); _defineProperty(_this, "getProductName", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() { var contextIdentifierProvider, context; return _regeneratorRuntime.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); }))); _defineProperty(_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(editorAnalyticsChannel); }); _defineProperty(_this, "logException", function (error, tags) { logException(error, tags); }); _this.featureFlags = props.featureFlags; return _this; } _inherits(ErrorBoundaryWithEditorView, _React$Component); return _createClass(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.createElement(IntlErrorBoundary, null, this.props.children); } }]); }(React.Component); _defineProperty(ErrorBoundaryWithEditorView, "defaultProps", { rethrow: true, errorTracking: true }); export var ErrorBoundaryWithEditorViewWithAnalyticsReactContext = /*#__PURE__*/function (_ErrorBoundaryWithEdi) { function ErrorBoundaryWithEditorViewWithAnalyticsReactContext(props) { var _this3; _classCallCheck(this, ErrorBoundaryWithEditorViewWithAnalyticsReactContext); _this3 = _callSuper(this, ErrorBoundaryWithEditorViewWithAnalyticsReactContext, [props]); _defineProperty(_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; logException(error, _objectSpread(_objectSpread({}, tags), extraTags)); }); return _this3; } _inherits(ErrorBoundaryWithEditorViewWithAnalyticsReactContext, _ErrorBoundaryWithEdi); return _createClass(ErrorBoundaryWithEditorViewWithAnalyticsReactContext); }(ErrorBoundaryWithEditorView); // eslint-disable-next-line @typescript-eslint/ban-types _defineProperty(ErrorBoundaryWithEditorViewWithAnalyticsReactContext, "contextType", AnalyticsReactContext); var _default_1 = componentWithCondition(function () { return fg('platform_editor_sentry_breadcrumbs'); }, WithEditorView(ErrorBoundaryWithEditorViewWithAnalyticsReactContext), WithEditorView(ErrorBoundaryWithEditorView)); export default _default_1;