This is a migrated thread and some comments may be shown as answers.

[Solved] Inserting text at the Current Cursor Position

18 Answers 133 Views
Editor
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jennifer
Top achievements
Rank 1
Jennifer asked on 19 Jul 2011, 07:29 PM
I'm new to working with the telerik editor, so this may be a simple issue, but I've yet to find an answer to my problem. I am adding a custom tool to the toolbar. It is a dropdown that I want to be able to insert the text from the drop down into the editor at the current cursor possition (similar to the current functionality of inserting a link), however I haven't found any way to do this in the MVC version of the editor.

I currently have: 

@( Html.Telerik().Editor()
.Name("Editor")
    .Value("Testing Telerik HTML Editor")
    .Tools(tools => tools
            .Custom(settings => settings.Template(@<text>
                @(Html.Telerik().DropDownList()
                    .Name("LanguagesAlso")
                    .Items(items => {
                        items.Add().Text("English").Value("en"); 
                        items.Add().Text("Spanish").Value("es"); 
                        items.Add().Text("German").Value("de"); 
                        .ClientEvents(events => events.OnChange("onLanguageChange")))
                    </text>))
       )
)
  
<script type="text/javascript">
    function onLanguageChange() {
        $('#Editor').data('tEditor').value('You changed the language to ' + $(this).data('tDropDownList').text());
    }
</script>

This works as far as replacing the text of the editor. What I'm having trouble with is finding what to replace "$('#Editor').data...." with to just insert text into the editor at the current cursor position.

Any help would be appriciated. Thanks.

18 Answers, 1 is accepted

Sort by
0
Jennifer
Top achievements
Rank 1
answered on 20 Jul 2011, 02:49 PM
So... I have everything working except getting the cursor position.

function onLanguageChange() {
        var cursorPos = 7
  
        var editor = $('#Editor').data('tEditor');
        var editorValue = editor.value().substring(0, cursorPos) + $(this).data('tDropDownList').text() + editor.value().substring(cursorPos);
        $('#Editor').data('tEditor').value(editorValue);
    }

Is there any way to get the cursor position, or am I on a wild goose chase?
0
Accepted
Atanas Korchev
Telerik team
answered on 20 Jul 2011, 04:07 PM
Hello Jennifer,

 You can try the exec method with "insertHtml" as command:

editor.exec('insertHtml', { value: '<strong>Hello World!</strong>' });

Regards,
Atanas Korchev
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
Jennifer
Top achievements
Rank 1
answered on 20 Jul 2011, 05:45 PM

That... kind of works.

I am running into one problem though. Focus switches from the editor to the drop down. So, when I do the editor.exec(), no matter where the cursor was, it pastes to the beginning of the text.

Is there a way to get around this issue?

My code:

function onLanguageChange() {
        var editor = $('#Editor').data('tEditor');
        editor.focus();
        editor.exec('insertHtml', { value: '<strong>Hello World!</strong>' });
    }
(without the editor.focus(), the page errors out on the editor.exec())

Edit: Or, I suppose more useful in my case - is there a way to keep the editor from losing focus? I've noticed when non custom drop downs are selected, the cursor continues blinking in the editor text area at the cursor position, which is not the case when I select my drop down.

Thanks for your help.
0
Atanas Korchev
Telerik team
answered on 21 Jul 2011, 06:56 AM
Hi Jennifer,

 Which browser are you using? In IE the editor will use focus because that's how IE works. In that case you can try making your dropdown unselectable. If this does not help please provide a sample project so we can check it out.

Regards,
Atanas Korchev
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
Jennifer
Top achievements
Rank 1
answered on 21 Jul 2011, 08:45 PM
I think that help is on the right track as far as what my problem is, but I'm still having the issue. I'm using IE. I tried my code out in Firefox, Safari, and Opera and it is working as expected.

I'm unable to post a sample project (the .zip is too big and the support ticket won't let me submit for a MVC tool).

I tried creating a new sample project with the following instructions to install: http://www.telerik.com/help/aspnet-mvc/getting-started-using-telerik-extensions-for-asp.net-mvc-in-your-project.html, though changing it to razor

And my .cshtml looks like:

@( Html.Telerik().Editor()
    .Name("Editor")
    .Value("Testing Telerik HTML Editor")
    .Tools(tools => tools
            .Custom(settings => settings.Template(@<text>
                @(Html.Telerik().DropDownList()
                    .Name("Languages")
                    .Items(items => {
                        items.Add().Text("English").Value("en"); 
                        items.Add().Text("Spanish").Value("es"); 
                        items.Add().Text("German").Value("de"); 
                    })
                    .HtmlAttributes(new { onselectstart = "return false", unselectable = "on", style = "-moz-user-select: none; -khtml-user-select: none; user-select: none;" })
                    .ClientEvents(events => events.OnChange("onLanguageChange")))
                    </text>))
       )
)
  
<script type="text/javascript">
    function onLanguageChange() {
        var editor = $('#Editor').data('tEditor');
        editor.focus();
        editor.exec('insertHtml', { value: $(this).data('tDropDownList').text() });
    }
  
    function makeUnselectable(node) {
        if (node.nodeType == 1) {
            node.unselectable = true;
        }
        var child = node.firstChild;
        while (child) {
            makeUnselectable(child);
            child = child.nextSibling;
        }
    }
    makeUnselectable(document.getElementById("Languages")); 
</script>

And I'm still getting the same issue.

Thank you for your help.
0
Atanas Korchev
Telerik team
answered on 22 Jul 2011, 08:08 AM
Hi Jennifer,

 Which version of IE are you testing in? I tried in IE8 and IE9 and everything worked as expected. Check this video which shows that.

Kind regards,
Atanas Korchev
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
Jennifer
Top achievements
Rank 1
answered on 27 Jul 2011, 02:39 PM
Atanas,

Thank you for looking into this. I've tested it in IE7 and IE8 and am still running into the same issue.

I appear to be able to post the project now, so if you could take a look and see if you can determine the issue, I would greatly appriciate it.

Thanks,
Jennifer
0
Accepted
Atanas Korchev
Telerik team
answered on 28 Jul 2011, 09:18 AM
Hi Jennifer,

 Your project is using an older version which may be the problem. Please try using the latest version which is 2011.2 712.

Regards,
Atanas Korchev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

0
Jennifer
Top achievements
Rank 1
answered on 28 Jul 2011, 02:12 PM
Yep, looks like my issue was fixed in the latest version.

Thank you for your help.
0
Juan
Top achievements
Rank 1
answered on 29 Nov 2011, 09:58 PM
the lastest version (2011.3.1115) will not work in IE 8,9. version 2011.2 712 will work.
0
Hao
Top achievements
Rank 1
answered on 31 Jul 2012, 01:23 AM
We're having the same issue with version 2012.2.607. Is there a solution/work around? It's a blocker for us.
0
Atanas Korchev
Telerik team
answered on 31 Jul 2012, 08:58 AM
Hello,

 We are not aware of such a regression. Could you provide some steps to reproduce this issue?

Regards,
Atanas Korchev
the Telerik team
Check out the successor of Telerik MVC Extensions - Kendo UI for ASP.NET MVC - and deem it for new ASP.NET MVC development.
0
Hao
Top achievements
Rank 1
answered on 31 Jul 2012, 03:01 PM
Attached is the repro project. The telerik version is 2012.2.607.340. Click "Insert Text" you will see the text is inserted at the start no matter where the cursor is.
0
Hao
Top achievements
Rank 1
answered on 31 Jul 2012, 03:02 PM
BTW,  I tested with IE9 on Windows7.
0
Hao
Top achievements
Rank 1
answered on 03 Aug 2012, 12:01 AM
Have you tried the repro? Any updates?
0
Alex Gyoshev
Telerik team
answered on 03 Aug 2012, 12:16 PM
Hello Jennifer,

You need to set unselectable=on to the HTML elements of the toolbar items. A simple snippet to do this is this:

    $(document).ready(function() {
        $("#editor .t-custom *").attr("unselectable", "on");
    });

All the best,
Alex Gyoshev
the Telerik team
Check out the successor of Telerik MVC Extensions - Kendo UI for ASP.NET MVC - and deem it for new ASP.NET MVC development.
0
Hao
Top achievements
Rank 1
answered on 03 Aug 2012, 07:27 PM
This does not work if the custom tool brings up a dialog and user inputs data there. Attached is a repro. Click the last icon on the toolbar and a dialog is displayed. Type something in the text box and close the dialog. "Foo" is inserted at the front of the editor regardless of what the cursor was before.

This is a regression from 2011.2.712.
0
Alex Gyoshev
Telerik team
answered on 07 Aug 2012, 10:19 AM
Yes, you need to manually restore the selection to the editor. The code that was enabling this functionality automatically caused bugs in many situations, so it was removed. If you get the editor range when the dialog is opening and restore it when the command finishes, the problem will disappear. I'm attaching the modified project.

Kind regards,
Alex Gyoshev
the Telerik team
Check out the successor of Telerik MVC Extensions - Kendo UI for ASP.NET MVC - and deem it for new ASP.NET MVC development.
Tags
Editor
Asked by
Jennifer
Top achievements
Rank 1
Answers by
Jennifer
Top achievements
Rank 1
Atanas Korchev
Telerik team
Juan
Top achievements
Rank 1
Hao
Top achievements
Rank 1
Alex Gyoshev
Telerik team
Share this question
or