Skip to content

Newcomer: programmatically set selection? #267

Open
Myrmex opened this Issue Nov 13, 2012 · 4 comments

2 participants

@Myrmex
Myrmex commented Nov 13, 2012

Hi, this lib looks great! Sorry for the stupid question, but I'm a newbie in JS world. I was trying to programmatically set the selection in the editor so that I can apply some formatting to specific words, but I cannot manage to do it. Here is my sample code, could anyone point me in the right direction?
Thanks!

var selection = editor.composer.selection;
var r = selection.getRange();
r.startOffset = 3;
r.endOffset = 6;
editor.composer.selection.setSelection(r);
editor.composer.commands.exec("bold");

I also tried using rangyinput to do the same; it works in a normal textarea, but not here. Sample:

var ta = editor.textarea.element;
$(ta).focus().setSelection(3, 6);
editor.composer.commands.exec("bold");

@gabrielengel

Hi @Myrmex, did you have any success here?

@Myrmex
Myrmex commented Jan 16, 2013

I'm sorry I didn't. I looked for several libraries, but it seems that none allows for programmatic selection.

@gabrielengel

I'm looking at it.. wysihtml5 uses rangy (http://code.google.com/p/rangy/), and it seems to make it possible.

There is a nice example here:
http://rangy.googlecode.com/svn/trunk/demos/saverestore.html

You can, for example, select a node:

editor.composer.selection.selectNode(editor.composer.element.firstChild)
editor.composer.selection.selectNode(editor.composer.element.lastChild)

I'm trying to make a selection based on an array-like reference, pointing beginning and end. The range object seems to carry the answer, needs some hacking.

@gabrielengel

Maybe, this is the path..

http://stackoverflow.com/questions/8312040/rangy-how-do-i-expand-the-selection-based-on-parent-node

Could not implement.
There is a module under development called "rangy-textrange", it adds some extra functions that can help us with this. Will be released at rangy 1.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.