{"id":179,"date":"2023-12-23T11:10:49","date_gmt":"2023-12-23T05:40:49","guid":{"rendered":"http:\/\/codetheory.in\/?p=179---eefd713e-e30b-4352-bea3-6db8d0b22442"},"modified":"2023-12-23T11:10:49","modified_gmt":"2023-12-23T05:40:49","slug":"integrating-an-in-browser-code-editor-to-your-playground","status":"publish","type":"post","link":"https:\/\/codetheory.in\/integrating-an-in-browser-code-editor-to-your-playground\/","title":{"rendered":"Integrating an In-browser Code Editor to Your Playground"},"content":{"rendered":"
<\/p>\n There are 2 popular and awesome in-browser code editing tools available:<\/p>\n CSSDeck<\/a> uses CodeMirror. No special reason for that, I just ended up using it.<\/p>\n Integrating CodeMirror is super easy. All we need to do is replace our textarea with a CodeMirror Instance by a simple call to the CM API:<\/p>\n First, you’ll need to download CodeMirror<\/a> and load the stylesheets (in Phew! Lots of includes. We’ll need to replace some of our old code with new parts:<\/p>\n Quite a bit of code, but definitely worth it. All we do is, get the We can get rid of the Check out the Final Result<\/a>.<\/p>\n CodeMirror also supports a lot of themes<\/a>. All you need to do is include the CSS file and pass the name in theme option<\/a>.<\/p>\n
\nWe have already created our environment<\/a> to write and render html, css, js code and also made it secured<\/a>. Now we’ll add an in-browser code editing tool that supports syntax highlighting, indentation, themes, zen coding<\/a> and tons of more cool features to help us code even better.<\/p>\nIn-Browser Code Editors<\/h2>\n
\n
Integrating CodeMirror<\/h2>\n
\r\n\/\/ - you can get the textarea_node using jQuery or DOM querySelector()\r\n\/\/ - cm_options should be a Javascript Object\r\nvar editor = CodeMirror.fromTextArea(textarea_node, cm_options);\r\n<\/pre>\n
<head><\/code>) and scripts (at the end of
<body><\/code>):<\/p>\n
\n
\/lib\/codemirror.css<\/code><\/li>\n
\/lib\/codemirror.js<\/code> – Core JS<\/li>\n
\/mode\/xml\/xml.js<\/code> – Script for XML\/HTML Mode<\/li>\n
\/mode\/htmlmixed\/htmlmixed.js<\/code> – Script for XML\/HTML Mode<\/li>\n
\/mode\/css\/css.js<\/code> – Script for CSS Mode<\/li>\n
\/mode\/javascript\/javascript.js<\/code> – Script for JavaScript Mode<\/li>\n<\/ul>\n
\r\n\/\/ EDITORS\r\n\r\n\/\/ CM OPTIONS\r\nvar cm_opt = {\r\n\tmode: 'text\/html',\r\n\tgutter: true,\r\n\tlineNumbers: true,\r\n\r\n\tonChange: function (instance, changes) {\r\n\t\trender();\r\n\t}\r\n};\r\n\r\n\/\/ HTML EDITOR\r\nvar html_box = document.querySelector('#html textarea');\r\nvar html_editor = CodeMirror.fromTextArea(html_box, cm_opt);\r\n\r\n\/\/ CSS EDITOR\r\ncm_opt.mode = 'css';\r\nvar css_box = document.querySelector('#css textarea');\r\nvar css_editor = CodeMirror.fromTextArea(css_box, cm_opt);\r\n\r\n\/\/ JAVASCRIPT EDITOR\r\ncm_opt.mode = 'javascript';\r\nvar js_box = document.querySelector('#js textarea');\r\nvar js_editor = CodeMirror.fromTextArea(js_box, cm_opt);\r\n\r\n\/\/ SETTING CODE EDITORS INITIAL CONTENT\r\nhtml_editor.setValue('Hello World');\r\ncss_editor.setValue('body { color: red; }');\r\n<\/pre>\n
textarea<\/code> nodes using
querySelector()<\/code> and pass them along with a set of basic options to
fromTextArea()<\/code>. This converts all the
textarea<\/code> elements to super awesome CodeMirror Editors. For a full list of options, settings and features, check out the single page CM Manual<\/a>. It has got everything you’ll ever need.<\/p>\n
keyup<\/code> event from
textarea<\/code> now, because the
onChange<\/code> option of CodeMirror accepts a callback that’ll get called everytime there is a user input detected. Also, to fetch the content of the editors, we cannot use
textarea_node.value<\/code> anymore. Instead, we just use
getValue()<\/code> on the CM object.<\/p>\n
Themes<\/h2>\n
Zen Coding<\/h2>\n