Skip to content

Commit

Permalink
do not crashe when used in node (isomorphic)
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanHotsiy committed Jan 9, 2018
1 parent 50e2a58 commit d67852f
Show file tree
Hide file tree
Showing 18 changed files with 128 additions and 82 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
"enzyme": "^3.1.1",
"enzyme-adapter-react-16": "^1.0.4",
"enzyme-to-json": "^3.2.2",
"extract-text-webpack-plugin": "^3.0.0",
"html-webpack-plugin": "^2.30.1",
"http-server": "^0.10.0",
"jest": "^21.1.0",
Expand Down
6 changes: 4 additions & 2 deletions src/common-elements/perfect-scrollbar.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import styled from '../styled-components';
import 'perfect-scrollbar/dist/css/perfect-scrollbar.css';
import styled, { injectGlobal } from '../styled-components';
import psStyles from 'perfect-scrollbar/dist/css/perfect-scrollbar.css';

import PerfectScrollbarOriginal from 'react-perfect-scrollbar';

injectGlobal`${psStyles}`;

export const PerfectScrollbar = styled(PerfectScrollbarOriginal)`
position: relative;
`;
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"weight": "300",
},
"code": Object {
"fontFamily": "\\"Lucida Console\\", Monaco, monospace",
"fontFamily": "Courirer, monospace",
"fontSize": "13px",
},
"colors": Object {
Expand Down Expand Up @@ -114,6 +114,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"width": 40,
},
"schemaView": Object {
"defaultDetailsWidth": "75%",
"linesColor": "#7f99cf",
},
"spacingUnit": 20,
Expand Down Expand Up @@ -176,7 +177,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"weight": "300",
},
"code": Object {
"fontFamily": "\\"Lucida Console\\", Monaco, monospace",
"fontFamily": "Courirer, monospace",
"fontSize": "13px",
},
"colors": Object {
Expand Down Expand Up @@ -218,6 +219,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"width": 40,
},
"schemaView": Object {
"defaultDetailsWidth": "75%",
"linesColor": "#7f99cf",
},
"spacingUnit": 20,
Expand Down Expand Up @@ -263,7 +265,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"weight": "300",
},
"code": Object {
"fontFamily": "\\"Lucida Console\\", Monaco, monospace",
"fontFamily": "Courirer, monospace",
"fontSize": "13px",
},
"colors": Object {
Expand Down Expand Up @@ -305,6 +307,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"width": 40,
},
"schemaView": Object {
"defaultDetailsWidth": "75%",
"linesColor": "#7f99cf",
},
"spacingUnit": 20,
Expand Down Expand Up @@ -404,7 +407,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"weight": "300",
},
"code": Object {
"fontFamily": "\\"Lucida Console\\", Monaco, monospace",
"fontFamily": "Courirer, monospace",
"fontSize": "13px",
},
"colors": Object {
Expand Down Expand Up @@ -446,6 +449,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"width": 40,
},
"schemaView": Object {
"defaultDetailsWidth": "75%",
"linesColor": "#7f99cf",
},
"spacingUnit": 20,
Expand Down Expand Up @@ -508,7 +512,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"weight": "300",
},
"code": Object {
"fontFamily": "\\"Lucida Console\\", Monaco, monospace",
"fontFamily": "Courirer, monospace",
"fontSize": "13px",
},
"colors": Object {
Expand Down Expand Up @@ -550,6 +554,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"width": 40,
},
"schemaView": Object {
"defaultDetailsWidth": "75%",
"linesColor": "#7f99cf",
},
"spacingUnit": 20,
Expand Down Expand Up @@ -595,7 +600,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"weight": "300",
},
"code": Object {
"fontFamily": "\\"Lucida Console\\", Monaco, monospace",
"fontFamily": "Courirer, monospace",
"fontSize": "13px",
},
"colors": Object {
Expand Down Expand Up @@ -637,6 +642,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"width": 40,
},
"schemaView": Object {
"defaultDetailsWidth": "75%",
"linesColor": "#7f99cf",
},
"spacingUnit": 20,
Expand Down Expand Up @@ -706,7 +712,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"weight": "300",
},
"code": Object {
"fontFamily": "\\"Lucida Console\\", Monaco, monospace",
"fontFamily": "Courirer, monospace",
"fontSize": "13px",
},
"colors": Object {
Expand Down Expand Up @@ -748,6 +754,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"width": 40,
},
"schemaView": Object {
"defaultDetailsWidth": "75%",
"linesColor": "#7f99cf",
},
"spacingUnit": 20,
Expand Down Expand Up @@ -843,7 +850,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"weight": "300",
},
"code": Object {
"fontFamily": "\\"Lucida Console\\", Monaco, monospace",
"fontFamily": "Courirer, monospace",
"fontSize": "13px",
},
"colors": Object {
Expand Down Expand Up @@ -885,6 +892,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"width": 40,
},
"schemaView": Object {
"defaultDetailsWidth": "75%",
"linesColor": "#7f99cf",
},
"spacingUnit": 20,
Expand Down Expand Up @@ -947,7 +955,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"weight": "300",
},
"code": Object {
"fontFamily": "\\"Lucida Console\\", Monaco, monospace",
"fontFamily": "Courirer, monospace",
"fontSize": "13px",
},
"colors": Object {
Expand Down Expand Up @@ -989,6 +997,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"width": 40,
},
"schemaView": Object {
"defaultDetailsWidth": "75%",
"linesColor": "#7f99cf",
},
"spacingUnit": 20,
Expand Down Expand Up @@ -1034,7 +1043,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"weight": "300",
},
"code": Object {
"fontFamily": "\\"Lucida Console\\", Monaco, monospace",
"fontFamily": "Courirer, monospace",
"fontSize": "13px",
},
"colors": Object {
Expand Down Expand Up @@ -1076,6 +1085,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"width": 40,
},
"schemaView": Object {
"defaultDetailsWidth": "75%",
"linesColor": "#7f99cf",
},
"spacingUnit": 20,
Expand Down Expand Up @@ -1173,7 +1183,7 @@ exports[`Components SchemaView discriminator should correctly render discriminat
"weight": "300",
},
"code": Object {
"fontFamily": "\\"Lucida Console\\", Monaco, monospace",
"fontFamily": "Courirer, monospace",
"fontSize": "13px",
},
"colors": Object {
Expand Down Expand Up @@ -1215,6 +1225,7 @@ exports[`Components SchemaView discriminator should correctly render discriminat
"width": 40,
},
"schemaView": Object {
"defaultDetailsWidth": "75%",
"linesColor": "#7f99cf",
},
"spacingUnit": 20,
Expand Down Expand Up @@ -1284,7 +1295,7 @@ exports[`Components SchemaView discriminator should correctly render discriminat
"weight": "300",
},
"code": Object {
"fontFamily": "\\"Lucida Console\\", Monaco, monospace",
"fontFamily": "Courirer, monospace",
"fontSize": "13px",
},
"colors": Object {
Expand Down Expand Up @@ -1326,6 +1337,7 @@ exports[`Components SchemaView discriminator should correctly render discriminat
"width": 40,
},
"schemaView": Object {
"defaultDetailsWidth": "75%",
"linesColor": "#7f99cf",
},
"spacingUnit": 20,
Expand Down
13 changes: 8 additions & 5 deletions src/components/StickySidebar/StickySidebar.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import * as React from 'react';
import Stickyfill from 'stickyfill';

