public static System.Windows.Controls.TextBlock CreateControl(TextBlock textBlock, RenderContext context)
        {
            Marked marked = new Marked();

            marked.Options.Renderer = new AdaptiveXamlMarkdownRenderer();
            marked.Options.Mangle   = false;
            marked.Options.Sanitize = true;

            string text = RendererUtilities.ApplyTextFunctions(textBlock.Text);
            // uiTextBlock.Text = textBlock.Text;
            string       xaml         = $"<TextBlock  xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">{marked.Parse(text)}</TextBlock>";
            StringReader stringReader = new StringReader(xaml);

            XmlReader xmlReader   = XmlReader.Create(stringReader);
            var       uiTextBlock = (System.Windows.Controls.TextBlock)XamlReader.Load(xmlReader);

            uiTextBlock.Style = context.GetStyle($"Adaptive.{textBlock.Type}");

            uiTextBlock.FontFamily   = new FontFamily(context.Config.FontFamily);
            uiTextBlock.TextWrapping = TextWrapping.NoWrap;

            switch (textBlock.Weight)
            {
            case TextWeight.Bolder:
                uiTextBlock.FontWeight = FontWeight.FromOpenTypeWeight(700);
                break;

            case TextWeight.Lighter:
                uiTextBlock.FontWeight = FontWeight.FromOpenTypeWeight(300);
                break;

            case TextWeight.Default:
            default:
                uiTextBlock.FontWeight = FontWeight.FromOpenTypeWeight(400);
                break;
            }

            uiTextBlock.TextTrimming = TextTrimming.CharacterEllipsis;

            if (textBlock.HorizontalAlignment != HorizontalAlignment.Left)
            {
                System.Windows.HorizontalAlignment alignment;
                if (Enum.TryParse <System.Windows.HorizontalAlignment>(textBlock.HorizontalAlignment.ToString(), out alignment))
                {
                    uiTextBlock.HorizontalAlignment = alignment;
                }
            }

            if (textBlock.Wrap)
            {
                uiTextBlock.TextWrapping = TextWrapping.Wrap;
            }

            return(uiTextBlock);
        }
示例#2
0
        protected static HtmlTag ChoiceSetRender(TypedElement element, RenderContext context)
        {
            ChoiceSet choiceSet  = (ChoiceSet)element;
            var       choiceText = GetFallbackText(choiceSet);

            if (choiceText == null)
            {
                var choices = choiceSet.Choices.Select(choice => choice.Title).ToList();
                if (choiceSet.Style == ChoiceInputStyle.Compact)
                {
                    if (choiceSet.IsMultiSelect)
                    {
                        choiceText = $"Choices: {RendererUtilities.JoinString(choices, ", ", " and ")}";
                    }
                    else
                    {
                        choiceText = $"Choices: {RendererUtilities.JoinString(choices, ", ", " or ")}";
                    }
                }
                else // if (this.Style == ChoiceInputStyle.Expanded)
                {
                    choiceText = $"* {RendererUtilities.JoinString(choices, "\n* ", "\n* ")}";
                }
            }
            var container = new Container {
                Separation = choiceSet.Separation
            };

            container.Items.Add(new TextBlock
            {
                Text = choiceText,
                Wrap = true
            });
            container.Items.Add(new TextBlock
            {
                Text =
                    RendererUtilities.JoinString(choiceSet.Choices.Where(c => c.IsSelected).Select(c => c.Title).ToList(), ", ", " and "),
                Color = TextColor.Accent,
                Wrap  = true
            });
            return(context.Render(container));
        }
