You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A easy function to print code from editor / export to html.
Use Case
This is a useful function for the editor. And has been requested / asked how to achieve before via Google and issues...
Proposed Solution
<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport" content="width=device-width, initial-scale=1.0"><title>Ace Editor Print Button</title><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.12/ace.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.12/ext-static_highlight.js"></script><style>
#editor {
height: 300px;
}
</style></head><body><divid="editor">function printCode() {
console.log("Hello, world!");
}</div><buttonid="printButton">Print Code</button><script>functionPrint(code,css,removeGutter=false){try{// Create an iframevarprintIframe=document.createElement('iframe');printIframe.style.width='800px';printIframe.style.height='400px';printIframe.style.display='none';// Hide the iframedocument.body.appendChild(printIframe);// Get the document of the iframevarprintDocument=printIframe.contentDocument||printIframe.contentWindow.document;printDocument.write(`<html><head><title>${document.title}</title></head><body>`);printDocument.write(`<style> ${css}</style>`)if(removeGutter){code=code.replaceAll(`<span class="ace_gutter ace_gutter-cell"></span>`,"").replaceAll("ace_show_gutter","")}printDocument.write(code);printDocument.write("</body></html>");printDocument.close();printIframe.contentWindow.print();document.body.removeChild(printIframe);// You may adjust the delay based on your needs}catch(error){console.error("Error printing:",error);}}// Initialize Ace editorvareditor=ace.edit("editor");editor.setTheme("ace/theme/monokai");editor.getSession().setMode("ace/mode/javascript");// Function to print code from Ace editor using ext/static_highlightfunctionprintCode(){require("ace/config").loadModule("ace/ext/static_highlight",function(m){varresult=m.renderSync(editor.getValue(),editor.session.getMode(),editor.renderer.theme);vard=document.createElement("div");d.innerHTML=result.html;Print(d.innerHTML,result.css)});}// Attach the printCode function to the button click eventdocument.getElementById("printButton").addEventListener("click",printCode);</script></body></html>
Other Information
Looking for some feedback from the Ace Editor team. (It would be cool if I could contribute it), just looking for some feedback on this solution.
This could also be used as export to HTML function. To properly structure the syntax highlighter as a HTML document.
Notes: print margin does NOT match... And need a better solution for removing gutters preferably via API.
Acknowledgements
I may be able to implement this feature request
This feature might incur a breaking change
ACE version used
1.4.12
The text was updated successfully, but these errors were encountered:
@nightwing - I seen your solution from 2014, but this just seemed like a better updated solution (that appears a few have needed via Google Searches). Tho don't ask me why I had the half baked idea to grab document element lol. We could just write it out as string the have it exportAsHTML.string() & exportAsHTML.html()````and then a print using the exportAsHTML.html().
Being 2 new API methods. .print() & .exportAsHTML(). I think it would be a nice add to main core / editor.
ps; quick question, wanna give me an example how to get the Ace Editor current mode including caption / extensions etc?
Describe the feature
A easy function to print code from editor / export to html.
Use Case
This is a useful function for the editor. And has been requested / asked how to achieve before via Google and issues...
Proposed Solution
Other Information
Looking for some feedback from the Ace Editor team. (It would be cool if I could contribute it), just looking for some feedback on this solution.
This could also be used as export to HTML function. To properly structure the syntax highlighter as a HTML document.
Notes: print margin does NOT match... And need a better solution for removing gutters preferably via API.
Acknowledgements
ACE version used
1.4.12
The text was updated successfully, but these errors were encountered: