Powerful, API-driven rich text editor
Recommended and mentioned products
CKEditor 4 is a rich text editor that enables you to write content inside of web pages or online applications.
You may want to look at not writing your own, but using a library to help. There are a ton of libraries out there. Quill was mentioned already, but there are a bunch. https://editorjs.io/, https://ckeditor.com/ ,etc.