Skip to content

Commit

Permalink
Example code for ItemsRepeater
Browse files Browse the repository at this point in the history
  • Loading branch information
NotYoojun committed Jan 23, 2025
1 parent 2c17f12 commit 0c1913c
Show file tree
Hide file tree
Showing 4 changed files with 676 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using ICSharpCode.AvalonEdit;
using ICSharpCode.AvalonEdit.Editing;
using ICSharpCode.AvalonEdit.Highlighting;
using iNKORE.UI.WPF.Modern.Gallery.Helpers;
using SamplesCommon;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -158,32 +159,13 @@ private void FormatAndRenderSampleFromString(string sampleString, TextEditor pre
if (presenter.SyntaxHighlighting != highlighter)
presenter.SyntaxHighlighting = highlighter;


if (sampleString != presenter.Text)
presenter.Text = RemoveLeadingAndTrailingEmptyLines(sampleString);
var formattedText = StringHelper.RemoveLeadingAndTrailingEmptyLines(sampleString);
if (formattedText != presenter.Text)
presenter.Text = formattedText;

presenter.Visibility = Visibility.Visible;
}

static string RemoveLeadingAndTrailingEmptyLines(string text)
{
if (string.IsNullOrWhiteSpace(text))
return string.Empty;

var lines = text.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
int start = 0;
while (start < lines.Length && string.IsNullOrWhiteSpace(lines[start]))
start++;

int end = lines.Length - 1;
while (end >= start && string.IsNullOrWhiteSpace(lines[end]))
end--;

var trimmedLines = lines.Skip(start).Take(end - start + 1);
return string.Join(Environment.NewLine, trimmedLines);
}


private void CopyCodeButton_Click(object sender, RoutedEventArgs e)
{
try
Expand Down
42 changes: 42 additions & 0 deletions source/iNKORE.UI.WPF.Modern.Gallery/Helpers/StringHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace iNKORE.UI.WPF.Modern.Gallery.Helpers
{
public static class StringHelper
{
public static string fIndent(this string text, double indent, bool doFirstLine = false)
{
string[] lines = RemoveLeadingAndTrailingEmptyLines(text).Split('\n');
string indentStr = new string(' ', (int)(indent * 4));
for (int i = 0; i < lines.Length; i++)
{
if (i == 0 && !doFirstLine) continue;
lines[i] = indentStr + lines[i];
}
return string.Join('\n', lines);
}

public static string RemoveLeadingAndTrailingEmptyLines(this string text)
{
if (string.IsNullOrWhiteSpace(text))
return string.Empty;

var lines = text.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
int start = 0;
while (start < lines.Length && string.IsNullOrWhiteSpace(lines[start]))
start++;

int end = lines.Length - 1;
while (end >= start && string.IsNullOrWhiteSpace(lines[end]))
end--;

var trimmedLines = lines.Skip(start).Take(end - start + 1);
return string.Join(Environment.NewLine, trimmedLines);
}

}
}
Loading

0 comments on commit 0c1913c

Please sign in to comment.