Skip to content

Commit 716ddf3

Browse files
authored
Merge pull request #77 from aspnet/taparik/InputLargeTextArea
Input Large Text Area Sample
2 parents be8ebb6 + 96229c4 commit 716ddf3

24 files changed

+752
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.31612.314
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InputLargeTextArea", "src\InputLargeTextArea\InputLargeTextArea.csproj", "{D3C10569-1320-4C66-B76B-FC02BDC9010F}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleServerApp", "SampleServerApp\SampleServerApp.csproj", "{8162C3A5-8054-4647-B597-64A01027EC24}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{D3C10569-1320-4C66-B76B-FC02BDC9010F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{D3C10569-1320-4C66-B76B-FC02BDC9010F}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{D3C10569-1320-4C66-B76B-FC02BDC9010F}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{D3C10569-1320-4C66-B76B-FC02BDC9010F}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{8162C3A5-8054-4647-B597-64A01027EC24}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{8162C3A5-8054-4647-B597-64A01027EC24}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{8162C3A5-8054-4647-B597-64A01027EC24}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{8162C3A5-8054-4647-B597-64A01027EC24}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {130EF2A6-BAD0-430B-9B31-D35406A46F67}
30+
EndGlobalSection
31+
EndGlobal
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
## Blazor `InputLargeTextArea` Component Sample
2+
A multiline input component for Blazor Server to enable editing large string values. Supports async content access without binding and without validations.
3+
4+
### Example:
5+
```csharp
6+
<InputLargeTextArea id="largeTextArea" @ref="TextArea" OnChange="TextAreaChanged" />
7+
8+
9+
@code {
10+
InputLargeTextArea? TextArea;
11+
12+
public async Task GetTextAsync()
13+
{
14+
var streamReader = await TextArea!.GetTextAsync(maxLength: 50_000);
15+
var textFromInputLargeTextArea = await streamReader.ReadToEndAsync();
16+
}
17+
18+
public async Task SetTextAsync()
19+
{
20+
var textToWrite = new string('c', 50_000);
21+
22+
var memoryStream = new MemoryStream();
23+
var streamWriter = new StreamWriter(memoryStream);
24+
await streamWriter.WriteAsync(textToWrite);
25+
await streamWriter.FlushAsync();
26+
await TextArea!.SetTextAsync(streamWriter);
27+
}
28+
29+
public void TextAreaChanged(InputLargeTextAreaChangeEventArgs args)
30+
{
31+
LastChangedLength = args.Length;
32+
}
33+
}
34+
```
35+
36+
## Why?
37+
Using Blazor Server's `InputTextArea` with large (ex. 20K chars) amounts of text can lead to a degraded user experience due to the constant round-trip communication to/from the server to enable binding and validations. This component provides an asynchronous ability to get & set the text area content. This approach **is not optimal** due to the additional complexity working with `StreamReader`/`StreamWriter` APIs, as well as the (large) amount of memory allocations which may occur when encoding/decoding the `UTF-8` `string`/`textarea` content into `byte`s. Due to these concerns, we've made this available as a sample instead of adding it to the core framework.
38+
39+
Note: If you're encountering slowdowns specifically in complex components or Blazor WebAssembly, we recommend reviewing the [Blazor WebAssembly Performance Best Practices](https://docs.microsoft.com/en-us/aspnet/core/blazor/webassembly-performance-best-practices?view=aspnetcore-6.0#avoid-rerendering-after-handling-events-without-state-changes) which detail rendering optimizations.
40+
41+
## Setup
42+
1. Add a package reference to `InputLargeTextArea`.
43+
2. Add `<script src="_content/InputLargeTextArea/js/InputLargeTextArea.js"></script>` to your `_Layout.cshtml` on Blazor Server.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Router AppAssembly="@typeof(App).Assembly">
2+
<Found Context="routeData">
3+
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
4+
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
5+
</Found>
6+
<NotFound>
7+
<PageTitle>Not found</PageTitle>
8+
<LayoutView Layout="@typeof(MainLayout)">
9+
<p role="alert">Sorry, there's nothing at this address.</p>
10+
</LayoutView>
11+
</NotFound>
12+
</Router>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
@page "/"
2+
@using Microsoft.AspNetCore.Components.Forms
3+
4+
<PageTitle>Input Large Text Area</PageTitle>
5+
6+
<InputLargeTextArea id="largeTextArea" @ref="TextArea" OnChange="TextAreaChanged" />
7+
8+
<br />
9+
10+
<button id="setTextBtn" @onclick="SetTextAsync">SetTextAsync</button>
11+
<button id="getTextBtn" @onclick="GetTextAsync">GetTextAsync</button>
12+
13+
<hr />
14+
15+
<h3>Last Changed:</h3>
16+
Length: <p id="lastChangedLength">@LastChangedLength</p>
17+
18+
<h3>Get Text Result:</h3>
19+
<p id="getTextResult">@GetTextResult</p>
20+
<p id="getTextError">@GetTextError</p>
21+
22+
23+
@code {
24+
public long LastChangedLength { get; set; }
25+
public string GetTextResult { get; set; } = string.Empty;
26+
public string GetTextError { get; set; } = string.Empty;
27+
28+
InputLargeTextArea? TextArea;
29+
30+
public async Task GetTextAsync()
31+
{
32+
var streamReader = await TextArea!.GetTextAsync(maxLength: 50_000);
33+
GetTextResult = await streamReader.ReadToEndAsync();
34+
StateHasChanged();
35+
}
36+
37+
public async Task SetTextAsync()
38+
{
39+
var memoryStream = new MemoryStream();
40+
var streamWriter = new StreamWriter(memoryStream);
41+
await streamWriter.WriteAsync(new string('c', 50_000));
42+
await streamWriter.FlushAsync();
43+
await TextArea!.SetTextAsync(streamWriter);
44+
}
45+
46+
public void TextAreaChanged(InputLargeTextAreaChangeEventArgs args)
47+
{
48+
LastChangedLength = args.Length;
49+
}
50+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@page "/"
2+
@namespace SampleServerApp.Pages
3+
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
4+
@{
5+
Layout = "_Layout";
6+
}
7+
8+
<component type="typeof(App)" render-mode="ServerPrerendered" />
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
@using Microsoft.AspNetCore.Components.Web
2+
@namespace SampleServerApp.Pages
3+
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
4+
5+
<!DOCTYPE html>
6+
<html lang="en">
7+
<head>
8+
<meta charset="utf-8" />
9+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
10+
<base href="~/" />
11+
<link rel="stylesheet" href="css/bootstrap/bootstrap.min.css" />
12+
<link href="css/site.css" rel="stylesheet" />
13+
<link href="SampleServerApp.styles.css" rel="stylesheet" />
14+
<component type="typeof(HeadOutlet)" render-mode="ServerPrerendered" />
15+
</head>
16+
<body>
17+
@RenderBody()
18+
19+
<div id="blazor-error-ui">
20+
<environment include="Staging,Production">
21+
An error has occurred. This application may no longer respond until reloaded.
22+
</environment>
23+
<environment include="Development">
24+
An unhandled exception has occurred. See browser dev tools for details.
25+
</environment>
26+
<a href="" class="reload">Reload</a>
27+
<a class="dismiss">🗙</a>
28+
</div>
29+
30+
<script src="_content/InputLargeTextArea/js/InputLargeTextArea.js"></script>
31+
<script src="_framework/blazor.server.js"></script>
32+
</body>
33+
</html>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Microsoft.AspNetCore.Components;
2+
using Microsoft.AspNetCore.Components.Web;
3+
4+
var builder = WebApplication.CreateBuilder(args);
5+
6+
// Add services to the container.
7+
builder.Services.AddRazorPages();
8+
builder.Services.AddServerSideBlazor();
9+
10+
var app = builder.Build();
11+
12+
// Configure the HTTP request pipeline.
13+
if (!app.Environment.IsDevelopment())
14+
{
15+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
16+
app.UseHsts();
17+
}
18+
19+
app.UseHttpsRedirection();
20+
21+
app.UseStaticFiles();
22+
23+
app.UseRouting();
24+
25+
26+
app.MapBlazorHub();
27+
app.MapFallbackToPage("/_Host");
28+
29+
app.Run();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<ProjectReference Include="..\src\InputLargeTextArea\InputLargeTextArea.csproj" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
@inherits LayoutComponentBase
2+
3+
<PageTitle>SampleServerApp</PageTitle>
4+
5+
<div class="page">
6+
<div class="sidebar">
7+
<NavMenu />
8+
</div>
9+
10+
<main>
11+
<div class="top-row px-4">
12+
<a href="https://docs.microsoft.com/aspnet/" target="_blank">About</a>
13+
</div>
14+
15+
<article class="content px-4">
16+
@Body
17+
</article>
18+
</main>
19+
</div>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
.page {
2+
position: relative;
3+
display: flex;
4+
flex-direction: column;
5+
}
6+
7+
main {
8+
flex: 1;
9+
}
10+
11+
.sidebar {
12+
background-image: linear-gradient(180deg, rgb(5, 39, 103) 0%, #3a0647 70%);
13+
}
14+
15+
.top-row {
16+
background-color: #f7f7f7;
17+
border-bottom: 1px solid #d6d5d5;
18+
justify-content: flex-end;
19+
height: 3.5rem;
20+
display: flex;
21+
align-items: center;
22+
}
23+
24+
.top-row ::deep a, .top-row .btn-link {
25+
white-space: nowrap;
26+
margin-left: 1.5rem;
27+
}
28+
29+
.top-row a:first-child {
30+
overflow: hidden;
31+
text-overflow: ellipsis;
32+
}
33+
34+
@media (max-width: 640.98px) {
35+
.top-row:not(.auth) {
36+
display: none;
37+
}
38+
39+
.top-row.auth {
40+
justify-content: space-between;
41+
}
42+
43+
.top-row a, .top-row .btn-link {
44+
margin-left: 0;
45+
}
46+
}
47+
48+
@media (min-width: 641px) {
49+
.page {
50+
flex-direction: row;
51+
}
52+
53+
.sidebar {
54+
width: 250px;
55+
height: 100vh;
56+
position: sticky;
57+
top: 0;
58+
}
59+
60+
.top-row {
61+
position: sticky;
62+
top: 0;
63+
z-index: 1;
64+
}
65+
66+
.top-row, article {
67+
padding-left: 2rem !important;
68+
padding-right: 1.5rem !important;
69+
}
70+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<div class="top-row ps-3 navbar navbar-dark">
2+
<div class="container-fluid">
3+
<a class="navbar-brand" href="">SampleServerApp</a>
4+
<button title="Navigation menu" class="navbar-toggler" @onclick="ToggleNavMenu">
5+
<span class="navbar-toggler-icon"></span>
6+
</button>
7+
</div>
8+
</div>
9+
10+
<div class="@NavMenuCssClass" @onclick="ToggleNavMenu">
11+
<nav class="flex-column">
12+
<div class="nav-item px-3">
13+
<NavLink class="nav-link" href="" Match="NavLinkMatch.All">
14+
Home
15+
</NavLink>
16+
</div>
17+
</nav>
18+
</div>
19+
20+
@code {
21+
private bool collapseNavMenu = true;
22+
23+
private string? NavMenuCssClass => collapseNavMenu ? "collapse" : null;
24+
25+
private void ToggleNavMenu()
26+
{
27+
collapseNavMenu = !collapseNavMenu;
28+
}
29+
}

0 commit comments

Comments
 (0)