JS. contentEditable track changes (correction) like Word style - javascript

I'm building project Word like on contentEditable.
I know this is not the best choice to make in case of rich text editors, but still I want to track changes in the document by user, with peer-review in the end by supervisor.
I have a state, isTrackingChanges, when true - it's overrides actions, as - on insert text, insert node with underline, on backspace - get a deleted character, insert node with strikethrough style, move caret by one character to the left. At this point I have a lot of inline nodes. But on peer review I would like to accept/decline whole part of characters. How can I group them programmatically?

Fixed the problem with another solution. On accept/decline change I'm checking for Node.nextSibling() and Node.previousSibling() to get whole group. Works fine for me

Related

Rangy for subdivided contenteditable div

So, I'm playing around with the (AWESOME) Rangy text selection package. I'm editing text inside of a span with contenteditable=true, with the goal of having a text input element which allows individual styling of each character. Let me stress that last point -- each character should be in its own span. This works fine if I don't need to preserve the styling from one keystroke to the next -- I just do something like
var newHTML = field.innerText.split('').map(function(c){
return ('<span class="letter">'+c+'</span>');
}).join('');
$(field).empty().append(newHTML);
and use Rangy's (1.3) saveCharacterRange() function to restore the selection, and it's all good.
The problem comes when I've already styled some of those spans, and I delete one, replacing it with new text. I want newly inserted text to be unstyled, but instead it gets inserted into the preceding (or following, if the selection is at the beginning) span. I've tried to work around this by explicitly collapsing the selection's region after (or before) the focusNode, but it seems to not allow the endContainer (or startContainer) to be anything other than a text node... I've even verified that I can create a text range which collapses where I want it, but the selection object's setSingleRange() seems to collapse back around the text node.
I tried inserting a new node and placing the selection wholly inside it, which does work when the new node has content in it before calling insertNode(), but I obviously can't be inserting extra content between each character...
Here's a fiddle demonstrating what I'm talking about. Thanks for any help!
I think you're falling foul of a common issue: browsers (particularly WebKit) have specific ideas of where in the document the selection and caret are allowed to be. Here's a recent answer of mine on this subject:
https://stackoverflow.com/a/21591165/96100
Maybe somebody out there can expand on this, but I think save/restoreCharacterRanges() is the key to fixing this problem. I've got it working (assuming your selection region is collapsed) in this fiddle, though not the way I'd like -- this approach (removing the just added content and moving into a new span) causes a flicker between keyup and keydown. I'd really like to be able to move the cursor (on keydown) into a fresh span, which would appear seamless to the user...
... and here's a fiddle that works with long selections, but not pasting...
Update
I've switched over to the 'insert a <span>὘</span> and move the selection region into it' approach, which seemed to be working great, until I started trying different browsers. The behavior is completely different in each browser! Chrome correctly moves the cursor into a 'blank' span when required, but then removes the accent; Firefox handles accented characters perfectly, but fails to move into the new span after deleting a character which follows a new span; Safari displays the accent mark then moves into the new span, leaving the accent mark orphaned... And all three will display the original bug when deleting an old letter which follows a new one! Also, it seems like the different browsers are reporting different keycodes when modifier keys are pressed. Is this a lost cause? Am I missing something? Here's a fiddle with the new code.

Problems with custom divs within WYSIWYG, cross-browser

I am working on a WYSIWYG editor (customising someone else's code) and have encountered a few problems that I just can't seem to overcome.
So far I have been able to get most custom divs working, but I am having some trouble with a few things:
Problem 1: If the cursor is before a div element, I am able to press delete and begin to remove the contents of the div without removing the actual div itself. This is how the element should look within the WYSIWYG for example:
But after pressing delete when the cursor is before the element, I get the following:
How can I check if the next element is this custom div and cancel a delete key press?
Problem 2: I am also able to press backspace after an element, which causes any text after the element to appear inside it, like so:
How can I check if the previous element is my custom div and cancel a backspace key press?
Problem 3: When inside a section (where the 'put content here' text is), I am using a div with the attribute contenteditable="true". Every time I press 'enter' within this div, a new <div> tag is created, rather than a line break tag (<br>). How can I force a line break tag to be created instead of a div element?
I have looked far and wide on stackoverflow and have yet to find a proper solution to the problem that is cross-browser.
Disclaimer: I am a CKEditor core developer.
If you want to customise this there are three ways:
You can spend few months (or more) on learning about contenteditable, ranges, selection and all that stuff and trying to implement your custom handlers. You could of course spend only one week or month on that, but the result won't be great, believe me.
You can choose good, existing WYSIWYG editor.
You can lower your expectations regarding the expected behaviour ;).
Now, if you would decide to use CKEditor there's one new feature called Widgets which was introduced in recently released CKEditor 4.3 beta (4.3 stable is going to be released in max. 2-3 weeks). As far as I can see it may be very helpful in your case. Check out the Introduction to Widgets guide. In very short - it is possible to configure how enter key behaves in so called "nested editables" as well as to secure integrity of your custom markup.

