github.com/raphaelreyna/latte@v0.11.2-0.20220317193248-98e2fcef4eef/docs/src/App.js (about) 1 import React from "react"; 2 import AceEditor from "react-ace"; 3 import { Document, Page } from "react-pdf"; 4 import { Base64 } from 'js-base64'; 5 6 import "ace-builds/src-noconflict/mode-latex"; 7 import "ace-builds/src-noconflict/mode-json"; 8 import "ace-builds/src-noconflict/theme-monokai"; 9 10 const latteHost = 'https://raphaelreyna.works/api/latte/generate'; 11 const headers = { 12 'Content-Type': 'application/json', 13 'Access-Control-Allow-Origin': '*', 14 }; 15 16 const sampleTex = `\\documentclass{article} 17 \\title{LaTTe Sample Document} 18 \\begin{document} 19 \\maketitle 20 Hello #! .name !#! 21 \\end{document}`; 22 const sampleJSON = `{"name": "world"}`; 23 24 export default class App extends React.Component { 25 constructor(props) { 26 super(props); 27 this.state = { 28 tex: sampleTex, 29 json: sampleJSON, 30 pdf: undefined, 31 }; 32 } 33 34 componentDidMount() { 35 this.submit(); 36 } 37 38 async submit() { 39 const { tex, json } = this.state; 40 const tmpl = Base64.encode(tex); 41 const dtls = JSON.parse(json); 42 const request = { 43 method: 'POST', 44 mode: 'cors', 45 headers: headers, 46 body: JSON.stringify({ template: tmpl, details: dtls}), 47 } 48 var ok = false; 49 const response = await fetch(latteHost, request) 50 .then(r => { 51 if (r.ok) { 52 ok = true; 53 return r.blob() 54 } 55 return r.json() 56 }) 57 .catch(err => console.log(err)); 58 console.log(response); 59 ok ? this.setState({ pdf: response }) : alert(response.data); 60 } 61 62 clearTex() { 63 this.setState({ tex: '' }); 64 } 65 66 clearJSON() { 67 this.setState({ json: '' }); 68 } 69 70 render() { 71 const pdf = this.state.pdf; 72 var pdfURL = undefined; 73 if (pdf != undefined) { 74 pdfURL = window.URL.createObjectURL(pdf); 75 } 76 return ( 77 <div> 78 <div id="button-banner"> 79 <button className="button" onClick={() => this.submit()}> 80 Submit 81 </button> 82 <button className="button" onClick={() => this.clearTex()}> 83 Clear LaTeX 84 </button> 85 <button className="button" onClick={() => this.clearJSON()}> 86 Clear JSON 87 </button> 88 { pdfURL ? <a className="linkButton button" href={pdfURL} download="LaTTe.pdf">Download PDF</a> : null } 89 <form id="github" action="https://github.com/raphaelreyna/latte"> 90 <input className="button" type="submit" value="Github" /> 91 </form> 92 </div> 93 <div className="App"> 94 <div className="editor"> 95 <AceEditor 96 mode="latex" 97 theme="monokai" 98 width="85%" 99 height="58vh" 100 onChange={v => this.setState({ tex: v })} 101 value={this.state.tex} 102 name="tex" 103 /> 104 <AceEditor 105 mode="json" 106 theme="monokai" 107 width="85%" 108 height="26vh" 109 onChange={v => this.setState({ json: v })} 110 value={this.state.json} 111 name="json" 112 /> 113 </div> 114 <div className="PDFHolder"> 115 <Document 116 className="PDFViewer" 117 file={this.state.pdf} 118 > 119 <Page size="A4" pageNumber={1}/> 120 </Document> 121 </div> 122 </div> 123 </div> 124 ); 125 } 126 }