]> _ Git - fluidbook-html5.git/commitdiff
wait #7950 @2.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 29 Jan 2026 16:27:17 +0000 (17:27 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 29 Jan 2026 16:27:21 +0000 (17:27 +0100)
js/libs/fluidbook/fluidbook.search.js

index 94118c4e809e84c76a2461d5a83a60a8a20b9c82..43d67605c5d7b39862463af79968bce399c8fe52 100644 (file)
@@ -1,6 +1,7 @@
 function FluidbookSearch(fluidbook) {
     this.fluidbook = fluidbook;
     this.indexLoaded = false;
+    this.indexLoading = false;
     this.termsToHighlight = [];
     this.highlights = [];
     this.highlightEnabled = fluidbook.settings.highlightResults;
@@ -238,21 +239,29 @@ FluidbookSearch.prototype = {
     },
 
     _loadLib: function (callback) {
-        if (this.indexLoaded) {
+        if (this.indexLoading || this.indexLoaded) {
             return callback();
         }
+        this.indexLoaded = true;
         var $this = this;
+        var __callback = function () {
+            if (this.fluidbook.settings.searchWords) {
+                $this._addLinkWords();
+            }
+            $this.indexLoaded = true;
+            $this.indexLoading = false;
+            callback();
+        };
+
 
         let __loadTexts = function () {
             $this._loadTexts(function () {
                 if ($this.highlightEnabled) {
                     loadJSLibrary($this.fluidbook.loader.getURL('data/search.highlight.js?j=' + this.fluidbook.settings.cacheDate), function () {
-                        $this.indexLoaded = true;
-                        callback();
+                        __callback();
                     });
                 } else {
-                    $this.indexLoaded = true;
-                    callback();
+                    __callback();
                 }
             });
         };
@@ -266,6 +275,50 @@ FluidbookSearch.prototype = {
         }
     },
 
+    _addLinkWords: function () {
+        let $this = this;
+        let z = 1;
+        // Add words added by links to the INDEX
+        $.each(this.fluidbook.settings.searchWords, function (word, data) {
+            if (INDEX[word] === undefined) {
+                INDEX[word] = {t: 0, p: {}};
+            }
+
+            $.each(data, function (k, d) {
+                INDEX[word].t++;
+                if (INDEX[word].p[d.page] === undefined) {
+                    INDEX[word].p[d.page] = 0;
+                }
+                INDEX[word].p[d.page]++;
+            });
+        });
+
+        // Add words to the HIGHLIGHT data
+        $.each(this.fluidbook.settings.searchWords, function (word, data) {
+            if (HIGHLIGHTS[word] === undefined) {
+                HIGHLIGHTS[word] = [];
+            }
+
+            $.each(data, function (k, d) {
+                let lw = [];
+                let letters = word.split('');
+                $.each(letters, function (kk, letter) {
+                    lw.push(((d.coords.width * z) / letters.length));
+                });
+                HIGHLIGHTS[word].push({
+                    page: d.page,
+                    y: (d.coords.y + d.coords.height) * z,
+                    x: (d.coords.x) * z,
+                    width: d.coords.width * z,
+                    height: d.coords.height * z,
+                    scaleY: 1,
+                    lw: lw,
+                    padding: 0,
+                });
+            });
+        });
+    },
+
     _loadTexts: function (callback) {
         if (this.robust || this.fluidbook.settings.searchWordSelectionAlgorithm === 'expression') {
             loadJSLibrary(this.fluidbook.loader.getURL('data/search.texts.js?j=' + this.fluidbook.settings.cacheDate), function () {
@@ -704,9 +757,11 @@ FluidbookSearch.prototype = {
             }
         }
 
-        var padding = position.height * 0.2;
+
+        var padding = position.padding == undefined ? position.height * 0.2 : position.padding;
+
         var z = this.fluidbook.settings.cssScale;
-        var h = $('<div class="highlight' + (this.robust ? ' robust' : '') + '" data-color="' + color + '"></div>');
+
         var coords = {
             top: (position.y - position.height - padding * 2) * z,
             left: (position.x + x - padding) * z + offset,
@@ -744,11 +799,14 @@ FluidbookSearch.prototype = {
             allCoords.push(coords);
         }
 
+        console.log(allCoords);
+
         $.each(allCoords, function (i, coords) {
             var hash = coords.top + '|' + coords.left + '|' + coords.width + '|' + coords.height;
             if ($('[data-hash="' + hash + '"]').length > 0) {
                 return;
             }
+            var h = $('<div class="highlight' + ($this.robust ? ' robust' : '') + '" data-color="' + color + '"></div>');
             $(h).attr('data-hash', hash);
             $(h).css(coords).transform(transform);
             if ($this.fluidbook.elasticslide.isActive()) {