Skip to content

Commit

Permalink
Add more options to CodeViewer
Browse files Browse the repository at this point in the history
  • Loading branch information
Nix1983 committed Dec 14, 2023
1 parent 8ce72bf commit cdb829e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 10 deletions.
69 changes: 60 additions & 9 deletions Components/Code/CodeViewer.razor
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,35 @@
{
<div class="row">
<div class="col-11">
<pre>
<code class="language-@(Language.ToString().ToLower())">
@Code
</code>
</pre>
@if (!ShowFullCode)
{
@if (!_expandCode)
{
<pre>
<code class="language-@(Language.ToString().ToLower())">@GetLimitedCode()</code>
</pre>
}
else
{
<pre>
<code class="language-@(Language.ToString().ToLower())">@Code</code>
</pre>
}
@if (!ShowFullCode && _linesCount > MaxCodeLines)
{
<div @onclick="ToggleFullCode" class="cursor-pointer m-2">
<Icon IconType="TablerIcons.Dots" />
</div>
}

}
else
{
<pre>
<code class="language-@(Language.ToString().ToLower())">@GetLimitedCode()</code>
</pre>
}

</div>
@if (ShowCopyButton)
{
Expand All @@ -37,10 +61,8 @@
}
</div>
}

</div>


@code {
[Parameter]
public string Code { get; set; } = string.Empty;
Expand All @@ -66,19 +88,47 @@
[Parameter]
public int PlaceHolderRows { get; set; } = 1;

[Parameter]
public bool ShowFullCode { get; set; } = false;

[Parameter]
public int MaxCodeLines { get; set; } = 10;

private bool _isClicked = false;
private bool _isInit = false;
private int _linesCount;
private bool _expandCode = false;

protected override Task OnInitializedAsync()
{
_linesCount = Code.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).Length;
return base.OnInitializedAsync();
}

protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await JSRuntime.InvokeVoidAsync("highlightCode");
_isInit = true;
await InvokeAsync(StateHasChanged);
await JSRuntime.InvokeVoidAsync("Prism.highlightAll");
}
}

private string GetLimitedCode()
{
var lines = Code.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
return string.Join(Environment.NewLine, lines.Take(MaxCodeLines));
}

private async Task ToggleFullCode()
{

await JSRuntime.InvokeVoidAsync("highlightCode");
_expandCode = !_expandCode;

}

private async Task CopyToClipboard(string code)
{
await tabService.CopyToClipboard(code);
Expand Down Expand Up @@ -106,7 +156,8 @@
await InvokeAsync(StateHasChanged);
}
}

}

}


8 changes: 8 additions & 0 deletions wwwroot/js/tablerForNet.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,5 +193,13 @@
}
},
}
function highlightCode() {
var checkExist = setInterval(function () {
if (document.querySelector("pre code")) {
clearInterval(checkExist);
Prism.highlightAll();
}
}, 100);
}


Loading

0 comments on commit cdb829e

Please sign in to comment.