示例#3
0
        public static FrameworkElement Render(TypedElement element, RenderContext context)
        {
            HttpAction action = (HttpAction)element;

            if (context.Config.SupportsInteractivity)
            {
                Button uiButton = XamlUtilities.CreateActionButton(action, context);
                uiButton.Click += (sender, e) =>
                {
                    dynamic data = new JObject();
                    try
                    {
                        data = context.MergeInputData(data);

                        string body = (string)action.Body?.ToString() ?? String.Empty;

                        context.Action(uiButton, new ActionEventArgs()
                        {
                            Action = new HttpAction()
                            {
                                Title   = action.Title,
                                Method  = action.Method,
                                Url     = RendererUtilities.BindData(data, action.Url, url: true),
                                Headers = action.Headers,
                                Body    = RendererUtilities.BindData(data, body),
                            },
                            Data = data
                        });
                    }
                    catch (MissingInputException err)
                    {
                        context.MissingInput(action, new MissingInputEventArgs(err.Input, err.FrameworkElement));
                    }
                };
                return(uiButton);
            }
            return(null);
        }
        public static Xamarin.Forms.TextBlock CreateControl(TextBlock textBlock, RenderContext context)
        {
            var uiTextBlock = new Xamarin.Forms.TextBlock();

            uiTextBlock.Text  = RendererUtilities.ApplyTextFunctions(textBlock.Text);
            uiTextBlock.Style = context.GetStyle("Adaptive.TextBlock");
            // TODO: confirm text trimming
            uiTextBlock.LineBreakMode = LineBreakMode.TailTruncation;

            switch (textBlock.HorizontalAlignment)
            {
            case HorizontalAlignment.Left:
                uiTextBlock.HorizontalTextAlignment = TextAlignment.Start;
                break;

            case HorizontalAlignment.Center:
                uiTextBlock.HorizontalTextAlignment = TextAlignment.Center;
                break;

            case HorizontalAlignment.Right:
                uiTextBlock.HorizontalTextAlignment = TextAlignment.End;
                break;
            }

            uiTextBlock.TextColor = context.Resources.TryGetValue <Color>($"Adaptive.{textBlock.Color}");

            if (textBlock.Weight == TextWeight.Bolder)
            {
                uiTextBlock.FontAttributes = FontAttributes.Bold;
            }

            if (textBlock.Wrap == true)
            {
                uiTextBlock.LineBreakMode = LineBreakMode.WordWrap;
            }

            return(uiTextBlock);
        }
        protected static HtmlTag TextBlockRender(TypedElement element, RenderContext context)
        {
            TextBlock textBlock = (TextBlock)element;

            int fontSize;

            switch (textBlock.Size)
            {
            case TextSize.Small:
                fontSize = context.Config.FontSizes.Small;
                break;

            case TextSize.Medium:
                fontSize = context.Config.FontSizes.Medium;
                break;

            case TextSize.Large:
                fontSize = context.Config.FontSizes.Large;
                break;

            case TextSize.ExtraLarge:
                fontSize = context.Config.FontSizes.ExtraLarge;
                break;

            case TextSize.Normal:
            default:
                fontSize = context.Config.FontSizes.Normal;
                break;
            }
            int weight = 400;

            switch (textBlock.Weight)
            {
            case TextWeight.Lighter:
                weight = 200;
                break;

            case TextWeight.Bolder:
                weight = 600;
                break;
            }
            var lineHeight = fontSize * 1.2;

            var uiTextBlock = new DivTag()
                              .AddClass($"ac-{element.Type.Replace(".", "").ToLower()}")
                              .Style("text-align", textBlock.HorizontalAlignment.ToString().ToLower())
                              .Style("box-sizing", "border-box")
                              .Style("color", context.GetColor(textBlock.Color, textBlock.IsSubtle))
                              .Style("line-height", $"{lineHeight.ToString("F")}px")
                              .Style("font-size", $"{fontSize}px")
                              .Style("font-weight", $"{weight}");

            if (!String.IsNullOrEmpty(context.Config.FontFamily))
            {
                uiTextBlock = uiTextBlock
                              .Style("font-family", context.Config.FontFamily);
            }

            if (textBlock.MaxLines > 0)
            {
                uiTextBlock = uiTextBlock
                              .Style("max-height", $"{lineHeight * textBlock.MaxLines}px")
                              .Style("overflow", "hidden");
            }

            var setWrapStyleOnParagraph = false;

            if (textBlock.Wrap == false)
            {
                uiTextBlock = uiTextBlock
                              .Style("white-space", "nowrap");
                setWrapStyleOnParagraph = true;
            }
            else
            {
                uiTextBlock = uiTextBlock
                              .Style("word-wrap", "break-word");
            }

            var textTags = MarkdownToHtmlTagConverter.Convert(RendererUtilities.ApplyTextFunctions(textBlock.Text));

            uiTextBlock.Children.AddRange(textTags);

            Action <HtmlTag> setParagraphStyles = null;

            setParagraphStyles = (HtmlTag htmlTag) =>
            {
                if (htmlTag.Element?.ToLowerInvariant() == "p")
                {
                    htmlTag.Style("margin-top", "0px");
                    htmlTag.Style("margin-bottom", "0px");
                    htmlTag.Style("width", "100%");

                    if (setWrapStyleOnParagraph)
                    {
                        htmlTag.Style("text-overflow", "ellipsis");
                        htmlTag.Style("overflow", "hidden");
                    }
                }

                foreach (var child in htmlTag.Children)
                {
                    setParagraphStyles(child);
                }
            };

            setParagraphStyles(uiTextBlock);

            return(uiTextBlock);
        }
