|
| 1 | +import { |
| 2 | + HIGHLIGHT_CHIP_CLASS, |
| 3 | + HIGHLIGHT_OVERLAY_CLASS, |
| 4 | + HIGHLIGHT_WRAPPER_CLASS, |
| 5 | +} from './constants'; |
| 6 | +import type { |
| 7 | + ElementWithGroupInfo, |
| 8 | +} from './models'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Retrieve the identifier of the element |
| 12 | + * @param element |
| 13 | + */ |
| 14 | +export function getIdentifier(element: ElementWithGroupInfo): string { |
| 15 | + const { tagName, attributes, classList } = element.htmlElement; |
| 16 | + const regexp = new RegExp(element.regexp, 'i'); |
| 17 | + if (!regexp.test(tagName)) { |
| 18 | + const attribute = Array.from(attributes).find((attr) => regexp.test(attr.name)); |
| 19 | + if (attribute) { |
| 20 | + return `${attribute.name}${attribute.value ? `="${attribute.value}"` : ''}`; |
| 21 | + } |
| 22 | + const className = Array.from(classList).find((cName) => regexp.test(cName)); |
| 23 | + if (className) { |
| 24 | + return className; |
| 25 | + } |
| 26 | + } |
| 27 | + return tagName; |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Compute the number of ancestors of a given element based on a list of elements |
| 32 | + * @param element |
| 33 | + * @param elementList |
| 34 | + */ |
| 35 | +export function computeNumberOfAncestors(element: HTMLElement, elementList: HTMLElement[]) { |
| 36 | + return elementList.filter((el: HTMLElement) => el.contains(element)).length; |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Throttle {@link fn} with a {@link delay} |
| 41 | + * @param fn method to run |
| 42 | + * @param delay given in ms |
| 43 | + */ |
| 44 | +export function throttle<T extends (...args: any[]) => any>(fn: T, delay: number): (...args: Parameters<T>) => void { |
| 45 | + let timerFlag: ReturnType<typeof setTimeout> | null = null; |
| 46 | + |
| 47 | + const throttleFn = (...args: Parameters<T>) => { |
| 48 | + if (timerFlag === null) { |
| 49 | + fn(...args); |
| 50 | + timerFlag = setTimeout(() => { |
| 51 | + fn(...args); |
| 52 | + timerFlag = null; |
| 53 | + }, delay); |
| 54 | + } |
| 55 | + }; |
| 56 | + return throttleFn; |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Run {@link refreshFn} if {@link mutations} implies to refresh elements inside {@link highlightWrapper} |
| 61 | + * @param mutations |
| 62 | + * @param highlightWrapper |
| 63 | + * @param refreshFn |
| 64 | + */ |
| 65 | +export function runRefreshIfNeeded(mutations: MutationRecord[], highlightWrapper: Element | null, refreshFn: () => void) { |
| 66 | + if ( |
| 67 | + mutations.some((mutation) => |
| 68 | + mutation.target !== highlightWrapper |
| 69 | + || ( |
| 70 | + mutation.target === document.body |
| 71 | + && Array.from<HTMLElement>(mutation.addedNodes.values() as any) |
| 72 | + .concat(...mutation.removedNodes.values() as any) |
| 73 | + .some((node) => !node.classList.contains(HIGHLIGHT_WRAPPER_CLASS)) |
| 74 | + ) |
| 75 | + ) |
| 76 | + ) { |
| 77 | + refreshFn(); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Options to create an overlay element |
| 83 | + */ |
| 84 | +export interface CreateOverlayOptions { |
| 85 | + top: string; |
| 86 | + left: string; |
| 87 | + position: string; |
| 88 | + width: string; |
| 89 | + height: string; |
| 90 | + backgroundColor: string; |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * Create an overlay element |
| 95 | + * @param doc HTML Document |
| 96 | + * @param opts |
| 97 | + */ |
| 98 | +export function createOverlay(doc: Document, opts: CreateOverlayOptions) { |
| 99 | + const overlay = doc.createElement('div'); |
| 100 | + overlay.classList.add(HIGHLIGHT_OVERLAY_CLASS); |
| 101 | + // All static style could be moved in a <style> |
| 102 | + overlay.style.top = opts.top; |
| 103 | + overlay.style.left = opts.left; |
| 104 | + overlay.style.width = opts.width; |
| 105 | + overlay.style.height = opts.height; |
| 106 | + overlay.style.border = `1px solid ${opts.backgroundColor}`; |
| 107 | + overlay.style.zIndex = '10000'; |
| 108 | + overlay.style.position = opts.position; |
| 109 | + overlay.style.pointerEvents = 'none'; |
| 110 | + return overlay; |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * Options to create a chip element |
| 115 | + */ |
| 116 | +export interface CreateChipOptions { |
| 117 | + displayName: string; |
| 118 | + depth: number; |
| 119 | + top: string; |
| 120 | + left: string; |
| 121 | + position: string; |
| 122 | + backgroundColor: string; |
| 123 | + color?: string; |
| 124 | + name: string; |
| 125 | +} |
| 126 | + |
| 127 | +/** |
| 128 | + * Create a chip element |
| 129 | + * @param doc HTML Document |
| 130 | + * @param opts |
| 131 | + */ |
| 132 | +export function createChip(doc: Document, opts: CreateChipOptions) { |
| 133 | + const chip = doc.createElement('div'); |
| 134 | + chip.classList.add(HIGHLIGHT_CHIP_CLASS); |
| 135 | + chip.textContent = `${opts.displayName} ${opts.depth}`; |
| 136 | + // All static style could be moved in a <style> |
| 137 | + chip.style.top = opts.top; |
| 138 | + chip.style.left = opts.left; |
| 139 | + chip.style.backgroundColor = opts.backgroundColor; |
| 140 | + chip.style.color = opts.color ?? '#FFF'; |
| 141 | + chip.style.position = opts.position; |
| 142 | + chip.style.display = 'inline-block'; |
| 143 | + chip.style.padding = '2px 4px'; |
| 144 | + chip.style.borderRadius = '0 0 4px'; |
| 145 | + chip.style.cursor = 'pointer'; |
| 146 | + chip.style.zIndex = '10000'; |
| 147 | + chip.style.textWrap = 'no-wrap'; |
| 148 | + chip.title = opts.name; |
| 149 | + chip.addEventListener('click', () => { |
| 150 | + // Should we log in the console as well ? |
| 151 | + void navigator.clipboard.writeText(opts.name); |
| 152 | + }); |
| 153 | + return chip; |
| 154 | +} |
0 commit comments