@d-i-t-a/reader 2.0.0-beta.12 → 2.0.0-beta.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/esm/index.js CHANGED
@@ -46855,7 +46855,7 @@ var ReflowableBookView = class {
46855
46855
  const wrapper = findRequiredElement(document, "#iframe-wrapper");
46856
46856
  if (this.scrollMode) {
46857
46857
  const leftHeight = wrapper.scrollTop;
46858
- const height = this.getScreenHeight();
46858
+ const height = this.getScreenHeight() - 40;
46859
46859
  const offset = leftHeight - height;
46860
46860
  if (offset >= 0) {
46861
46861
  wrapper.scrollTop = offset;
@@ -46881,7 +46881,7 @@ var ReflowableBookView = class {
46881
46881
  const wrapper = findRequiredElement(document, "#iframe-wrapper");
46882
46882
  if (this.scrollMode) {
46883
46883
  const leftHeight = wrapper.scrollTop;
46884
- const height = this.getScreenHeight();
46884
+ const height = this.getScreenHeight() - 40;
46885
46885
  const scrollHeight = this.scrollingElement.scrollHeight;
46886
46886
  const offset = leftHeight + height;
46887
46887
  if (offset < scrollHeight) {
@@ -52688,7 +52688,7 @@ var MediaOverlayModule = class {
52688
52688
  console.log("ontimeupdate");
52689
52689
  this.trackCurrentTime();
52690
52690
  };
52691
- this.ensureOnTimeUpdate = (remove) => {
52691
+ this.ensureOnTimeUpdate = (remove, replace) => {
52692
52692
  if (this.audioElement) {
52693
52693
  if (remove) {
52694
52694
  if (this.__ontimeupdate) {
@@ -52697,8 +52697,11 @@ var MediaOverlayModule = class {
52697
52697
  cancelAnimationFrame(this.myReq);
52698
52698
  }
52699
52699
  } else {
52700
- if (!this.__ontimeupdate) {
52700
+ if (!this.__ontimeupdate || replace) {
52701
52701
  this.__ontimeupdate = true;
52702
+ if (replace) {
52703
+ this.audioElement.removeEventListener("timeupdate", this.ontimeupdate);
52704
+ }
52702
52705
  this.audioElement.addEventListener("timeupdate", this.ontimeupdate);
52703
52706
  }
52704
52707
  }
@@ -52742,7 +52745,7 @@ var MediaOverlayModule = class {
52742
52745
  async playLink() {
52743
52746
  let link = this.currentLinks[this.currentLinkIndex];
52744
52747
  if (link?.Properties?.MediaOverlay) {
52745
- this.ensureOnTimeUpdate(false);
52748
+ this.ensureOnTimeUpdate(false, false);
52746
52749
  const moUrl = link.Properties?.MediaOverlay;
52747
52750
  const moUrlObjFull = new URL(moUrl, this.publication.manifestUrl);
52748
52751
  const moUrlFull = moUrlObjFull.toString();
@@ -52797,6 +52800,7 @@ var MediaOverlayModule = class {
52797
52800
  const timeToSeekTo = this.currentAudioBegin ? this.currentAudioBegin : 0;
52798
52801
  this.audioElement.currentTime = timeToSeekTo;
52799
52802
  await this.audioElement.play();
52803
+ this.ensureOnTimeUpdate(false, true);
52800
52804
  this.audioElement.volume = this.settings.volume;
52801
52805
  this.audioElement.playbackRate = this.settings.rate;
52802
52806
  } else {
@@ -53110,25 +53114,22 @@ var MediaOverlayModule = class {
53110
53114
  if (IS_DEV) {
53111
53115
  console.log("playMediaOverlaysAudio() - playClip() - _currentAudioElement.play()");
53112
53116
  }
53113
- this.ensureOnTimeUpdate(false);
53117
+ this.ensureOnTimeUpdate(false, false);
53114
53118
  this.audioElement.playbackRate = this.settings.rate;
53115
53119
  this.audioElement.volume = this.settings.volume;
53116
53120
  if (this.settings.playing) {
53117
- if (!initial) {
53118
- let checkReady = function() {
53119
- if (!self2.settings.resourceReady) {
53120
- setTimeout(checkReady, 200);
53121
- } else {
53122
- setTimeout(async () => {
53123
- await self2.audioElement.play();
53124
- }, self2.settings.wait * 1200);
53125
- }
53126
- };
53127
- let self2 = this;
53128
- checkReady();
53129
- } else {
53130
- await this.audioElement.play();
53131
- }
53121
+ let checkReady = function() {
53122
+ if (!self2.settings.resourceReady) {
53123
+ setTimeout(checkReady, 200);
53124
+ } else {
53125
+ setTimeout(async () => {
53126
+ await self2.audioElement.play();
53127
+ self2.ensureOnTimeUpdate(false, true);
53128
+ }, self2.settings.wait * 1e3);
53129
+ }
53130
+ };
53131
+ let self2 = this;
53132
+ checkReady();
53132
53133
  }
53133
53134
  } else {
53134
53135
  if (IS_DEV) {
@@ -53139,26 +53140,23 @@ var MediaOverlayModule = class {
53139
53140
  if (IS_DEV) {
53140
53141
  console.log("playMediaOverlaysAudio() - playClip() - ontimeupdateSeeked - .play()");
53141
53142
  }
53142
- this.ensureOnTimeUpdate(false);
53143
+ this.ensureOnTimeUpdate(false, false);
53143
53144
  if (this.audioElement) {
53144
53145
  this.audioElement.playbackRate = this.settings.rate;
53145
53146
  this.audioElement.volume = this.settings.volume;
53146
53147
  if (this.settings.playing) {
53147
- if (!initial) {
53148
- let checkReady = function() {
53149
- if (!self2.settings.resourceReady) {
53150
- setTimeout(checkReady, 200);
53151
- } else {
53152
- setTimeout(async () => {
53153
- await self2.audioElement.play();
53154
- }, self2.settings.wait * 1200);
53155
- }
53156
- };
53157
- let self2 = this;
53158
- checkReady();
53159
- } else {
53160
- await this.audioElement.play();
53161
- }
53148
+ let checkReady = function() {
53149
+ if (!self2.settings.resourceReady) {
53150
+ setTimeout(checkReady, 200);
53151
+ } else {
53152
+ setTimeout(async () => {
53153
+ await self2.audioElement.play();
53154
+ self2.ensureOnTimeUpdate(false, true);
53155
+ }, self2.settings.wait * 1e3);
53156
+ }
53157
+ };
53158
+ let self2 = this;
53159
+ checkReady();
53162
53160
  }
53163
53161
  }
53164
53162
  };
@@ -53167,7 +53165,7 @@ var MediaOverlayModule = class {
53167
53165
  }
53168
53166
  } else {
53169
53167
  const contiguous = this.previousAudioUrl === this.currentAudioUrl && typeof this.previousAudioEnd !== "undefined" && this.previousAudioEnd > timeToSeekTo - 0.02 && this.previousAudioEnd <= timeToSeekTo && this.audioElement.currentTime >= timeToSeekTo - 0.1;
53170
- this.ensureOnTimeUpdate(false);
53168
+ this.ensureOnTimeUpdate(false, false);
53171
53169
  if (contiguous) {
53172
53170
  if (IS_DEV) {
53173
53171
  console.log("playMediaOverlaysAudio() - playClip() - ensureOnTimeUpdate");
@@ -58751,10 +58749,12 @@ var TTSModule2 = class {
58751
58749
  }
58752
58750
  });
58753
58751
  }
58754
- cancel() {
58755
- if (this.api?.stopped)
58756
- this.api?.stopped();
58757
- this.delegate.emit("readaloud.stopped", "stopped");
58752
+ cancel(api = true) {
58753
+ if (api) {
58754
+ if (this.api?.stopped)
58755
+ this.api?.stopped();
58756
+ this.delegate.emit("readaloud.stopped", "stopped");
58757
+ }
58758
58758
  this.userScrolled = false;
58759
58759
  this.speaking = false;
58760
58760
  setTimeout(() => {
@@ -58771,7 +58771,7 @@ var TTSModule2 = class {
58771
58771
  this.delegate.emit("readaloud.started", "started");
58772
58772
  const self2 = this;
58773
58773
  this.userScrolled = false;
58774
- this.cancel();
58774
+ this.cancel(false);
58775
58775
  let utterance;
58776
58776
  if (partial) {
58777
58777
  let iframe = document.querySelector("main#iframe-wrapper iframe");
@@ -58911,7 +58911,9 @@ var TTSModule2 = class {
58911
58911
  callback();
58912
58912
  }
58913
58913
  speakPlay() {
58914
- this.cancel();
58914
+ this.cancel(false);
58915
+ if (this.api?.started)
58916
+ this.api?.started();
58915
58917
  this.delegate.emit("readaloud.started", "started");
58916
58918
  let self2 = this;
58917
58919
  let iframe = document.querySelector("main#iframe-wrapper iframe");
@@ -58921,47 +58923,27 @@ var TTSModule2 = class {
58921
58923
  let node = self2.highlighter.visibleTextRects[0];
58922
58924
  let doc = self2.delegate.iframes[0].contentDocument;
58923
58925
  if (doc) {
58924
- let isOutsideViewport = function(rect) {
58925
- const windowLeft = window.scrollX;
58926
- const windowRight = windowLeft + window.innerWidth;
58927
- const right = rect.left + rect.width;
58928
- const bottom = rect.top + rect.height;
58929
- const windowTop = window.scrollY;
58930
- const windowBottom = windowTop + window.innerHeight;
58931
- const isAbove = bottom < windowTop;
58932
- const isBelow = rect.top > windowBottom;
58933
- const isLeft = right < windowLeft;
58934
- const isRight = rect.left > windowRight;
58935
- return isAbove || isBelow || isLeft || isRight;
58936
- };
58937
58926
  const range = self2.highlighter.dom(doc.body).getWindow().document.createRange();
58938
58927
  const selection = self2.highlighter.dom(self2.delegate.iframes[0].contentDocument?.body).getSelection();
58939
58928
  selection.removeAllRanges();
58940
58929
  range.selectNodeContents(node.node);
58941
58930
  selection.addRange(range);
58942
- const clientRects = getClientRectsNoOverlap(range, false);
58943
58931
  let index2 = 0;
58944
- for (const rect of clientRects) {
58945
- if (!isOutsideViewport(rect)) {
58946
- const endNode = selection.focusNode;
58947
- const endOffset = selection.focusOffset;
58948
- selection.collapse(selection.anchorNode, selection.anchorOffset);
58949
- for (let i = 0; i < index2; i++) {
58950
- selection.modify("move", "forward", "line");
58951
- }
58952
- selection.extend(endNode, endOffset);
58953
- selection.collapse(selection.anchorNode, selection.anchorOffset);
58954
- if (rootEl) {
58955
- const idx = self2.findTtsQueueItemIndex(ttsQueue, selection.anchorNode, selection.focusNode, selection.focusOffset, rootEl);
58956
- if (idx >= 0) {
58957
- ttsQueueIndex = idx;
58958
- }
58959
- }
58960
- selection.removeAllRanges();
58961
- break;
58932
+ const endNode = selection.focusNode;
58933
+ const endOffset = selection.focusOffset;
58934
+ selection.collapse(selection.anchorNode, selection.anchorOffset);
58935
+ for (let i = 0; i < index2; i++) {
58936
+ selection.modify("move", "forward", "line");
58937
+ }
58938
+ selection.extend(endNode, endOffset);
58939
+ selection.collapse(selection.anchorNode, selection.anchorOffset);
58940
+ if (rootEl) {
58941
+ const idx = self2.findTtsQueueItemIndex(ttsQueue, selection.anchorNode, selection.focusNode, selection.focusOffset, rootEl);
58942
+ if (idx >= 0) {
58943
+ ttsQueueIndex = idx;
58962
58944
  }
58963
- index2++;
58964
58945
  }
58946
+ selection.removeAllRanges();
58965
58947
  }
58966
58948
  };
58967
58949
  const ttsQueue = this.generateTtsQueue(rootEl, true);
@@ -58975,7 +58957,7 @@ var TTSModule2 = class {
58975
58957
  }
58976
58958
  setTimeout(() => {
58977
58959
  this.startTTSSession(ttsQueue, ttsQueueIndex);
58978
- }, 100);
58960
+ }, 200);
58979
58961
  }
58980
58962
  }
58981
58963
  speakPause() {
@@ -59663,7 +59645,7 @@ var DefinitionsModule = class {
59663
59645
  }
59664
59646
  });
59665
59647
  }, { threshold: 1 });
59666
- if (highlightFragments) {
59648
+ if (highlightFragments && highlightFragments.length > 0) {
59667
59649
  observer.observe(highlightFragments[0]);
59668
59650
  }
59669
59651
  });
@@ -59730,6 +59712,7 @@ var LineFocusModule = class {
59730
59712
  this.isActive = false;
59731
59713
  this.isDebug = false;
59732
59714
  this.lineFocusContainer = document.getElementById(`lineFocusContainer`);
59715
+ this.readerContainer = document.getElementById(`D2Reader-Container`);
59733
59716
  this.lineFocusTopBlinder = document.getElementById(`lineFocusTopBlinder`);
59734
59717
  this.lineFocusBottomBlinder = document.getElementById(`lineFocusBottomBlinder`);
59735
59718
  this.wrapperHeight = void 0;
@@ -59750,6 +59733,16 @@ var LineFocusModule = class {
59750
59733
  }
59751
59734
  async start() {
59752
59735
  this.delegate.lineFocusModule = this;
59736
+ const wrapper = findRequiredElement(document, "#iframe-wrapper");
59737
+ if (wrapper.style.height.length > 0) {
59738
+ this.wrapperHeight = wrapper.style.height;
59739
+ if (this.lineFocusContainer)
59740
+ this.lineFocusContainer.style.height = this.wrapperHeight;
59741
+ if (this.readerContainer) {
59742
+ this.readerContainer.style.height = this.wrapperHeight;
59743
+ this.readerContainer.style.overflow = "hidden";
59744
+ }
59745
+ }
59753
59746
  }
59754
59747
  handleResize() {
59755
59748
  if (this.isActive) {
@@ -59759,7 +59752,6 @@ var LineFocusModule = class {
59759
59752
  async enableLineFocus() {
59760
59753
  if (!this.isActive) {
59761
59754
  const wrapper = findRequiredElement(document, "#iframe-wrapper");
59762
- wrapper.style.overflow = "hidden";
59763
59755
  if (wrapper.style.height.length > 0) {
59764
59756
  this.wrapperHeight = wrapper.style.height;
59765
59757
  }
@@ -59772,12 +59764,14 @@ var LineFocusModule = class {
59772
59764
  disableLineFocus(resetHeight = true) {
59773
59765
  this.isActive = false;
59774
59766
  const wrapper = findRequiredElement(document, "#iframe-wrapper");
59775
- wrapper.style.overflow = "auto";
59776
59767
  if (this.wrapperHeight) {
59777
59768
  wrapper.style.height = this.wrapperHeight;
59778
59769
  } else if (resetHeight) {
59779
59770
  wrapper.style.removeProperty("height");
59780
59771
  }
59772
+ const doc = this.delegate.iframes[0].contentDocument;
59773
+ const html = findIframeElement(doc, "html");
59774
+ html.style.removeProperty("--USER__maxMediaHeight");
59781
59775
  this.wrapperHeight = void 0;
59782
59776
  if (this.lineFocusContainer)
59783
59777
  this.lineFocusContainer.style.display = "none";
@@ -59797,6 +59791,10 @@ var LineFocusModule = class {
59797
59791
  }
59798
59792
  lineFocus() {
59799
59793
  const wrapper = findRequiredElement(document, "#iframe-wrapper");
59794
+ const doc = this.delegate.iframes[0].contentDocument;
59795
+ const html = findIframeElement(doc, "html");
59796
+ let maxHeight = this.properties.maxHeight ? getHeight() * this.properties.maxHeight / 100 : getHeight() / 2;
59797
+ html.style.setProperty("--USER__maxMediaHeight", maxHeight + "px");
59800
59798
  function insertAfter(referenceNode, newNode) {
59801
59799
  referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
59802
59800
  }
@@ -59828,8 +59826,6 @@ var LineFocusModule = class {
59828
59826
  insertAfter(wrapper, divAfter2);
59829
59827
  }
59830
59828
  this.lines = [];
59831
- const self2 = this;
59832
- const doc = self2.delegate.iframes[0].contentDocument;
59833
59829
  if (doc) {
59834
59830
  let random_rgba = function() {
59835
59831
  const o = Math.round, r = Math.random, s = 255;
@@ -59992,10 +59988,8 @@ var LineFocusModule = class {
59992
59988
  }
59993
59989
  currentLine() {
59994
59990
  let current = this.lines[this.index];
59995
- let previous = this.lines[this.index];
59996
- let next = this.lines[this.index];
59997
- let top = previous.style.top;
59998
- let bottom = parseInt(next.style.top.replace("px", "")) + parseInt(next.style.height.replace("px", ""));
59991
+ let top = current.style.top;
59992
+ let bottom = parseInt(current.style.top.replace("px", "")) + parseInt(current.style.height.replace("px", ""));
59999
59993
  let height = bottom - parseInt(top.replace("px", ""));
60000
59994
  if (this.lineFocusContainer) {
60001
59995
  let lineFocusHeight = parseInt(getComputedStyle(this.lineFocusContainer).height.replace("px", ""));
@@ -60018,10 +60012,8 @@ var LineFocusModule = class {
60018
60012
  this.index = this.lines.length - 1;
60019
60013
  }
60020
60014
  let current = this.lines[this.index];
60021
- let previous = this.lines[this.index];
60022
- let next = this.lines[this.index];
60023
- let top = previous.style.top;
60024
- let bottom = parseInt(next.style.top.replace("px", "")) + parseInt(next.style.height.replace("px", ""));
60015
+ let top = current.style.top;
60016
+ let bottom = parseInt(current.style.top.replace("px", "")) + parseInt(current.style.height.replace("px", ""));
60025
60017
  let height = bottom - parseInt(top.replace("px", ""));
60026
60018
  if (this.lineFocusContainer) {
60027
60019
  let lineFocusHeight = parseInt(getComputedStyle(this.lineFocusContainer).height.replace("px", ""));
@@ -60045,10 +60037,8 @@ var LineFocusModule = class {
60045
60037
  this.index = 0;
60046
60038
  }
60047
60039
  let current = this.lines[this.index];
60048
- let previous = this.lines[this.index];
60049
- let next = this.lines[this.index];
60050
- let top = previous.style.top;
60051
- let bottom = parseInt(next.style.top.replace("px", "")) + parseInt(next.style.height.replace("px", ""));
60040
+ let top = current.style.top;
60041
+ let bottom = parseInt(current.style.top.replace("px", "")) + parseInt(current.style.height.replace("px", ""));
60052
60042
  let height = bottom - parseInt(top.replace("px", ""));
60053
60043
  if (this.lineFocusContainer) {
60054
60044
  let lineFocusHeight = parseInt(getComputedStyle(this.lineFocusContainer).height.replace("px", ""));
@@ -60070,10 +60060,14 @@ var LineFocusModule = class {
60070
60060
  }
60071
60061
  findRects(parent) {
60072
60062
  const textNodes = this.findTextNodes(parent);
60063
+ const imageNodes = Array.from(parent.getElementsByTagName("img"));
60073
60064
  let newNodes = [];
60074
60065
  textNodes.forEach((node) => {
60075
60066
  newNodes.push(...this.measureTextNodes(node));
60076
60067
  });
60068
+ imageNodes.forEach((node) => {
60069
+ newNodes.push(...this.measureImageNodes(node));
60070
+ });
60077
60071
  return newNodes;
60078
60072
  }
60079
60073
  findTextNodes(parentElement, nodes = []) {
@@ -60106,6 +60100,21 @@ var LineFocusModule = class {
60106
60100
  }
60107
60101
  }
60108
60102
  }
60103
+ measureImageNodes(node) {
60104
+ try {
60105
+ const range = document.createRange();
60106
+ range.selectNode(node);
60107
+ const rect = Array.from(range.getClientRects());
60108
+ range.detach();
60109
+ return rect;
60110
+ } catch (error) {
60111
+ if (IS_DEV) {
60112
+ console.log("measureTextNode " + error);
60113
+ console.log("measureTextNode " + node);
60114
+ console.log(node.textContent);
60115
+ }
60116
+ }
60117
+ }
60109
60118
  };
60110
60119
 
60111
60120
  // src/reader.ts
@@ -60535,14 +60544,23 @@ var D2Reader = class {
60535
60544
  }
60536
60545
  async applyLineFocusSettings(userSettings) {
60537
60546
  if (userSettings.lines) {
60538
- if (this.lineFocusModule)
60547
+ if (this.lineFocusModule) {
60548
+ const lines = this.lineFocusModule.properties.lines ?? 1;
60549
+ this.lineFocusModule.index = this.lineFocusModule.index * lines / parseInt(userSettings.lines);
60550
+ this.lineFocusModule.index = Math.abs(parseInt(this.lineFocusModule.index.toFixed()));
60539
60551
  this.lineFocusModule.properties.lines = parseInt(userSettings.lines);
60540
- await this.lineFocusModule?.enableLineFocus();
60552
+ if (this.lineFocusModule.isActive) {
60553
+ await this.lineFocusModule.enableLineFocus();
60554
+ }
60555
+ }
60541
60556
  }
60542
60557
  if (userSettings.debug !== void 0) {
60543
- if (this.lineFocusModule)
60558
+ if (this.lineFocusModule) {
60544
60559
  this.lineFocusModule.isDebug = userSettings.debug;
60545
- await this.lineFocusModule?.enableLineFocus();
60560
+ if (this.lineFocusModule.isActive) {
60561
+ await this.lineFocusModule.enableLineFocus();
60562
+ }
60563
+ }
60546
60564
  }
60547
60565
  }
60548
60566
  lineUp() {