Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge editor-kb-add-icons-2300 into production #2362

Merged
merged 24 commits into from
Sep 3, 2024
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
70d08d3
docs(editor): kb for adding icons
ntacheva Jul 19, 2024
1ed2b06
chore(editor): add description and one more sample
ntacheva Aug 2, 2024
f6674ac
Update knowledge-base/editor-add-icons.md
ntacheva Aug 5, 2024
240136b
Update knowledge-base/editor-add-icons.md
ntacheva Aug 5, 2024
8dfa000
Update knowledge-base/editor-add-icons.md
ntacheva Aug 5, 2024
e932db6
Update knowledge-base/editor-add-icons.md
ntacheva Aug 5, 2024
4d5fb07
chore(editor): address feedback
ntacheva Aug 5, 2024
c0ca39a
chore(editor): update iframe section
ntacheva Aug 5, 2024
47b7ad8
Update knowledge-base/editor-add-icons.md
ntacheva Aug 12, 2024
33fc877
Update knowledge-base/editor-add-icons.md
ntacheva Aug 12, 2024
bb8f7e4
Update knowledge-base/editor-add-icons.md
ntacheva Aug 12, 2024
a09530c
Update knowledge-base/editor-add-icons.md
ntacheva Aug 12, 2024
74930d0
Update knowledge-base/editor-add-icons.md
ntacheva Aug 12, 2024
45950f6
Update knowledge-base/editor-add-icons.md
ntacheva Aug 12, 2024
ce47697
Update knowledge-base/editor-add-icons.md
ntacheva Aug 12, 2024
ae32083
Update knowledge-base/editor-add-icons.md
ntacheva Aug 12, 2024
0a66fdf
Update knowledge-base/editor-add-icons.md
ntacheva Aug 12, 2024
b73e255
chore(editor): address feedback
ntacheva Aug 12, 2024
f4a4d2b
Update knowledge-base/editor-add-icons.md
ntacheva Aug 29, 2024
6b3aecc
Update knowledge-base/editor-add-icons.md
ntacheva Aug 29, 2024
4a7adb8
Update knowledge-base/editor-add-icons.md
ntacheva Aug 29, 2024
9b88af6
Update knowledge-base/editor-add-icons.md
ntacheva Aug 29, 2024
8b18b4d
Update knowledge-base/editor-add-icons.md
ntacheva Aug 29, 2024
4e14ca2
Update knowledge-base/editor-add-icons.md
ntacheva Aug 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
207 changes: 207 additions & 0 deletions knowledge-base/editor-add-icons.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
---
title: Add Font Icons in Editor
description: How to insert <i> tags in the Editor for Blazor to render custom icons in the Editor content?
type: how-to
page_title: How to Add Font Icons in the Telerik Editor for Blazor
slug: editor-kb-add-icons
position:
tags: blazor, editor, prosemirror, schema, icon
ticketid:
res_type: kb
---

## Environment
<table>
<tbody>
<tr>
<td>Product</td>
<td>Editor for Blazor</td>
</tr>
</tbody>
</table>


## Description

This KB article answers the following questions:

* How to add custom icons in the Editor content? When adding an icon such as `<i class="fa-light fa-envelope">`, the icon's HTML disappears upon saving.
* How can I add an `<i>` element inside the Editor?

## Solution

The default schema of the Telerik Editor does not include an `<i>` tag, so the Editor strips such elements automatically. To allow adding font icons through `<i>` tags in the Editor content:

