content-ui.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 = determineTargetElementFloor(target, event.clientX - boundingRect.left, getMatchedParents(target, "left"));
  93. if (newTarget == target) {
  94. newTarget = determineTargetElementCeil(target, boundingRect.left + boundingRect.width - event.clientX, getMatchedParents(target, "right"));
  95. }
  96. if (newTarget == target) {
  97. newTarget = determineTargetElementFloor(target, event.clientY - boundingRect.top, getMatchedParents(target, "top"));
  98. }
  99. if (newTarget == target) {
  100. newTarget = determineTargetElementCeil(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. requestAnimationFrame(() => {
  110. const selectorElement = createAreaSelector();
  111. const boundingRect = target.getBoundingClientRect();
  112. selectorElement.style.setProperty("top", (scrollY + boundingRect.top - 10) + "px");
  113. selectorElement.style.setProperty("left", (scrollX + boundingRect.left - 10) + "px");
  114. selectorElement.style.setProperty("width", (boundingRect.width + 20) + "px");
  115. selectorElement.style.setProperty("height", (boundingRect.height + 20) + "px");
  116. });
  117. }
  118. function createAreaSelector() {
  119. let selectorElement = document.querySelector(SELECTION_ZONE_TAGNAME);
  120. if (!selectorElement) {
  121. selectorElement = createElement(SELECTION_ZONE_TAGNAME, document.body);
  122. selectorElement.style.setProperty("box-sizing", "border-box", "important");
  123. selectorElement.style.setProperty("background-color", "#3ea9d7", "important");
  124. selectorElement.style.setProperty("border", "10px solid #0b4892", "important");
  125. selectorElement.style.setProperty("border-radius", "2px", "important");
  126. selectorElement.style.setProperty("opacity", ".25", "important");
  127. selectorElement.style.setProperty("pointer-events", "none", "important");
  128. selectorElement.style.setProperty("position", "absolute", "important");
  129. selectorElement.style.setProperty("transition", "all 100ms", "important");
  130. selectorElement.style.setProperty("cursor", "pointer", "important");
  131. selectorElement.style.setProperty("z-index", "2147483647", "important");
  132. selectorElement.style.removeProperty("border-inline-end");
  133. selectorElement.style.removeProperty("border-inline-start");
  134. selectorElement.style.removeProperty("inline-size");
  135. selectorElement.style.removeProperty("block-size");
  136. selectorElement.style.removeProperty("inset-block-start");
  137. selectorElement.style.removeProperty("inset-inline-end");
  138. selectorElement.style.removeProperty("inset-block-end");
  139. selectorElement.style.removeProperty("inset-inline-start");
  140. }
  141. return selectorElement;
  142. }
  143. function createMaskElement() {
  144. let maskElement = document.querySelector(MASK_TAGNAME);
  145. if (!maskElement) {
  146. maskElement = createElement(MASK_TAGNAME, document.body);
  147. maskElement.style.setProperty("opacity", 0, "important");
  148. maskElement.style.setProperty("background-color", "transparent", "important");
  149. maskElement.offsetWidth;
  150. maskElement.style.setProperty("position", "fixed", "important");
  151. maskElement.style.setProperty("top", "0", "important");
  152. maskElement.style.setProperty("left", "0", "important");
  153. maskElement.style.setProperty("width", "100%", "important");
  154. maskElement.style.setProperty("height", "100%", "important");
  155. maskElement.style.setProperty("z-index", 2147483647, "important");
  156. maskElement.style.setProperty("transition", "opacity 250ms", "important");
  157. }
  158. return maskElement;
  159. }
  160. function createProgressBarElement(maskElement) {
  161. let progressBarElement = document.querySelector(PROGRESS_BAR_TAGNAME);
  162. if (!progressBarElement) {
  163. progressBarElement = createElement(PROGRESS_BAR_TAGNAME, maskElement);
  164. progressBarElement.style.setProperty("background-color", "white", "important");
  165. progressBarElement.style.setProperty("position", "fixed", "important");
  166. progressBarElement.style.setProperty("top", "0", "important");
  167. progressBarElement.style.setProperty("left", "0", "important");
  168. progressBarElement.style.setProperty("width", "0", "important");
  169. progressBarElement.style.setProperty("height", "8px", "important");
  170. progressBarElement.style.setProperty("transition", "width 100ms", "important");
  171. progressBarElement.style.setProperty("will-change", "width", "important");
  172. }
  173. return progressBarElement;
  174. }
  175. function getMatchedParents(target, property) {
  176. let element = target, matchedParent, parents = [];
  177. do {
  178. const boundingRect = element.getBoundingClientRect();
  179. if (element.parentElement) {
  180. const parentBoundingRect = element.parentElement.getBoundingClientRect();
  181. matchedParent = Math.abs(parentBoundingRect[property] - boundingRect[property]) <= SELECT_PX_THRESHOLD;
  182. if (matchedParent) {
  183. if (element.parentElement.clientWidth > SELECT_PX_THRESHOLD && element.parentElement.clientHeight > SELECT_PX_THRESHOLD &&
  184. ((element.parentElement.clientWidth - element.clientWidth > SELECT_PX_THRESHOLD) || (element.parentElement.clientHeight - element.clientHeight > SELECT_PX_THRESHOLD))) {
  185. parents.push(element.parentElement);
  186. }
  187. element = element.parentElement;
  188. }
  189. } else {
  190. matchedParent = false;
  191. }
  192. } while (matchedParent && element);
  193. return parents;
  194. }
  195. function determineTargetElementCeil(target, widthDistance, parents) {
  196. if (Math.ceil(widthDistance / SELECT_PX_THRESHOLD) <= parents.length) {
  197. target = parents[parents.length - Math.ceil(widthDistance / SELECT_PX_THRESHOLD) - 1];
  198. }
  199. return target;
  200. }
  201. function determineTargetElementFloor(target, widthDistance, parents) {
  202. if (Math.floor(widthDistance / SELECT_PX_THRESHOLD) <= parents.length) {
  203. target = parents[parents.length - Math.floor(widthDistance / SELECT_PX_THRESHOLD) - 1];
  204. }
  205. return target;
  206. }
  207. function createElement(tagName, parentElement) {
  208. const element = document.createElement(tagName);
  209. parentElement.appendChild(element);
  210. Array.from(getComputedStyle(element)).forEach(property => element.style.setProperty(property, "initial", "important"));
  211. return element;
  212. }
  213. })();