You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dom.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. import {debounce} from 'throttle-debounce';
  2. function elementsCall(el, func, ...args) {
  3. if (typeof el === 'string' || el instanceof String) {
  4. el = document.querySelectorAll(el);
  5. }
  6. if (el instanceof Node) {
  7. func(el, ...args);
  8. } else if (el.length !== undefined) {
  9. // this works for: NodeList, HTMLCollection, Array, jQuery
  10. for (const e of el) {
  11. func(e, ...args);
  12. }
  13. } else {
  14. throw new Error('invalid argument to be shown/hidden');
  15. }
  16. }
  17. /**
  18. * @param el string (selector), Node, NodeList, HTMLCollection, Array or jQuery
  19. * @param force force=true to show or force=false to hide, undefined to toggle
  20. */
  21. function toggleShown(el, force) {
  22. if (force === true) {
  23. el.classList.remove('tw-hidden');
  24. } else if (force === false) {
  25. el.classList.add('tw-hidden');
  26. } else if (force === undefined) {
  27. el.classList.toggle('tw-hidden');
  28. } else {
  29. throw new Error('invalid force argument');
  30. }
  31. }
  32. export function showElem(el) {
  33. elementsCall(el, toggleShown, true);
  34. }
  35. export function hideElem(el) {
  36. elementsCall(el, toggleShown, false);
  37. }
  38. export function toggleElem(el, force) {
  39. elementsCall(el, toggleShown, force);
  40. }
  41. export function isElemHidden(el) {
  42. const res = [];
  43. elementsCall(el, (e) => res.push(e.classList.contains('tw-hidden')));
  44. if (res.length > 1) throw new Error(`isElemHidden doesn't work for multiple elements`);
  45. return res[0];
  46. }
  47. function applyElemsCallback(elems, fn) {
  48. if (fn) {
  49. for (const el of elems) {
  50. fn(el);
  51. }
  52. }
  53. return elems;
  54. }
  55. export function queryElemSiblings(el, selector = '*', fn) {
  56. return applyElemsCallback(Array.from(el.parentNode.children).filter((child) => child !== el && child.matches(selector)), fn);
  57. }
  58. // it works like jQuery.children: only the direct children are selected
  59. export function queryElemChildren(parent, selector = '*', fn) {
  60. return applyElemsCallback(parent.querySelectorAll(`:scope > ${selector}`), fn);
  61. }
  62. export function queryElems(selector, fn) {
  63. return applyElemsCallback(document.querySelectorAll(selector), fn);
  64. }
  65. export function onDomReady(cb) {
  66. if (document.readyState === 'loading') {
  67. document.addEventListener('DOMContentLoaded', cb);
  68. } else {
  69. cb();
  70. }
  71. }
  72. // checks whether an element is owned by the current document, and whether it is a document fragment or element node
  73. // if it is, it means it is a "normal" element managed by us, which can be modified safely.
  74. export function isDocumentFragmentOrElementNode(el) {
  75. try {
  76. return el.ownerDocument === document && el.nodeType === Node.ELEMENT_NODE || el.nodeType === Node.DOCUMENT_FRAGMENT_NODE;
  77. } catch {
  78. // in case the el is not in the same origin, then the access to nodeType would fail
  79. return false;
  80. }
  81. }
  82. // autosize a textarea to fit content. Based on
  83. // https://github.com/github/textarea-autosize
  84. // ---------------------------------------------------------------------
  85. // Copyright (c) 2018 GitHub, Inc.
  86. //
  87. // Permission is hereby granted, free of charge, to any person obtaining
  88. // a copy of this software and associated documentation files (the
  89. // "Software"), to deal in the Software without restriction, including
  90. // without limitation the rights to use, copy, modify, merge, publish,
  91. // distribute, sublicense, and/or sell copies of the Software, and to
  92. // permit persons to whom the Software is furnished to do so, subject to
  93. // the following conditions:
  94. //
  95. // The above copyright notice and this permission notice shall be
  96. // included in all copies or substantial portions of the Software.
  97. // ---------------------------------------------------------------------
  98. export function autosize(textarea, {viewportMarginBottom = 0} = {}) {
  99. let isUserResized = false;
  100. // lastStyleHeight and initialStyleHeight are CSS values like '100px'
  101. let lastMouseX, lastMouseY, lastStyleHeight, initialStyleHeight;
  102. function onUserResize(event) {
  103. if (isUserResized) return;
  104. if (lastMouseX !== event.clientX || lastMouseY !== event.clientY) {
  105. const newStyleHeight = textarea.style.height;
  106. if (lastStyleHeight && lastStyleHeight !== newStyleHeight) {
  107. isUserResized = true;
  108. }
  109. lastStyleHeight = newStyleHeight;
  110. }
  111. lastMouseX = event.clientX;
  112. lastMouseY = event.clientY;
  113. }
  114. function overflowOffset() {
  115. let offsetTop = 0;
  116. let el = textarea;
  117. while (el !== document.body && el !== null) {
  118. offsetTop += el.offsetTop || 0;
  119. el = el.offsetParent;
  120. }
  121. const top = offsetTop - document.defaultView.scrollY;
  122. const bottom = document.documentElement.clientHeight - (top + textarea.offsetHeight);
  123. return {top, bottom};
  124. }
  125. function resizeToFit() {
  126. if (isUserResized) return;
  127. if (textarea.offsetWidth <= 0 && textarea.offsetHeight <= 0) return;
  128. try {
  129. const {top, bottom} = overflowOffset();
  130. const isOutOfViewport = top < 0 || bottom < 0;
  131. const computedStyle = getComputedStyle(textarea);
  132. const topBorderWidth = parseFloat(computedStyle.borderTopWidth);
  133. const bottomBorderWidth = parseFloat(computedStyle.borderBottomWidth);
  134. const isBorderBox = computedStyle.boxSizing === 'border-box';
  135. const borderAddOn = isBorderBox ? topBorderWidth + bottomBorderWidth : 0;
  136. const adjustedViewportMarginBottom = bottom < viewportMarginBottom ? bottom : viewportMarginBottom;
  137. const curHeight = parseFloat(computedStyle.height);
  138. const maxHeight = curHeight + bottom - adjustedViewportMarginBottom;
  139. textarea.style.height = 'auto';
  140. let newHeight = textarea.scrollHeight + borderAddOn;
  141. if (isOutOfViewport) {
  142. // it is already out of the viewport:
  143. // * if the textarea is expanding: do not resize it
  144. if (newHeight > curHeight) {
  145. newHeight = curHeight;
  146. }
  147. // * if the textarea is shrinking, shrink line by line (just use the
  148. // scrollHeight). do not apply max-height limit, otherwise the page
  149. // flickers and the textarea jumps
  150. } else {
  151. // * if it is in the viewport, apply the max-height limit
  152. newHeight = Math.min(maxHeight, newHeight);
  153. }
  154. textarea.style.height = `${newHeight}px`;
  155. lastStyleHeight = textarea.style.height;
  156. } finally {
  157. // ensure that the textarea is fully scrolled to the end, when the cursor
  158. // is at the end during an input event
  159. if (textarea.selectionStart === textarea.selectionEnd &&
  160. textarea.selectionStart === textarea.value.length) {
  161. textarea.scrollTop = textarea.scrollHeight;
  162. }
  163. }
  164. }
  165. function onFormReset() {
  166. isUserResized = false;
  167. if (initialStyleHeight !== undefined) {
  168. textarea.style.height = initialStyleHeight;
  169. } else {
  170. textarea.style.removeProperty('height');
  171. }
  172. }
  173. textarea.addEventListener('mousemove', onUserResize);
  174. textarea.addEventListener('input', resizeToFit);
  175. textarea.form?.addEventListener('reset', onFormReset);
  176. initialStyleHeight = textarea.style.height ?? undefined;
  177. if (textarea.value) resizeToFit();
  178. return {
  179. resizeToFit,
  180. destroy() {
  181. textarea.removeEventListener('mousemove', onUserResize);
  182. textarea.removeEventListener('input', resizeToFit);
  183. textarea.form?.removeEventListener('reset', onFormReset);
  184. },
  185. };
  186. }
  187. export function onInputDebounce(fn) {
  188. return debounce(300, fn);
  189. }
  190. // Set the `src` attribute on an element and returns a promise that resolves once the element
  191. // has loaded or errored. Suitable for all elements mention in:
  192. // https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/load_event
  193. export function loadElem(el, src) {
  194. return new Promise((resolve) => {
  195. el.addEventListener('load', () => resolve(true), {once: true});
  196. el.addEventListener('error', () => resolve(false), {once: true});
  197. el.src = src;
  198. });
  199. }
  200. // some browsers like PaleMoon don't have "SubmitEvent" support, so polyfill it by a tricky method: use the last clicked button as submitter
  201. // it can't use other transparent polyfill patches because PaleMoon also doesn't support "addEventListener(capture)"
  202. const needSubmitEventPolyfill = typeof SubmitEvent === 'undefined';
  203. export function submitEventSubmitter(e) {
  204. e = e.originalEvent ?? e; // if the event is wrapped by jQuery, use "originalEvent", otherwise, use the event itself
  205. return needSubmitEventPolyfill ? (e.target._submitter || null) : e.submitter;
  206. }
  207. function submitEventPolyfillListener(e) {
  208. const form = e.target.closest('form');
  209. if (!form) return;
  210. form._submitter = e.target.closest('button:not([type]), button[type="submit"], input[type="submit"]');
  211. }
  212. export function initSubmitEventPolyfill() {
  213. if (!needSubmitEventPolyfill) return;
  214. console.warn(`This browser doesn't have "SubmitEvent" support, use a tricky method to polyfill`);
  215. document.body.addEventListener('click', submitEventPolyfillListener);
  216. document.body.addEventListener('focus', submitEventPolyfillListener);
  217. }
  218. /**
  219. * Check if an element is visible, equivalent to jQuery's `:visible` pseudo.
  220. * Note: This function doesn't account for all possible visibility scenarios.
  221. * @param {HTMLElement} element The element to check.
  222. * @returns {boolean} True if the element is visible.
  223. */
  224. export function isElemVisible(element) {
  225. if (!element) return false;
  226. return Boolean(element.offsetWidth || element.offsetHeight || element.getClientRects().length);
  227. }
  228. // extract text and images from "paste" event
  229. export function getPastedContent(e) {
  230. const images = [];
  231. for (const item of e.clipboardData?.items ?? []) {
  232. if (item.type?.startsWith('image/')) {
  233. images.push(item.getAsFile());
  234. }
  235. }
  236. const text = e.clipboardData?.getData?.('text') ?? '';
  237. return {text, images};
  238. }
  239. // replace selected text in a textarea while preserving editor history, e.g. CTRL-Z works after this
  240. export function replaceTextareaSelection(textarea, text) {
  241. const before = textarea.value.slice(0, textarea.selectionStart ?? undefined);
  242. const after = textarea.value.slice(textarea.selectionEnd ?? undefined);
  243. let success = true;
  244. textarea.contentEditable = 'true';
  245. try {
  246. success = document.execCommand('insertText', false, text);
  247. } catch {
  248. success = false;
  249. }
  250. textarea.contentEditable = 'false';
  251. if (success && !textarea.value.slice(0, textarea.selectionStart ?? undefined).endsWith(text)) {
  252. success = false;
  253. }
  254. if (!success) {
  255. textarea.value = `${before}${text}${after}`;
  256. textarea.dispatchEvent(new CustomEvent('change', {bubbles: true, cancelable: true}));
  257. }
  258. }