-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #142 from gaelj/fix-diagrams-on-startup
Fix diagrams on startup
- Loading branch information
Showing
17 changed files
with
273 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using Microsoft.AspNetCore.Components.Forms; | ||
using System.IO; | ||
|
||
namespace Examples.BlazorServer; | ||
|
||
public class BrowserFileStream(IBrowserFile browserFile) : Stream | ||
{ | ||
private readonly Stream _baseStream = browserFile.OpenReadStream(); | ||
|
||
public override bool CanRead => _baseStream.CanRead; | ||
public override bool CanSeek => _baseStream.CanSeek; | ||
public override bool CanWrite => _baseStream.CanWrite; | ||
public override long Length => _baseStream.Length; | ||
|
||
public override long Position | ||
{ | ||
get => _baseStream.Position; | ||
set => _baseStream.Position = value; | ||
} | ||
|
||
public override void Flush() => _baseStream.Flush(); | ||
|
||
public override int Read(byte[] buffer, int offset, int count) => | ||
_baseStream.Read(buffer, offset, count); | ||
|
||
public override long Seek(long offset, SeekOrigin origin) => | ||
_baseStream.Seek(offset, origin); | ||
|
||
public override void SetLength(long value) => | ||
_baseStream.SetLength(value); | ||
|
||
public override void Write(byte[] buffer, int offset, int count) => | ||
_baseStream.Write(buffer, offset, count); | ||
|
||
protected override void Dispose(bool disposing) | ||
{ | ||
if (disposing) | ||
{ | ||
_baseStream.Dispose(); | ||
} | ||
|
||
base.Dispose(disposing); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using System.Threading.Tasks; | ||
|
||
namespace Examples.BlazorServer.Controllers; | ||
|
||
[ApiController] | ||
[Route("[controller]")] | ||
public class FileUploadController : ControllerBase | ||
{ | ||
[HttpPost("upload")] | ||
public async Task<IActionResult> Upload(IFormFile file) | ||
{ | ||
if (file == null || file.Length == 0) | ||
{ | ||
return BadRequest("Upload a file."); | ||
} | ||
|
||
// Process the file here | ||
// Example: Save the file to the server, database, etc. | ||
|
||
return Ok(new { file.FileName, file.Length }); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using Microsoft.AspNetCore.Components.Forms; | ||
using Microsoft.AspNetCore.Http; | ||
using System.IO; | ||
|
||
namespace Examples.BlazorServer; | ||
|
||
public static class WebFileConverter | ||
{ | ||
public static IFormFile ConvertToFormFile(this IBrowserFile browserFile) | ||
{ | ||
ArgumentNullException.ThrowIfNull(browserFile); | ||
|
||
var fileStream = new BrowserFileStream(browserFile); | ||
|
||
var formFile = new FormFile(fileStream, 0, browserFile.Size, browserFile.Name, browserFile.Name) | ||
{ | ||
Headers = new HeaderDictionary(), | ||
ContentType = browserFile.ContentType | ||
}; | ||
|
||
return formFile; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,131 @@ | ||
@page "/example2" | ||
|
||
<Examples.Common.Example /> | ||
@page "/example2" | ||
|
||
<h1>Code Mirror 6 Wrapper Demo</h1> | ||
|
||
<h4>File upload with IFormFile</h4> | ||
|
||
<CodeMirror6Wrapper | ||
@bind-Doc=@Text | ||
Placeholder="Upload a file by dropping it or pasting it here" | ||
TabSize=@TabSize | ||
IndentationUnit=@TabSize | ||
Theme=@Theme | ||
Language=@Language | ||
AutoFormatMarkdown=@AutoFormatMarkdown | ||
Setup=@Setup | ||
UploadBrowserFile=@UploadBrowserFile | ||
PreviewImages=@true | ||
FullScreen=@FullScreen | ||
LocalStorageKey=@LocalStorageKey | ||
MaxHeight="60em" | ||
> | ||
<ContentBefore Context="c"> | ||
<div class="toolbar sticky-top alert alert-info my-3"> | ||
<button class=@ButtonClass(c.State, "StrongEmphasis") @onclick=@(() => c.Commands.Dispatch(CodeMirrorSimpleCommand.ToggleMarkdownBold)) title="Toggle bold text (Ctrl-B)"> | ||
<i class="fa fa-bold"></i> | ||
</button> | ||
<button class=@ButtonClass(c.State, "Emphasis") @onclick=@(() => c.Commands.Dispatch(CodeMirrorSimpleCommand.ToggleMarkdownItalic)) title="Toggle italic text (Ctrl-I)"> | ||
<i class="fa fa-italic"></i> | ||
</button> | ||
<button class=@ButtonClass(c.State, "Strikethrough") @onclick=@(() => c.Commands.Dispatch(CodeMirrorSimpleCommand.ToggleMarkdownStrikethrough)) title="Toggle strike-through text"> | ||
<i class="fa fa-strikethrough"></i> | ||
</button> | ||
<button class=@ButtonClass(c.State, "InlineCode") @onclick=@(() => c.Commands.Dispatch(CodeMirrorSimpleCommand.ToggleMarkdownCode)) title="Toggle inline code text"> | ||
<i class="fa fa-code"></i> | ||
</button> | ||
<button class=@ButtonClass(c.State, "FencedCode") @onclick=@(() => c.Commands.Dispatch(CodeMirrorSimpleCommand.ToggleMarkdownCodeBlock)) title="Toggle code block text"> | ||
<i class="fab fa-codepen"></i> | ||
</button> | ||
<button class=@ButtonClass(c.State, "Blockquote") @onclick=@(() => c.Commands.Dispatch(CodeMirrorSimpleCommand.ToggleMarkdownQuote)) title="Toggle quoted text"> | ||
<i class="fa fa-quote-right"></i> | ||
</button> | ||
<button class=@ButtonClass(c.State) @onclick=@(() => c.Commands.Dispatch(CodeMirrorSimpleCommand.IncreaseMarkdownHeadingLevel)) title="Increase Markdown heading level"> | ||
<i class="fa fa-heading"></i> + | ||
</button> | ||
<button class=@ButtonClass(c.State) @onclick=@(() => c.Commands.Dispatch(CodeMirrorSimpleCommand.DecreaseMarkdownHeadingLevel)) title="Decrease Markdown heading level"> | ||
<i class="fa fa-heading"></i> - | ||
</button> | ||
@for (var i = 1; i <= 6; i++) { | ||
var headingLevel = i; // Capture the current value of i in a local variable | ||
<button class=@ButtonClass(c.State, $"ATXHeading{headingLevel}") @onclick=@(() => c.Commands.Dispatch(CodeMirrorCommandOneParameter.ToggleMarkdownHeading, headingLevel)) title=@($"Toggle heading text {i}")> | ||
<i class="fa fa-heading"></i> @($"{headingLevel}") | ||
</button> | ||
} | ||
<button class=@ButtonClass(c.State, "BulletList") @onclick=@(() => c.Commands.Dispatch(CodeMirrorSimpleCommand.ToggleMarkdownUnorderedList)) title="Toggle unordered list"> | ||
<i class="fa fa-list-ul"></i> | ||
</button> | ||
<button class=@ButtonClass(c.State, "OrderedList") @onclick=@(() => c.Commands.Dispatch(CodeMirrorSimpleCommand.ToggleMarkdownOrderedList)) title="Toggle ordered list"> | ||
<i class="fa fa-list-ol"></i> | ||
</button> | ||
<button class=@ButtonClass(c.State, "Task") @onclick=@(() => c.Commands.Dispatch(CodeMirrorSimpleCommand.ToggleMarkdownTaskList)) title="Toggle task list"> | ||
<i class="fa fa-tasks"></i> | ||
</button> | ||
<button class=@ButtonClass(c.State) @onclick=@(() => c.Commands.Dispatch(CodeMirrorCommandOneParameter.InsertOrReplaceText, "test")) title="Insert or replace text"> | ||
Insert or replace text | ||
</button> | ||
<button class=@ButtonClass(c.State, "HorizontalRule") @onclick=@(() => c.Commands.Dispatch(CodeMirrorCommandOneParameter.InsertTextAbove, "\n---\n")) title="Insert separator above"> | ||
<i class="fas fa-minus"></i> | ||
</button> | ||
<button class=@ButtonClass(LineWrapping) @onclick=@(() => LineWrapping = !LineWrapping) title="Toggle long line wrapping"> | ||
<i class="fas fa-paragraph"></i> | ||
</button> | ||
<button class=@ButtonClass(c.State) @onclick=@(() => c.Commands.Dispatch(CodeMirrorSimpleCommand.Undo)) title="Undo (Ctrl-Z)"> | ||
<i class="fas fa-undo"></i> | ||
</button> | ||
<button class=@ButtonClass(c.State) @onclick=@(() => c.Commands.Dispatch(CodeMirrorSimpleCommand.Redo)) title="Redo (Ctrl-Y)"> | ||
<i class="fas fa-redo"></i> | ||
</button> | ||
<button class=@ButtonClass(c.State) @onclick=@(() => c.Commands.Dispatch(CodeMirrorSimpleCommand.Cut)) title="Cut (Ctrl-X)"> | ||
<i class="fas fa-scissors"></i> | ||
</button> | ||
<button class=@ButtonClass(c.State) @onclick=@(() => c.Commands.Dispatch(CodeMirrorSimpleCommand.Copy)) title="Copy (Ctrl-C)"> | ||
<i class="fas fa-copy"></i> | ||
</button> | ||
<button class=@ButtonClass(c.State) @onclick=@(() => c.Commands.Dispatch(CodeMirrorSimpleCommand.Paste)) title="Paste (Ctrl-V)"> | ||
<i class="fas fa-paste"></i> | ||
</button> | ||
<button class=@ButtonClass(c.State) @onclick=@(() => c.Commands.Dispatch(CodeMirrorSimpleCommand.ClearLocalStorage)) title="Clear local storage"> | ||
<i class="fas fa-trash"></i> | ||
</button> | ||
<button class=@ButtonClass(FullScreen) @onclick=@(() => FullScreen = !FullScreen) title="Toggle full-screen mode"> | ||
<i class=@(FullScreen ? "fas fa-window-restore" : "fas fa-window-maximize")></i> | ||
</button> | ||
</div> | ||
</ContentBefore> | ||
</CodeMirror6Wrapper> | ||
|
||
|
||
@code | ||
{ | ||
private string? Text = string.Empty; | ||
private int TabSize = 4; | ||
private ThemeMirrorTheme Theme = ThemeMirrorTheme.OneDark; | ||
private CodeMirrorLanguage Language = CodeMirrorLanguage.Markdown; | ||
private bool AutoFormatMarkdown = true; | ||
private bool LineWrapping = true; | ||
private bool FullScreen = false; | ||
private string LocalStorageKey = "CodeMirror6WrapperDemo_FileUpload"; | ||
private readonly CodeMirrorSetup Setup = new() { | ||
HighlightSelectionMatches = true, | ||
ScrollToEnd = true, | ||
BindMode = DocumentBindMode.OnDelayedInput, | ||
}; | ||
private string ButtonClass(CodeMirrorState state, string docStyleTag = "") => ButtonClass(state.MarkdownStylesAtSelections?.Contains(docStyleTag) == true); | ||
private string ButtonClass(bool enabled) => enabled | ||
? "btn btn-sm btn-primary" | ||
: "btn btn-sm btn-outline-secondary"; | ||
|
||
private async Task<string> UploadBrowserFile(IBrowserFile file) | ||
{ | ||
var formFile = file.ConvertToFormFile(); | ||
|
||
var content = new MultipartFormDataContent(); | ||
var fileContent = new StreamContent(formFile.OpenReadStream()); | ||
content.Add(fileContent, "\"file\"", file.Name); | ||
|
||
using var httpClient = new HttpClient(); | ||
var response = await httpClient.PostAsync("http://localhost:5188/FileUpload/upload", content); // Adjust the URL as needed | ||
return response.Content.ReadAsStringAsync().Result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.sticky-top { | ||
padding-top: 10px; | ||
} | ||
|
||
.cm-full-screen .sticky-top { | ||
margin: 0px !important; | ||
padding: 0px !important; | ||
} |
Oops, something went wrong.