Hi there,
I'm trying to create a plugin that looks for the user typing "@username", and displays a dropdown list of users in the system for possible auto-complete.
My current question is this - what is the best/correct way to get the current word after the user types something? Here is my current strategy:
I guess when I bullet it out like that, it doesn't seem too complex, but travelling around adjacent text nodes is what seems like there would already be a method for. I tried range.expand(), but that went all the way to the block level, and I just need the word. Is there a built-in function or mechanism that would be better to use?
My over-arching question is this - is there a plugin like this that already exists? I couldn't find anything, but it is common enough in many of todays large web applications (such as Jive), that it seems like it would have been done before.
Any help of guidance is greatly appreciated. Thanks!
-Sean
I'm trying to create a plugin that looks for the user typing "@username", and displays a dropdown list of users in the system for possible auto-complete.
My current question is this - what is the best/correct way to get the current word after the user types something? Here is my current strategy:
- Listen for the "key" event
- Get the current range
- If the range is collapsed, travel backwards finding non-whitespace
- Check adjacent nodes if whitespace hasn't been found, and if they are text nodes, continue building the word to search for
I guess when I bullet it out like that, it doesn't seem too complex, but travelling around adjacent text nodes is what seems like there would already be a method for. I tried range.expand(), but that went all the way to the block level, and I just need the word. Is there a built-in function or mechanism that would be better to use?
My over-arching question is this - is there a plugin like this that already exists? I couldn't find anything, but it is common enough in many of todays large web applications (such as Jive), that it seems like it would have been done before.
Any help of guidance is greatly appreciated. Thanks!
-Sean
try your self
try your self
It's hard
A lot of people have tried this and failed... some guidance would really be appreciated. This appears to be an unsolved problem :-(
There's a plugin in Drupal
There's a plugin in Drupal that does something like that: https://drupal.org/project/ckeditor_mentions
Customer and Community Manager, CKSource
Follow us on: Facebook, Twitter, LinkedIn
If you think you found a bug in CKEditor, read this!
Get Word - Current Position
function getCurrentWord() {
var range = editor.getSelection().getRanges()[ 0 ],
startNode = range.startContainer;
if ( startNode.type == CKEDITOR.NODE_TEXT && range.startOffset ) {
var indexPrevSpace = startNode.getText().lastIndexOf(' ', range.startOffset) + 1;
var indexNextSpace = startNode.getText().indexOf(' ', range.startOffset);
if(indexPrevSpace == -1) {
indexPrevSpace=0;
}
if(indexNextSpace == -1) {
indexNextSpace = startNode.getText().length;
}
var filteredWord = startNode.getText().substring(indexPrevSpace,indexNextSpace);
// Range at the non-zero position of a text node.
return startNode.getText().substring(indexPrevSpace,indexNextSpace);
}
// Selection starts at the 0 index of the text node and/or there's no previous text node in contents.
return null;
}
This is great, thank you
This is great, thank you harpreet. I need the same function except to automatically select the word. I spend over 1 hour cracking at it, but no luck yet.