コード例 #1
0
        protected override void Write(UIElementRenderer renderer, EmphasisInline obj)
        {
            string delimiterClass = null;

            switch (obj.DelimiterChar)
            {
            case '*':
            case '_':
                delimiterClass = obj.DelimiterCount == 2 ? "bold" : "italic";
                break;

            case '~':
                delimiterClass = obj.DelimiterCount == 2 ? "strikethrough" : "subscript";
                break;

            case '^':
                delimiterClass = "superscript";
                break;

            case '+':
                delimiterClass = "inserted";
                break;

            case '=':
                delimiterClass = "marked";
                break;
            }

            if (delimiterClass != null)
            {
                renderer.Push(GetClassedElement <VisualElement>(delimiterClass));
                renderer.WriteChildren(obj);
                renderer.Pop();
            }
            else
            {
                renderer.WriteChildren(obj);
            }
        }
コード例 #2
0
        protected override void Write(UIElementRenderer renderer, ListBlock listBlock)
        {
            renderer.Push(GetClassedElement <VisualElement>("list"));

            foreach (var item in listBlock.OfType <ListItemBlock>())
            {
                var listItem = GetClassedElement <VisualElement>("list-item");

                renderer.Push(listItem);

                var marker = listBlock.IsOrdered ? $"{item.Order}." : $"{listBlock.BulletType}";

                var classes = listBlock.IsOrdered ? "inline" : "bullet";

                renderer.WriteChildren(item);

                listItem.Children()?.FirstOrDefault()?.Insert(0, GetTextElement <Label>(marker, classes));

                renderer.Pop();
            }

            renderer.Pop();
        }
コード例 #3
0
        protected override void Write(UIElementRenderer renderer, LinkInline link)
        {
            var url = link.Url;

            if (link.IsImage)
            {
                var imageElement = GetClassedElement <Image>("image");
                if (IsAssetDirectory(url))
                {
                    var image = AssetDatabase.LoadAssetAtPath <Texture>(url);
                    if (image)
                    {
                        SetupImage(imageElement, image);
                    }
                }
                else
                {
                    async Task DownloadImage(string MediaUrl)
                    {
                        UnityWebRequest request = UnityWebRequestTexture.GetTexture(MediaUrl);
                        var             asyncOp = request.SendWebRequest();

                        while (!asyncOp.isDone)
                        {
                            await Task.Delay(100);
                        }

#if UNITY_2020_1_OR_NEWER
                        if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError)
#else
                        if (request.isNetworkError || request.isHttpError)
#endif
                        { Debug.Log(request.error); }
                        else
                        {
                            SetupImage(imageElement, ((DownloadHandlerTexture)request.downloadHandler).texture);
                        }
                    }

                    _ = DownloadImage(link.Url);
                }

                renderer.Push(imageElement);
                renderer.WriteChildren(link);
                foreach (var child in imageElement.Children())
                {
                    child.AddToClassList("alt-text");
                }
                renderer.Pop();
            }
            else
            {
                var lowerScheme = string.Empty;
                var isValidUri  = Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute);
                if (!isValidUri)
                {
                    var match = LinkInlineRenderer.SchemeCheck.Match(url);
                    if (match.Success)
                    {
                        lowerScheme = match.Groups[1].Value.ToLower();
                    }
                    else
                    {
                        lowerScheme = "#";
                    }
                }
                else
                {
                    var uri = new Uri(url);
                    lowerScheme = uri.Scheme.ToLower();
                }

                var linkLabel = GetClassedElement <VisualElement>("link", lowerScheme);
                linkLabel.tooltip = url;
                if (isValidUri)
                {
                    linkLabel.RegisterCallback <MouseUpEvent>(evt =>
                    {
                        if (LinkInlineRenderer.SchemeLinkHandlers.ContainsKey(lowerScheme))
                        {
                            LinkInlineRenderer.SchemeLinkHandlers[lowerScheme]?.Invoke(url);
                        }
                    });
                }

                renderer.Push(linkLabel);
                renderer.WriteChildren(link);
                renderer.Pop();
            }
        }
コード例 #4
0
 protected override void Write(UIElementRenderer renderer, DelimiterInline obj)
 {
     renderer.WriteText(obj.ToLiteral());
     renderer.WriteChildren(obj);
 }
コード例 #5
0
 protected override void Write(UIElementRenderer renderer, QuoteBlock obj)
 {
     renderer.Push(GetClassedElement <VisualElement>("quote"));
     renderer.WriteChildren(obj);
     renderer.Pop();
 }
コード例 #6
0
        protected override void Write(UIElementRenderer renderer, LinkInline link)
        {
            var url = link.Url;

            if (link.IsImage)
            {
                var imageElement = GetClassedElement <Image>("image");
                if (IsAssetDirectory(url))
                {
                    var image = AssetDatabase.LoadAssetAtPath <Texture>(url);
                    if (image)
                    {
                        SetupImage(imageElement, image);
                    }
                }
                else
                {
                    var imageLoaderObject = new GameObject("MarkdownImageLoader", typeof(ImageLoadBehaviour))
                    {
                        isStatic = true, hideFlags = HideFlags.HideAndDontSave
                    };
                    var imageLoader = imageLoaderObject.GetComponent <ImageLoadBehaviour>();
                    var c           = imageLoader.StartCoroutine(LoadImage(url, imageElement));
                }

                renderer.Push(imageElement);
                renderer.WriteChildren(link);
                foreach (var child in imageElement.Children())
                {
                    child.AddToClassList("alt-text");
                }
                renderer.Pop();
            }
            else
            {
                var lowerScheme = string.Empty;
                var isValidUri  = Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute);
                if (!isValidUri)
                {
                    var match = LinkInlineRenderer.SchemeCheck.Match(url);
                    if (match.Success)
                    {
                        lowerScheme = match.Groups[1].Value.ToLower();
                    }
                    else
                    {
                        lowerScheme = "#";
                    }
                }
                else
                {
                    var uri = new Uri(url);
                    lowerScheme = uri.Scheme.ToLower();
                }

                var linkLabel = GetClassedElement <VisualElement>("link", lowerScheme);
                linkLabel.tooltip = url;
                if (isValidUri)
                {
                    linkLabel.RegisterCallback <MouseUpEvent>(evt =>
                    {
                        if (SchemeLinkHandlers.TryGetValue(lowerScheme, out var handler))
                        {
                            handler?.Invoke(url);
                        }
                    });
                }

                renderer.Push(linkLabel);
                renderer.WriteChildren(link);
                renderer.Pop();
            }
        }