Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 4e12f0cc

Přidáno uživatelem Vojtěch Bartička před asi 2 roky(ů)

Caching generated CSS, returning it separate from HTML

Zobrazit rozdíly:

Backend/Core/Entities/Annotation.cs
33 33
        public string CachedLengths { get; set; } = "";
34 34
        public string CachedClosingPositions { get; set; } = "";
35 35
        public string CachedClosingLengths { get; set; } = "";
36
        //public string CachedNodeDict { get; set; } = "";
36
        public string CachedCSS { get; set; } = "";
37 37

  
38 38
        public Guid? LastModifiedTagId { get; set; }
39 39
        public EModified ModifiedType { get; set; } = EModified.NONE;
Backend/Core/Entities/AnnotationClass.cs
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel.DataAnnotations;
4
using System.ComponentModel.DataAnnotations.Schema;
5
using System.Linq;
6
using System.Text;
7
using System.Threading.Tasks;
8

  
9
namespace Core.Entities
10
{
11
   /* public class AnnotationClass : BaseEntity
12
    {
13
        [ForeignKey("Class")]
14
        public int ClassId { get; set; }
15
        public Class Class { get; set; }
16

  
17
        [ForeignKey("Annotation")]
18
        public int AnnotationId { get; set; }
19
        public Annotation Annotation { get; set; }
20
    }*/
21
}
Backend/Core/Services/AnnotationService/AnnotationServiceEF.cs
162 162
                annotation.CachedLengths = JsonConvert.SerializeObject(TagStartLengths);
163 163
                annotation.CachedClosingPositions = JsonConvert.SerializeObject(TagClosingPositions);
164 164
                annotation.CachedClosingLengths = JsonConvert.SerializeObject(TagClosingLengths);
165
                //annotation.CachedNodeDict = JsonConvert.SerializeObject(NodeDict);
165
                annotation.CachedCSS = JsonConvert.SerializeObject(TagInstanceCSS);
166 166
                annotation.ModifiedType = EModified.NONE;
167 167
                annotation.CachedDocumentHTML = docToRender;
168 168
                context.SaveChanges();
......
222 222
        private List<int> TagClosingPositions = new();
223 223
        private List<int> TagClosingLengths = new();
224 224
        private Dictionary<HtmlNode, HtmlNode> NodeDict = new();
225
        private List<TagInstanceCSSInfo> TagInstanceCSS = new();
225 226

  
226 227
        /*
227 228
         *      Full HTML Preprocessing -------------------------------------------------------------------------------
......
313 314
            {
314 315
                sanitizer.AllowedTags.Add("style");
315 316
            }
316
            docToRender = sanitizer.Sanitize(docToRender);
317

  
318
            HtmlDocument doc = new HtmlDocument();
319
            doc.LoadHtml(docToRender);
320
            var cssNode = GenerateCSS(doc, tags);
321
            doc.DocumentNode.ChildNodes.Insert(0, cssNode);
322

  
323
            return doc.DocumentNode.OuterHtml;
317
            docToRender = sanitizer.Sanitize(docToRender);        
318
            GenerateCSS(tags);
319
            return docToRender;
324 320
        }
325 321

  
326 322
        private HtmlNode SolveFullFill(HtmlNode node, int selectionStart, int selectionEnd, int start, int end, HtmlDocument docToEdit, AnnotationTag tag)
......
541 537
            }
542 538
        }
543 539

  
544
        private HtmlNode GenerateCSS(HtmlDocument docToEdit, List<AnnotationTag> tags)
540
        private void GenerateCSS(List<AnnotationTag> tags)
545 541
        {
546
            HtmlNode style = docToEdit.CreateElement("style");
547

  
548
            string inner = "span.annotation {border-bottom: 2px solid;}";
549
            inner += "span {line-height: 30px}\n";
542
            /*string inner = "span.annotation {border-bottom: 2px solid;}";
543
            inner += "span {line-height: 30px}\n";*/
550 544

  
551 545
            var tagPaddingDict = Intersections.ColorGraph(Intersections.FindIntersections(tags));
552

  
553 546
            foreach (var tag in tags)
554 547
            {
555 548
                var padding = (tagPaddingDict[tag] + 1) * 2;
556
                inner += $"span[{TAG_INSTANCE_ATTRIBUTE_NAME}=\"{tag.Instance}\"] {{ border-color:{tag.Tag.Color}; padding-bottom: {padding}px }}";
549
                TagInstanceCSS.Add(new()
550
                {
551
                    InstanceId = tag.Instance,
552
                    Color = tag.Tag.Color,
553
                    Padding = padding
554
                });             
557 555
            }
558

  
559
            inner += "span[end=\"1\"] {border-end-end-radius: 0px; border-right: 1px solid darkgray}\n";
560
            inner += "span[start=\"1\"] {border-end-start-radius: 0px; border-left: 1px solid darkgray}\n";
561

  
562
            style.InnerHtml = inner;
563
            return style;
564 556
        }
565 557

  
566 558
        private void AssignIdsToOriginalDocument(IEnumerable<HtmlNode> descendantsOriginal, ref int currentId)
......
619 611
            docToEdit.LoadHtml(htmlToEdit);
620 612

  
621 613
            var descendantsToEdit = docToEdit.DocumentNode.DescendantsAndSelf().ToList();
622
            RemoveCSS(docToEdit);
623 614

  
624 615
            int i = 0;
625 616
            List<HtmlNode> addedForSelection = new();
......
669 660
                }
670 661
            }
671 662

  
672
            var cssNode = GenerateCSS(docToEdit, tags);
673
            docToEdit.DocumentNode.ChildNodes.Insert(0, cssNode);
674

  
663
            GenerateCSS(tags);
675 664
            return docToEdit.DocumentNode.OuterHtml;
676 665
        }
677 666

  
......
684 673
            docToEdit.LoadHtml(htmlToEdit);
685 674

  
686 675
            var descendantsToEdit = docToEdit.DocumentNode.DescendantsAndSelf().ToList();
687
            RemoveCSS(docToEdit);
688 676

  
689 677
            int i = 0;
690 678
            int descendantsCount = descendantsToEdit.Count();
......
739 727
                }
740 728
            }
741 729

  
742
            var cssNode = GenerateCSS(docToEdit, tags);
743
            docToEdit.DocumentNode.ChildNodes.Insert(0, cssNode);
744

  
730
            GenerateCSS(tags);
745 731
            return docToEdit.DocumentNode.OuterHtml;
746 732
        }
747 733

  
748
        private void RemoveCSS(HtmlDocument doc)
749
        {
750
            doc.DocumentNode.ChildNodes.RemoveAt(0);
751
        }
752

  
753 734
        /*
754 735
         *      Partial HTML Preprocessing ----------------------------------------------------------------------------
755 736
         */
Backend/Models/Annotations/AnnotationInfo.cs
18 18
        public EDocumentType Type { get; set; }
19 19
        public string Note { get; set; }
20 20
        public List<TagInstanceInfo> TagInstances { get; set; } = new();
21
        public List<TagInstanceCSSInfo> CSSInfo { get; set; } = new();
21 22
    }
22 23
}
Backend/Models/Tags/TagInstanceCSSInfo.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6

  
7
namespace Models.Tags
8
{
9
    public class TagInstanceCSSInfo
10
    {
11
        public Guid InstanceId { get; set; }
12
        public string Color { get; set; }
13
        public int Padding { get; set; }
14
    }
15
}

Také k dispozici: Unified diff