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
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?
You can try the exec method with "insertHtml" as command:
editor.exec('insertHtml', { value: '<strong>Hello World!</strong>' });
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!
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>' }); }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.
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!
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.
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!
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
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
Thank you for your help.
We are not aware of such a regression. Could you provide some steps to reproduce this issue?
Regards,Atanas Korchev
the Telerik team
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");
});
Alex Gyoshev
the Telerik team
This is a regression from 2011.2.712.
Kind regards,
Alex Gyoshev
the Telerik team