finally able to support html output, fixes the random chaos chinese characters on the output of word-to-html

This commit is contained in:
Sahatsawat Kanpai 2025-01-19 21:13:39 +07:00
parent 907bee0527
commit b6c510de54

View File

@ -27,84 +27,52 @@ export default class Import extends Plugin {
tooltip: true tooltip: true
} ); } );
// // Insert a text into the editor after clicking the button. // POST fetch html from docx
// this.listenTo( view, 'execute', () => {
// model.change( writer => {
// const textNode = writer.createText( 'Hello CKEditor 5!' );
// model.insertContent( textNode );
// } );
// editor.editing.view.focus();
// } );
// Insert a text into the editor after clicking the button.
// this.listenTo( view, 'execute', async () => {
// asBlob(this.editor.getData()).then((data: any) => {
// saveAs(data, 'file.docx') // save as docx file
// }) // asBlob() return Promise<Blob|Buffer>
// editor.editing.view.focus();
// } );
// POST fetch docx and download
this.listenTo( view, 'execute', async () => { this.listenTo( view, 'execute', async () => {
const parser = new DOMParser(); // curl -X 'POST' \
const doc = parser.parseFromString(this.editor.getData(), "text/html"); // 'http://localhost:8080/aspose/word-to-html' \
// -H 'accept: text/plain' \
// -H 'Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document' \
// --data-binary '@stou(207).docx'
// pre-process mathlive script to html MathML tag with the help of temml // file accpt docx
for (const mathliveElement of doc.querySelectorAll('script[type="math/tex"]')) { const input = document.createElement('input');
const temmlWrapperElement = document.createElement('span'); input.type = 'file';
const temmlMathMLString = Temml.render(mathliveElement.textContent ?? "", temmlWrapperElement); input.accept = '.docx';
mathliveElement.replaceWith(temmlWrapperElement); input.click();
}
// pre-process for table input.addEventListener('change', async () => {
for (const table of doc.querySelectorAll('table')) { const file = input.files![0];
// pre-process table for border and centering and inner border const formData = new FormData();
table.style.borderCollapse = 'collapse'; formData.append("file", file);
table.style.borderSpacing = '0';
table.style.border = '1px solid #b3b3b3';
table.style.margin = 'auto';
// pre-process table for padding
for (const td of table.querySelectorAll('td')) {
td.style.padding = '8px';
}
// pre-process table for inner border
for (const tr of table.querySelectorAll('tr')) {
for (const td of tr.querySelectorAll('td')) {
td.style.border = '1px solid #b3b3b3';
}
}
// pre-process table for header background color rgba(0,0,0,.05) to hex #f2f2f2
// and it's own border
// and padding
const ths = table.querySelectorAll('th');
for (const th of ths) {
th.style.backgroundColor = '#f2f2f2';
th.style.border = '1px solid #b3b3b3';
th.style.padding = '8px';
}
}
console.log(doc.body.innerHTML); try {
const response = await fetch("http://localhost:8080/aspose/word-to-html", {
fetch('http://localhost:8080/aspose/html-to-word', { method: "POST",
method: 'POST',
body: doc.body.innerHTML,
headers: { headers: {
'Content-Type': 'text/plain' // Do not set `Content-Type` manually when using FormData;
} // the browser will automatically set the correct boundary for multipart/form-data.
}) },
.then(response => response.blob()) body: file, // directly sending file bytes as the backend expects byte[]
.then(blob => {
saveAs(blob, 'stou.docx') // save as docx file
})
.catch(error => {
console.error('Error:', error);
}); });
if (!response.ok) {
throw new Error(`Failed to upload file: ${response.statusText}`);
}
const htmlText = await response.text();
// console.log("Converted HTML:", htmlText);
const htmlDoc = new DOMParser().parseFromString(htmlText, "text/html");
const body = htmlDoc.body;
this.editor.setData(body.innerHTML);
} catch (error) {
console.error("Error uploading file:", error);
throw error;
}
editor.editing.view.focus(); editor.editing.view.focus();
}); });
} );
return view; return view;
} ); } );