layout | title | description | platform | control | documentation | domainurl |
---|---|---|---|---|---|---|
post |
Link in Angular Document editor component | Syncfusion |
Learn here all about Link in Syncfusion Angular Document editor component of Syncfusion Essential JS 2 and more. |
ej2-angular |
Link |
ug |
Document Editor supports hyperlink field. You can link a part of the document content to Internet or file location, mail address, or any text within the document.
Document Editor triggers requestNavigate
event whenever user clicks Ctrl key or tap a hyperlink within the document. This event provides necessary details about link type, navigation URL, and local URL (if any) as arguments, and allows you to easily customize the hyperlink navigation functionality.
The following example illustrates how to add requestNavigate event for DocumentEditor.
{% tabs %} {% highlight ts tabtitle="app.component.ts" %} {% include code-snippet/document-editor/link-cs2/src/app.component.ts %} {% endhighlight %} {% highlight ts tabtitle="app.module.ts" %} {% include code-snippet/document-editor/link-cs2/src/app.module.ts %} {% endhighlight %} {% highlight ts tabtitle="main.ts" %} {% include code-snippet/document-editor/link-cs2/src/main.ts %} {% endhighlight %} {% endtabs %}
{% previewsample "page.domainurl/samples/document-editor/link-cs2" %}
The following example illustrates how to add requestNavigate event for DocumentEditorContainer component.
import { Component, ViewEncapsulation, ViewChild } from '@angular/core';
import {
DocumentEditorContainerComponent, ToolbarService
} from '@syncfusion/ej2-angular-documenteditor';
@Component({
selector: 'app-container',
// specifies the template string for the Document Editor container component
template: `<div><button ejs-button (click)="insertText()" >Insert Text</button>
<ejs-documenteditorcontainer #documenteditor_default serviceUrl="https://ej2services.syncfusion.com/production/web-services/api/documenteditor/" height="600px" style="display:block" [enableToolbar]=true (created)="onCreated()"> </ejs-documenteditorcontainer></div>`,
encapsulation: ViewEncapsulation.None,
providers: [ToolbarService]
})
export class AppComponent {
@ViewChild('documenteditor_default')
public container: DocumentEditorContainerComponent;
public onCreated(): void {
this.container.documentEditor.requestNavigate = (args) => {
if (args.linkType !== 'Bookmark') {
let link: string = args.navigationLink;
if (args.localReference.length > 0) {
link += '#' + args.localReference;
}
//Navigate to the specified URL.
window.open(link);
args.isHandled = true;
}
};
}
}
If the selection is in hyperlink, trigger this event by calling navigateHyperlink
method of Selection
instance. Refer to the following example.
this.documentEditor.selection.navigateHyperlink();
Document Editor copies link text of a hyperlink field to the clipboard if the selection is in hyperlink. Refer to the following example.
this.documentEditor.selection.copyHyperlink();
To create a basic hyperlink in the document, press ENTER
/ SPACEBAR
/ SHIFT + ENTER
/ TAB
key after typing the address, for instance http://www.google.com
. Document Editor automatically converts this address to a hyperlink field. The text can be considered as a valid URL if it starts with any of the following.
<http://>
<https://>
file:///
www.
mailto:
Refer to the following example.
{% tabs %} {% highlight ts tabtitle="app.component.ts" %} {% include code-snippet/document-editor/link-cs3/src/app.component.ts %} {% endhighlight %} {% highlight ts tabtitle="app.module.ts" %} {% include code-snippet/document-editor/link-cs3/src/app.module.ts %} {% endhighlight %} {% highlight ts tabtitle="main.ts" %} {% include code-snippet/document-editor/link-cs3/src/main.ts %} {% endhighlight %} {% endtabs %}
{% previewsample "page.domainurl/samples/document-editor/link-cs3" %}
You can customize the screen tip text for the hyperlink by using below sample code.
this.documentEditor.insertHyperlink('https://www.google.com', 'Google', '<<Screen tip text>>');
Screen tip text can be modified through UI by using the Hyperlink dialog
To remove link from hyperlink in the document, press Backspace key at the end of a hyperlink. By removing the link, it will be converted as plain text. You can use ‘removeHyperlink’ method of ‘Editor’ instance if the selection is in hyperlink. Refer to the following example.
this.documentEditor.editor.removeHyperlink();
Document Editor provides dialog support to insert or edit a hyperlink. Refer to the following example.
{% tabs %} {% highlight ts tabtitle="app.component.ts" %} {% include code-snippet/document-editor/link-cs4/src/app.component.ts %} {% endhighlight %} {% highlight ts tabtitle="app.module.ts" %} {% include code-snippet/document-editor/link-cs4/src/app.module.ts %} {% endhighlight %} {% highlight ts tabtitle="main.ts" %} {% include code-snippet/document-editor/link-cs4/src/main.ts %} {% endhighlight %} {% endtabs %}
{% previewsample "page.domainurl/samples/document-editor/link-cs4" %}
You can use the following keyboard shortcut to open the hyperlink dialog if the selection is in hyperlink.
Key Combination | Description |
---|---|
Ctrl + K | Open hyperlink dialog that allows you to create or edit hyperlink |