Пример #1
0
 public InlineImage(InlineImageCache cache = null)
 {
     _cache = cache;
 }
Пример #2
0
        private void Parse(Span span)
        {
            var context = new ParseContext(span);

            while (true)
            {
                var token = La(1);
                Consume();

                switch (token.TokenType)
                {
                case BbCodeLexer.TokenStartTag:
                    ParseTag(token.Value, true, context);
                    break;

                case BbCodeLexer.TokenEndTag:
                    ParseTag(token.Value, false, context);
                    break;

                case BbCodeLexer.TokenText:
                    var parent = span;

                    // parse uri value for optional parameter and/or target, eg [url=cmd://foo|parameter|target]
                    if (NavigationHelper.TryParseUriWithParameters(context.NavigateUri, out var parsedUri, out var parsedParameter,
                                                                   out var parsedTargetName))
                    {
                        var link = new Hyperlink {
                            ToolTip = context.NavigateUri
                        };

                        if (context.IconGeometry != null)
                        {
                            link.TextDecorations.Clear();
                        }

                        // assign ICommand instance if available, otherwise set NavigateUri
                        if (Commands != null && Commands.TryGetValue(parsedUri, out var command))
                        {
                            link.Command          = command;
                            link.CommandParameter = parsedParameter;
                            if (parsedTargetName != null)
                            {
                                link.CommandTarget = _source?.FindName(parsedTargetName) as IInputElement;
                            }
                        }
                        else
                        {
                            link.NavigateUri = parsedUri;
                            link.TargetName  = parsedParameter;
                        }

                        parent = link;
                        span.Inlines.Add(parent);
                    }

                    if (context.IconGeometry != null)
                    {
                        var icon = new Path {
                            Stretch = Stretch.Uniform,
                            Data    = context.IconGeometry
                        };

                        icon.SetBinding(Shape.FillProperty, new Binding {
                            Path           = new PropertyPath("(TextBlock.Foreground)"),
                            RelativeSource = new RelativeSource(RelativeSourceMode.Self),
                        });

                        var border = new Border {
                            Background = new SolidColorBrush(Colors.Transparent),
                            Child      = icon,
                            ToolTip    = new ToolTip {
                                Content = new TextBlock {
                                    Text = token.Value
                                }
                            }
                        };

                        border.SetBinding(FrameworkElement.HeightProperty, new Binding {
                            Path               = new PropertyPath("(TextBlock.FontSize)"),
                            RelativeSource     = new RelativeSource(RelativeSourceMode.Self),
                            Converter          = new MultiplyConverter(),
                            ConverterParameter = 0.7
                        });

                        border.SetBinding(FrameworkElement.WidthProperty, new Binding {
                            Path           = new PropertyPath(nameof(Border.Height)),
                            RelativeSource = new RelativeSource(RelativeSourceMode.Self)
                        });

                        parent.Inlines.Add(new InlineUIContainer {
                            Child = border
                        });
                        continue;
                    }
                    {
                        Uri              url;
                        ImageSource      imageSource = null;
                        double           maxSize;
                        bool             expand, toolTip;
                        InlineImageCache cache;

                        if (context.ImageUri?.StartsWith(@"emoji://") == true)
                        {
                            maxSize = 0;
                            expand  = false;
                            toolTip = false;

                            var emojiCode = context.ImageUri.Substring(8);
                            url = BbCodeBlock.OptionEmojiProvider?.GetUri(emojiCode);
                            if (url == null)
                            {
                                cache       = null;
                                imageSource = BbCodeBlock.OptionEmojiProvider?.GetImageSource(emojiCode);
                            }
                            else
                            {
                                cache = BbCodeBlock.OptionEmojiCacheDirectory == null ? null :
                                        _emojiCache ?? (_emojiCache = new InlineImageCache(BbCodeBlock.OptionEmojiCacheDirectory));
                            }
                        }
                        else
                        {
                            toolTip = true;

                            if (NavigationHelper.TryParseUriWithParameters(context.ImageUri, out var temporary, out var parameter, out _))
                            {
                                try {
                                    url = new Uri(temporary.OriginalString);
                                    if (double.TryParse(parameter, out maxSize))
                                    {
                                        expand = true;
                                    }
                                    else
                                    {
                                        maxSize = double.NaN;
                                        expand  = false;
                                    }

                                    cache = BbCodeBlock.OptionImageCacheDirectory == null ? null :
                                            _imageCache ?? (_imageCache = new InlineImageCache(BbCodeBlock.OptionImageCacheDirectory));
                                } catch {
                                    url     = null;
                                    maxSize = double.NaN;
                                    expand  = false;
                                    cache   = null;
                                }
                            }