import * as React from 'react'; import * as types from '../types'; import { style, classes, cssRaw, media } from 'typestyle'; import * as csstips from 'csstips'; import * as styles from '../internal/styles'; import { highlightCodeWithMode, MarkDownStyles } from '../internal/markdown'; import { Expandible } from 'expandible'; import * as gls from './components/gls'; import { Toggle } from './components/toggle'; import * as icons from './components/icons'; import { Loader } from "./components/loader"; import { P } from "./components/txt"; import { IframeC2PResize } from './components/toc'; const heightStore = new class { get(iframeId: string) { return parseInt(localStorage.getItem('heightStore' + iframeId) || '1'); } set(iframeId: string, height: number) { localStorage.setItem('heightStore' + iframeId, height.toString()); } } export class HtmlRenderer extends React.PureComponent { render() { const { props } = this; return
; } } export class CodeRenderer extends React.PureComponent { constructor(props) { super(props); this.state = { viewCode: !props.collapsed } } render() { const { props } = this; return { !!props.collapsed && this.setState({ viewCode: !this.state.viewCode })} /> }
; } } namespace AppRendererStyles { const borderColor = '#bbb' const topBorderHeight = 2; const bottomBorderHeight = 2; /** Keep centered with fixed widths */ export const centerWidth = { position: 'relative' as 'relative', left: '50%', transform: 'translateX(-50%)' } export const iframe = style({ display: 'block', /** Bit big border needed as mobile browsers have rendering issues with 1px */ borderTop: `${topBorderHeight}px solid ${borderColor}`, borderBottom: `${bottomBorderHeight}px solid ${borderColor}`, borderLeft: `4px solid ${borderColor}`, borderRight: `2px solid ${borderColor}`, transition: 'width .2s, height .2s', }); export const auto = style({ width: '100%', }); export const desktop = style(centerWidth, { width: '1200px', }); export const tablet = style(centerWidth, { width: '768px', }); export const mobile = style(centerWidth, { width: '320px', }); /** Autosize the iframe to remove scroll bars http://stackoverflow.com/a/9976309/390330 */ export function resizeIframe(frame: HTMLIFrameElement) { /** * Without this we expand constantly slowly and slowly **/ if (frame.style.height === frame.contentWindow.document.body.scrollHeight + 24 + 'px') { return; } /** Without the +borderHeight we still get scrollbars :-/ */ frame.style.height = frame.contentWindow.document.body.scrollHeight + 24 + 'px'; } } export type AppRensponsiveMode = 'auto' | 'desktop' | 'tablet' | 'mobile'; export class AppRenderer extends React.PureComponent { constructor(props) { super(props); this.state = { mode: 'auto', viewCode: false, loading: true, } } componentDidMount() { IframeC2PResize.on(({ iframeId, height }) => { if (iframeId === types.makeIframeId(this.props.index)) { this.ctrls.frame.style.height = height + 'px'; } }); } ctrls: { frame?: HTMLIFrameElement } = {} render() { const { props } = this; return {this.state.loading && } {/** iframe the html */}