1
0
Fork 0
mirror of https://github.com/Nioux/AideDeJeu.git synced 2025-10-30 15:06:06 +00:00

Bookmarks en markdown

This commit is contained in:
Yan Maniez 2018-08-30 23:32:35 +02:00
parent ef5df06d02
commit 6413830117
7 changed files with 120 additions and 64 deletions

View file

@ -9,6 +9,14 @@ namespace AideDeJeuLib
{ {
public class LinkItem : Item public class LinkItem : Item
{ {
public override string Markdown
{
get
{
return $"\n\n<!--LinkItem-->\n\n{new string('#', NameLevel + 1)} {NameLink}\n\n<!--/LinkItem-->\n\n";
}
set => base.Markdown = value;
}
public string Link { get; set; } public string Link { get; set; }
public string NameLink public string NameLink
{ {
@ -16,7 +24,7 @@ namespace AideDeJeuLib
{ {
if (Name != null && Link != null) if (Name != null && Link != null)
{ {
return $"[{Name}]({Link})"; return $"<!--NameLink-->[{Name}]({Link})<!--/NameLink-->";
} }
return null; return null;
} }

View file

@ -87,23 +87,26 @@ namespace AideDeJeu.Tools
public static string IdFromName(string name) public static string IdFromName(string name)
{ {
string id = string.Empty; string id = string.Empty;
foreach(var c in name) if (name != null)
{ {
if(c >= 'A' && c <= 'Z') foreach (var c in name)
{ {
id += c.ToString().ToLower(); if (c >= 'A' && c <= 'Z')
} {
else if(c == ' ') id += c.ToString().ToLower();
{ }
id += '-'; else if (c == ' ')
} {
else if(c== '\'' || c == '/' || c == '(' || c ==')' || c == ':' || c == '+' || c == ',') id += '-';
{ }
// vide else if (c == '\'' || c == '/' || c == '(' || c == ')' || c == ':' || c == '+' || c == ',')
} {
else // vide
{ }
id += c; else
{
id += c;
}
} }
} }
return id; return id;

View file

@ -3,6 +3,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.IO; using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json; using System.Runtime.Serialization.Json;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -93,48 +94,73 @@ namespace AideDeJeu.ViewModels
public string ToString(List<Item> items) public string ToString(List<Item> items)
{ {
var serializer = ItemJsonSerializer; string md = string.Empty;
using(var stream = new MemoryStream()) md += "\n<!--Items-->\n\n";
foreach(var item in items)
{ {
serializer.WriteObject(stream, items); md += item.Markdown;
stream.Seek(0, SeekOrigin.Begin); }
using (StreamReader reader = new StreamReader(stream)) md += "\n\n<!--/Items-->\n";
return md;
}
public List<Item> ToItems(string md)
{
var item = Store.ToItem("", md);
if(item is Items)
{
var items = item as Items;
return items.ToList();
}
return new List<Item> { item };
}
/*
public string ToString(List<Item> items)
{
var serializer = ItemJsonSerializer;
using(var stream = new MemoryStream())
{ {
return reader.ReadToEnd(); serializer.WriteObject(stream, items);
stream.Seek(0, SeekOrigin.Begin);
using (StreamReader reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
} }
} }
}
public List<Item> ToItems(string str) public List<Item> ToItems(string str)
{
var serializer = ItemJsonSerializer;
byte[] byteArray = Encoding.UTF8.GetBytes(str);
using (var stream = new MemoryStream(byteArray))
{ {
return serializer.ReadObject(stream) as List<Item>; var serializer = ItemJsonSerializer;
} byte[] byteArray = Encoding.UTF8.GetBytes(str);
} using (var stream = new MemoryStream(byteArray))
public DataContractJsonSerializer ItemJsonSerializer
{
get
{
var settings = new DataContractJsonSerializerSettings();
settings.KnownTypes = new List<Type>()
{ {
typeof(HomeItem), return serializer.ReadObject(stream) as List<Item>;
typeof(Spell), }
typeof(Monster),
//typeof(Items),
typeof(LinkItem),
typeof(Equipment),
//typeof(Spells),
//typeof(Monsters),
//typeof(Equipments),
typeof(PageItem),
};
return new DataContractJsonSerializer(typeof(List<Item>), settings);
} }
public DataContractJsonSerializer ItemJsonSerializer
{
get
{
var settings = new DataContractJsonSerializerSettings();
settings.KnownTypes = new List<Type>()
{
typeof(HomeItem),
typeof(Spell),
typeof(Monster),
//typeof(Items),
typeof(LinkItem),
typeof(Equipment),
//typeof(Spells),
//typeof(Monsters),
//typeof(Equipments),
typeof(PageItem),
};
return new DataContractJsonSerializer(typeof(List<Item>), settings);
}
}
*/
} }
} }
}

View file

@ -140,7 +140,7 @@ namespace AideDeJeu.ViewModels
{ {
if (s != null) if (s != null)
{ {
var regex = new Regex("/(?<file>.*?)(_with_(?<with>.*))?\\.md(#(?<anchor>.*))?"); var regex = new Regex("/?(?<file>.*?)(_with_(?<with>.*))?\\.md(#(?<anchor>.*))?");
var match = regex.Match(s); var match = regex.Match(s);
var file = match.Groups["file"].Value; var file = match.Groups["file"].Value;
var anchor = match.Groups["anchor"].Value; var anchor = match.Groups["anchor"].Value;
@ -181,6 +181,10 @@ namespace AideDeJeu.ViewModels
{ {
await GotoItemDetailPageAsync(item); await GotoItemDetailPageAsync(item);
} }
var tabbedPage = App.Current.MainPage as MainTabbedPage;
tabbedPage.SelectedItem = null;
tabbedPage.SelectedItem = tabbedPage.MainNavigationPage;
} }
else else
{ {

View file

@ -64,7 +64,10 @@ namespace AideDeJeu.ViewModels
if (IsClosingItem(block)) if (IsClosingItem(block))
{ {
currentItem.Id = GetNewAnchorId(source, currentItem.Name); currentItem.Id = GetNewAnchorId(source, currentItem.Name);
_AllItems[currentItem.Id] = currentItem; if (currentItem.Id != null)
{
_AllItems[currentItem.Id] = currentItem;
}
return currentItem; return currentItem;
} }
else if (IsNewItem(block)) else if (IsNewItem(block))
@ -227,18 +230,22 @@ namespace AideDeJeu.ViewModels
public string GetNewAnchorId(string source, string name) public string GetNewAnchorId(string source, string name)
{ {
var baseid = Helpers.IdFromName(name); if (name != null)
var id = $"{source}.md#{baseid}";
int index = 0;
while (true)
{ {
if (!_AllItems.ContainsKey(name)) var baseid = Helpers.IdFromName(name);
var id = $"{source}.md#{baseid}";
int index = 0;
while (true)
{ {
return id; if (!_AllItems.ContainsKey(name))
{
return id;
}
index++;
name = $"{source}.md#{baseid}{index}";
} }
index++;
name = $"{source}.md#{baseid}{index}";
} }
return null;
} }
/* /*
void AddAnchor(string source, Dictionary<string, Item> anchors, Item item) void AddAnchor(string source, Dictionary<string, Item> anchors, Item item)

View file

@ -20,13 +20,13 @@
<Label Grid.Column="0" Grid.Row="1" HorizontalOptions="Center" VerticalOptions="Center" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" Margin="15,0,15,0" Style="{StaticResource subsubsection}" Text="Cette liste est vide, ajoutez des éléments à partir de la bibliothèque !" IsVisible="{Binding BookmarkCollection.Count, Converter={StaticResource IntToBooleanConverter}}" /> <Label Grid.Column="0" Grid.Row="1" HorizontalOptions="Center" VerticalOptions="Center" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" Margin="15,0,15,0" Style="{StaticResource subsubsection}" Text="Cette liste est vide, ajoutez des éléments à partir de la bibliothèque !" IsVisible="{Binding BookmarkCollection.Count, Converter={StaticResource IntToBooleanConverter}}" />
<ListView Grid.Column="0" Grid.Row="1" x:Name="ItemsListView" ItemsSource="{Binding BookmarkCollection}" VerticalOptions="FillAndExpand" HasUnevenRows="true" CachingStrategy="RecycleElement" SelectedItem="{Binding SelectedItem}"> <ListView Grid.Column="0" Grid.Row="1" x:Name="ItemsListView" ItemsSource="{Binding BookmarkCollection}" VerticalOptions="FillAndExpand" HasUnevenRows="true" CachingStrategy="RecycleElement" SelectedItem="{Binding SelectedItem}" ItemTapped="ItemsListView_ItemTapped">
<ListView.ItemTemplate> <ListView.ItemTemplate>
<DataTemplate> <DataTemplate>
<ViewCell AutomationProperties.IsInAccessibleTree="True" AutomationId="machin" AutomationProperties.Name="hop"> <ViewCell AutomationProperties.IsInAccessibleTree="True" AutomationId="machin" AutomationProperties.Name="hop">
<StackLayout Padding="10" Orientation="Vertical"> <StackLayout Padding="10" Orientation="Vertical">
<Label Text="{Binding Name}" LineBreakMode="WordWrap" Style="{DynamicResource subsubsection}" FontSize="16" /> <Label Text="{Binding Name}" LineBreakMode="WordWrap" Style="{DynamicResource subsubsection}" FontSize="16" />
<Label Text="{Binding Id}" LineBreakMode="WordWrap" Style="{DynamicResource subsubsection}" FontSize="12" /> <Label Text="{Binding Link}" LineBreakMode="WordWrap" Style="{DynamicResource subsubsection}" FontSize="12" />
</StackLayout> </StackLayout>
</ViewCell> </ViewCell>
</DataTemplate> </DataTemplate>

View file

@ -1,4 +1,5 @@
using AideDeJeu.ViewModels; using AideDeJeu.ViewModels;
using AideDeJeuLib;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -19,5 +20,12 @@ namespace AideDeJeu.Views
BindingContext = DependencyService.Get<BookmarksViewModel>(); BindingContext = DependencyService.Get<BookmarksViewModel>();
} }
}
private async void ItemsListView_ItemTapped(object sender, ItemTappedEventArgs e)
{
var item = e.Item as LinkItem;
var Main = DependencyService.Get<MainViewModel>();
await Main.Navigator.NavigateToLinkAsync(item.Link);
}
}
} }