I'm trying to implement mentions within contenteditable div. I'm using external library called TributeJS (https://github.com/zurb/tribute). Mostly it is working fine, but there is an edge case if I write mentions together without space and in multiple lines and then try to delete them with backspace key, then at some point I cannot delete mentions anymore.
Mentions inside input use span with contenteditable="false" like this:
<span contenteditable="false" class="mention" data-id="user1">
#<label class="mention-label">User 1</label>
</span>
I've recorded a video, so I can explain the problem easier:
https://files.fm/f/ga5dak22b
And I've also created a Codepen to reproduce this issue:
https://codepen.io/tilenhosnar/pen/powZqBw
This problem occurs for me in Chrome browser. I'm wondering if there is any way to "force" delete child HTML nodes in contenteditable div on backspace key? I've pretty much tried every solution I could find in the last few days but it is still happening.
Maybe try to use ANSI escape code to go up a line instead of backspace, This question might help you.
Related
Because of Windows not supporting flag emoji I need to implement an editable div which behave like a textarea, so it will be possible to display tag images instead of emoji characters. Does anyone know how to do it and fix strage behaviours like copy/paste to allow only text?
Edit
I already know about the attribuite contenteditable, but the problem is that it does not behave like a textarea, specially when copy/paste content to/from it. How to fix it?
maybe contenteditable attribute will wok for you check out this guide
Yes,It's possible! If you want to include more future on the editable div then you can use document.execCommand(). For more information about execCommand just visit to:https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Editable_content
Example here:
<div contenteditable="true">
This text can be edited by the user.
</div>
I have a WYSIWYG Editor using contenteditable that allows users to insert "code snippets" using a <code> element. For instance:
<div contenteditable="true">
<p>
This is a paragraph with an <code>inline snippet</code>!
</p>
</div>
https://jsfiddle.net/wyeack/pyta77zd/2/
In firefox, if you place the caret directly before the first character of <code>, it will prepend the element:
However, if you try to do the same thing in Chrome, it appends it to the previous element:
This means that if you use chrome, there is no way of adding content to the beginning of this element.
What's going on here? Is there a way for me to make the behavior more consistent?
Firefox has a superior selection implementation in this case. You can place it inside an inline element like <code> if you move it from right to left using arrow keys. If you move it from left to right, it will stick to the left. This is, so called, gravity.
What's going on here? Is there a way for me to make the behavior more consistent?
First of all – don't use bare contentEditable. Use a good RTE.
Second (and last :P) of all – I don't know about any RTE which normalises this specific behaviour. It's an expensive thing to do and few users would notice it. It is possible, though, but you would need to use an RTE with a proper data model (where the selection is fully abstracted and all input intercepted) and based on that handle the input accordingly.
I could give you more details on how to do it with CKEditor 5, but it's not production ready yet. I've got no idea how to do that with other RTEs, but I know one thing for sure – I'd never ever attempted to fix this on a native contentEditable.
I'm talking about content from inside a contenteditable div, and the target is the same contenteditable div. So no external programs involved.
The structure of the HTML in this div is that each individual word is inside a span with some data we need to track. Then the whitespace is left as text nodes between the spans. This works fine for the most part (screw you newlines) but I've encountered a strange problem when copy and pasting.
Chrome turns this
<span attrs="stuff">word</span> <span attrs="stuff">another</span>
into this:
<span attrs="stuff">word </span><span attrs="stuff">another</span>
or this:
<span attrs="stuff">word</span><span style="line-height: 16.79999"> </span><span attrs="stuff">another</span>
This obviously means that if the user copy and pastes over more than one line, then the formatting is completely screwed up, and the content of the span has changed which invalidates our data that we need to track.
The core problem is that other stuff in the div may contain non-breaking spaces for real reasons, so if I globally start swapping them out, then I might break that.
For my spans with my attrs, then I know what should be in them so it's easy to strip out the non-breaking spaces and restore it to how it should be. But for these strange spans with the odd line height, I've no idea how to clean them out without nuking everything.
Right now, I've stripped all the inserted spans that contain just a non-breaking space. But what I'd really like is to either stop Chrome from doing this in the first place, or an unambiguous means to identify the problematic extra spans so that I can clean them up in safety without breaking any similar spans that exist for real reasons. I could use this strange line-height I guess but that's pretty brittle and unsafe it feels.
How can I prevent the spans from appearing or identify them unambiguously?
The problem is not a Chrome problem only. All the time you copy HTML Code somewhere something like this can happen.
This is why you can use editors like CKEditor. They have advanced filter techniques to remove such bad HTML code.
I recommend to use a clipboard program to see how the HTML code is when you copy from different places: https://softwarerecs.stackexchange.com/questions/17710/see-clipboard-contents-hex-text
But implementing this on your own would be a waste of time in my opinion.
CKEditor can be configured very well to prevent the bad HTML code.
Recent versions of CKEditor have a very sophisticated content filtering approach. It is called "Advanced Content Filter".
Basically "Advanced Content Filter" means: The whole HTML code gets parsed or checked. In the case that there is no rule which matches to the given HTML code, it gets filtered out.
Adding HTML/any tags to either side of selection - Javascript
The problem:
After creating a textarea box in my PHP/html file I wished to add a little more functionality and decided to make an textarea that can use formatting, for example
<textarea>
This is text that was inserted. <b>this text was selected and applied a style
via a button<b>
</textarea>
It doesn't matter what the tags are, (could be bubbles for all that I care due to the fact the PHP script, on receiving the $_POST data will automatically apply the correct tags with the tag as the style ID. Not relevant)
The Question/s
How can I create this feature using javascript?
Are there any links that may help?
And can, if there is information, can you explain it?
EDIT: Other close example but not quite is stackoverflow's editor and note that I do not wish to use 3rd party scripts, this is a learning process for me.
The tags that are inserted in the text are saved to a database and then when the page is requested the PHP replaces the tags with the style ID. If there is a work around not involving 3rd party scripts please suggest
And for the anti-research skeptics on a google search, little was found that made sense and there was Previous Research on SOF:
- https://stackoverflow.com/questions/8752123/how-to-make-an-online-html-editor
- Adding tags to selection
Thanks in Advance
<textarea> elements cannot contain special markup, only values. You can't apply any styling in a textarea.
What you'll need to do is fake everything that a text box would normally do, including drawing a cursor. This is a lot of work, as hackattack said.
You can do a lot if you grab jQuery and start poking around. Toss a <div> tag out there with an ID for ease and start hacking away.
I've never made one personally, but there is a lot to it. HTML5's contentEditable can maybe get you a good chunk of the way there: http://html5demos.com/contenteditable/
If you want to pass this data back to the server, you'll need to grab the innerHTML of the container and slap that into a hidden input upon submission of your form.
Here's other some things you can check out if you're just messing around:
tabindex HTML attribute, to get focus in your box from tabbing
jQuery.focus() http://api.jquery.com/focus/, to determine when someone clicks in your box
cursor: text in CSS for looks http://wap.w3schools.com/cssref/pr_class_cursor.asp
jQuery.keypress() http://api.jquery.com/keypress/, or similar for grabbing keystrokes
Edit: I think I completely misunderstood
If you're not looking for a rich text editor, and just want some helper buttons for code, maybe selectionStart and selectionEnd is what you're after. I don't know what the browser support is, but it's working in Chrome:
http://jsfiddle.net/5yXsd/
you can not do anything beside basic formatting inside a texarea. If you want complex formatting, look into setting a div's contentEditable attribute to true. Or you can make a wysisyg editor, but that is a big project. I strongly suggest using 3rd party code on this one.
I suggest you using the iframe to implement the WYSIWYG effect.
There is a property in iframe called designMode
See here for more
https://developer.mozilla.org/en/Rich-Text_Editing_in_Mozilla
Also there is a lightweight example maybe you would like to take a look:
http://code.google.com/p/rte-light/source/browse/trunk/jquery.rte.js
I am trying to build a specialized WYSIWYG text editor in the browser, and have a very limited set of functionality, but the biggest part of that is wrapping certain text in span tags.
I can find many resources explaining standard stuff (execCommand and whatnot), but have looked and looked and can't find anything to do what I need.
Basically, it's as simple as it sounds: user selects some text, clicks a button or whatever, and the text gets replaced with some other text (the initial case is that same text wrapped in some HTML tags).
I can find ways to do this in a textarea, but I'm just in regular HTML land, with the content in question inside a div with contentEditable marked as true.
I have also found ways to replace all occurences of text, or the first occurence, but not a specific one. Most solutions I find fail when trying to replace anything but the first occurence.
I'm hoping jQuery can do this in some way.
Have you tried the jQuery wrapSelection plugin?
This is pretty similar to this question. It might help.