using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using UnityEngine; using CommonMark; using CommonMark.Syntax; using UnityEngine.UI; using TMPro; using CustomUI.BeatSaber; using IPA.Utilities; namespace BSIPA_ModList.UI.ViewControllers { [RequireComponent(typeof(RectTransform))] public class MarkdownView : MonoBehaviour { private class TagTypeComponent : MonoBehaviour { internal BlockTag Tag; internal HeadingData hData; } private string markdown = ""; private bool mdDirty = false; public string Markdown { get => markdown; set { markdown = value; mdDirty = true; } } public RectTransform rectTransform => GetComponent(); private ScrollView scrView; private RectTransform content; private RectTransform viewport; private CommonMarkSettings settings; public MarkdownView() { settings = CommonMarkSettings.Default.Clone(); settings.AdditionalFeatures = CommonMarkAdditionalFeatures.All; settings.RenderSoftLineBreaksAsLineBreaks = false; settings.UriResolver = ResolveUri; } public Func HasEmbeddedImage; private string ResolveUri(string arg) { var name = arg.Substring(3); if (!arg.StartsWith("!::") && !arg.StartsWith("w::")) { // !:: means embedded, w:: means web // this block is for when neither is specified Logger.md.Debug($"Resolving nonspecific URI {arg}"); // check if its embedded if (HasEmbeddedImage != null && HasEmbeddedImage(arg)) return "!::" + arg; else return "w::" + arg; } Logger.md.Debug($"Resolved specific URI {arg}"); return arg; } protected void Awake() { gameObject.SetActive(false); var vpgo = new GameObject("Viewport"); viewport = vpgo.AddComponent(); viewport.SetParent(transform); viewport.localPosition = Vector2.zero; viewport.anchorMin = Vector2.zero; viewport.anchorMax = Vector2.one; viewport.anchoredPosition = new Vector2(.5f, .5f); viewport.sizeDelta = Vector2.zero; var vpmask = vpgo.AddComponent(); var vpim = vpgo.AddComponent(); // supposedly Mask needs an Image? vpmask.showMaskGraphic = false; vpim.color = Color.white; vpim.sprite = WhitePixel; vpim.material = CustomUI.Utilities.UIUtilities.NoGlowMaterial; content = new GameObject("Content Wrapper").AddComponent(); content.SetParent(viewport); content.gameObject.AddComponent(); content.localPosition = Vector2.zero; content.anchorMin = new Vector2(0f, 1f); content.anchorMax = new Vector2(1f, 1f); content.anchoredPosition = Vector2.zero; var contentLayout = content.gameObject.AddComponent(); var contentFitter = content.gameObject.AddComponent(); contentFitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize; contentFitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize; contentLayout.childControlHeight = true; contentLayout.childControlWidth = false; contentLayout.childForceExpandHeight = false; contentLayout.childForceExpandWidth = true; contentLayout.childAlignment = TextAnchor.UpperCenter; contentLayout.spacing = 0f; var pageUp = Instantiate(Resources.FindObjectsOfTypeAll