Editable, expanding input list control for Bootstrap

We're looking for a certain type of control, preferably with a Bootstrap implementation. We don't believe it's really uncommon, but we might be wrong.
It's essentially a listbox, but also:
It's like an editable combobox, but without a dropdown.
Like a tag input/pillbox, but vertical, with a more traditional style, and directly editable.
Like an expanding grid, but with a single column.
Like a series of vertically attached text input field boxes, on steroids.
Like a text area, but properly structured and expanding.
The ultimate goal is to be able to quickly and efficiently input a bunch of values (phone numbers, ...) without leaving the keyboard, while allowing the user to edit or remove existing values painlessly. (The form is relatively large and part of a LOB application.)
Does this control have a name?
Does anyone know of a Bootstrap plugin that provides such a control?
If not, then we're not exactly sure about how to proceed, would you have any pointers for us so that we can implement this in a straightforward way? (Is it a good idea to start with a series of text input fields for example?)
Details:
It's essentially a list,
It starts as something that looks very much like a simple text input field,
When the user presses ENTER, a new row is added below and the cursor is moved to it (the list expands dynamically),
When the user presses TAB, the cursor is moved to the next control,
(Optional) When the user presses the UP or DOWN arrow key, the cursor jumps in the previous or next element, respectively,
Each row is directly editable (the user can click inside any of them and change any character, even in the middle, thus without deleting the element first),
(Optional) There can only be one empty row at the end,
(Optional) If the user focuses another control (for example by pressing TAB) or another element (for example by using the arrow keys or by clicking), the current element is removed if it's empty,
(Optional) In addition to pressing ENTER when the cursor is in the last field, the user can press a button to add a new empty element if the last element is not empty,
There is only one column (no need for a column header),
(Optional) An inline label should be displayed in the last element if it's empty,
There is a button next to each element that allows the user to remove them,
(Optional) When the user presses DELETE twice at the end of an element, the focused element is removed (it must be pressed twice so that a user who keeps the button pressed to delete a bunch of characters doesn't remove the element by accident if that wasn't her intention -- this requirement can be formulated in other ways),
(Optional) The user can undo a delete operation,
(Optional) If the number of elements exceeds a certain limit, a scroll bar appears and the height of the control is fixed at that limit,
(NTH) If the number of elements exceeds a second (normally higher) limit, the list is paginated.
As I don't know any control that suits your needs, I'll try to suggest some implementation options:
Contenteditable:
Take a look at the contenteditable attribute - demo here.
This won't match all your requirements (point 12 is the main blocker here), but it's pretty nice OOTB, and (according to MDN) cross-browser.
The demo provided above may be a good starting point for your own solution.
One input per line:
The other approach will be to use one input for each of the lines.
I've provided simple demo that adds "new line" as enter is pressed.
Although this implementation will also need a bit of boxing with keyboard events, this one-element-per-line model seems to better reflect your bussiness needs and is pretty flexible.
I'd be afraid about performance with bigger data sets, though.
This can be also mixed with contenteditable approach (e.g. list of editable <li> elements), but it doesn't seem to bring in any benefits. It even brings some problem, because contenteditable won't restrict number of lines by default; with regular input, you have a guarantee that one element contains one line.

Mentioning system that mimics Facebook's

