views:

167

answers:

3

I'm creating a Javascript WYSWYG editor (yes I know a million already exist but none met my needs) and I've hit a wall.

I need only two features in my editor:

1) Users should be able to select any font size specified in pts. 2) Users should be able to have text of various sizes in the same editor window.

Sounds simple but you'd be surprised at how hard this is.

I've already looked at TinyMCE, NicEdit, FreeRichTextEditor, TinyEditor, openWYSIWYG, WYMeditor, jHtmlArea, uEditor, CLEditor, jQRTE, jQuery Simple WYSIWYG Editor, and xinha and none of them have both these features.

The only editors that I can find that meet the bill are Google Docs, CKEditor, and YUI Rich Text. Google Docs isn't an option, CKEditor is $850 and is overkill for my project, and YUI Rich Text editor is too complicated as well. I just want a dead simple editor with these two features.

This is a big roadblock to my project.

Whatever solution is found has to be lightweight and free. YUI Text Editor for example works but depends on the YUI javascript library which is too big and not easy to take apart and strip down.

A: 

I've done this in an IE based control before. In summary:

  1. User selects a segment of text and chooses a different font size in your GUI.
  2. In Javascript:
    1. Use the browser DOM to get the current text selection object.
    2. Create a new SPAN element, set the CSS style font-size attribute as required (eg: 12pt).
    3. Set the inner text of the SPAN to the originally selected text.
    4. Delete the selected text from the document.
    5. Insert the new SPAN at the same location.

Of course the specifics depend on the browser/s you are targeting. If you need to support many browsers using a library (JQuery etc) may be the way to go.

If I remember correctly, in IE you use document.selection and selection.createRange() to get the current selection.

There is also a delete() or collapse() on the range object to delete the selected text. To insert the span you can use insertBefore() / insertAfter() or possibly range.pasteHTML()

Ash
I need at least Chrome and Firefox support. I follow your solution until the last step - How do I insert the span in the middle of a sentance? Won't that only work if I am trying to change the size of an entire span? If I have text like this:<span>This is a boring sentence</span><span> other text</span>and I just want to make 'sentance and other' size 17pt font how would this work in your solution?
Bryce
Yes, I remember this is a problem with the approach I gave. To get around this I think I ended up using the selection.execCommand("FontSize",false,"3"). In IE this creates a FONT element (dodgy I know) with a html 1-7 size, not a PT size. IE applies this new element correctly so that it adds two FONT elements within each parent element if needed. I then get the element my current selection is within (now a FONT element) remove the size attribute and replace with a font-size style attibute. Not ideal, but it works in IE.
Ash
A: 

CKEditor is $850 only if you require a special license. As long as you can respect the GPL, LGPL or MPL you can use it without buying anything. http://ckeditor.com/license

AlfonsoML
A: 

Using CLEditor, you could write a simple plug-in that replaces the built-in font popup with point size values (rather than font sizes 1-7). Then use the CLEditor .selectedText() and .execCommand() methods (along with the 'inserthtml' command) to replace the current selection with a span tag containing the selected text. This would work cross browser, would be very lightweight and should not be that dificult to write.

Chris Landowski
I considered this but if the user has some complex html selected, like the end of one div and the beginning of another, this won't work.
Bryce
CLEditor Version 1.2.4 (just released) now contains a .selectedHTML() method which should solve the problem you described. If the user has a complex selection, the returned HTML string will contain the correct opening and closing tags.
Chris Landowski