@d-i-t-a/reader 2.0.0-beta.14 → 2.0.0-beta.17

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
@@ -55643,6 +55643,14 @@ var IFrameNavigator = class extends import_events.default {
55643
55643
  this.nextChapterBottomAnchorElement.style.display = "none";
55644
55644
  if (this.previousChapterTopAnchorElement)
55645
55645
  this.previousChapterTopAnchorElement.style.display = "none";
55646
+ if (this.keyboardEventHandler) {
55647
+ this.keyboardEventHandler.onBackwardSwipe = this.handlePreviousChapterClick.bind(this);
55648
+ this.keyboardEventHandler.onForwardSwipe = this.handleNextChapterClick.bind(this);
55649
+ }
55650
+ if (this.touchEventHandler) {
55651
+ this.touchEventHandler.onBackwardSwipe = this.handlePreviousPageClick.bind(this);
55652
+ this.touchEventHandler.onForwardSwipe = this.handleNextPageClick.bind(this);
55653
+ }
55646
55654
  } else {
55647
55655
  this.settings.isPaginated().then((paginated) => {
55648
55656
  if (paginated) {
@@ -59707,11 +59715,13 @@ init_polyfills();
59707
59715
  var DEFAULT_BACKGROUND_COLOR_OPACITY2 = 0.5;
59708
59716
  var LineFocusModule = class {
59709
59717
  constructor(delegate, properties, highlighter, api) {
59718
+ this.hasEventListener = false;
59710
59719
  this.lines = [];
59711
59720
  this.index = 0;
59712
59721
  this.isActive = false;
59713
59722
  this.isDebug = false;
59714
59723
  this.lineFocusContainer = document.getElementById(`lineFocusContainer`);
59724
+ this.readerContainer = document.getElementById(`D2Reader-Container`);
59715
59725
  this.lineFocusTopBlinder = document.getElementById(`lineFocusTopBlinder`);
59716
59726
  this.lineFocusBottomBlinder = document.getElementById(`lineFocusBottomBlinder`);
59717
59727
  this.wrapperHeight = void 0;
@@ -59732,6 +59742,44 @@ var LineFocusModule = class {
59732
59742
  }
59733
59743
  async start() {
59734
59744
  this.delegate.lineFocusModule = this;
59745
+ const wrapper = findRequiredElement(document, "#iframe-wrapper");
59746
+ if (wrapper.style.height.length > 0) {
59747
+ this.wrapperHeight = wrapper.style.height;
59748
+ if (this.lineFocusContainer && this.lineFocusContainer.style.height.length == 0)
59749
+ this.lineFocusContainer.style.height = this.wrapperHeight;
59750
+ if (this.readerContainer && this.readerContainer.style.height.length == 0) {
59751
+ this.readerContainer.style.height = this.wrapperHeight;
59752
+ }
59753
+ if (this.readerContainer) {
59754
+ this.readerContainer.style.overflow = "hidden";
59755
+ }
59756
+ }
59757
+ }
59758
+ keydown(event) {
59759
+ if (event instanceof KeyboardEvent) {
59760
+ const key = event.key;
59761
+ switch (key) {
59762
+ case "ArrowUp":
59763
+ event.stopPropagation();
59764
+ break;
59765
+ case "ArrowDown":
59766
+ event.stopPropagation();
59767
+ break;
59768
+ }
59769
+ }
59770
+ }
59771
+ keyup(event) {
59772
+ if (event instanceof KeyboardEvent) {
59773
+ const key = event.key;
59774
+ switch (key) {
59775
+ case "ArrowUp":
59776
+ this.lineUp();
59777
+ break;
59778
+ case "ArrowDown":
59779
+ this.lineDown();
59780
+ break;
59781
+ }
59782
+ }
59735
59783
  }
59736
59784
  handleResize() {
59737
59785
  if (this.isActive) {
@@ -59739,27 +59787,39 @@ var LineFocusModule = class {
59739
59787
  }
59740
59788
  }
59741
59789
  async enableLineFocus() {
59742
- if (!this.isActive) {
59743
- const wrapper = findRequiredElement(document, "#iframe-wrapper");
59744
- wrapper.style.overflow = "hidden";
59745
- if (wrapper.style.height.length > 0) {
59746
- this.wrapperHeight = wrapper.style.height;
59747
- }
59748
- wrapper.style.height = "100vh";
59749
- }
59750
59790
  this.isActive = true;
59751
59791
  await this.delegate.settings.scroll(true);
59752
59792
  this.lineFocus();
59793
+ if (!this.hasEventListener) {
59794
+ this.hasEventListener = true;
59795
+ addEventListenerOptional(document, "keydown", this.keydown.bind(this));
59796
+ addEventListenerOptional(document, "keyup", this.keyup.bind(this));
59797
+ addEventListenerOptional(this.delegate.iframes[0].contentDocument, "keydown", this.keydown.bind(this));
59798
+ addEventListenerOptional(this.delegate.iframes[0].contentDocument, "keyup", this.keyup.bind(this));
59799
+ }
59753
59800
  }
59754
59801
  disableLineFocus(resetHeight = true) {
59755
59802
  this.isActive = false;
59803
+ if (this.hasEventListener) {
59804
+ this.hasEventListener = false;
59805
+ removeEventListenerOptional(document, "keydown", this.keydown.bind(this));
59806
+ removeEventListenerOptional(document, "keyup", this.keyup.bind(this));
59807
+ removeEventListenerOptional(this.delegate.iframes[0].contentDocument, "keydown", this.keydown.bind(this));
59808
+ removeEventListenerOptional(this.delegate.iframes[0].contentDocument, "keyup", this.keyup.bind(this));
59809
+ }
59756
59810
  const wrapper = findRequiredElement(document, "#iframe-wrapper");
59757
- wrapper.style.overflow = "auto";
59758
59811
  if (this.wrapperHeight) {
59759
59812
  wrapper.style.height = this.wrapperHeight;
59760
59813
  } else if (resetHeight) {
59814
+ this.index = 0;
59761
59815
  wrapper.style.removeProperty("height");
59762
59816
  }
59817
+ if (!resetHeight) {
59818
+ this.index = 0;
59819
+ }
59820
+ const doc = this.delegate.iframes[0].contentDocument;
59821
+ const html = findIframeElement(doc, "html");
59822
+ html.style.removeProperty("--USER__maxMediaHeight");
59763
59823
  this.wrapperHeight = void 0;
59764
59824
  if (this.lineFocusContainer)
59765
59825
  this.lineFocusContainer.style.display = "none";
@@ -59779,6 +59839,10 @@ var LineFocusModule = class {
59779
59839
  }
59780
59840
  lineFocus() {
59781
59841
  const wrapper = findRequiredElement(document, "#iframe-wrapper");
59842
+ const doc = this.delegate.iframes[0].contentDocument;
59843
+ const html = findIframeElement(doc, "html");
59844
+ let maxHeight = this.properties.maxHeight ? getHeight() * this.properties.maxHeight / 100 : getHeight() / 2;
59845
+ html.style.setProperty("--USER__maxMediaHeight", maxHeight + "px");
59782
59846
  function insertAfter(referenceNode, newNode) {
59783
59847
  referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
59784
59848
  }
@@ -59810,8 +59874,6 @@ var LineFocusModule = class {
59810
59874
  insertAfter(wrapper, divAfter2);
59811
59875
  }
59812
59876
  this.lines = [];
59813
- const self2 = this;
59814
- const doc = self2.delegate.iframes[0].contentDocument;
59815
59877
  if (doc) {
59816
59878
  let random_rgba = function() {
59817
59879
  const o = Math.round, r = Math.random, s = 255;
@@ -59974,10 +60036,8 @@ var LineFocusModule = class {
59974
60036
  }
59975
60037
  currentLine() {
59976
60038
  let current = this.lines[this.index];
59977
- let previous = this.lines[this.index];
59978
- let next = this.lines[this.index];
59979
- let top = previous.style.top;
59980
- let bottom = parseInt(next.style.top.replace("px", "")) + parseInt(next.style.height.replace("px", ""));
60039
+ let top = current.style.top;
60040
+ let bottom = parseInt(current.style.top.replace("px", "")) + parseInt(current.style.height.replace("px", ""));
59981
60041
  let height = bottom - parseInt(top.replace("px", ""));
59982
60042
  if (this.lineFocusContainer) {
59983
60043
  let lineFocusHeight = parseInt(getComputedStyle(this.lineFocusContainer).height.replace("px", ""));
@@ -59987,7 +60047,6 @@ var LineFocusModule = class {
59987
60047
  if (this.lineFocusBottomBlinder)
59988
60048
  this.lineFocusBottomBlinder.style.height = blindersHeight + "px";
59989
60049
  }
59990
- current.focus();
59991
60050
  current.scrollIntoView({
59992
60051
  block: "center",
59993
60052
  behavior: "smooth"
@@ -60000,10 +60059,8 @@ var LineFocusModule = class {
60000
60059
  this.index = this.lines.length - 1;
60001
60060
  }
60002
60061
  let current = this.lines[this.index];
60003
- let previous = this.lines[this.index];
60004
- let next = this.lines[this.index];
60005
- let top = previous.style.top;
60006
- let bottom = parseInt(next.style.top.replace("px", "")) + parseInt(next.style.height.replace("px", ""));
60062
+ let top = current.style.top;
60063
+ let bottom = parseInt(current.style.top.replace("px", "")) + parseInt(current.style.height.replace("px", ""));
60007
60064
  let height = bottom - parseInt(top.replace("px", ""));
60008
60065
  if (this.lineFocusContainer) {
60009
60066
  let lineFocusHeight = parseInt(getComputedStyle(this.lineFocusContainer).height.replace("px", ""));
@@ -60013,7 +60070,6 @@ var LineFocusModule = class {
60013
60070
  if (this.lineFocusBottomBlinder)
60014
60071
  this.lineFocusBottomBlinder.style.height = blindersHeight + "px";
60015
60072
  }
60016
- current.focus();
60017
60073
  current.scrollIntoView({
60018
60074
  block: "center",
60019
60075
  behavior: "smooth"
@@ -60027,10 +60083,8 @@ var LineFocusModule = class {
60027
60083
  this.index = 0;
60028
60084
  }
60029
60085
  let current = this.lines[this.index];
60030
- let previous = this.lines[this.index];
60031
- let next = this.lines[this.index];
60032
- let top = previous.style.top;
60033
- let bottom = parseInt(next.style.top.replace("px", "")) + parseInt(next.style.height.replace("px", ""));
60086
+ let top = current.style.top;
60087
+ let bottom = parseInt(current.style.top.replace("px", "")) + parseInt(current.style.height.replace("px", ""));
60034
60088
  let height = bottom - parseInt(top.replace("px", ""));
60035
60089
  if (this.lineFocusContainer) {
60036
60090
  let lineFocusHeight = parseInt(getComputedStyle(this.lineFocusContainer).height.replace("px", ""));
@@ -60040,7 +60094,6 @@ var LineFocusModule = class {
60040
60094
  if (this.lineFocusBottomBlinder)
60041
60095
  this.lineFocusBottomBlinder.style.height = blindersHeight + "px";
60042
60096
  }
60043
- current.focus();
60044
60097
  current.scrollIntoView({
60045
60098
  block: "center",
60046
60099
  behavior: "smooth"
@@ -60052,10 +60105,14 @@ var LineFocusModule = class {
60052
60105
  }
60053
60106
  findRects(parent) {
60054
60107
  const textNodes = this.findTextNodes(parent);
60108
+ const imageNodes = Array.from(parent.getElementsByTagName("img"));
60055
60109
  let newNodes = [];
60056
60110
  textNodes.forEach((node) => {
60057
60111
  newNodes.push(...this.measureTextNodes(node));
60058
60112
  });
60113
+ imageNodes.forEach((node) => {
60114
+ newNodes.push(...this.measureImageNodes(node));
60115
+ });
60059
60116
  return newNodes;
60060
60117
  }
60061
60118
  findTextNodes(parentElement, nodes = []) {
@@ -60088,6 +60145,21 @@ var LineFocusModule = class {
60088
60145
  }
60089
60146
  }
60090
60147
  }
60148
+ measureImageNodes(node) {
60149
+ try {
60150
+ const range = document.createRange();
60151
+ range.selectNode(node);
60152
+ const rect = Array.from(range.getClientRects());
60153
+ range.detach();
60154
+ return rect;
60155
+ } catch (error) {
60156
+ if (IS_DEV) {
60157
+ console.log("measureTextNode " + error);
60158
+ console.log("measureTextNode " + node);
60159
+ console.log(node.textContent);
60160
+ }
60161
+ }
60162
+ }
60091
60163
  };
60092
60164
 
60093
60165
  // src/reader.ts
@@ -60517,14 +60589,23 @@ var D2Reader = class {
60517
60589
  }
60518
60590
  async applyLineFocusSettings(userSettings) {
60519
60591
  if (userSettings.lines) {
60520
- if (this.lineFocusModule)
60592
+ if (this.lineFocusModule) {
60593
+ const lines = this.lineFocusModule.properties.lines ?? 1;
60594
+ this.lineFocusModule.index = this.lineFocusModule.index * lines / parseInt(userSettings.lines);
60595
+ this.lineFocusModule.index = Math.abs(parseInt(this.lineFocusModule.index.toFixed()));
60521
60596
  this.lineFocusModule.properties.lines = parseInt(userSettings.lines);
60522
- await this.lineFocusModule?.enableLineFocus();
60597
+ if (this.lineFocusModule.isActive) {
60598
+ await this.lineFocusModule.enableLineFocus();
60599
+ }
60600
+ }
60523
60601
  }
60524
60602
  if (userSettings.debug !== void 0) {
60525
- if (this.lineFocusModule)
60603
+ if (this.lineFocusModule) {
60526
60604
  this.lineFocusModule.isDebug = userSettings.debug;
60527
- await this.lineFocusModule?.enableLineFocus();
60605
+ if (this.lineFocusModule.isActive) {
60606
+ await this.lineFocusModule.enableLineFocus();
60607
+ }
60608
+ }
60528
60609
  }
60529
60610
  }
60530
60611
  lineUp() {