-
Notifications
You must be signed in to change notification settings - Fork 0
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
Handle appear
events
#115
Merged
Merged
Handle appear
events
#115
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
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -70,7 +70,7 @@ interface LoadStep { | |||
} | ||||
|
||||
/** | ||||
* Click step | ||||
* Step with additional query | ||||
*/ | ||||
export interface StepWithQuery { | ||||
/** | ||||
|
@@ -91,6 +91,16 @@ export interface StepWithQuery { | |||
urlCondition?: UrlCondition; | ||||
} | ||||
|
||||
/** | ||||
* Step with query and found elements | ||||
*/ | ||||
export type StepWithQueryAndElements = StepWithQuery & { | ||||
/** | ||||
* Elements found | ||||
*/ | ||||
elements?: HTMLElement[]; | ||||
}; | ||||
|
||||
const debounce = (func: () => void, timeout = 300): (() => void) => { | ||||
let timer: NodeJS.Timer | null = null; | ||||
return () => { | ||||
|
@@ -126,6 +136,44 @@ const matchesUrlCondition = (urlCondition: UrlCondition): boolean => { | |||
return false; | ||||
}; | ||||
|
||||
const withElements = async ( | ||||
steps: StepWithQuery[], | ||||
): Promise<StepWithQueryAndElements[]> => { | ||||
const targets = await Promise.all( | ||||
steps | ||||
.filter( | ||||
({ urlCondition }) => | ||||
urlCondition == null || matchesUrlCondition(urlCondition), | ||||
) | ||||
.map(async (step) => { | ||||
try { | ||||
return { | ||||
...step, | ||||
elements: await find(step.query), | ||||
}; | ||||
} catch (e) { | ||||
return step; | ||||
} | ||||
}), | ||||
); | ||||
return targets; | ||||
}; | ||||
|
||||
const withElementsSync = ( | ||||
steps: StepWithQuery[], | ||||
): StepWithQueryAndElements[] => { | ||||
return filterMap(steps, (step) => { | ||||
if (step.urlCondition != null && !matchesUrlCondition(step.urlCondition)) { | ||||
return null; | ||||
} | ||||
const elements = getAll(step.query); | ||||
if (elements.length === 0) { | ||||
return null; | ||||
} | ||||
return { ...step, elements }; | ||||
}); | ||||
}; | ||||
|
||||
const localStorageKey = (conversationId: string): string => | ||||
`jb-triggered-steps-${conversationId}`; | ||||
|
||||
|
@@ -317,17 +365,18 @@ export const run = async (props: RunProps): Promise<RunOutput> => { | |||
* Handle load steps | ||||
*/ | ||||
|
||||
const loadSteps: LoadStep[] = Object.entries(triggers).reduce( | ||||
(prev: LoadStep[], [stepId, trigger]: [StepId, Trigger]) => { | ||||
const loadSteps: LoadStep[] = filterMap( | ||||
Object.entries(triggers), | ||||
([stepId, trigger]: [StepId, Trigger]) => { | ||||
if (trigger.event === "pageLoad") { | ||||
return [ | ||||
...prev, | ||||
{ stepId, urlCondition: trigger.urlCondition, once: trigger.once }, | ||||
]; | ||||
return { | ||||
stepId, | ||||
urlCondition: trigger.urlCondition, | ||||
once: trigger.once, | ||||
}; | ||||
} | ||||
return prev; | ||||
return null; | ||||
}, | ||||
[], | ||||
); | ||||
|
||||
let previousUrl = window.location.toString(); | ||||
|
@@ -355,52 +404,43 @@ export const run = async (props: RunProps): Promise<RunOutput> => { | |||
|
||||
handleLoadSteps(); | ||||
|
||||
const clickSteps: StepWithQuery[] = Object.entries(triggers).reduce( | ||||
(prev: StepWithQuery[], [stepId, trigger]: [StepId, Trigger]) => { | ||||
const clickSteps: StepWithQuery[] = filterMap( | ||||
Object.entries(triggers), | ||||
([stepId, trigger]: [StepId, Trigger]) => { | ||||
if (trigger.event === "click" && trigger.query != null) { | ||||
const newEntry: StepWithQuery = { | ||||
return { | ||||
stepId, | ||||
query: decode(trigger.query), | ||||
urlCondition: trigger.urlCondition, | ||||
once: trigger.once, | ||||
}; | ||||
return [...prev, newEntry]; | ||||
} | ||||
return prev; | ||||
return null; | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
not necessary There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Explicit returns read clearer to me. |
||||
}, | ||||
); | ||||
|
||||
const appearSteps: StepWithQuery[] = filterMap( | ||||
Object.entries(triggers), | ||||
([stepId, trigger]: [StepId, Trigger]) => { | ||||
if (trigger.event === "appear" && trigger.query != null) { | ||||
return { | ||||
stepId, | ||||
query: decode(trigger.query), | ||||
urlCondition: trigger.urlCondition, | ||||
once: trigger.once, | ||||
}; | ||||
} | ||||
return null; | ||||
}, | ||||
[], | ||||
); | ||||
|
||||
const handleGlobalClickForAnnotations = async (ev: any): Promise<void> => { | ||||
const targets = await Promise.all( | ||||
clickSteps | ||||
.filter( | ||||
({ urlCondition }) => | ||||
urlCondition == null || matchesUrlCondition(urlCondition), | ||||
) | ||||
.map(async ({ stepId, query }) => { | ||||
try { | ||||
return { | ||||
stepId, | ||||
query, | ||||
elements: await find(query), | ||||
}; | ||||
} catch (e) { | ||||
return { stepId, query }; | ||||
} | ||||
}), | ||||
); | ||||
const targets = await withElements(clickSteps); | ||||
const node = ev.target; | ||||
const clickStep: | ||||
| (StepWithQuery & { | ||||
/** | ||||
* | ||||
*/ | ||||
elements?: HTMLElement[]; | ||||
}) | ||||
| undefined = targets.find(({ elements }) => | ||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument | ||||
(elements ?? []).some((element: HTMLElement) => element.contains(node)), | ||||
const clickStep: StepWithQueryAndElements | undefined = targets.find( | ||||
({ elements }) => | ||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument | ||||
(elements ?? []).some((element: HTMLElement) => element.contains(node)), | ||||
); | ||||
if (clickStep != null) { | ||||
sendStep(clickStep.stepId, clickStep.once ?? false); | ||||
|
@@ -493,7 +533,22 @@ export const run = async (props: RunProps): Promise<RunOutput> => { | |||
* Change detection | ||||
*/ | ||||
|
||||
const documentObserver = new MutationObserver(() => { | ||||
const documentObserver = new MutationObserver((mutations) => { | ||||
// If any of the added nodes are inside matches on appear events, trigger those events | ||||
const targets = withElementsSync(appearSteps); | ||||
mutations.forEach((mutation) => { | ||||
targets.forEach(({ stepId, once, elements }) => { | ||||
if ( | ||||
(elements ?? []).some((element) => { | ||||
return [...mutation.addedNodes].some((addedNode) => { | ||||
return addedNode.contains(element); | ||||
}); | ||||
}) | ||||
) { | ||||
sendStep(stepId, once ?? false); | ||||
} | ||||
}); | ||||
}); | ||||
debouncedSetHighlights(); | ||||
// If the document changed for any reason (click, popstate event etc.), check if the URL also changed | ||||
// If it did, handle page load events | ||||
|
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
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.
For appear on mutation observer, I'd imagine you want the sync version rather than waiting up to a second for the element to appear.