DEV Community

Cover image for Build your own WYSIWYG markdown editor for Vue 📝👀

Build your own WYSIWYG markdown editor for Vue 📝👀

Pascal Thormeier on January 28, 2021

HTML5 and modern JavaScript make a lot of things a lot easier than they used to be back in the days. Complex things don't require a lot of hacks an...
Collapse
 
bogdaniel profile image
Bogdan Olteanu

What would be the alternative to document.execCommand since it's deprecated ? :D I've been looking for ideas for quite some time.

Collapse
 
thormeier profile image
Pascal Thormeier

A very good question. There's a few options, actually, but none are quite as elegant as this. According to caniuse, all major browsers still offer support for execCommand.

Turning on/off different modes, like bold or italic text can be achieved by creating a tree-like structure where you edit nodes in the back. The experimental selection API could help with features that work on selected text, as in "select text and mark as bold" for example. I would probably still use contenteditable, but try to emulate the features that execCommand uses. There's also the possibility of using a polyfill at some point in the future, but none is available right now from what I can tell.

What have you stumbled upon during your research?

Collapse
 
bogdaniel profile image
Bogdan Olteanu

Selection API is something really nice but as it states is experimental, second option would be range combined with a tree-like structure as you said and contenteditable since it's closest to what we want. prob's textarea would work fine too. The Range API I don't really like it for the moment .. :-) specially if you would want to combine it with a tree structure or maybe I'm missing a few things here and there. :D

Thread Thread
 
thormeier profile image
Pascal Thormeier

To be honest, I would have to read a lot deeper into the range API to really understand it, but it does look promising! I hope that the Selection API will get some more attention and leave the "experimental" state soon, since it has a lot of potential. What's the reason for you not liking the Range API at the moment?

Thread Thread
 
hasnaindev profile image
Muhammad Hasnain

Despite being experimental, the selection API has the same support as the range API. Perhaps an abstraction layer would do the trick but will take a lot of time and careful engineering.

About a year ago I really wanted to create my own WYSIWYG editor. Things I read about document exec were not good. For instance, what happens when someone tries to paste text from outside, how are we going to handle the tags in the pasted text.

Moreover, it is deprecated which is literally a mood killer. Someday I'll create a really simple WYSIWYG editor using range and or selection API. I have general knowledge of trees but working with nodes, correctly inserting and removing nodes is NOT an easy thing.

Thread Thread
 
bogdaniel profile image
Bogdan Olteanu • Edited

Yeap, same here would really love to build my own WYSIWYG even for testing / learning purposes I know a great range of editors but still want to dive in this part and build one from scratch..
document exec is great and easy to use most of the editors these days are based on them except the very new ones. but as you said it's deprecated and you can't count on it + building with exec it's a 20 - 30 minutes jobs to do simple things. Diving into selection and range that's the real stuff to play with.

Thread Thread
 
hasnaindev profile image
Muhammad Hasnain

Indeed. When I was looking into this initially an agency shared a kind of a case study saying that making a WYSIWYG was one of the most difficult things that they had ever done. They even went on to say that it is one of the most complicated thing on the front-end.

Thread Thread
 
thormeier profile image
Pascal Thormeier

Oh yes, it absolutely is. I find it an absolute pity that execCommand got deprecated. This thing shows how simple it could be, but ultimately isn't when you want to have support on all browsers and not rewrite that thing in a year or two.

Collapse
 
ewencodes profile image
ewencodes

So helpful! Love it!

Collapse
 
thormeier profile image
Pascal Thormeier

Glad you liked it!

Collapse
 
jonnykio profile image
Jonathan Kumar • Edited

Not sure if this is due to Vue 3 or not. But, the emit 'input' doesn't work directly on Vue.js for custom components. You have to use update:modelValue to trigger the change and read the prop from modelValue. You can find out more from here vuejs.org/guide/components/events....

Collapse
 
thormeier profile image
Pascal Thormeier

You're absolutely right! I've written this post begining of 2021, and Vue3 wasn't the default until January 2022. Since people still find this useful, I'll update it in the coming days.

Collapse
 
simonholdorf profile image
Simon Holdorf

Awesome, thanks!

Collapse
 
thormeier profile image
Pascal Thormeier

You're very welcome! I had so much fun writing and building this!

Collapse
 
samcodestack profile image
Godwin Samson

That's nice

Collapse
 
thormeier profile image
Pascal Thormeier

Thank you, so glad you liked it! :)

Collapse
 
atellmer profile image
AlexPlex

Great article

Collapse
 
thormeier profile image
Pascal Thormeier

Thank you!

Collapse
 
gok9ok profile image
Gokce Tosun

In case you'd like to take on a similar challenge, this article may be helpful ckeditor.com/blog/ContentEditable-...

Collapse
 
thormeier profile image
Pascal Thormeier

Thank you for this article! Very well stated why this shouldn't go beyond hobby projects and experimenting. Off-the-shelf WYSIWYG editors are amazing, and I most probably wouldn't use a self-made one in a larger client project, too, but trying to build one made me understand the challenges editors like Quill and CKEditor are facing, so this was an amazing learning opportunity!

Collapse
 
karthikeyan676 profile image
Karthikeyan

document.execCommand - it's deprecated

Collapse
 
thormeier profile image
Pascal Thormeier

Yup, that's what I wrote in the article as well.🙂 I still think for such an experiment it's sufficient, as most browsers still support a basic version of the commands.

Collapse
 
juandjara profile image
Juan D.

Woah, this is so cool and simple. Does it work ok in mobile devices?

Collapse
 
thormeier profile image
Pascal Thormeier

Good question! As long as the browser supports document.execCommand, then, in theory, yes, but a lot of it is deprecated now. This was mostly an experiment and is not really meant for large production use cases, but rather for hobby projects and as a learning opportunity.

Collapse
 
aneesh777 profile image
aneesh777

Great

Collapse
 
thormeier profile image
Pascal Thormeier

Thank you!

Collapse
 
vkbishu profile image
Venkatesh Bishu

Nice article.