For fun, I thought I’d look into making my own online editor for HTML
, JS
and SCSS
. I ended up with a page I’m relatively happy with and I learnt a bit along the way. The page features SCSS compilation as well as saving to the URL.
You can check the page out here
By default – Javascript is disabled and not evaluated until you enable it. This can be seen in this demo which will only set the background to pink once you enable JS and interact with the editor. Be warned though, once Javascript has been enabled – if a script loops endlessly you may lock up your browser. Don’t run anything here you wouldn’t run yourself in the console!
Saving to URL was an unexpected issue for this project, Most URLs can only be about 2000 characters long1! This was overcome by using a GZIP implementation to first compress the data before inserting it into the URL parameter. This obviously doesn’t solve every use case, however the editor will now warn you when trying to load a code snippet that is too long from the URL.
I used the following packages to help with development:
Some of the main hurdles I encountered while working on this project were:
- There are no children inside a
<textarea />
element, to modify styles of written content you must first mirror the content to another element. Syncing the display & scroll state of these two elements to create a seamless editing experience was a bit of a challenge. However once they were synced up, prism did a majority of the heavy lifting. - Compressing the code into a URL safe string was quite difficult. Initially I implemented an LZ77 compressor which worked quite well with plain strings however it really only lowered string size by 5-10%. I was told to look into GZip, this implementation seemed much too complex to do myself, so I opted into using an existing solution. The most popular library I could find called pako only worked with
UInt8Buffer
’s which I had never worked with before. It was interesting to work with thestring
->
UInt8Buffer
and back conversion process.
An unexpect side effect from this project is I now have inline code blocks & previews using plain iFrames! I don’t know if this is a good or bad thing yet haha.