Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-21-2012, 04:10 AM   PM User | #1
djh101
Regular Coder

 
djh101's Avatar
 
Join Date: May 2009
Location: Santa Clarita
Posts: 603
Thanks: 48
Thanked 63 Times in 63 Posts
djh101 is an unknown quantity at this point
anchorOffset and contenteditable

I am working on a function that will style text in a contenteditable div. For example, when Ctrl+b is pressed, <b></b> tags will be inserted around the currently selected text. The problem is that, unlike a textarea, a contenteditable div is not made up of a single text node.
Code:
function moobar(element,e){
	if(e.keyCode == "17"){ //IF CTRL IS PRESSED
		var selectionStart = window.getSelection().anchorOffset;
		var selectionEnd = window.getSelection().focusOffset;
		var range = document.createRange(), selection = window.getSelection();
		var foobar = document.createElement('div');
		
		if(selectionStart == selectionEnd){
			//NO TEXT IS HIGHLIGHTED
			//I'LL DEAL WITH THIS PART LATER
		} else {
			if(selectionStart > selectionEnd){
				foobar.innerHTML = element.innerHTML.substring(0, selectionEnd);
				element.innerHTML = element.innerHTML.substring(0, selectionEnd) + "<b>" + element.innerHTML.substring(selectionEnd,selectionStart) + "</b>" + element.innerHTML.substring(selectionStart);
			} else {
				foobar.innerHTML = element.innerHTML.substring(0, selectionStart);
				element.innerHTML = element.innerHTML.substring(0, selectionStart) + "<b>" + element.innerHTML.substring(selectionStart,selectionEnd) + "</b>" + element.innerHTML.substring(selectionEnd);
			}
			range.setStart(element.childNodes[foobar.children.length+2],0);
		}
		range.collapse(true);
		selection.removeAllRanges();
		selection.addRange(range);
	}
}
Might there be a possible way to find out which child of the editable div the offsets are in? For example, if div1's innerHTML was "Some <b>bolded</b> text" and "ex" was highlighted, would there be a way to find out that anchorOffset is inside div1.childNode[2]? If not, then what I really need are just some general suggestions about where to go from here?
__________________
"Yeah science!"
Online Science Tools
djh101 is offline   Reply With Quote
Old 01-21-2012, 02:26 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
Why not use the build-in functionality for contenteditable elements?

For example: The browser will automatically insert <b> and </b> around your selected text of a contenteditable element if you do this
Code:
document.execCommand('bold', false, null);
See this: https://developer.mozilla.org/en/Ric...ing_in_Mozilla

It works in all normal browsers
devnull69 is offline   Reply With Quote
Old 12-06-2012, 11:08 AM   PM User | #3
BubikolRamios
Senior Coder

 
Join Date: Dec 2005
Location: Slovenia
Posts: 1,876
Thanks: 114
Thanked 76 Times in 76 Posts
BubikolRamios is on a distinguished road
Bringing this up, coz I have related problem.

1.I need to read (crossbrowser) start and end point of selection in contentEditable div.
2.set selection, from java script, 'right back' where I read it.

reason:
Code:
document.execCommand('insertHTML'...
replaces HTML, but selection & focus are lost.

I'm getting nowhere here, so please help.
__________________
Found a flower or bug and don't know what it is ?
agrozoo.net galery
if you don't spot search button at once, there is search form:
agrozoo.net galery search

Last edited by BubikolRamios; 12-06-2012 at 01:24 PM..
BubikolRamios is offline   Reply With Quote
Old 12-06-2012, 11:47 AM   PM User | #4
007julien
Regular Coder

 
Join Date: May 2012
Location: France
Posts: 115
Thanks: 0
Thanked 17 Times in 15 Posts
007julien is an unknown quantity at this point
It's very difficult to work contenteditable elements. See the source of NicEdit or use it.
007julien is offline   Reply With Quote
Old 12-06-2012, 01:34 PM   PM User | #5
BubikolRamios
Senior Coder

 
Join Date: Dec 2005
Location: Slovenia
Posts: 1,876
Thanks: 114
Thanked 76 Times in 76 Posts
BubikolRamios is on a distinguished road
Nope, it is not, once you decide , that only command from execCommand you will ever use is : 'insertHTML'.

Then everything is under your control.

For this to work same as all the rest of execCommand (is applayed but focus and selection are not lost), I need to make selection right after execCommand( 'insertHTML'... is executed. Selection on newly inserted html chunk.

Help on that needed.

And yes, NicEdit, surfed to that, to much code for realy simple thing. Havent seen ever any 'plugin' that I could use rigth out of the box , same goes for NicEdit.
Trying to modify any plugin, realy, allways shows more time waster comparing to DIY.
__________________
Found a flower or bug and don't know what it is ?
agrozoo.net galery
if you don't spot search button at once, there is search form:
agrozoo.net galery search

Last edited by BubikolRamios; 12-06-2012 at 01:39 PM..
BubikolRamios is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:50 AM.


Advertisement
Log in to turn off these ads.