Are there any alternatives to using contenteditable or a textarea? - wysiwyg

I am working on an online rich-text editor, similar to the WordPress page creator, or the Stack Overflow post creator. It has been pointed out that there are two distinct types of online rich-text editors:
WYSIWYG editors, and
HTML editors
I'm building the second type. Unfortunately, neither <textarea> nor using contenteditable are very convenient for HTML rich-text editing.
The problem with <textarea> (as used here in Stack Overflow) is that you can't show text-level semantics in the edit field. You can't just highlight a word and make it bold, you have to insert some kind of markup (e.g. *****bold*****). Not very user friendly and not really "rich" text either.
On the other hand, using contenteditable solves those problems but introduces a problem of control. Browsers will insert all sorts of HTML and CSS to make the edit field look good. If you hit Enter, the browser will insert <p>, or <div>, or <br>, or <div><br></div>... depending on the browser. If you paste in a few paragraphs copied from HTML, you get tons of excessive markup--beyond what was even in the source HTML. For example, the following source code:
<p>This is one paragraph!</p>
<p>This is another.</p>
Shows up on a website as:
This is one paragraph!
This is another.
...which, if you copy and paste into a contenteditable form, can give you something like this:
<p style="line-height: 1em; color: rgb(34, 34, 34); font-size: 12px;
white-space: normal;">This is one paragraph!</p>
<p style="line-height: 1em; color: rgb(34, 34, 34); font-size: 12px;
white-space: normal;">This is another.</p>
...bringing inline styles with it, and in some cases additional HTML.
I've been trying to figure out how to limit and control the amount of HTML that the browser inserts in a contenteditable element, but I am beginning to think that contenteditable is only suited for WYSIWYG type editing, and will never work for HTML style rich-text editing.
Is there a third alternative, or has anyone built some sort of Javascript editor that meets my needs?

The alternative is to code it all by yourself using click events on normal divs, and so on. There are a lot of things that would need to be done using this approach and there are a lot of questions on stack overflowthat would help, including setting up all event handlers & handling adding a caret when they click on a location in text [1], keypress events for everything including Enter, Backspace, Delete, all alphanumeric keys, and the rest. You would need to create the caret as a visual element when they clicked something, enter text when they typed, delete text when they hit backspace, enter newlines when Enter is pressed, and so on. Google Docs and Online Word probably use this approach, but it is a massive amount of work and I don't know of any open source libraries that implement it, but it would give you full control of everything, including formatting of everything (since you would be entering it all).
[1] Detect which word has been clicked on within a text

Related

How to write text on right of an image using GitHub markdown

I want to write text on the right side of an image in GitHub .md file. I mean image should be displayed on the left side of the text (text including heading, list, para and links). I know I can do it using HTML or CSS using flex or so but is it possible to do it using GitHub markdown? And is it also possible for the text to be vertically aligned with respect to image size?
Markdown doesn't provide a set of rules or syntax for how the text is to be displayed, other than allowing HTML elements with CSS. That's because Markdown, like HTML, is a markup language, and presentation is supposed to be done outside of it at a separate level (usually CSS).
However, for a variety of reasons, including security, aesthetics, and accessibility, GitHub strips out all CSS when it's rendering HTML from a README or other text document, so there isn't a way to do what you want.
Unfortunately the accepted answer is wrong. It is possible.
Github-Markdown:
<img align="left" width="200" src="https://www.rd.com/wp-content/uploads/2018/02/25_Hilarious-Photos-that-Will-Get-You-Through-the-Week_280228817_Doty911.jpg" />
# Headline
Some text
Rendered result on Github:
For the sake of completeness:
See also this answer: GitHub Pages: How can I wrap text around an image?

Tiny MCE - Stopping wrapping of images

I am using the TinyMCE Editor, after some Googling it is apparent that the editor needs a root element, which by default is a paragraph.
So everything wrapped in a p tag <p></p>
However, I don't want my images to be wrapped in a paragraph as I want them to be a standalone item.
I know you can strip all p tags but can you be selective with the imagery?
There's currently no way to do this within TinyMCE. You could always strip the root <p> tag on content export.

Non Editable element in TinyMCE (related to Mathjax)

I'm building an extended editor around TinyMCE and I have to implement a Math formula module. I've choosen Mathjax for formula rendering, using plain html/css. So far, I've managed to create a plugin that popup a panel with a textarea, you can enter your latex in, there's a preview in the panel.
Once you validate the formula, it's injected into tinymce content. This new content is A LOT of spans with inlined styles, and OF COURSE, I don't want tinymce to be able to edit that directly. (sidenote: we only store latex, not the rendered html output from mathjax)
Basically, I want that a piece of html inside Tinymce to be ignored totally, but displayed in place. I want my carret to be able to move before that piece of html, and after, but not inside.
The "non editable content" plugin seems to be close of what I'm looking for, but it has some limitation (you can't ADD no editable content on the fly, having nested html content inside an element flagged as "non editable" broke some things, etc.)
Is someone could help with that ? I started to tweak the non editable plugin, but it's really really hard to understand existing code. Has someone already did something like that, or is there another third party plugin ?
thanks

tinyMCE - point between block elements

I am using tinyMCE in show block elements mode.
I have written custom plugin that inserts prepared html blocks (layout partials) in actual cursor position.
It's problematic to point a space between two divs.
If I have markup like that:
<div id="first"></div><div id="second"></div>
When I click beteween those divs I would land in first or second div, never between.
So I try to edit html source and result in markup whit br's:
<div id="first"></div><br/><div id="second"></div>
Now I can point between those divs, but it does not work with elements that I add dynamically via tinyMCE. If I add partial eg.
<div></div><br/>
clicking after that div is not posibble. But it's posibble when I edit source manually. Weird. Do you have any solution at this subject?
Tinymce takes care that the user is not able to click between div or p tags.
The reason for this is easy: If a user could click inbetween and he would type in a letter - then there would be another div or p created containing that letter. This is not the way rtes are designed to work. If you want to insert somthing between two divs you will need to use a special button or own plugin to take care of this, but placing the cursor where you won't it to be by hand won't work.

Editable text widget for GWT?

I have an app in GWT that provides lots of little paragraphs and let's the user edit them. Right now I just have a view mode, just an HTML paragraph, and an edit mode, for which I use a standard HTML TextArea.
Is there a fluid GWT paragraph editing widget that does not require using a TextArea or switching between modes? Note that I do not need a rich text widget as I don't want bold, italic, bullet points, etc.
I would be willing to use a jQuery or other JavaScript library solution as long as it was really robust and cleanly separated from everything else I'm doing. I don't want to babysit/hack on JavaScript.
Somehow I think this problem is hard, as Google Docs doesn't do it well and even stackoverflow has me using a very TextArea-like box to edit text that is later displayed as an HTML paragraph.
I would use HTML ContentEditable. http://html5demos.com/contenteditable/
<div contenteditable="true">
</div>
You can use a JS framework to bind to changes. I have noticed issues with the JS 'change' event, but the 'blur' event works well.