示例#6
0
        protected static HtmlTag TextBlockRender(TypedElement element, RenderContext context)
        {
            TextBlock textBlock = (TextBlock)element;

            int fontSize;

            switch (textBlock.Size)
            {
            case TextSize.Small:
                fontSize = context.Config.FontSizes.Small;
                break;

            case TextSize.Medium:
                fontSize = context.Config.FontSizes.Medium;
                break;

            case TextSize.Large:
                fontSize = context.Config.FontSizes.Large;
                break;

            case TextSize.ExtraLarge:
                fontSize = context.Config.FontSizes.ExtraLarge;
                break;

            case TextSize.Normal:
            default:
                fontSize = context.Config.FontSizes.Normal;
                break;
            }
            int weight = 400;

            switch (textBlock.Weight)
            {
            case TextWeight.Lighter:
                weight = 200;
                break;

            case TextWeight.Bolder:
                weight = 600;
                break;
            }
            var lineHeight = fontSize * 1.2;

            var uiTextBlock = new DivTag()
                              .AddClass($"ac-{element.Type.Replace(".", "").ToLower()}")
                              .Style("text-align", textBlock.HorizontalAlignment.ToString().ToLower())
                              .Style("box-sizing", "border-box")
                              .Style("color", context.GetColor(textBlock.Color, textBlock.IsSubtle))
                              .Style("line-height", $"{lineHeight.ToString("F")}px")
                              .Style("font-size", $"{fontSize}px")
                              .Style("font-weight", $"{weight}");

            if (!String.IsNullOrEmpty(context.Config.FontFamily))
            {
                uiTextBlock = uiTextBlock
                              .Style("font-family", context.Config.FontFamily);
            }

            if (textBlock.MaxLines > 0)
            {
                uiTextBlock = uiTextBlock
                              .Style("max-height", $"{lineHeight * textBlock.MaxLines}px")
                              .Style("overflow", "hidden");
            }

            var wrapStyle = "";

            if (textBlock.Wrap == false)
            {
                uiTextBlock = uiTextBlock
                              .Style("white-space", "nowrap");
                wrapStyle = "text-overflow: ellipsis; overflow: hidden";
            }
            else
            {
                uiTextBlock = uiTextBlock
                              .Style("word-wrap", "break-word");
            }

            var marked = new Marked();

            marked.Options.Mangle   = false;
            marked.Options.Sanitize = true;

            var html = marked.Parse(RendererUtilities.ApplyTextFunctions(textBlock.Text))
                       .Replace("<p>", $"<p style='margin-top: 0px;margin-bottom: 0px;width: 100%{wrapStyle}'>");

            uiTextBlock.Children.Add(new LiteralTag(html));

            return(uiTextBlock);
        }
