Browse Source

Enhanced the EnsureSpace element to work dynamically without a pre-configured vertical space threshold

Marcin Ziąbek 1 year ago
parent
commit
42b02d3533

+ 33 - 7
Source/QuestPDF/Elements/EnsureSpace.cs

@@ -3,21 +3,47 @@ using QuestPDF.Infrastructure;
 
 
 namespace QuestPDF.Elements
 namespace QuestPDF.Elements
 {
 {
-    internal sealed class EnsureSpace : ContainerElement
+    internal sealed class EnsureSpace : ContainerElement, IStateful
     {
     {
-        public const float DefaultMinHeight = 150;
-        public float MinHeight { get; set; } = DefaultMinHeight;
+        public float? MinHeight { get; set; } = null;
 
 
         internal override SpacePlan Measure(Size availableSpace)
         internal override SpacePlan Measure(Size availableSpace)
         {
         {
             var measurement = base.Measure(availableSpace);
             var measurement = base.Measure(availableSpace);
 
 
-            if (measurement.Type == SpacePlanType.PartialRender && availableSpace.Height < MinHeight)
-                return SpacePlan.Wrap("The available vertical space is smaller than requested in the constraint.");
+            if (IsFirstPageRendered)
+                return measurement;
 
 
-            return measurement;
+            if (measurement.Type != SpacePlanType.PartialRender)
+                return measurement;
+
+            if (MinHeight == null || MinHeight <= availableSpace.Height)
+                return measurement;
+            
+            return SpacePlan.Wrap("The available vertical space is smaller than requested in the constraint.");
+        }
+        
+        internal override void Draw(Size availableSpace)
+        {
+            base.Draw(availableSpace);
+            IsFirstPageRendered = true;
         }
         }
 
 
-        internal override string? GetCompanionHint() => $"at least {MinHeight}";
+        internal override string? GetCompanionHint() => MinHeight.HasValue ? $"at least {MinHeight.Value}" : null;
+        
+        #region IStateful
+        
+        private bool IsFirstPageRendered { get; set; }
+
+        public void ResetState(bool hardReset = false)
+        {
+            if (hardReset)
+                IsFirstPageRendered = false;
+        }
+        
+        public object GetState() => IsFirstPageRendered;
+        public void SetState(object state) => IsFirstPageRendered = (bool) state;
+    
+        #endregion
     }
     }
 }
 }

+ 1 - 1
Source/QuestPDF/Fluent/ElementExtensions.cs

@@ -223,7 +223,7 @@ namespace QuestPDF.Fluent
         /// <remarks>
         /// <remarks>
         /// This is especially useful for elements like tables, where you'd want to display several rows together. By setting the minHeight, you can avoid scenarios where only a single row appears at the page's end, ensuring a more cohesive presentation.
         /// This is especially useful for elements like tables, where you'd want to display several rows together. By setting the minHeight, you can avoid scenarios where only a single row appears at the page's end, ensuring a more cohesive presentation.
         /// </remarks>
         /// </remarks>
-        public static IContainer EnsureSpace(this IContainer element, float minHeight = Elements.EnsureSpace.DefaultMinHeight)
+        public static IContainer EnsureSpace(this IContainer element, float? minHeight = null)
         {
         {
             return element.Element(new EnsureSpace
             return element.Element(new EnsureSpace
             {
             {

+ 4 - 0
Source/QuestPDF/Resources/ReleaseNotes.txt

@@ -23,3 +23,7 @@ Further improvements:
 Version 2024.12.1
 Version 2024.12.1
 - Fixed: The library now provides hints when an additional dependency is required.
 - Fixed: The library now provides hints when an additional dependency is required.
 - Improved license-related exception.
 - Improved license-related exception.
+
+
+Version 2024.12.2
+- Enhanced the EnsureSpace element to work dynamically without a pre-configured vertical space threshold.