예제 #1
0
        public static bool TryParse(
            string height,
            string width,
            out Measurement2d result)
        {
            decimal heightValue = default;
            decimal widthValue  = default;

            if (
                !decimal.TryParse(height.Split(' ').FirstOrDefault(), out heightValue) ||
                !decimal.TryParse(width.Split(' ').FirstOrDefault(), out widthValue))
            {
                result = null;

                return(false);
            }

            var heightUnit = height
                             .Replace(heightValue.ToString(), string.Empty)
                             .Trim();
            var widthUnit = width
                            .Replace(widthValue.ToString(), string.Empty)
                            .Trim();

            if (string.IsNullOrEmpty(heightUnit) || string.IsNullOrEmpty(widthUnit))
            {
                result = null;

                return(false);
            }

            result = new Measurement2d()
            {
                Height = new Measurement()
                {
                    Unit  = heightUnit,
                    Value = heightValue
                },
                Width = new Measurement()
                {
                    Unit  = widthUnit,
                    Value = widthValue
                }
            };

            return(true);
        }
예제 #2
0
        public async override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (Measurement2d.TryParse(Height, Width, out var measurement))
            {
                context.Items.Add(nameof(ScaleCosmosContext), new ScaleCosmosContext()
                {
                    Measurements = measurement,
                    Translator   = Translator ?? DefaultTranslator.SingleInstance
                });

                Translator.TryTranslation(measurement.Height.Value, measurement.Height.Unit, measurement.Width.Unit, out var translatedHeight);

                var childContent = await output.GetChildContentAsync();

                output.Content.Clear();

                output.Content.AppendHtml(
                    $"<div data-sc-fluid-container style=\"height:calc(100% * {translatedHeight / measurement.Width.Value});width:calc(100% * {measurement.Width.Value / translatedHeight});max-height:100%;max-width:100%;\">");

                output.Content.AppendHtml(childContent);

                output.Content.AppendHtml("</div>");

                output.Attributes.SetAttribute(
                    "style",
                    $"--sc-ratio-height:{translatedHeight / measurement.Width.Value};--sc-ratio-width:1");
            }
            else
            {
                throw new Exception(
                          "Could not determine measurements. Make sure to have a space between the value and unit.");
            }

            if (output.Attributes.TryGetAttribute("sc-container", out var attribute))
            {
                output.Attributes.Remove(attribute);
            }

            output.Attributes.Add("data-sc-fixed-container", null);
        }
예제 #3
0
        public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (context.Items.TryGetValue(nameof(ScaleCosmosContext), out var tempContext) && tempContext is ScaleCosmosContext cosmosContext)
            {
                if (Measurement2d.TryParse(Height, Width, out var measurement))
                {
                    cosmosContext.Translator.TryTranslation(measurement.Height.Value, measurement.Height.Unit, cosmosContext.Measurements.Height.Unit, out var objectHeight);

                    cosmosContext.Translator.TryTranslation(measurement.Width.Value, measurement.Width.Unit, cosmosContext.Measurements.Width.Unit, out var objectWidth);

                    var scaleHeight = (objectHeight / cosmosContext.Measurements.Height.Value) * 100.0m;
                    var scaleWidth  = (objectWidth / cosmosContext.Measurements.Width.Value) * 100.0m;

                    output.Attributes.SetAttribute("style", $"height:{scaleHeight}%;width:{scaleWidth}%;");
                }
                else
                {
                    throw new Exception(
                              "Could not determine measurements. Make sure to have a space between the value and unit.");
                }
            }
            else
            {
                throw new Exception(
                          "Could not retrieve parent-tag context.");
            }

            if (output.Attributes.TryGetAttribute("sc-object", out var attribute))
            {
                output.Attributes.Remove(attribute);
            }

            output.Attributes.Add("data-sc-object", null);

            return(Task.FromResult(true));
        }