示例#7
0
        public static FrameworkElement Render(ChoiceSet choiceSet, RenderContext context)
        {
            var chosen = choiceSet.Value?.Split(',').Select(p => p.Trim()).Where(s => !string.IsNullOrEmpty(s)).ToList() ?? new List <string>();

#if WPF
            if (context.Config.SupportsInteractivity)
            {
                var uiGrid = new Grid();
                uiGrid.RowDefinitions.Add(new RowDefinition()
                {
                    Height = GridLength.Auto
                });
                uiGrid.RowDefinitions.Add(new RowDefinition()
                {
                    Height = new GridLength(1, GridUnitType.Star)
                });

                var uiComboBox = new ComboBox();
                uiComboBox.Style       = context.GetStyle("Adaptive.Input.ChoiceSet.ComboBox");
                uiComboBox.DataContext = choiceSet;

                var uiChoices = new ListBox();
                ScrollViewer.SetHorizontalScrollBarVisibility(uiChoices, ScrollBarVisibility.Disabled);
                var itemsPanelTemplate = new ItemsPanelTemplate();
                var factory            = new FrameworkElementFactory(typeof(WrapPanel));
                itemsPanelTemplate.VisualTree = factory;
                uiChoices.ItemsPanel          = itemsPanelTemplate;
                uiChoices.DataContext         = choiceSet;
                uiChoices.Style = context.GetStyle("Adaptive.Input.ChoiceSet");

                foreach (var choice in choiceSet.Choices)
                {
                    if (choiceSet.IsMultiSelect == true)
                    {
                        var uiCheckbox = new CheckBox();
                        uiCheckbox.Content     = choice.Title;
                        uiCheckbox.IsChecked   = chosen.Contains(choice.Value);
                        uiCheckbox.DataContext = choice;
                        uiCheckbox.Style       = context.GetStyle("Adaptive.Input.ChoiceSet.CheckBox");
                        uiChoices.Items.Add(uiCheckbox);
                    }
                    else
                    {
                        if (choiceSet.Style == ChoiceInputStyle.Compact)
                        {
                            var uiComboItem = new ComboBoxItem();
                            uiComboItem.Style       = context.GetStyle("Adaptive.Input.ChoiceSet.ComboBoxItem");
                            uiComboItem.Content     = choice.Title;
                            uiComboItem.DataContext = choice;
                            uiComboBox.Items.Add(uiComboItem);
                            if (chosen.Contains(choice.Value))
                            {
                                uiComboBox.SelectedItem = uiComboItem;
                            }
                        }
                        else
                        {
                            var uiRadio = new RadioButton();
                            uiRadio.Content     = choice.Title;
                            uiRadio.IsChecked   = chosen.Contains(choice.Value);
                            uiRadio.GroupName   = choiceSet.Id;
                            uiRadio.DataContext = choice;
                            uiRadio.Style       = context.GetStyle("Adaptive.Input.ChoiceSet.Radio");
                            uiChoices.Items.Add(uiRadio);
                        }
                    }
                }
                context.InputBindings.Add(choiceSet.Id, () =>
                {
                    if (choiceSet.IsMultiSelect == true)
                    {
                        string values = string.Empty;
                        foreach (var item in uiChoices.Items)
                        {
                            CheckBox checkBox = (CheckBox)item;
                            Choice choice     = checkBox.DataContext as Choice;
                            if (checkBox.IsChecked == true)
                            {
                                values += (values == string.Empty ? "" : ",") + choice.Value;
                            }
                        }
                        return(values);
                    }
                    else
                    {
                        if (choiceSet.Style == ChoiceInputStyle.Compact)
                        {
                            ComboBoxItem item = uiComboBox.SelectedItem as ComboBoxItem;
                            if (item != null)
                            {
                                Choice choice = item.DataContext as Choice;
                                return(choice.Value);
                            }
                            return(null);
                        }
                        else
                        {
                            foreach (var item in uiChoices.Items)
                            {
                                RadioButton radioBox = (RadioButton)item;
                                Choice choice        = radioBox.DataContext as Choice;
                                if (radioBox.IsChecked == true)
                                {
                                    return(choice.Value);
                                }
                            }
                            return(null);
                        }
                    }
                });
                if (choiceSet.Style == ChoiceInputStyle.Compact)
                {
                    Grid.SetRow(uiComboBox, 1);
                    uiGrid.Children.Add(uiComboBox);
                    return(uiGrid);
                }
                else
                {
                    Grid.SetRow(uiChoices, 1);
                    uiGrid.Children.Add(uiChoices);
                    return(uiGrid);
                }
            }
#endif

            string choiceText = XamlUtilities.GetFallbackText(choiceSet);
            if (choiceText == null)
            {
                List <string> choices = choiceSet.Choices.Select(choice => choice.Title).ToList();
                if (choiceSet.Style == ChoiceInputStyle.Compact)
                {
                    if (choiceSet.IsMultiSelect)
                    {
                        choiceText = $"Choices: {RendererUtilities.JoinString(choices, ", ", " and ")}";
                    }
                    else
                    {
                        choiceText = $"Choices: {RendererUtilities.JoinString(choices, ", ", " or ")}";
                    }
                }
                else // if (choiceSet.Style == ChoiceInputStyle.Expanded)
                {
                    choiceText = $"* {RendererUtilities.JoinString(choices, "\n* ", "\n* ")}";
                }
            }
            Container container = TypedElementConverter.CreateElement <Container>();
            container.Separation = choiceSet.Separation;
            TextBlock textBlock = TypedElementConverter.CreateElement <TextBlock>();
            textBlock.Text = choiceText;
            textBlock.Wrap = true;
            container.Items.Add(textBlock);

            textBlock       = TypedElementConverter.CreateElement <TextBlock>();
            textBlock.Text  = RendererUtilities.JoinString(choiceSet.Choices.Where(c => chosen.Contains(c.Value)).Select(c => c.Title).ToList(), ", ", " and ");
            textBlock.Color = TextColor.Accent;
            textBlock.Wrap  = true;
            container.Items.Add(textBlock);
            return(context.Render(container));
        }