|
| 1 | +import React, { useContext, useState, useRef, useEffect } from 'react'; |
| 2 | +import StateContext from '../../context/context'; |
| 3 | +import AceEditor from 'react-ace'; |
| 4 | +import 'ace-builds/src-noconflict/mode-css'; |
| 5 | +import 'ace-builds/src-noconflict/theme-monokai'; |
| 6 | +import 'ace-builds/src-noconflict/theme-github'; |
| 7 | +import 'ace-builds/src-noconflict/theme-solarized_dark'; |
| 8 | +import 'ace-builds/src-noconflict/theme-solarized_light'; |
| 9 | +import 'ace-builds/src-noconflict/theme-terminal'; |
| 10 | +import 'ace-builds/src-min-noconflict/ext-searchbox'; |
| 11 | +import { Component } from '../../interfaces/Interfaces'; |
| 12 | +import useResizeObserver from '../../tree/useResizeObserver'; |
| 13 | +import { string } from 'prop-types'; |
| 14 | +import Fab from '@material-ui/core/Fab'; |
| 15 | +import SaveIcon from '@material-ui/icons/Save'; |
| 16 | + |
| 17 | +const StylesEditor: React.FC<{ |
| 18 | + theme: string | null; |
| 19 | + setTheme: any | null; |
| 20 | +}> = ({ theme, setTheme }) => { |
| 21 | + const wrapper = useRef(); |
| 22 | + const [css, setCss] = useState(); |
| 23 | + // const [state, dispatch] = useContext(StateContext); |
| 24 | + |
| 25 | + useEffect(() => { |
| 26 | + loadFile(); |
| 27 | + }, []); |
| 28 | + |
| 29 | + const loadFile = () => { |
| 30 | + const myHeaders = new Headers({ |
| 31 | + 'Content-Type': 'text/css', |
| 32 | + Accept: 'text/css', |
| 33 | + }); |
| 34 | + fetch('/demoRender', { |
| 35 | + headers: myHeaders, |
| 36 | + }) |
| 37 | + .then(response => response.text()) |
| 38 | + .then((data) => { |
| 39 | + setCss(data); |
| 40 | + }); |
| 41 | + } |
| 42 | + |
| 43 | + const saveFile = () => { |
| 44 | + console.log('saveFile: ', css); |
| 45 | + // const myHeaders = new Headers({ |
| 46 | + // headers: { 'Content-Type': 'application/json' }, |
| 47 | + // }); |
| 48 | + fetch('/user-styles/save', { |
| 49 | + method: 'POST', |
| 50 | + headers: { 'Content-Type': 'application/json' }, |
| 51 | + body: JSON.stringify({ data: css }), |
| 52 | + }) |
| 53 | + .then(response => response.text()) |
| 54 | + .then((data) => { |
| 55 | + // setCss(data); |
| 56 | + //message save! |
| 57 | + }); |
| 58 | + } |
| 59 | + const saveCss = (e) => { |
| 60 | + e.preventDefault(); |
| 61 | + saveFile(); |
| 62 | + } |
| 63 | + |
| 64 | + const handleChange = (text) => { |
| 65 | + setCss(text); |
| 66 | + } |
| 67 | + |
| 68 | + return ( |
| 69 | + <div |
| 70 | + className='text-editor' |
| 71 | + ref={wrapper} |
| 72 | + style={{ |
| 73 | + height: '40vh', |
| 74 | + maxWidth: '100%', |
| 75 | + justifyContent: 'center', |
| 76 | + }} |
| 77 | + > |
| 78 | + <AceEditor |
| 79 | + mode="css" |
| 80 | + theme={'solarized_dark'} |
| 81 | + width="100%" |
| 82 | + height="100%" |
| 83 | + onChange={handleChange} |
| 84 | + value={css} |
| 85 | + name="Css_div" |
| 86 | + // readOnly={false} |
| 87 | + fontSize={16} |
| 88 | + tabSize={2} |
| 89 | + enableBasicAutocompletion={true} |
| 90 | + enableLiveAutocompletion={true} |
| 91 | + /> |
| 92 | + <Fab className='btn' onClick={saveCss} color="secondary" aria-label="add"> |
| 93 | + <SaveIcon /> |
| 94 | + </Fab> |
| 95 | + </div> |
| 96 | + ); |
| 97 | +}; |
| 98 | + |
| 99 | +export default StylesEditor; |
0 commit comments