Skip to content

Commit

Permalink
add ability to customize toolbar options (#12)
Browse files Browse the repository at this point in the history
* add ability to customize toolbar options
  • Loading branch information
erinnmclaughlin authored May 22, 2024
1 parent 9f8dce9 commit 913162f
Show file tree
Hide file tree
Showing 7 changed files with 313 additions and 56 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,49 @@ Then add the following to your `_Imports.razor`:
@using Tizzani.MudBlazor.HtmlEditor
```

## Configuring Toolbar Options (available since v2.1)
There are several options available for customizing the HTML editor toolbar.

To customize options for a specific editor instance, define a `<MudHtmlToolbarOptions>` inside the `<MudHtmlEditor>`:

```html
<MudHtmlEditor>
<MudHtmlToolbarOptions InsertImage="false" /> <!-- This will exclude the "insert image" toolbar option -->
</MudHtmlEditor>
```

For all available options, see [here](./src/Tizzani.MudBlazor.HtmlEditor/MudHtmlToolbarOptions.razor.cs).

### Configuring Default Options
To configure default options for all instances of the HTML editor, you can wrap your razor content with `<CascadingMudHtmlToolbarOptions>`.

#### App.razor or Routes.razor
```html
<CascadingMudHtmlToolbarOptions InsertImage="false">
<Router AppAssembly="@typeof(Program).Assembly">
<!-- etc. -->
</Router>
</CascadingMudHtmlToolbarOptions>
```

Child components will inherit the default options, unless they override them with their own `<MudHtmlToolbarOptions>` instance.

### Advanced Customization
For more advanced customization, you can define your own toolbar options inside of an individual `<MudHtmlEditor>` component:

```html
<MudHtmlEditor>
<span class="ql-formats">
<button class="ql-bold" type="button"></button>
<button class="ql-italic" type="button"></button>
<button class="ql-underline" type="button"></button>
<button class="ql-strike" type="button"></button>
</span>
</MudHtmlEditor>
```

See the [QuillJS documentation](https://quilljs.com/docs/modules/toolbar/) for more information on customizing the toolbar.

## Migrating from v1.0 to v2.0
* Remove the `services.AddMudBlazorHtmlEditor();` call from your `Startup.cs` or `Program.cs` file.
* Remove the `<script src="_content/Tizzani.MudBlazor.HtmlEditor/HtmlEditor.js">` tag from the document body. The required JS is now included by default.
2 changes: 1 addition & 1 deletion samples/BlazorWasm/App.razor
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
<p role="alert">Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
</Router>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@inherits MudHtmlToolbarOptions

<CascadingValue Value="this">
@ChildContent
</CascadingValue>

@code {
[Parameter]
public RenderFragment? ChildContent { get; set; }
}
55 changes: 1 addition & 54 deletions src/Tizzani.MudBlazor.HtmlEditor/MudHtmlEditor.razor
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,7 @@
<div @ref="_toolbar">
@if (ChildContent is null)
{
<span class="ql-formats">
<select class="ql-header">
<option value="1">Heading 1</option>
<option value="2">Heading 2</option>
<option value="3">Heading 3</option>
<option value="4">Heading 4</option>
<option value="5">Heading 5</option>
<option value="6">Heading 6</option>
<option value="" selected>Paragraph</option>
</select>
</span>

<span class="ql-formats">
<button class="ql-bold"></button>
<button class="ql-italic"></button>
<button class="ql-underline"></button>
<button class="ql-strike"></button>
</span>

<span class="ql-formats">
<select class="ql-color"></select>
<select class="ql-background"></select>
</span>

<span class="ql-formats">
<button class="ql-script" value="sub"></button>
<button class="ql-script" value="super"></button>
</span>

<span class="ql-formats">
<button class="ql-align" value=""></button>
<button class="ql-align" value="center"></button>
<button class="ql-align" value="right"></button>
<button class="ql-align" value="justify"></button>
</span>

<span class="ql-formats">
<button class="ql-list" value="ordered"></button>
<button class="ql-list" value="bullet"></button>
<button class="ql-indent" value="+1"></button>
<button class="ql-indent" value="-1"></button>
</span>

<span class="ql-formats">
<button class="ql-link"></button>
<button class="ql-image"></button>
<button class="ql-blockquote"></button>
<button class="ql-code-block"></button>
<button class="ql-hr" title="Horizontal Line">
<svg viewBox="0 0 24 24">
<path d="M4 19h16c.55 0 1-.45 1-1v-4c0-.55-.45-1-1-1H4c-.55 0-1 .45-1 1v4c0 .55.45 1 1 1zm0-8h16c.55 0 1-.45 1-1s-.45-1-1-1H4c-.55 0-1 .45-1 1s.45 1 1 1zM3 6c0 .55.45 1 1 1h16c.55 0 1-.45 1-1s-.45-1-1-1H4c-.55 0-1 .45-1 1z" class="ql-fill"></path>
</svg>
</button>
</span>
<MudHtmlToolbarOptions />
}
else
{
Expand Down
117 changes: 117 additions & 0 deletions src/Tizzani.MudBlazor.HtmlEditor/MudHtmlToolbarOptions.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
@if (Show(TypographyPicker))
{
<span class="ql-formats">
<select class="ql-header">
<option value="1">Heading 1</option>
<option value="2">Heading 2</option>
<option value="3">Heading 3</option>
<option value="4">Heading 4</option>
<option value="5">Heading 5</option>
<option value="6">Heading 6</option>
<option value="" selected>Paragraph</option>
</select>
</span>
}

@if (Show(Bold, Italic, Underline, Strike))
{
<span class="ql-formats">
@if (Show(Bold))
{
<button class="ql-bold" type="button"></button>
}
@if (Show(Italic))
{
<button class="ql-italic" type="button"></button>
}
@if (Show(Underline))
{
<button class="ql-underline" type="button"></button>
}
@if (Show(Strike))
{
<button class="ql-strike" type="button"></button>
}
</span>
}

@if (Show(ForegroundColorPicker, BackgroundColorPicker))
{
<span class="ql-formats">
@if (Show(ForegroundColorPicker))
{
<select class="ql-color"></select>
}
@if (Show(BackgroundColorPicker))
{
<select class="ql-background"></select>
}
</span>
}

@if (Show(SubSuperScript))
{
<span class="ql-formats">
<button class="ql-script" value="sub" type="button"></button>
<button class="ql-script" value="super" type="button"></button>
</span>
}

@if (Show(Align))
{
<span class="ql-formats">
<button class="ql-align" type="button" value=""></button>
<button class="ql-align" type="button" value="center"></button>
<button class="ql-align" type="button" value="right"></button>
<button class="ql-align" type="button" value="justify"></button>
</span>
}

@if (Show(OrderedList, UnorderedList, Indent))
{
<span class="ql-formats">
@if (Show(OrderedList))
{
<button class="ql-list" type="button" value="ordered"></button>
}
@if (Show(UnorderedList))
{
<button class="ql-list" type="button" value="bullet"></button>
}
@if (Show(Indent))
{
<button class="ql-indent" type="button" value="+1"></button>
<button class="ql-indent" type="button" value="-1"></button>
}
</span>
}

@if (Show(InsertLink, InsertImage, Blockquote, CodeBlock, HorizontalRule))
{
<span class="ql-formats">
@if (Show(InsertLink))
{
<button class="ql-link" type="button"></button>
}
@if (Show(InsertImage))
{
<button class="ql-image" type="button"></button>
}
@if (Show(Blockquote))
{
<button class="ql-blockquote" type="button"></button>
}
@if (Show(CodeBlock))
{
<button class="ql-code-block" type="button"></button>
}
@if (Show(HorizontalRule))
{
<button class="ql-hr" title="Horizontal Line">
<svg viewBox="0 0 24 24">
<path d="M4 19h16c.55 0 1-.45 1-1v-4c0-.55-.45-1-1-1H4c-.55 0-1 .45-1 1v4c0 .55.45 1 1 1zm0-8h16c.55 0 1-.45 1-1s-.45-1-1-1H4c-.55 0-1 .45-1 1s.45 1 1 1zM3 6c0 .55.45 1 1 1h16c.55 0 1-.45 1-1s-.45-1-1-1H4c-.55 0-1 .45-1 1z" class="ql-fill"></path>
</svg>
</button>
}
</span>
}
140 changes: 140 additions & 0 deletions src/Tizzani.MudBlazor.HtmlEditor/MudHtmlToolbarOptions.razor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
using Microsoft.AspNetCore.Components;

namespace Tizzani.MudBlazor.HtmlEditor;

/// <summary>
/// Defines the toolbar options that are available in the HtmlEditor.
/// </summary>
public partial class MudHtmlToolbarOptions
{
[CascadingParameter]
private MudHtmlToolbarOptions? DefaultOptions { get; set; }

/// <summary>
/// If <see langword="true"/>, the toolbar will include the option to select different headings and paragraph styles.
/// </summary>
[Parameter]
public bool? TypographyPicker { get; set; }

/// <summary>
/// If <see langword="true"/>, the toolbar will include the option to bold text.
/// </summary>
[Parameter]
public bool? Bold { get; set; }

/// <summary>
/// If <see langword="true"/>, the toolbar will include the option to italicize text.
/// </summary>
[Parameter]
public bool? Italic { get; set; }

/// <summary>
/// If <see langword="true"/>, the toolbar will include the option to underline text.
/// </summary>
[Parameter]
public bool? Underline { get; set; }

/// <summary>
/// If <see langword="true"/>, the toolbar will include the option to strike through text.
/// </summary>
[Parameter]
public bool? Strike { get; set; }

/// <summary>
/// If <see langword="true"/>, a color picker for the foreground color will be available in the toolbar.
/// </summary>
[Parameter]
public bool? ForegroundColorPicker { get; set; }

/// <summary>
/// If <see langword="true"/>, a color picker for the background color will be available in the toolbar.
/// </summary>
[Parameter]
public bool? BackgroundColorPicker { get; set; }

/// <summary>
/// If <see langword="true"/>, the toolbar will include the option to insert subscripts and superscripts.
/// </summary>
[Parameter]
public bool? SubSuperScript { get; set; }

/// <summary>
/// If <see langword="true"/>, the toolbar will include the option to align text (e.g., left, center, right, justify).
/// </summary>
[Parameter]
public bool? Align { get; set; }

/// <summary>
/// If <see langword="true"/>, the toolbar will include the option to insert an unordered (numbered) list.
/// </summary>
[Parameter]
public bool? OrderedList { get; set; }

/// <summary>
/// If <see langword="true"/>, the toolbar will include the option to insert an unordered (bulleted) list.
/// </summary>
[Parameter]
public bool? UnorderedList { get; set; }

/// <summary>
/// If <see langword="true"/>, the toolbar will include the option of increasing/decreasing the indent size.
/// </summary>
[Parameter]
public bool? Indent { get; set; }

/// <summary>
/// If <see langword="true"/>, the toolbar will include the option of inserting a hyperlink.
/// </summary>
[Parameter]
public bool? InsertLink { get; set; }

/// <summary>
/// If <see langword="true"/>, the toolbar will include the option of inserting an image.
/// </summary>
[Parameter]
public bool? InsertImage { get; set; }

/// <summary>
/// If <see langword="true"/>, the toolbar will include the option of inserting a blockquote.
/// </summary>
[Parameter]
public bool? Blockquote { get; set; }

/// <summary>
/// If <see langword="true"/>, the toolbar will include the option of inserting a code block.
/// </summary>
[Parameter]
public bool? CodeBlock { get; set; }

/// <summary>
/// If <see langword="true"/>, the toolbar will include the option of inserting a horizontal rule.
/// </summary>
[Parameter]
public bool? HorizontalRule { get; set; }

private static bool Show(params bool?[] options) => options.Any(o => o is not false);

protected override void OnInitialized()
{
if (DefaultOptions != null)
{
TypographyPicker ??= DefaultOptions.TypographyPicker;
Bold ??= DefaultOptions.Bold;
Italic ??= DefaultOptions.Italic;
Underline ??= DefaultOptions.Underline;
Strike ??= DefaultOptions.Strike;
ForegroundColorPicker ??= DefaultOptions.ForegroundColorPicker;
BackgroundColorPicker ??= DefaultOptions.BackgroundColorPicker;
SubSuperScript ??= DefaultOptions.SubSuperScript;
Align ??= DefaultOptions.Align;
OrderedList ??= DefaultOptions.OrderedList;
UnorderedList ??= DefaultOptions.UnorderedList;
Indent ??= DefaultOptions.Indent;
InsertLink ??= DefaultOptions.InsertLink;
InsertImage ??= DefaultOptions.InsertImage;
Blockquote ??= DefaultOptions.Blockquote;
CodeBlock ??= DefaultOptions.CodeBlock;
HorizontalRule ??= DefaultOptions.HorizontalRule;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<RepositoryUrl>https://github.com/erinnmclaughlin/MudBlazor.HtmlEditor</RepositoryUrl>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageReadmeFile>README.md</PackageReadmeFile>
<Version>2.0.0</Version>
<Version>2.1.0</Version>
<Description>A customizable HTML editor component for MudBlazor, powered by QuillJS.</Description>
<Copyright>2024 Erin McLaughlin</Copyright>
<PackageProjectUrl>https://github.com/erinnmclaughlin/MudBlazor.HtmlEditor</PackageProjectUrl>
Expand Down

0 comments on commit 913162f

Please sign in to comment.