1. [Modify the ProseMirror schema]({%slug editor-modify-default-schema%}) to include a node for the `<i>` element.
2. Ensure the required font icon stylesheets can affect the Editor content. The approach varies depending on the [edit mode of the Editor]({%slug editor-edit-modes-overview%}):
* [Iframe Edit Mode](#add-icons-in-iframe-edit-mode)
* [Div Edit Mode](#add-icons-in-div-edit-mode)


### Add Icons in Iframe Edit Mode

When the [Editor `EditMode` is set to `EditorEditMode.Iframe`]({%slug editor-edit-modes-iframe%}), the content area is inside an `<iframe>` element that does not apply the CSS rules from the current page.

This means that you need to inject the icons stylesheet into the `<iframe>`, so the icons are properly rendered. At the time of writing (UI for Blazor **6.1.0**), [the Editor does not support injecting your CSS files into the iframe](https://feedback.telerik.com/blazor/1543925-add-the-ability-to-inject-css-files-into-the-iframe) but you can inject them with JSInterop in `OnAfterRenderAsync`.

>caption Add icons in an Editor with Iframe edit mode

````CSHTML
@using Telerik.Blazor.Components.Editor
@inject IJSRuntime js

<TelerikEditor @bind-Value="@EditorValue"
Tools="@EditorTools"
Schema="schemaProvider"
Height="300px">
</TelerikEditor>

@code {
private string EditorValue { get; set; } = @"<p>Here is an example icon in the Editor content <i class='fa fa-info-circle'></i></p>";

private List<IEditorTool> EditorTools { get; set; } = new List<IEditorTool>() { new ViewHtml() };

protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await js.InvokeVoidAsync("injectEditorStylesheet");
}
}
}

@* Move JavaScript code to a separate JS file in production *@
<script suppress-error="BL9992">

//define the icon node
var iconNode = {
attrs: {
class: { default: null },
type: { default: null },
},
group: "inline",
content: "text*",
inline: true,
parseDOM: [
{
tag: "i",
getAttrs: (dom) => ({
class: dom.getAttribute("class"),
}),
},
],
toDOM: (node) => {
const attrs = {
class: node.attrs.class
};
return ["i", attrs];
},
};

//add the icon node to the Editor ProseMirror schema
window.schemaProvider = (args) => {
const schema = args.getSchema();
const Schema = args.ProseMirror.Schema

let nodes = schema.spec.nodes.addToEnd("i", iconNode);

const newSchema = new Schema({ nodes });
return newSchema;
}

//inject the stylesheet for the icons, so they are properly visualized
function injectEditorStylesheet() {
var doc = document.querySelector("iframe").contentWindow.document;
var head = doc.querySelector("head");

var bootstrapCssLink = document.createElement("link");
bootstrapCssLink.href = "https://cdnjs.cloudflare.com/ajax/libs/open-iconic/1.1.1/font/css/open-iconic-bootstrap.min.css";
bootstrapCssLink.rel = "stylesheet";

var fontAwesomeCssLink = document.createElement("link");
fontAwesomeCssLink.href = "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css";
fontAwesomeCssLink.rel = "stylesheet";

head.appendChild(bootstrapCssLink);
head.appendChild(fontAwesomeCssLink);
};

</script>
````

### Add Icons in Div Edit Mode

When the [Editor `EditMode` is set to `EditorEditMode.Div`]({%slug editor-edit-modes-div%}), the content area is a `<div contenteditable="true">` element that inherits the CSS rules from the current page.

This allows you to include the icon stylesheets in the `<head>` of the web page along with the other stylesheets.

>caption Add icons in an Editor with Div edit mode

````CSHTML
@using Telerik.Blazor.Components.Editor

@* Just one example of including custom font icon libraries.
Make sure to use the correct way and resources for your actual project *@
<link href="https://cdnjs.cloudflare.com/ajax/libs/open-iconic/1.1.1/font/css/open-iconic-bootstrap.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" rel="stylesheet" />

<TelerikEditor @bind-Value="@EditorValue"
Tools="@EditorTools"
Schema="schemaProvider"
EditMode="@EditorEditMode.Div"
Height="300px">
</TelerikEditor>

@code {
private string EditorValue { get; set; } = @"Here is an example icon in the Editor content <i class='fa fa-info-circle'></i>";

private List<IEditorTool> EditorTools { get; set; } = new List<IEditorTool>() { new ViewHtml() };
}

@* Move JavaScript code to a separate JS file in production *@
<script suppress-error="BL9992">

//define the icon node
var iconNode = {
attrs: {
class: { default: null },
type: { default: null },
},
group: "inline",
content: "text*",
inline: true,
parseDOM: [
{
tag: "i",
getAttrs: (dom) => ({
class: dom.getAttribute("class"),
}),
},
],
toDOM: (node) => {
const attrs = {
class: node.attrs.class
};
return ["i", attrs];
},
};
debugger

//add the icon node to the Editor ProseMirror schema
window.schemaProvider = (args) => {
const schema = args.getSchema();
const Schema = args.ProseMirror.Schema

let nodes = schema.spec.nodes.addToEnd("i", iconNode);

const newSchema = new Schema({ nodes });
return newSchema;
}

</script>
````

## See Also

* [Custom Editor Tools]({%slug editor-custom-tools%})
* [Modify the ProseMirror Schema]({%slug editor-modify-default-schema%})
Loading