import { classy } from '@audentio/utils/src/classy'; import { throttle } from '@audentio/utils/src/throttle'; import ContentCopyIcon from 'mdi-react/ContentCopyIcon'; import React, { Component } from 'react'; import { Tooltip } from '../Tooltip'; import style from './CodeBlock.scss'; interface CodeBlockProps { /** * code language (enables syntax highlighting) */ language?: string; /** * Automatically fix indentation issues (removes indentation at start of code, so it doens't look odd) * @default true */ fixWhiteSpace?: boolean; /** * Codeblock label. Pass boolean to use language as labbel */ label?: string | boolean; className?: string; } export class CodeBlock extends Component { _isMounted: boolean; __label: HTMLElement; __code: HTMLElement; copyTimer: NodeJS.Timer; static defaultProps = { highlight: true, fixWhiteSpace: true, theme: 'tomorrow', }; public state = { copied: false, }; public componentDidMount(): void { this._isMounted = true; this.runHighlighter(); } shouldComponentUpdate(nextProps, nextState) { // update if source changed return ( this.getSource(nextProps.children) !== this.getSource(this.props.children) || this.state.copied !== nextState.copied ); } componentDidUpdate() { this.runHighlighter(); } public componentWillUnmount(): void { this._isMounted = false; } getSource(children) { if (typeof children === 'object') { const stringchildren = children.filter((child): boolean => typeof child === 'string'); return stringchildren.join(); } if (typeof children === 'string') return children; return children || ''; } /* eslint-disable */ private copyToClipboard = (str): void => { const el = document.createElement('textarea'); // Create a