content-ui.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 SELECTION_ZONE_TAGNAME = "single-file-selection-zone";
  25. const SELECT_PX_THRESHOLD = 8;
  26. let selectedAreaElement;
  27. return {
  28. init() {
  29. let maskElement = document.querySelector(MASK_TAGNAME);
  30. if (!maskElement) {
  31. requestAnimationFrame(() => {
  32. const maskElement = createMaskElement();
  33. createProgressBarElement(maskElement);
  34. maskElement.offsetWidth;
  35. maskElement.style.setProperty("background-color", "black", "important");
  36. maskElement.style.setProperty("opacity", .3, "important");
  37. document.body.offsetWidth;
  38. });
  39. }
  40. },
  41. onprogress(index, maxIndex) {
  42. const progressBarElement = document.querySelector(PROGRESS_BAR_TAGNAME);
  43. if (progressBarElement && maxIndex) {
  44. const width = Math.floor((index / maxIndex) * 100) + "%";
  45. if (progressBarElement.style.width != width) {
  46. requestAnimationFrame(() => progressBarElement.style.setProperty("width", Math.floor((index / maxIndex) * 100) + "%", "important"));
  47. }
  48. }
  49. },
  50. end() {
  51. const maskElement = document.querySelector(MASK_TAGNAME);
  52. if (maskElement) {
  53. requestAnimationFrame(() => maskElement.remove());
  54. }
  55. },
  56. getSelectedArea
  57. };
  58. function getSelectedArea() {
  59. return new Promise(resolve => {
  60. addEventListener("mousemove", mousemoveListener, true);
  61. addEventListener("click", clickListener, true);
  62. addEventListener("keyup", keypressListener, true);
  63. function mousemoveListener(event) {
  64. const targetElement = getTarget(event);
  65. if (targetElement) {
  66. selectedAreaElement = targetElement;
  67. moveAreaSelector(targetElement);
  68. }
  69. }
  70. function clickListener(event) {
  71. select(selectedAreaElement);
  72. event.preventDefault();
  73. event.stopPropagation();
  74. }
  75. function keypressListener(event) {
  76. if (event.key == "Escape") {
  77. select();
  78. }
  79. }
  80. function select(selectedElement) {
  81. removeEventListener("mousemove", mousemoveListener, true);
  82. removeEventListener("click", clickListener, true);
  83. removeEventListener("keyup", keypressListener, true);
  84. createAreaSelector().remove();
  85. resolve(selectedElement);
  86. selectedAreaElement = null;
  87. }
  88. });
  89. }
  90. function getTarget(event) {
  91. let newTarget, target = event.target, boundingRect = target.getBoundingClientRect();
  92. newTarget = determineTargetElement(target, event.clientX - boundingRect.left, getMatchedParents(target, "left"));
  93. if (newTarget == target) {
  94. newTarget = determineTargetElement(target, boundingRect.left + boundingRect.width - event.clientX, getMatchedParents(target, "right"));
  95. }
  96. if (newTarget == target) {
  97. newTarget = determineTargetElement(target, event.clientY - boundingRect.top, getMatchedParents(target, "top"));
  98. }
  99. if (newTarget == target) {
  100. newTarget = determineTargetElement(target, boundingRect.top + boundingRect.height - event.clientY, getMatchedParents(target, "bottom"));
  101. }
  102. target = newTarget;
  103. while (target && target.clientWidth <= SELECT_PX_THRESHOLD && target.clientHeight <= SELECT_PX_THRESHOLD) {
  104. target = target.parentElement;
  105. }
  106. return target;
  107. }
  108. function moveAreaSelector(target) {
  109. const selectorElement = createAreaSelector();
  110. const boundingRect = target.getBoundingClientRect();
  111. selectorElement.style.setProperty("top", (scrollY + boundingRect.top - 10) + "px");
  112. selectorElement.style.setProperty("left", (scrollX + boundingRect.left - 10) + "px");
  113. selectorElement.style.setProperty("width", (boundingRect.width + 20) + "px");
  114. selectorElement.style.setProperty("height", (boundingRect.height + 20) + "px");
  115. }
  116. function createAreaSelector() {
  117. let selectorElement = document.querySelector(SELECTION_ZONE_TAGNAME);
  118. if (!selectorElement) {
  119. selectorElement = createElement(SELECTION_ZONE_TAGNAME, document.body);
  120. selectorElement.style.setProperty("box-sizing", "border-box", "important");
  121. selectorElement.style.setProperty("background-color", "#3ea9d7", "important");
  122. selectorElement.style.setProperty("border", "10px solid #0b4892", "important");
  123. selectorElement.style.setProperty("border-radius", "2px", "important");
  124. selectorElement.style.setProperty("opacity", ".25", "important");
  125. selectorElement.style.setProperty("pointer-events", "none", "important");
  126. selectorElement.style.setProperty("position", "absolute", "important");
  127. selectorElement.style.setProperty("transition", "all 100ms", "important");
  128. selectorElement.style.setProperty("cursor", "pointer", "important");
  129. selectorElement.style.setProperty("z-index", "2147483647", "important");
  130. selectorElement.style.removeProperty("border-inline-end");
  131. selectorElement.style.removeProperty("border-inline-start");
  132. selectorElement.style.removeProperty("inline-size");
  133. selectorElement.style.removeProperty("block-size");
  134. selectorElement.style.removeProperty("inset-block-start");
  135. selectorElement.style.removeProperty("inset-inline-end");
  136. selectorElement.style.removeProperty("inset-block-end");
  137. selectorElement.style.removeProperty("inset-inline-start");
  138. }
  139. return selectorElement;
  140. }
  141. function createMaskElement() {
  142. let maskElement = document.querySelector(MASK_TAGNAME);
  143. if (!maskElement) {
  144. maskElement = createElement(MASK_TAGNAME, document.body);
  145. maskElement.style.setProperty("opacity", 0, "important");
  146. maskElement.style.setProperty("background-color", "transparent", "important");
  147. maskElement.offsetWidth;
  148. maskElement.style.setProperty("position", "fixed", "important");
  149. maskElement.style.setProperty("top", "0", "important");
  150. maskElement.style.setProperty("left", "0", "important");
  151. maskElement.style.setProperty("width", "100%", "important");
  152. maskElement.style.setProperty("height", "100%", "important");
  153. maskElement.style.setProperty("z-index", 2147483647, "important");
  154. maskElement.style.setProperty("transition", "opacity 250ms", "important");
  155. }
  156. return maskElement;
  157. }
  158. function createProgressBarElement(maskElement) {
  159. let progressBarElement = document.querySelector(PROGRESS_BAR_TAGNAME);
  160. if (!progressBarElement) {
  161. progressBarElement = createElement(PROGRESS_BAR_TAGNAME, maskElement);
  162. progressBarElement.style.setProperty("background-color", "white", "important");
  163. progressBarElement.style.setProperty("position", "fixed", "important");
  164. progressBarElement.style.setProperty("top", "0", "important");
  165. progressBarElement.style.setProperty("left", "0", "important");
  166. progressBarElement.style.setProperty("width", "0", "important");
  167. progressBarElement.style.setProperty("height", "8px", "important");
  168. progressBarElement.style.setProperty("transition", "width 100ms", "important");
  169. progressBarElement.style.setProperty("will-change", "width", "important");
  170. }
  171. return progressBarElement;
  172. }
  173. function getMatchedParents(target, property) {
  174. let element = target, matchedParent, parents = [];
  175. do {
  176. const boundingRect = element.getBoundingClientRect();
  177. if (element.parentElement) {
  178. const parentBoundingRect = element.parentElement.getBoundingClientRect();
  179. matchedParent = Math.abs(parentBoundingRect[property] - boundingRect[property]) <= SELECT_PX_THRESHOLD;
  180. if (matchedParent) {
  181. if (element.parentElement.clientWidth > SELECT_PX_THRESHOLD && element.parentElement.clientHeight > SELECT_PX_THRESHOLD &&
  182. ((element.parentElement.clientWidth - element.clientWidth > SELECT_PX_THRESHOLD) || (element.parentElement.clientHeight - element.clientHeight > SELECT_PX_THRESHOLD))) {
  183. parents.push(element.parentElement);
  184. }
  185. element = element.parentElement;
  186. }
  187. } else {
  188. matchedParent = false;
  189. }
  190. } while (matchedParent && element);
  191. return parents;
  192. }
  193. function determineTargetElement(target, widthDistance, parents) {
  194. if (Math.floor(widthDistance / SELECT_PX_THRESHOLD) <= parents.length) {
  195. target = parents[parents.length - Math.floor(widthDistance / SELECT_PX_THRESHOLD) - 1];
  196. }
  197. return target;
  198. }
  199. function createElement(tagName, parentElement) {
  200. const element = document.createElement(tagName);
  201. parentElement.appendChild(element);
  202. Array.from(getComputedStyle(element)).forEach(property => element.style.setProperty(property, "initial", "important"));
  203. return element;
  204. }
  205. })();