I've had a horrible problem that I've been wracking my brain for the past two days for, and have yet to come up with a solution. As such, I think this needs someone smarter than I to accomplish.
What I'm trying to build is a textbox that simulates that of Facebook's; essentially, the tagging function.
Now if you've used Facebook, you'll have noticed that Facebook allows you to tag people in a comment/post, simply by typing in their name and selecting from a dropdown list. The name of the person you've selected then appears in highlighted text in that very textarea. I've successfully managed to create and populate the dropdown list a combination of JQuery and AJAX, but the tagging process itself is the stumper.
Once a dropdown item has been selected (by Enter or clicking), the query text will be replaced with the tagged name. Now, it's difficult to see how one can give text in a textarea any kind of a highlight, so I've discovered (by inspecting elements in Google Chrome and deleting the textarea node) that the textarea itself is transparent, and there is a white div below "simulating" the text. Highlighted words are placed in a tag with custom CSS, which gives it that blue background. All of this I've found out myself, and I have successfully simulated this - but I can only do one tag.
Now I've investigated further and found an input type="hidden" element, of class "mentionsHidden". This input element has a value attribute, which dynamically populates itself based on the content of the textarea. So if I typed "ABC", the value of the element becomes "ABC". If I included a tag, say "hi [Rei]!" (where the name in [] is the tag), the value of the element becomes "hi #[member_id:Rei]!".
So I HAVE done my homework. But here comes the part I can't figure out.
I can't figure out how exactly to dynamically populate the hidden input element with the value of the textbox. It's obvious that the underlying div giving the blue tag background is populated from the input element. But the input element is giving me a headache.
You see, I can't do the following:
-I can't simply "copy" the entire value of the current textarea and "paste" it into the input element's value, because that would override any previously tagged people in the input element (after all, the textarea can only possess plaintext).
-Even though I CAN locate the current index of the caret (the flashing black line in the textarea that tells you where you're going to be typing into), that's only for the textarea. Index position 10 in the textarea and in the input element's value might be different things, because this way of "tagging" people will result in adding additional characters to the value String.
-I can't simply do a "replace" of the text I am intending to replace, because there might be other instances of that same text in other parts of the value String.
I know it's a very long and confusing post, but I do hope you get what I mean. I really need a solution and I don't want to use contenteditable, because it's only for HTML5 and some older browsers might not support it.
Yours,
Rei
I hope you were able to come up with, or find, a solution to your problem. Since there doesn't seem to be one here, i'd like to offer one for and anyone who might stumble upon this (as well as you if my assumption was incorrect).
You are going to need to maintain explicit locational data of each existing mention in the textarea in the order in which they appear. If, after a modification of the content in textarea, the position of a mention in it is changed, you will need to determine which appearance of its value, if any, will be used to represent it, and appropriately update the locational data of the mention.
With such a collection of data, it becomes trivial to construct the value of mentionsHidden, though the existence of such data makes the element unnecessary.
Mentionator is an existing, robust solution which takes this approach in providing the functionality you are trying to recreate. Considering it is well-structured, easy to follow, and copiously commented, it should be of use to you as either out-of-the box solution or reference material to cite as you roll out your own. It is maintained by yours truly :) .

autocomplete middle of a textarea selector javascript jquery

I have seen how to have multiple autocompletes in a single textarea, but what I want to due is have something that can select a term in the middle of the textarea, or more precisely wherever the cursor is/was in the textarea. Most solutions I have seen only work if the user is working on the end of the input string. I would like to have something along the lines of being able to start with the string:
"#George stepped on #foot."
then go back to the middle of the string and start typing to change it to this:
"#George stepped on #Fredrick's #foot. according to #Mary"
and have #Fredrick and #Mary each separately show up as an autocomplete option.
for the specific use I want this functionality for, the autocomplete will happen on character strings that start with either a "#" or a "#" symbol, but they will not necessarily be the first or last of the given symbol.
I am using javascript and jquery-ui for this task. this is for use on mobile devices so the position of the autocomplete will always just be at the bottom of the text area.
I'm actually implementing something very similar. I've looked at Google+ and they're using contenteditable for Chrome and some iframe hybrid for Firefox.
I've had moderate success with rangy for getting the current selection.
Things I haven't solved yet:
Properly detecting when the users starts typing # or #, this is harder than it seems, especially if you're injecting HTML in the input area.
Getting the x,y position of the current selection so you can position the auto-complete suggestions. Supposedly rangy can do it already but it's not in the official release yet (there is a working demo though).
I found out that I can do pretty much every thing I wanted by using the textarea.selectionStart value, and then cycling back from that index to the start of the tag, and then using selectionStart again to find the end of the tag. the conditions for when to stop increasing/decreasing the indices for the start and end of the tag can be a little complex, for mine it checks that it does not go beyond the start/end of the textarea text, or for the start of a tag with "#", "#" or a space. to replace the text when you have made your selection uses pretty much the same process to find the text, and then replaces the text with the selected tag. and sets the cursor after replacing the text, by setting the cursor to the start position of the tag offset by the length of the selected tag.

Categories

Resources