overlay.js 5.67 KiB
/* @flow */
import {
  register as registerError,
  unregister as unregisterError,
} from './effects/unhandledError';
import {
  register as registerPromise,
  unregister as unregisterPromise,
} from './effects/unhandledRejection';
import {
  register as registerShortcuts,
  unregister as unregisterShortcuts,
  handler as keyEventHandler,
  SHORTCUT_ESCAPE,
  SHORTCUT_LEFT,
  SHORTCUT_RIGHT,
} from './effects/shortcuts';
import {
  register as registerStackTraceLimit,
  unregister as unregisterStackTraceLimit,
} from './effects/stackTraceLimit';
import {
  permanentRegister as permanentRegisterConsole,
  registerReactStack,
  unregisterReactStack,
} from './effects/proxyConsole';
import { massage as massageWarning } from './utils/warnings';
import {
  consume as consumeError,
  getErrorRecord,
  drain as drainErrors,
} from './utils/errorRegister';
import type { ErrorRecordReference } from './utils/errorRegister';
import type { StackFrame } from './utils/stack-frame';
import { iframeStyle } from './styles';
import { injectCss, applyStyles } from './utils/dom/css';
import { createOverlay } from './components/overlay';
import { updateAdditional } from './components/additional';
const CONTEXT_SIZE: number = 3;
let iframeReference: HTMLIFrameElement | null = null;
let additionalReference = null;
let errorReferences: ErrorRecordReference[] = [];
let currReferenceIndex: number = -1;
const css = [
  '.cra-container {',
  '  padding-right: 15px;',
  '  padding-left: 15px;',
  '  margin-right: auto;',
  '  margin-left: auto;',
  '}',
  '',
  '@media (min-width: 768px) {',
  '  .cra-container {',
  '    width: calc(750px - 6em);',
  '  }',
  '}',
  '',
  '@media (min-width: 992px) {',
  '  .cra-container {',
  '    width: calc(970px - 6em);',
  '  }',
  '}',
  '',
  '@media (min-width: 1200px) {',
  '  .cra-container {',
  '    width: calc(1170px - 6em);',
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
' }', '}', ].join('\n'); function render(name: ?string, message: string, resolvedFrames: StackFrame[]) { disposeCurrentView(); const iframe = window.document.createElement('iframe'); applyStyles(iframe, iframeStyle); iframeReference = iframe; iframe.onload = () => { if (iframeReference == null) { return; } const w = iframeReference.contentWindow; const document = iframeReference.contentDocument; const { overlay, additional } = createOverlay( document, name, message, resolvedFrames, CONTEXT_SIZE, currReferenceIndex + 1, errorReferences.length, offset => { switchError(offset); }, () => { unmount(); } ); if (w != null) { w.onkeydown = event => { keyEventHandler(type => shortcutHandler(type), event); }; } injectCss(iframeReference.contentDocument, css); if (document.body != null) { document.body.appendChild(overlay); } additionalReference = additional; }; window.document.body.appendChild(iframe); } function renderErrorByIndex(index: number) { currReferenceIndex = index; const { error, unhandledRejection, enhancedFrames } = getErrorRecord( errorReferences[index] ); if (unhandledRejection) { render( 'Unhandled Rejection (' + error.name + ')', error.message, enhancedFrames ); } else { render(error.name, error.message, enhancedFrames); } } function switchError(offset) { const nextView = currReferenceIndex + offset; if (nextView < 0 || nextView >= errorReferences.length) { return; } renderErrorByIndex(nextView);