# Insert a Link in the Editor Without Selecting Text

## Environment

| Product         | Progress® KendoReact Editor |
| --------------- | --------------------------- |
| Product Version | 16.2.0                      |

## Description

How can I insert a link in the KendoReact Editor without selecting text first? The built-in Link tool is disabled when I place the caret in the content.

This KB also answers the following questions:

* Can the Editor Link tool insert a link at the caret position?
* How do I add a link with its own text in the Editor?
* How do I create a custom Editor tool that inserts a hyperlink?

## Solution

The built-in [`Link`](https://www.telerik.com/kendo-react-ui/components/editor/api/editortools) tool applies a link mark to the current selection. Its dialog contains the **Web address**, **Title**, and **Open link in new window** fields, and has no field for the link text. For this reason, the tool is disabled when the selection is empty, unless the caret is already inside a link.

To insert a link without selecting text, create a [custom tool](https://www.telerik.com/kendo-react-ui/components/editor/tools) that collects the address and the link text, and inserts a text node with a link mark at the caret by dispatching a ProseMirror transaction. If text is selected, the new link replaces the selection.

The following example demonstrates the complete implementation. Click in the content without selecting text, and then click the custom **Insert link at caret** tool. Compare it with the built-in Link tool, which stays disabled.

[Demo Source](https://demos.telerik.com/kendo-react-ui/knowledge-base/editor/editor-insert-link-without-selection/source.txt)

The solution consists of the following steps:

1. Create a tool component that receives the Editor `view` through its props. Render a button, and open a dialog where users enter the web address and the link text. The following snippet shows how the button state depends on whether a text node can be inserted at the current selection.

   ```tsx
   const canInsert = !!view && EditorUtils.canInsert(view.state, view.state.schema.nodes.text);
   ```

2. On insert, create a text node that carries the link mark, and insert it by dispatching a transaction that calls `replaceSelectionWith` with its second argument set to `false`. By default, `replaceSelectionWith` replaces the marks of the inserted node with the marks at the caret, which removes the link mark and leaves plain text. Pass the address through the `sanitizeUrl` function of `@progress/kendo-react-common` to remove unsafe URL schemes. The following snippet shows the insertion.

   ```tsx
   const href = sanitizeUrl(address);
   const { schema } = view.state;
   const linkMark = schema.marks.link.create({ href });

   view.dispatch(view.state.tr.replaceSelectionWith(schema.text(text, [linkMark]), false).scrollIntoView());
   ```

3. Add the custom tool to the [`tools`](https://www.telerik.com/kendo-react-ui/components/editor/api/editorprops#tools) array of the Editor.

   ```tsx
   <Editor tools={[[EditorTools.Link, InsertLinkAtCaret]]} />
   ```

## See Also

* [Editor Tools](https://www.telerik.com/kendo-react-ui/components/editor/tools)
* [Custom Rendering in the Editor](https://www.telerik.com/kendo-react-ui/components/editor/custom-rendering)
* [Editor Selection](https://www.telerik.com/kendo-react-ui/components/editor/selection)
* [API Reference of the EditorUtils](https://www.telerik.com/kendo-react-ui/components/editor/api/editorutils)
