-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(devexp): highlight components by group
- Loading branch information
1 parent
f4e354c
commit 5e2bf4a
Showing
6 changed files
with
450 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
packages/@o3r/components/src/devkit/highlight/constants.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* Class applied on the wrapper of highlight elements | ||
*/ | ||
export const HIGHLIGHT_WRAPPER_CLASS = 'highlight-wrapper'; | ||
|
||
/** | ||
* Class applied on the overlay elements | ||
*/ | ||
export const HIGHLIGHT_OVERLAY_CLASS = 'highlight-overlay'; | ||
|
||
/** | ||
* Class applied on the chip elements | ||
*/ | ||
export const HIGHLIGHT_CHIP_CLASS = 'highlight-chip'; | ||
|
||
/** | ||
* Default value for maximum number of ancestors | ||
*/ | ||
export const DEFAULT_MAX_DEPTH = 10; | ||
|
||
/** | ||
* Default value for element min height | ||
*/ | ||
export const DEFAULT_ELEMENT_MIN_HEIGHT = 30; | ||
/** | ||
* Default value for element min width | ||
*/ | ||
export const DEFAULT_ELEMENT_MIN_WIDTH = 60; | ||
/** | ||
* Default value for throttle interval | ||
*/ | ||
export const DEFAULT_THROTTLE_INTERVAL = 500; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { | ||
HIGHLIGHT_WRAPPER_CLASS, | ||
} from './constants'; | ||
import { | ||
ElementWithGroupInfo, | ||
} from './models'; | ||
|
||
/** | ||
* Retrieve the identifier of the element | ||
* @param element | ||
*/ | ||
export function getIdentifier(element: ElementWithGroupInfo): string { | ||
const { tagName, attributes, classList } = element.htmlElement; | ||
const regexp = new RegExp(element.regexp, 'i'); | ||
if (!regexp.test(tagName)) { | ||
const attribute = Array.from(attributes).find((att) => regexp.test(att.name)); | ||
if (attribute) { | ||
return `${attribute.name}${attribute.value ? `="${attribute.value}"` : ''}`; | ||
} | ||
const className = Array.from(classList).find((cName) => regexp.test(cName)); | ||
if (className) { | ||
return className; | ||
} | ||
} | ||
return tagName; | ||
} | ||
|
||
/** | ||
* Compute the number of ancestors of a given element based on a list of elements | ||
* @param element | ||
* @param elementList | ||
*/ | ||
export function computeNumberOfAncestors(element: HTMLElement, elementList: HTMLElement[]) { | ||
return elementList.filter((el: HTMLElement) => el.contains(element)).length; | ||
} | ||
|
||
/** | ||
* Throttle {@link fn} with a {@link delay} | ||
* @param fn method to run | ||
* @param delay given in ms | ||
*/ | ||
export function throttle<T extends (...args: any[]) => any>(fn: T, delay: number): (...args: Parameters<T>) => void { | ||
let timerFlag: ReturnType<typeof setTimeout> | null = null; | ||
|
||
const throttleFn = (...args: Parameters<T>) => { | ||
if (timerFlag === null) { | ||
fn(...args); | ||
timerFlag = setTimeout(() => { | ||
fn(...args); | ||
timerFlag = null; | ||
}, delay); | ||
} | ||
}; | ||
return throttleFn; | ||
} | ||
|
||
/** | ||
* Run {@link refreshFn} if {@link mutations} implies to refresh elements inside {@link highlightWrapper} | ||
* @param mutations | ||
* @param highlightWrapper | ||
* @param refreshFn | ||
*/ | ||
export function runRefreshIfNeeded(mutations: MutationRecord[], highlightWrapper: Element | null, refreshFn: () => void) { | ||
if ( | ||
mutations.some((mutation) => | ||
mutation.target !== highlightWrapper | ||
|| ( | ||
mutation.target === document.body | ||
&& Array.from<HTMLElement>(mutation.addedNodes.values() as any) | ||
.concat(...mutation.removedNodes.values() as any) | ||
.some((node) => !node.classList.contains(HIGHLIGHT_WRAPPER_CLASS)) | ||
) | ||
) | ||
) { | ||
refreshFn(); | ||
} | ||
} |
Oops, something went wrong.