-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(devexp): highlight components by group #2540
Draft
matthieu-crouzet
wants to merge
1
commit into
main
Choose a base branch
from
feat/highlight-components
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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; |
66 changes: 66 additions & 0 deletions
66
packages/@o3r/components/src/devkit/highlight/helpers.spec.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,66 @@ | ||
import { | ||
getIdentifier, | ||
} from './helpers'; | ||
import { | ||
ElementWithGroupInfo, | ||
} from './models'; | ||
|
||
describe('Highlight helpers', () => { | ||
describe('getIdentifier', () => { | ||
it('should return the tagName', () => { | ||
const element = { | ||
htmlElement: { | ||
tagName: 'prefix-selector' | ||
} as HTMLElement, | ||
regexp: '^prefix', | ||
backgroundColor: 'blue', | ||
displayName: 'prefix' | ||
} satisfies ElementWithGroupInfo; | ||
expect(getIdentifier(element)).toBe('prefix-selector'); | ||
}); | ||
|
||
it('should return the first attributeName matching', () => { | ||
const element = { | ||
htmlElement: { | ||
tagName: 'custom-selector', | ||
attributes: [ | ||
{ name: 'custom-attribute' }, | ||
{ name: 'prefix-attribute' }, | ||
{ name: 'prefix-attribute-2' } | ||
] as any as NamedNodeMap | ||
} as HTMLElement, | ||
regexp: '^prefix', | ||
backgroundColor: 'blue', | ||
displayName: 'prefix' | ||
} satisfies ElementWithGroupInfo; | ||
expect(getIdentifier(element)).toBe('prefix-attribute'); | ||
}); | ||
|
||
it('should return the first attributeName matching with its value', () => { | ||
const element = { | ||
htmlElement: { | ||
tagName: 'custom-selector', | ||
attributes: [{ name: 'prefix-attribute', value: 'value' }] as any as NamedNodeMap | ||
} as HTMLElement, | ||
regexp: '^prefix', | ||
backgroundColor: 'blue', | ||
displayName: 'prefix' | ||
} satisfies ElementWithGroupInfo; | ||
expect(getIdentifier(element)).toBe('prefix-attribute="value"'); | ||
}); | ||
|
||
it('should return the first className matching', () => { | ||
const element = { | ||
htmlElement: { | ||
tagName: 'custom-selector', | ||
attributes: [] as any as NamedNodeMap, | ||
classList: ['custom-class', 'prefix-class', 'prefix-class-2'] as any as DOMTokenList | ||
} as HTMLElement, | ||
regexp: '^prefix', | ||
backgroundColor: 'blue', | ||
displayName: 'prefix' | ||
} satisfies ElementWithGroupInfo; | ||
expect(getIdentifier(element)).toBe('prefix-class'); | ||
}); | ||
}); | ||
}); |
154 changes: 154 additions & 0 deletions
154
packages/@o3r/components/src/devkit/highlight/helpers.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,154 @@ | ||
import { | ||
HIGHLIGHT_CHIP_CLASS, | ||
HIGHLIGHT_OVERLAY_CLASS, | ||
HIGHLIGHT_WRAPPER_CLASS, | ||
} from './constants'; | ||
import type { | ||
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((attr) => regexp.test(attr.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(); | ||
} | ||
} | ||
|
||
/** | ||
* Options to create an overlay element | ||
*/ | ||
export interface CreateOverlayOptions { | ||
top: string; | ||
left: string; | ||
position: string; | ||
width: string; | ||
height: string; | ||
backgroundColor: string; | ||
} | ||
|
||
/** | ||
* Create an overlay element | ||
* @param doc HTML Document | ||
* @param opts | ||
*/ | ||
export function createOverlay(doc: Document, opts: CreateOverlayOptions) { | ||
const overlay = doc.createElement('div'); | ||
overlay.classList.add(HIGHLIGHT_OVERLAY_CLASS); | ||
// All static style could be moved in a <style> | ||
overlay.style.top = opts.top; | ||
overlay.style.left = opts.left; | ||
overlay.style.width = opts.width; | ||
overlay.style.height = opts.height; | ||
overlay.style.border = `1px solid ${opts.backgroundColor}`; | ||
overlay.style.zIndex = '10000'; | ||
overlay.style.position = opts.position; | ||
overlay.style.pointerEvents = 'none'; | ||
return overlay; | ||
} | ||
|
||
/** | ||
* Options to create a chip element | ||
*/ | ||
export interface CreateChipOptions { | ||
displayName: string; | ||
depth: number; | ||
top: string; | ||
left: string; | ||
position: string; | ||
backgroundColor: string; | ||
color?: string; | ||
name: string; | ||
} | ||
|
||
/** | ||
* Create a chip element | ||
* @param doc HTML Document | ||
* @param opts | ||
*/ | ||
export function createChip(doc: Document, opts: CreateChipOptions) { | ||
const chip = doc.createElement('div'); | ||
chip.classList.add(HIGHLIGHT_CHIP_CLASS); | ||
chip.textContent = `${opts.displayName} ${opts.depth}`; | ||
// All static style could be moved in a <style> | ||
chip.style.top = opts.top; | ||
chip.style.left = opts.left; | ||
chip.style.backgroundColor = opts.backgroundColor; | ||
chip.style.color = opts.color ?? '#FFF'; | ||
chip.style.position = opts.position; | ||
chip.style.display = 'inline-block'; | ||
chip.style.padding = '2px 4px'; | ||
chip.style.borderRadius = '0 0 4px'; | ||
chip.style.cursor = 'pointer'; | ||
chip.style.zIndex = '10000'; | ||
chip.style.textWrap = 'no-wrap'; | ||
chip.title = opts.name; | ||
chip.addEventListener('click', () => { | ||
// Should we log in the console as well ? | ||
void navigator.clipboard.writeText(opts.name); | ||
}); | ||
return chip; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there is some issue with the refresh