aboutsummaryrefslogtreecommitdiffstats
path: root/packages/dev-tools-pages/ts/components/Code.tsx
blob: 72acc386510c98d9e74fd4300f167bb85d1ad2e9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import * as React from 'react';
import styled from 'styled-components';

import { colors } from 'ts/variables';

import { Button as BaseButton } from './Button';

const isTouch = Boolean(
    'ontouchstart' in window ||
        (window as any).navigator.maxTouchPoints > 0 ||
        (window as any).navigator.msMaxTouchPoints > 0,
);

interface CodeProps {
    language?: string;
    isLight?: boolean;
    isDiff?: boolean;
    gutter?: Array<number | undefined>;
    gutterLength?: number;
    canCopy?: boolean;
    isEtc?: boolean;
}

interface CodeState {
    hlCode?: string;
    didCopy?: boolean;
}

const Button = styled(BaseButton)`
    opacity: ${isTouch ? '1' : '0'};
    position: absolute;
    top: 1rem;
    right: 1rem;
    transition: opacity 0.2s;
    :focus {
        opacity: 1;
    }
`;

const Container = styled.div`
    position: relative;
    &:hover ${Button} {
        opacity: 1;
    }
`;

const Base =
    styled.div <
    CodeProps >
    `
    font-size: .875rem;
    color: ${props => (props.language === undefined ? colors.white : 'inherit')};
    background-color: ${props =>
        props.isLight ? 'rgba(255,255,255,.15)' : props.language === undefined ? colors.black : '#F1F4F5'};
    white-space: ${props => (props.language === undefined ? 'nowrap' : '')};
    position: relative;

    ${props =>
        props.isDiff
            ? `
        background-color: #E9ECED;
        display: flex;
        padding-top: 1.5rem;
        padding-bottom: 1.5rem;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    `
            : ``}
`;

const StyledCodeDiff = styled(({ gutterLength, children, ...props }: any) => <code {...props}>{children}</code>)`
    ::before {
        content: '';
        width: calc(0.75rem + ${props => props.gutterLength}ch);
        background-color: #e2e5e6;
        position: absolute;
        top: 0;
        left: 0;
        bottom: 0;
    }

    [class^='line-'] {
        display: inline-block;
        width: 100%;
        position: relative;
        padding-right: 1.5rem;
        padding-left: calc(2.25rem + ${props => props.gutterLength}ch);

        ::before {
            content: attr(data-gutter);

            width: ${props => props.gutterLength};
            padding-left: 0.375rem;
            padding-right: 0.375rem;
            position: absolute;
            top: 50%;
            left: 0;
            transform: translateY(-50%);
            z-index: 1;
        }
    }

    .line-addition {
        background-color: rgba(0, 202, 105, 0.1);
    }
    .line-deletion {
        background-color: rgba(255, 0, 0, 0.07);
    }
`;

const StyledPre = styled.pre`
    margin: 0;
    ${(props: { isDiff: boolean }) =>
        !props.isDiff
            ? `
        padding: 1.5rem;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    `
            : ``};
`;

const StyledCopyInput = styled.textarea`
    opacity: 0;
    height: 0;
    position: absolute;
    top: 0;
    right: 0;
    z-index: -1;
`;

const CopyInput = StyledCopyInput as any;

class Code extends React.Component<CodeProps, CodeState> {
    public state: CodeState = {};
    private readonly _code = React.createRef<HTMLTextAreaElement>();
    public componentDidMount(): void {
        /*
        * _onMountAsync is only setting state, so no point in handling the promise
        */
        // tslint:disable-next-line:no-floating-promises
        this._onMountAsync();
    }
    public render(): React.ReactNode {
        const { language, isLight, isDiff, children, gutterLength, canCopy } = this.props;
        const { hlCode } = this.state;

        let CodeComponent = 'code';
        let codeProps = {};
        if (isDiff) {
            codeProps = { gutterLength };
            CodeComponent = StyledCodeDiff as any;
        }

        return (
            <Container>
                <Base language={language} isDiff={isDiff} isLight={isLight}>
                    <StyledPre isDiff={isDiff}>
                        <CodeComponent
                            {...codeProps}
                            dangerouslySetInnerHTML={hlCode ? { __html: this.state.hlCode } : null}
                        >
                            {hlCode === undefined ? children : null}
                        </CodeComponent>
                    </StyledPre>
                    {!('clipboard' in navigator) ? (
                        <CopyInput readOnly={true} aria-hidden="true" ref={this._code} value={children} />
                    ) : null}
                </Base>
                {navigator.userAgent !== 'ReactSnap' && canCopy ? (
                    <Button onClick={this._handleCopyAsync}>{this.state.didCopy ? 'Copied' : 'Copy'}</Button>
                ) : null}
            </Container>
        );
    }
    private async _onMountAsync(): Promise<void> {
        const { language, children, isDiff, gutter, isEtc } = this.props;

        const code = children as string;

        if (language !== undefined) {
            const { highlight } = await System.import(/* webpackChunkName: 'highlightjs' */ 'ts/highlight');

            this.setState({
                hlCode: highlight({ language, code, isDiff, gutter, isEtc }),
            });
        }
    }
    private readonly _handleCopyAsync = async () => {
        try {
            if ('clipboard' in navigator) {
                await (navigator as any).clipboard.writeText(this.props.children);
                this.setState({ didCopy: true });
            } else {
                const lastActive = document.activeElement as HTMLElement;
                this._code.current.focus();
                this._code.current.select();
                document.execCommand('copy');
                lastActive.focus();
                this.setState({ didCopy: true });
            }
        } catch (error) {
            this.setState({ didCopy: false });
        }
    };
}

export { Code };