Tools
The Editor provides a predefined collection of tools that are used to interact with the control.
You can enable any of these tools by using the Tools()
method and configure it with the specific tool method. Custom tools and tools that require configuration are defined using inner methods for the tool.
Default Tools
The default tools in the Editor are included in the toolbar of the Editor upon setting the respective tool method. For a runnable example, refer to the demo on the built-in tools in the Editor.
The following example demonstrates the default tool methods of the Editor.
@(Html.Kendo().Editor()
.Name("editor")
.Tools(tools => tools
.Clear()
.Bold()
.Italic()
.Underline()
.Strikethrough()
.JustifyLeft()
.JustifyCenter()
.JustifyRight()
.JustifyFull()
.InsertUnorderedList()
.InsertOrderedList()
.Outdent()
.Indent()
.CreateLink()
.Unlink()
.InsertImage()
.InsertFile()
.SubScript()
.SuperScript()
.TableEditing()
.ViewHtml()
.Formatting()
.CleanFormatting()
.FontName()
.FontSize()
.ForeColor()
.FormatPainter()
.BackColor()
.Pdf()
.Print()
.Snippets(snippets => snippets
.Add("Signature", "<p>Regards,<br /> John Doe,<br /><a href='mailto:john.doe@example.com'>john.doe@example.com</a></p>")
.Add("Telerik UI for ASP.NET Core Demos", " <a href='https://demos.telerik.com/aspnet-core/'>Telerik UI for ASP.NET Core Online Demos</a> ")
)
)
)
Custom Tools
To define the custom tools of the Editor, use the CustomButton()
and CustomTemplate()
methods. You can use CustomButton()
for scenarios where only a single action has to be executed upon a button click. The custom template allows you to define a more complicated tool and also embed other widgets within the Editor toolbar. You can use CustomTemplate()
for creating a DropDownList which changes the background color for the editable area of the Editor.
Using the Template Component
The Telerik UI for ASP.NET MVC v2023.2.606 introduced the new Template Component. You can use it to define custom Tools for the Toolbar. The following example demonstrates how to add a custom Button and custom DropDownList as tools using the Template Component.
@(Html.Kendo().Editor()
.Name("editor")
.Tools(tools => tools
.Clear()
.CustomTemplate(x => x
.Name("customDropDownList")
.Template(
Html.Kendo().Template()
.AddHtml("<label for='templateTool' style='vertical-align:middle;'>Background:</label>")
.AddComponent(c=>c.DropDownList()
.Name("templateTool")
.BindTo(new List<SelectListItem>() {
new SelectListItem() {
Text = "none",
Value = ""
},
new SelectListItem() {
Text = "yellow",
Value = "#ff9"
},
new SelectListItem() {
Text = "green",
Value = "#dfd"
}
})
.Events(ev=>ev.Change("templateToolChange"))
)
)
)
.CustomButton(x => x.Name("customButton").ToolTip("Insert a horizontal rule").Exec(@<text>
function(e) {
var editor = this;
editor.exec("inserthtml", { value: "<hr />" });
}
</text>))
)
)
<script>
function templateToolChange(e){
$("#editor").data("kendoEditor").body.style.backgroundColor = e.sender.value();
}
</script>
Using Kendo Template
The following example demonstrates the same scenarios implemented using Kendo Templates. Note the Name()
method that is used for the CustomButton()
configuration. The passed string value will be later used to populate the class for the <span>
element for whose tool icon the :before
pseudo element is used. In this case, the final result for that class will be k-i-custom
. As the k-i-custom
class is used by one of the Kendo UI for jQuery icons, the respective icon will be displayed for the tool button. Note that the undo
and redo
tool names are reserved (forbidden). For a runnable example, refer to the demo on custom tools in the Editor.
@(Html.Kendo().Editor()
.Name("editor")
.Tools(t => t
.CustomTemplate(x => x
.Name("customDropDownList)
.Template("<label for='templateTool' style='vertical-align:middle;'>Background:</label>" +
"<select id='templateTool'>" +
"<option value=''>none</option>" +
"<option value='\\#ff9'>yellow</option>" +
"<option value='\\#dfd'>green</option>"+
"</select>")
)
.CustomButton(x => x
.Name("customButton")
.Tooltip("Insert a horizontal rule")
.Exec("onCustomButtonExec")
)
)
)
<script>
function onCustomButtonExec(e) {
var editor = this;
editor.exec("inserthtml", { value: "<hr />" });
}
$(document).ready(function () {
$("#templateTool").kendoDropDownList({
change: function (e) {
var editor = $("#editor").data("kendoEditor")
editor.body.style.backgroundColor = e.sender.value();
}
});
});
</script>
Extending the Editor with a Custom Tool
To extend the Editor with a custom tool:
-
Create a custom command through the
kendo.ui.editor.Command.extend
methodvar MyCustomCommand = kendo.ui.editor.Command.extend({ exec: function (e) { // Custom Logic. } });
-
Insert the Command in the Editor widget object instance.
kendo.ui.editor.MyCustomCommand = MyCustomCommand;
-
Register the tool by using the built-in
registerTool()
method.kendo.ui.editor.EditorUtils.registerTool( 'MyCustomCommand', new kendo.ui.editor.Tool({ command: MyCustomCommand, ui:{ type:"button", component: "Button", componentOptions: { themeColor: "success", click: () => $("#editor").getKendoEditor().exec ("MyCustomCommand", "test") } } }));
-
Use either the
CustomButton()
orCustomTemplate()
configurations for single or multiple action scenario.@(Html.Kendo().Editor() .Name("editor") .HtmlAttributes(new { style = "width: 100%; height:790px" }) .Tools(tools => tools .Clear() .CustomButton(button => button .Name("ToggleFullScreen") .Template(Html.Kendo().Template() .AddComponent(component => component .Button() .Name("toggleFullScreen") .Icon("toggle-full-screen-mode") .Events(events => events.Click("onClick")) ) ) ) ) )
-
Within an event handler in the custom tool, execute the required command by using the
exec()
method.<script> function onClick(e){ $("#editor").getKendoEditor().exec("ToggleFullScreen"); } </script>
Note that the tool name should correspond with the registered command.