Spaces:
Running
Running
Make a Microsoft Word like web app with the possibility to write the document, customise it with colors, font, size and simple format, and save documents to computer and export to pdf
6079a1c verified | <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>WordWizard Editor</title> | |
| <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"> | |
| <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet"> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"></script> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js"></script> | |
| <style> | |
| .toolbar { | |
| background-color: #f8f9fa; | |
| padding: 10px; | |
| border-bottom: 1px solid #ddd; | |
| } | |
| .editor-container { | |
| height: calc(100vh - 150px); | |
| border: 1px solid #ddd; | |
| padding: 20px; | |
| overflow-y: auto; | |
| margin-top: 10px; | |
| } | |
| #content { | |
| min-height: 100%; | |
| outline: none; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <div class="container-fluid"> | |
| <div class="toolbar mb-3"> | |
| <div class="btn-group me-2"> | |
| <select id="font-family" class="form-select form-select-sm"> | |
| <option value="Arial">Arial</option> | |
| <option value="Times New Roman">Times New Roman</option> | |
| <option value="Courier New">Courier New</option> | |
| <option value="Georgia">Georgia</option> | |
| </select> | |
| </div> | |
| <div class="btn-group me-2"> | |
| <select id="font-size" class="form-select form-select-sm"> | |
| <option value="1">8pt</option> | |
| <option value="2">10pt</option> | |
| <option value="3">12pt</option> | |
| <option value="4">14pt</option> | |
| <option value="5">18pt</option> | |
| <option value="6">24pt</option> | |
| <option value="7">36pt</option> | |
| </select> | |
| </div> | |
| <div class="btn-group me-2"> | |
| <button class="btn btn-sm btn-outline-secondary" onclick="formatText('bold')"><i class="fas fa-bold"></i></button> | |
| <button class="btn btn-sm btn-outline-secondary" onclick="formatText('italic')"><i class="fas fa-italic"></i></button> | |
| <button class="btn btn-sm btn-outline-secondary" onclick="formatText('underline')"><i class="fas fa-underline"></i></button> | |
| </div> | |
| <div class="btn-group me-2"> | |
| <input type="color" id="text-color" class="form-control form-control-color" value="#000000" title="Text Color"> | |
| <input type="color" id="bg-color" class="form-control form-control-color" value="#ffffff" title="Background Color"> | |
| </div> | |
| <div class="btn-group me-2"> | |
| <button class="btn btn-sm btn-outline-primary" onclick="alignText('left')"><i class="fas fa-align-left"></i></button> | |
| <button class="btn btn-sm btn-outline-primary" onclick="alignText('center')"><i class="fas fa-align-center"></i></button> | |
| <button class="btn btn-sm btn-outline-primary" onclick="alignText('right')"><i class="fas fa-align-right"></i></button> | |
| <button class="btn btn-sm btn-outline-primary" onclick="alignText('justify')"><i class="fas fa-align-justify"></i></button> | |
| </div> | |
| <div class="btn-group float-end"> | |
| <button class="btn btn-sm btn-success" onclick="saveAsPDF()"><i class="fas fa-file-pdf"></i> PDF</button> | |
| <button class="btn btn-sm btn-primary" onclick="saveAsDoc()"><i class="fas fa-save"></i> Save</button> | |
| </div> | |
| </div> | |
| <div class="editor-container"> | |
| <div id="content" contenteditable="true"> | |
| <h1>Start typing here...</h1> | |
| <p>Welcome to WordWizard! Format your text using the toolbar above.</p> | |
| </div> | |
| </div> | |
| </div> | |
| <script> | |
| function formatText(command) { | |
| document.execCommand(command, false, null); | |
| } | |
| function alignText(align) { | |
| document.execCommand('justify' + align, false, null); | |
| } | |
| document.getElementById('font-family').addEventListener('change', function() { | |
| document.execCommand('fontName', false, this.value); | |
| }); | |
| document.getElementById('font-size').addEventListener('change', function() { | |
| document.execCommand('fontSize', false, this.value); | |
| }); | |
| document.getElementById('text-color').addEventListener('change', function() { | |
| document.execCommand('foreColor', false, this.value); | |
| }); | |
| document.getElementById('bg-color').addEventListener('change', function() { | |
| document.execCommand('hiliteColor', false, this.value); | |
| }); | |
| function saveAsPDF() { | |
| const { jsPDF } = window.jspdf; | |
| const element = document.getElementById('content'); | |
| html2canvas(element).then(canvas => { | |
| const imgData = canvas.toDataURL('image/png'); | |
| const pdf = new jsPDF(); | |
| const imgProps = pdf.getImageProperties(imgData); | |
| const pdfWidth = pdf.internal.pageSize.getWidth(); | |
| const pdfHeight = (imgProps.height * pdfWidth) / imgProps.width; | |
| pdf.addImage(imgData, 'PNG', 0, 0, pdfWidth, pdfHeight); | |
| pdf.save('document.pdf'); | |
| }); | |
| } | |
| function saveAsDoc() { | |
| const content = document.getElementById('content').innerHTML; | |
| const blob = new Blob([content], { type: 'text/html' }); | |
| const url = URL.createObjectURL(blob); | |
| const a = document.createElement('a'); | |
| a.href = url; | |
| a.download = 'document.html'; | |
| document.body.appendChild(a); | |
| a.click(); | |
| document.body.removeChild(a); | |
| URL.revokeObjectURL(url); | |
| } | |
| </script> | |
| </body> | |
| </html> |