content-ui.js 10 KB

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