|
| 1 | +import { DiffEditor } from '@monaco-editor/react'; |
| 2 | +import React, { useEffect, useState } from 'react'; |
| 3 | +import {Button, ButtonGroup, FormControl, Grid, Tooltip} from '@material-ui/core'; |
| 4 | +import { useStyles } from '../../utils/hooks'; |
| 5 | +import * as monaco from 'monaco-editor'; |
| 6 | +import { useEffectOnce } from 'react-use'; |
| 7 | +import { Select, SelectItem } from '@backstage/core-components'; |
| 8 | + |
| 9 | +import { |
| 10 | + ClearValueButton, |
| 11 | + FileUploadButton, |
| 12 | + CopyToClipboardButton, |
| 13 | + PasteFromClipboardButton |
| 14 | +} from "../Buttons"; |
| 15 | +import Input from "@material-ui/icons/Input"; |
| 16 | + |
| 17 | +export type MonacoLanguages = { name: string; extensions: string[] }; |
| 18 | + |
| 19 | +type SampleButtonProps = { |
| 20 | + sample: string[]; |
| 21 | + setInput: (input: string[]) => void; |
| 22 | +}; |
| 23 | + |
| 24 | +const options: monaco.editor.IDiffEditorConstructionOptions = { |
| 25 | + originalEditable: true, |
| 26 | + diffCodeLens: true, |
| 27 | + dragAndDrop: true, |
| 28 | + tabCompletion: 'on', |
| 29 | + renderSideBySide: true, |
| 30 | +}; |
| 31 | + |
| 32 | +function getLanguage(allowedLanguages: MonacoLanguages[], extension: string) { |
| 33 | + return allowedLanguages.find(monacoLanguage => |
| 34 | + monacoLanguage.extensions.includes(extension as string))?.name; |
| 35 | +} |
| 36 | + |
| 37 | +function readFileAndSetText( |
| 38 | + file: File | undefined, |
| 39 | + setText: (value: ((prevState: string) => string) | string) => void, |
| 40 | + setLanguage: (value: ((prevState: string) => string) | string) => void, |
| 41 | + allowedLanguages: MonacoLanguages[], |
| 42 | +) { |
| 43 | + if(!file) { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + const reader = new FileReader(); |
| 48 | + reader.onload = async e => { |
| 49 | + // @ts-ignore |
| 50 | + setText(e.target.result); |
| 51 | + }; |
| 52 | + reader.readAsText(file); |
| 53 | + let newLanguage = 'plaintext'; |
| 54 | + const extension = `.${file.name.split('.').pop()}`; |
| 55 | + if (allowedLanguages?.length) { |
| 56 | + newLanguage = getLanguage(allowedLanguages, extension) || newLanguage; |
| 57 | + } |
| 58 | + setLanguage(newLanguage); |
| 59 | +} |
| 60 | + |
| 61 | +export const SampleButton = (props: SampleButtonProps) => { |
| 62 | + const { sample, setInput } = props; |
| 63 | + return ( |
| 64 | + <Tooltip arrow title="Input sample"> |
| 65 | + <Button |
| 66 | + size="small" |
| 67 | + startIcon={<Input />} |
| 68 | + onClick={() => setInput(sample)} |
| 69 | + > |
| 70 | + Sample |
| 71 | + </Button> |
| 72 | + </Tooltip> |
| 73 | + ); |
| 74 | +}; |
| 75 | + |
| 76 | +function Diff() { |
| 77 | + const styles = useStyles(); |
| 78 | + const [originalFile, setOriginalFile] = useState<File>(); |
| 79 | + const [modifiedFile, setModifiedFile] = useState<File>(); |
| 80 | + |
| 81 | + const [originalText, setOriginalText] = useState(''); |
| 82 | + const [modifiedText, setModifiedText] = useState(''); |
| 83 | + |
| 84 | + const [language, setLanguage] = useState('plaintext'); |
| 85 | + const [allowedLanguages, setAllowedLanguages] = useState<MonacoLanguages[]>([],); |
| 86 | + |
| 87 | + const exampleOriginalText = 'Backstage toolbox\n\ncompare text'; |
| 88 | + const exampleModifiedText = 'Backstage toolbox\ndiff editor'; |
| 89 | + const handleLanguageSelect = (selected: any) => { |
| 90 | + setLanguage(selected); |
| 91 | + }; |
| 92 | + |
| 93 | + useEffect(() => { |
| 94 | + readFileAndSetText( |
| 95 | + modifiedFile, |
| 96 | + setModifiedText, |
| 97 | + setLanguage, |
| 98 | + allowedLanguages, |
| 99 | + ); |
| 100 | + }, [modifiedFile, allowedLanguages]); |
| 101 | + |
| 102 | + useEffect(() => { |
| 103 | + readFileAndSetText( |
| 104 | + originalFile, |
| 105 | + setOriginalText, |
| 106 | + setLanguage, |
| 107 | + allowedLanguages, |
| 108 | + ); |
| 109 | + }, [originalFile, allowedLanguages]); |
| 110 | + |
| 111 | + useEffectOnce(() => { |
| 112 | + const languages: MonacoLanguages[] = monaco.languages |
| 113 | + .getLanguages() |
| 114 | + .map(each => { |
| 115 | + return { name: each.id, extensions: each.extensions || [] }; |
| 116 | + }); |
| 117 | + setAllowedLanguages(languages); |
| 118 | + }); |
| 119 | + |
| 120 | + const languageOptions: SelectItem[] = allowedLanguages |
| 121 | + ? allowedLanguages.map(i => ({ label: i.name, value: i.name })) |
| 122 | + : [{ label: 'Loading...', value: 'loading' }]; |
| 123 | + |
| 124 | + return ( |
| 125 | + <> |
| 126 | + <FormControl className={styles.fullWidth}> |
| 127 | + <Grid container style={{ width: '100%' }}> |
| 128 | + <Grid item style={{ minWidth: '200px' }}> |
| 129 | + <Select |
| 130 | + selected={language} |
| 131 | + onChange={handleLanguageSelect} |
| 132 | + items={languageOptions} |
| 133 | + label="Select Text Language" |
| 134 | + /> |
| 135 | + </Grid> |
| 136 | + </Grid> |
| 137 | + <Grid container style={{ width: '100%' }}> |
| 138 | + <Grid item> |
| 139 | + {exampleOriginalText && exampleModifiedText && |
| 140 | + <SampleButton |
| 141 | + setInput={input => { |
| 142 | + setOriginalText( input[0] ); |
| 143 | + setModifiedText( input[1] ); |
| 144 | + }} |
| 145 | + sample={[exampleOriginalText, exampleModifiedText]} |
| 146 | + />} |
| 147 | + </Grid> |
| 148 | + </Grid> |
| 149 | + <Grid container style={{ marginBottom: '5px', width: '100%' }}> |
| 150 | + <Grid item style={{ width: '50%' }}> |
| 151 | + <ButtonGroup size="small"> |
| 152 | + <FileUploadButton |
| 153 | + onFileLoad={setOriginalFile} |
| 154 | + id="originalFile" |
| 155 | + buttonText="Original File" |
| 156 | + /> |
| 157 | + <ClearValueButton setValue={setOriginalText} /> |
| 158 | + <PasteFromClipboardButton setInput={setOriginalText} /> |
| 159 | + {originalText && <CopyToClipboardButton output={originalText} />} |
| 160 | + </ButtonGroup> |
| 161 | + </Grid> |
| 162 | + <Grid item style={{ width: '50%' }}> |
| 163 | + <ButtonGroup size="small"> |
| 164 | + <FileUploadButton |
| 165 | + onFileLoad={setModifiedFile} |
| 166 | + id="modifiedFile" |
| 167 | + buttonText="Modified File" |
| 168 | + /> |
| 169 | + <ClearValueButton setValue={setModifiedText} /> |
| 170 | + <PasteFromClipboardButton setInput={setModifiedText} /> |
| 171 | + {modifiedText && <CopyToClipboardButton output={modifiedText} />} |
| 172 | + </ButtonGroup> |
| 173 | + </Grid> |
| 174 | + </Grid> |
| 175 | + <DiffEditor |
| 176 | + height="100vh" |
| 177 | + original={originalText} |
| 178 | + modified={modifiedText} |
| 179 | + options={options} |
| 180 | + language={language} |
| 181 | + /> |
| 182 | + </FormControl> |
| 183 | + </> |
| 184 | + ); |
| 185 | +} |
| 186 | + |
| 187 | +export default Diff; |
0 commit comments