1
0
Fork 0
mirror of https://github.com/Nioux/AideDeJeu.git synced 2025-10-29 22:45:44 +00:00
This commit is contained in:
Yan Maniez 2019-06-20 14:14:57 +02:00
parent a52deaa805
commit 968a050d98

View file

@ -64,13 +64,13 @@
view.RenderMarkdown(); view.RenderMarkdown();
} }
private StackLayout stack; private StackLayout _Stack;
private List<KeyValuePair<string, string>> links = new List<KeyValuePair<string, string>>(); private List<KeyValuePair<string, string>> links = new List<KeyValuePair<string, string>>();
private void RenderMarkdown() private void RenderMarkdown()
{ {
stack = new StackLayout() _Stack = new StackLayout()
{ {
Spacing = this.Theme.Margin, Spacing = this.Theme.Margin,
}; };
@ -83,19 +83,27 @@
{ {
var pipeline = new Markdig.MarkdownPipelineBuilder().UseYamlFrontMatter().UsePipeTables().Build(); var pipeline = new Markdig.MarkdownPipelineBuilder().UseYamlFrontMatter().UsePipeTables().Build();
var parsed = Markdig.Markdown.Parse(this.Markdown, pipeline); var parsed = Markdig.Markdown.Parse(this.Markdown, pipeline);
this.Render(parsed.AsEnumerable()); var views = this.Render(parsed.AsEnumerable());
views.ToList().ForEach(view => _Stack.Children.Add(view));
} }
this.Content = stack; this.Content = _Stack;
} }
private void Render(IEnumerable<Block> blocks) private IEnumerable<View> Render(IEnumerable<Block> blocks)
{ {
var views = new List<View>();
foreach (var block in blocks) foreach (var block in blocks)
{ {
this.Render(block); var subviews = this.Render(block);
if(subviews != null)
{
views.AddRange(subviews);
//views.Add(view);
}
} }
return views;
} }
private void AttachLinks(View view) private void AttachLinks(View view)
@ -103,24 +111,21 @@
if (links.Any()) if (links.Any())
{ {
var blockLinks = links.Distinct().OrderBy(l => l.Key).ToList(); var blockLinks = links.Distinct().OrderBy(l => l.Key).ToList();
if (blockLinks.Count > 1) if (blockLinks.Count > 1 && Device.RuntimePlatform == Device.GTK)
{ {
if (Device.RuntimePlatform == Device.GTK) view.GestureRecognizers.Add(new TapGestureRecognizer
{ {
view.GestureRecognizers.Add(new TapGestureRecognizer Command = new Command(async () =>
{ {
Command = new Command(async () => try
{ {
try var result = await Application.Current.MainPage.DisplayActionSheet("Ouvrir le lien", "Annuler", null, blockLinks.Select(x => x.Key).ToArray());
{ var link = blockLinks.FirstOrDefault(x => x.Key == result);
var result = await Application.Current.MainPage.DisplayActionSheet("Ouvrir le lien", "Annuler", null, blockLinks.Select(x => x.Key).ToArray()); NavigateToLinkCommand?.Execute(link.Value);
var link = blockLinks.FirstOrDefault(x => x.Key == result); }
NavigateToLinkCommand?.Execute(link.Value); catch (Exception) { }
} }),
catch (Exception) { } });
}),
});
}
} }
else else
{ {
@ -159,40 +164,42 @@
#region Rendering blocks #region Rendering blocks
private void Render(Block block) private IEnumerable<View> Render(Block block)
{ {
var views = new List<View>();
IEnumerable<View> subviews = null;
switch (block) switch (block)
{ {
case HeadingBlock heading: case HeadingBlock heading:
Render(heading); subviews = Render(heading);
break; break;
case ParagraphBlock paragraph: case ParagraphBlock paragraph:
Render(paragraph); subviews = Render(paragraph);
break; break;
case QuoteBlock quote: case QuoteBlock quote:
Render(quote); subviews = Render(quote);
break; break;
case CodeBlock code: case CodeBlock code:
Render(code); subviews = Render(code);
break; break;
case ListBlock list: case ListBlock list:
Render(list); subviews = Render(list);
break; break;
case ThematicBreakBlock thematicBreak: case ThematicBreakBlock thematicBreak:
Render(thematicBreak); subviews = Render(thematicBreak);
break; break;
case HtmlBlock html: case HtmlBlock html:
Render(html); subviews = Render(html);
break; break;
case Markdig.Extensions.Tables.Table table: case Markdig.Extensions.Tables.Table table:
Render(table); subviews = Render(table);
break; break;
default: default:
@ -200,54 +207,65 @@
break; break;
} }
if(queuedViews.Any()) if (views != null)
{ {
foreach (var view in queuedViews) if (queuedViews.Any())
{ {
this.stack.Children.Add(view); foreach (var view in queuedViews)
{
views.Add(view);
}
queuedViews.Clear();
} }
queuedViews.Clear();
} }
return views;
} }
private int listScope; private int listScope;
private void Render(ThematicBreakBlock block) private IEnumerable<View> Render(ThematicBreakBlock block)
{ {
var style = this.Theme.Separator; var style = this.Theme.Separator;
if (style.BorderSize > 0) if (style.BorderSize > 0)
{ {
stack.Children.Add(new BoxView return new View[] { new BoxView
{ {
HeightRequest = style.BorderSize, HeightRequest = style.BorderSize,
BackgroundColor = style.BorderColor, BackgroundColor = style.BorderColor,
}); } };
} }
return null;
} }
private void Render(ListBlock block) private IEnumerable<View> Render(ListBlock block)
{ {
listScope++; listScope++;
var views = new List<View>();
for (int i = 0; i < block.Count(); i++) for (int i = 0; i < block.Count(); i++)
{ {
var item = block.ElementAt(i); var item = block.ElementAt(i);
if (item is ListItemBlock itemBlock) if (item is ListItemBlock itemBlock)
{ {
this.Render(block, i + 1, itemBlock); var subviews = this.Render(block, i + 1, itemBlock);
if(subviews != null)
{
views.AddRange(subviews);
}
} }
} }
listScope--; listScope--;
return views;
} }
private void Render(ListBlock parent, int index, ListItemBlock block) private IEnumerable<View> Render(ListBlock parent, int index, ListItemBlock block)
{ {
var initialStack = this.stack; //var initialStack = this.stack;
this.stack = new StackLayout() var stack = new StackLayout()
{ {
Spacing = this.Theme.Margin, Spacing = this.Theme.Margin,
}; };
@ -299,13 +317,14 @@
horizontalStack.Children.Add(bullet); horizontalStack.Children.Add(bullet);
} }
horizontalStack.Children.Add(this.stack); horizontalStack.Children.Add(stack);
initialStack.Children.Add(horizontalStack); //initialStack.Children.Add(horizontalStack);
this.stack = initialStack; //this.stack = initialStack;
return new View[] { horizontalStack };
} }
private void Render(HeadingBlock block) private IEnumerable<View> Render(HeadingBlock block)
{ {
MarkdownStyle style; MarkdownStyle style;
@ -349,15 +368,15 @@
HeightRequest = style.BorderSize, HeightRequest = style.BorderSize,
BackgroundColor = style.BorderColor, BackgroundColor = style.BorderColor,
}); });
stack.Children.Add(headingStack); return new View[] { headingStack };
} }
else else
{ {
stack.Children.Add(label); return new View[] { label };
} }
} }
private void Render(ParagraphBlock block) private IEnumerable<View> Render(ParagraphBlock block)
{ {
var style = this.Theme.Paragraph; var style = this.Theme.Paragraph;
var foregroundColor = isQuoted ? this.Theme.Quote.ForegroundColor : style.ForegroundColor; var foregroundColor = isQuoted ? this.Theme.Quote.ForegroundColor : style.ForegroundColor;
@ -366,10 +385,10 @@
FormattedText = CreateFormatted(block.Inline, style.FontFamily, style.Attributes, foregroundColor, style.BackgroundColor, style.FontSize), FormattedText = CreateFormatted(block.Inline, style.FontFamily, style.Attributes, foregroundColor, style.BackgroundColor, style.FontSize),
}; };
AttachLinks(label); AttachLinks(label);
this.stack.Children.Add(label); return new View[] { label };
} }
private void Render(HtmlBlock block) private IEnumerable<View> Render(HtmlBlock block)
{ {
if(block.Type == HtmlBlockType.NonInterruptingBlock || block.Type == HtmlBlockType.Comment) if(block.Type == HtmlBlockType.NonInterruptingBlock || block.Type == HtmlBlockType.Comment)
{ {
@ -380,7 +399,7 @@
{ {
Text = "\n", Text = "\n",
}; };
this.stack.Children.Add(label); return new View[] { label };
} }
else else
@ -393,15 +412,17 @@
// ? // ?
} }
// ? // ?
return null;
} }
private void Render(QuoteBlock block) private IEnumerable<View> Render(QuoteBlock block)
{ {
var initialIsQuoted = this.isQuoted; var initialIsQuoted = this.isQuoted;
var initialStack = this.stack; //var initialStack = this.stack;
var views = new List<View>();
this.isQuoted = true; this.isQuoted = true;
this.stack = new StackLayout() var stack = new StackLayout()
{ {
Spacing = this.Theme.Margin, Spacing = this.Theme.Margin,
}; };
@ -422,23 +443,25 @@
BackgroundColor = style.BorderColor, BackgroundColor = style.BorderColor,
}); });
horizontalStack.Children.Add(this.stack); horizontalStack.Children.Add(stack);
initialStack.Children.Add(horizontalStack); views.Add(horizontalStack);
} }
else else
{ {
stack.BackgroundColor = this.Theme.Quote.BackgroundColor; stack.BackgroundColor = this.Theme.Quote.BackgroundColor;
initialStack.Children.Add(this.stack); views.Add(stack);
} }
this.Render(block.AsEnumerable()); var subviews = this.Render(block.AsEnumerable());
this.isQuoted = initialIsQuoted; this.isQuoted = initialIsQuoted;
this.stack = initialStack; //this.stack = initialStack;
return subviews;
} }
private void Render(CodeBlock block) private IEnumerable<View> Render(CodeBlock block)
{ {
var views = new List<View>();
var style = this.Theme.Code; var style = this.Theme.Code;
var label = new Label var label = new Label
{ {
@ -448,7 +471,7 @@
FontSize = style.FontSize, FontSize = style.FontSize,
Text = string.Join(Environment.NewLine, block.Lines), Text = string.Join(Environment.NewLine, block.Lines),
}; };
stack.Children.Add(new Frame() views.Add(new Frame()
{ {
CornerRadius = 3, CornerRadius = 3,
HasShadow = false, HasShadow = false,
@ -456,9 +479,10 @@
BackgroundColor = style.BackgroundColor, BackgroundColor = style.BackgroundColor,
Content = label Content = label
}); });
return views;
} }
private void Render(Markdig.Extensions.Tables.Table tableBlock) private IEnumerable<View> Render(Markdig.Extensions.Tables.Table tableBlock)
{ {
var scroll = new ScrollView() { HorizontalScrollBarVisibility = ScrollBarVisibility.Default, Orientation = ScrollOrientation.Horizontal }; var scroll = new ScrollView() { HorizontalScrollBarVisibility = ScrollBarVisibility.Default, Orientation = ScrollOrientation.Horizontal };
var grid = new Grid() { HorizontalOptions = LayoutOptions.Start, Margin = 0, Padding = 1, BackgroundColor = Theme.TableHeader.BackgroundColor, RowSpacing = 1, ColumnSpacing = 1 }; var grid = new Grid() { HorizontalOptions = LayoutOptions.Start, Margin = 0, Padding = 1, BackgroundColor = Theme.TableHeader.BackgroundColor, RowSpacing = 1, ColumnSpacing = 1 };
@ -514,8 +538,9 @@
} }
top++; top++;
} }
stack.Children.Add(scroll); //stack.Children.Add(scroll);
scroll.Content = grid; scroll.Content = grid;
return new View[] { scroll };
} }
@ -586,7 +611,7 @@
image.Source = url; image.Source = url;
} }
queuedViews.Add(image); //queuedViews.Add(image);
return new Span[0]; return new Span[0];
} }
else else