content-ui.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright 2018 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * SingleFile is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * SingleFile is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with SingleFile. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /* global document, getComputedStyle, addEventListener, removeEventListener, requestAnimationFrame, scrollX, scrollY */
  21. this.singlefile.ui = this.singlefile.ui || (() => {
  22. const MASK_TAGNAME = "singlefile-mask";
  23. const PROGRESS_BAR_TAGNAME = "singlefile-progress-var";
  24. const SELECT_PX_THRESHOLD = 8;
  25. let selectedAreaElement;
  26. return {
  27. init() {
  28. let maskElement = document.querySelector(MASK_TAGNAME);
  29. if (!maskElement) {
  30. requestAnimationFrame(() => {
  31. maskElement = createElement(MASK_TAGNAME, document.body);
  32. maskElement.style.setProperty("opacity", 0, "important");
  33. maskElement.style.setProperty("background-color", "transparent", "important");
  34. maskElement.offsetWidth;
  35. maskElement.style.setProperty("position", "fixed", "important");
  36. maskElement.style.setProperty("top", "0", "important");
  37. maskElement.style.setProperty("left", "0", "important");
  38. maskElement.style.setProperty("width", "100%", "important");
  39. maskElement.style.setProperty("height", "100%", "important");
  40. maskElement.style.setProperty("z-index", 2147483647, "important");
  41. maskElement.style.setProperty("transition", "opacity 250ms", "important");
  42. maskElement.style.setProperty("will-change", "opacity, background-color", "important");
  43. const progressBarElement = createElement(PROGRESS_BAR_TAGNAME, maskElement);
  44. progressBarElement.style.setProperty("background-color", "white", "important");
  45. progressBarElement.style.setProperty("position", "fixed", "important");
  46. progressBarElement.style.setProperty("top", "0", "important");
  47. progressBarElement.style.setProperty("left", "0", "important");
  48. progressBarElement.style.setProperty("width", "0", "important");
  49. progressBarElement.style.setProperty("height", "8px", "important");
  50. progressBarElement.style.setProperty("transition", "width 100ms", "important");
  51. progressBarElement.style.setProperty("will-change", "width", "important");
  52. maskElement.offsetWidth;
  53. maskElement.style.setProperty("background-color", "black", "important");
  54. maskElement.style.setProperty("opacity", .3, "important");
  55. document.body.offsetWidth;
  56. });
  57. }
  58. },
  59. onprogress(index, maxIndex) {
  60. const progressBarElement = document.querySelector(PROGRESS_BAR_TAGNAME);
  61. if (progressBarElement && maxIndex) {
  62. const width = Math.floor((index / maxIndex) * 100) + "%";
  63. if (progressBarElement.style.width != width) {
  64. requestAnimationFrame(() => progressBarElement.style.setProperty("width", Math.floor((index / maxIndex) * 100) + "%", "important"));
  65. }
  66. }
  67. },
  68. end() {
  69. const maskElement = document.querySelector(MASK_TAGNAME);
  70. if (maskElement) {
  71. requestAnimationFrame(() => maskElement.remove());
  72. }
  73. },
  74. getSelectedArea
  75. };
  76. function getSelectedArea() {
  77. return new Promise(resolve => {
  78. addEventListener("mousemove", mousemoveListener, true);
  79. addEventListener("click", clickListener, true);
  80. addEventListener("keyup", keypressListener, true);
  81. function mousemoveListener(event) {
  82. const targetElement = getTarget(event);
  83. if (targetElement) {
  84. selectedAreaElement = targetElement;
  85. moveAreaSelector(targetElement);
  86. }
  87. }
  88. function clickListener(event) {
  89. select(selectedAreaElement);
  90. event.preventDefault();
  91. event.stopPropagation();
  92. }
  93. function keypressListener(event) {
  94. if (event.key == "Escape") {
  95. select();
  96. }
  97. }
  98. function select(selectedElement) {
  99. removeEventListener("mousemove", mousemoveListener, true);
  100. removeEventListener("click", clickListener, true);
  101. removeEventListener("keyup", keypressListener, true);
  102. getAreaSelector().remove();
  103. resolve(selectedElement);
  104. selectedAreaElement = null;
  105. }
  106. });
  107. }
  108. function getTarget(event) {
  109. let newTarget, target = event.target, boundingRect = target.getBoundingClientRect();
  110. newTarget = determineTargetElement(target, event.clientX - boundingRect.left, getMatchedParents(target, "left"));
  111. if (newTarget == target) {
  112. newTarget = determineTargetElement(target, boundingRect.left + boundingRect.width - event.clientX, getMatchedParents(target, "right"));
  113. }
  114. if (newTarget == target) {
  115. newTarget = determineTargetElement(target, event.clientY - boundingRect.top, getMatchedParents(target, "top"));
  116. }
  117. if (newTarget == target) {
  118. newTarget = determineTargetElement(target, boundingRect.top + boundingRect.height - event.clientY, getMatchedParents(target, "bottom"));
  119. }
  120. target = newTarget;
  121. while (target && target.clientWidth <= SELECT_PX_THRESHOLD && target.clientHeight <= SELECT_PX_THRESHOLD) {
  122. target = target.parentElement;
  123. }
  124. return target;
  125. }
  126. function moveAreaSelector(target) {
  127. const selectorElement = getAreaSelector();
  128. const boundingRect = target.getBoundingClientRect();
  129. selectorElement.style.setProperty("top", (scrollY + boundingRect.top - 10) + "px");
  130. selectorElement.style.setProperty("left", (scrollX + boundingRect.left - 10) + "px");
  131. selectorElement.style.setProperty("width", (boundingRect.width + 20) + "px");
  132. selectorElement.style.setProperty("height", (boundingRect.height + 20) + "px");
  133. }
  134. function getAreaSelector() {
  135. let selectorElement = document.querySelector("single-file-selection-zone");
  136. if (!selectorElement) {
  137. selectorElement = createElement("single-file-selection-zone", document.body);
  138. selectorElement.style.setProperty("box-sizing", "border-box", "important");
  139. selectorElement.style.setProperty("background-color", "#3ea9d7", "important");
  140. selectorElement.style.setProperty("border", "10px solid #0b4892", "important");
  141. selectorElement.style.setProperty("border-radius", "2px", "important");
  142. selectorElement.style.setProperty("opacity", ".25", "important");
  143. selectorElement.style.setProperty("pointer-events", "none", "important");
  144. selectorElement.style.setProperty("position", "absolute", "important");
  145. selectorElement.style.setProperty("transition", "all 100ms", "important");
  146. selectorElement.style.setProperty("cursor", "pointer", "important");
  147. selectorElement.style.setProperty("z-index", "2147483647", "important");
  148. selectorElement.style.removeProperty("border-inline-end");
  149. selectorElement.style.removeProperty("border-inline-start");
  150. selectorElement.style.removeProperty("inline-size");
  151. selectorElement.style.removeProperty("block-size");
  152. selectorElement.style.removeProperty("inset-block-start");
  153. selectorElement.style.removeProperty("inset-inline-end");
  154. selectorElement.style.removeProperty("inset-block-end");
  155. selectorElement.style.removeProperty("inset-inline-start");
  156. }
  157. return selectorElement;
  158. }
  159. function getMatchedParents(target, property) {
  160. let element = target, matchedParent, parents = [];
  161. do {
  162. const boundingRect = element.getBoundingClientRect();
  163. if (element.parentElement) {
  164. const parentBoundingRect = element.parentElement.getBoundingClientRect();
  165. matchedParent = Math.abs(parentBoundingRect[property] - boundingRect[property]) <= SELECT_PX_THRESHOLD;
  166. if (matchedParent) {
  167. if (element.parentElement.clientWidth > SELECT_PX_THRESHOLD && element.parentElement.clientHeight > SELECT_PX_THRESHOLD &&
  168. ((element.parentElement.clientWidth - element.clientWidth > SELECT_PX_THRESHOLD) || (element.parentElement.clientHeight - element.clientHeight > SELECT_PX_THRESHOLD))) {
  169. parents.push(element.parentElement);
  170. }
  171. element = element.parentElement;
  172. }
  173. } else {
  174. matchedParent = false;
  175. }
  176. } while (matchedParent && element);
  177. return parents;
  178. }
  179. function determineTargetElement(target, widthDistance, parents) {
  180. if (Math.floor(widthDistance / SELECT_PX_THRESHOLD) <= parents.length) {
  181. target = parents[parents.length - Math.floor(widthDistance / SELECT_PX_THRESHOLD) - 1];
  182. }
  183. return target;
  184. }
  185. function createElement(tagName, parentElement) {
  186. const element = document.createElement(tagName);
  187. parentElement.appendChild(element);
  188. Array.from(getComputedStyle(element)).forEach(property => element.style.setProperty(property, "initial", "important"));
  189. return element;
  190. }
  191. })();