import { ComponentWithOptions } from '../OptionsProvider';
import { RedocNormalizedOptions, RedocRawOptions } from '../../services/RedocNormalizedOptions';
import styled from '../../styled-components';

let Stickyfill;
if (typeof window !== 'undefined') {
Stickyfill = require('stickyfill').default;
}

export interface StickySidebarProps {
className?: string;
scrollYOffset?: RedocRawOptions['scrollYOffset']; // passed directly or via context
}

const stickyfill = Stickyfill();
const stickyfill = Stickyfill && Stickyfill();

const StyledStickySidebar = styled.div`
width: ${props => props.theme.menu.width};
Expand All @@ -31,11 +34,11 @@ export class StickySidebar extends ComponentWithOptions<StickySidebarProps> {
stickyElement: Element;

componentDidMount() {
stickyfill.add(this.stickyElement);
stickyfill && stickyfill.add(this.stickyElement);
}

componentWillUnmount() {
stickyfill.remove(this.stickyElement);
stickyfill && stickyfill.remove(this.stickyElement);
}

get scrollYOffset() {
Expand Down
5 changes: 4 additions & 1 deletion src/services/ClipboardService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const isSupported = document.queryCommandSupported && document.queryCommandSupported('copy');
const isSupported =
typeof document !== 'undefined' &&
document.queryCommandSupported &&
document.queryCommandSupported('copy');

export class ClipboardService {
static isSupported(): boolean {
Expand Down
19 changes: 14 additions & 5 deletions src/services/HistoryService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { bind, debounce } from 'decko';
import { EventEmitter } from 'eventemitter3';
import { isBrowser } from '../utils/';

const EVENT = 'hashchange';

Expand All @@ -17,7 +18,7 @@ class _HistoryService {
}

get hash(): string {
return window.location.hash;
return isBrowser ? window.location.hash : '';
}

subscribe(cb): () => void {
Expand All @@ -34,11 +35,15 @@ class _HistoryService {
};

bind() {
window.addEventListener('hashchange', this.emit, false);
if (isBrowser) {
window.addEventListener('hashchange', this.emit, false);
}
}

dispose() {
window.removeEventListener('hashchange', this.emit);
if (isBrowser) {
window.removeEventListener('hashchange', this.emit);
}
this.causedHashChange = false;
}

Expand All @@ -47,11 +52,15 @@ class _HistoryService {
update(hash: string | null, rewriteHistory: boolean = false) {
if (hash == null || isSameHash(hash, this.hash)) return;
if (rewriteHistory) {
window.history.replaceState(null, '', window.location.href.split('#')[0] + '#' + hash);
if (isBrowser) {
window.history.replaceState(null, '', window.location.href.split('#')[0] + '#' + hash);
}
return;
}
this.causedHashChange = true;
window.location.hash = hash;
if (isBrowser) {
window.location.hash = hash;
}
}
}

Expand Down
9 changes: 1 addition & 8 deletions src/services/MarkdownRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as Remarkable from 'remarkable';
import { MDComponent } from '../components/Markdown/Markdown';
import { IMenuItem, SECTION_ATTR } from './MenuStore';
import { GroupModel } from './models';
import { highlight } from '../utils';
import { highlight, html2Str } from '../utils';

const md = new Remarkable('default', {
html: true,
Expand Down Expand Up @@ -64,13 +64,6 @@ export class MarkdownRenderer {
attachHeadingsContent(rawText: string) {
const buildRegexp = heading => new RegExp(`<h\\d ${SECTION_ATTR}="section/${heading.id}">`);

const tmpEl = document.createElement('DIV');

const html2Str = html => {
tmpEl.innerHTML = html;
return tmpEl.innerText;
};

let flatHeadings = this.flattenHeadings(this.headings);
if (flatHeadings.length < 1) return;
let prevHeading = flatHeadings[0];
Expand Down
Loading

0 comments on commit d67852f

Please sign in to comment.