-
-
Notifications
You must be signed in to change notification settings - Fork 24
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
Add useCustomLocator() for non-DOM locators #302
Open
Alloyed
wants to merge
2
commits into
thetarnav:main
Choose a base branch
from
Alloyed:dom-locator
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.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
import {makeHoverElementListener} from '@solid-devtools/shared/primitives' | ||
import {warn} from '@solid-devtools/shared/utils' | ||
import {makeEventListener} from '@solid-primitives/event-listener' | ||
import {createKeyHold} from '@solid-primitives/keyboard' | ||
import {scheduleIdle} from '@solid-primitives/scheduled' | ||
import {defer} from '@solid-primitives/utils' | ||
import {createEffect, createMemo, createSignal, onCleanup} from 'solid-js' | ||
import * as registry from '../../main/component-registry' | ||
import {ObjectType, getObjectById} from '../../main/id' | ||
import {NodeID} from '../../main/types' | ||
import {HighlightElementPayload, LocatorFactory, SourceElementType, SourceLocation} from '../types' | ||
import {createElementsOverlay} from './element-overlay' | ||
import { | ||
LocatorComponent, | ||
TargetIDE, | ||
TargetURLFunction, | ||
getLocationAttr, | ||
getProjectPath, | ||
getSourceCodeData, | ||
openSourceCode, | ||
} from './find-components' | ||
import {LocatorOptions} from './types' | ||
|
||
export function createDOMLocatorFactory(options: LocatorOptions): LocatorFactory<HTMLElement> { | ||
return props => { | ||
const [enabledByPressingSignal, setEnabledByPressingSignal] = createSignal( | ||
(): boolean => false, | ||
) | ||
props.setLocatorEnabledSignal(createMemo(() => enabledByPressingSignal()())) | ||
|
||
const [hoverTarget, setHoverTarget] = createSignal<HTMLElement | null>(null) | ||
const [devtoolsTarget, setDevtoolsTarget] = createSignal<HighlightElementPayload>(null) | ||
|
||
const [highlightedComponents, setHighlightedComponents] = createSignal<LocatorComponent[]>( | ||
[], | ||
) | ||
|
||
const calcHighlightedComponents = ( | ||
target: HTMLElement | HighlightElementPayload, | ||
): LocatorComponent[] => { | ||
if (!target) return [] | ||
|
||
// target is an elementId | ||
if ('type' in target && target.type === 'element') { | ||
const element = getObjectById(target.id, ObjectType.Element) | ||
if (!(element instanceof HTMLElement)) return [] | ||
target = element | ||
} | ||
|
||
// target is an element | ||
if (target instanceof HTMLElement) { | ||
const comp = registry.findComponent(target) | ||
if (!comp) return [] | ||
return [ | ||
{ | ||
location: getLocationAttr(target), | ||
element: target, | ||
id: comp.id, | ||
name: comp.name, | ||
}, | ||
] | ||
} | ||
|
||
// target is a component or an element of a component (in DOM walker mode) | ||
const comp = registry.getComponent(target.id) | ||
if (!comp) return [] | ||
return comp.elements.map(element => ({ | ||
element, | ||
id: comp.id, | ||
name: comp.name, | ||
})) | ||
} | ||
|
||
createEffect( | ||
defer( | ||
() => hoverTarget() ?? devtoolsTarget(), | ||
scheduleIdle(target => | ||
setHighlightedComponents(() => calcHighlightedComponents(target)), | ||
), | ||
), | ||
) | ||
|
||
createElementsOverlay(highlightedComponents) | ||
|
||
// notify of component hovered by using the debugger | ||
createEffect((prev: NodeID | undefined) => { | ||
const target = hoverTarget() | ||
const comp = target && registry.findComponent(target) | ||
if (prev) props.emit('HoveredComponent', {nodeId: prev, state: false}) | ||
if (comp) { | ||
const {id} = comp | ||
props.emit('HoveredComponent', {nodeId: id, state: true}) | ||
return id | ||
} | ||
}) | ||
|
||
let targetIDE: TargetIDE | TargetURLFunction | undefined | ||
|
||
createEffect(() => { | ||
if (!props.locatorEnabled()) return | ||
|
||
// set hovered element as target | ||
makeHoverElementListener(el => setHoverTarget(el)) | ||
onCleanup(() => setHoverTarget(null)) | ||
|
||
// go to selected component source code on click | ||
makeEventListener( | ||
window, | ||
'click', | ||
e => { | ||
const {target} = e | ||
if (!(target instanceof HTMLElement)) return | ||
const highlighted = highlightedComponents() | ||
const comp = | ||
highlighted.find(({element}) => target.contains(element)) ?? highlighted[0] | ||
if (!comp) return | ||
const sourceCodeData = | ||
comp.location && getSourceCodeData(comp.location, comp.element) | ||
|
||
// intercept on-page components clicks and send them to the devtools overlay | ||
props.onComponentClick(comp.id, () => { | ||
if (!targetIDE || !sourceCodeData) return | ||
e.preventDefault() | ||
e.stopPropagation() | ||
openSourceCode(targetIDE, sourceCodeData) | ||
}) | ||
}, | ||
true, | ||
) | ||
}) | ||
|
||
if (options.targetIDE) targetIDE = options.targetIDE | ||
if (options.key !== false) { | ||
const isHoldingKey = createKeyHold(options.key ?? 'Alt', {preventDefault: true}) | ||
setEnabledByPressingSignal(() => isHoldingKey) | ||
} | ||
|
||
return { | ||
setDevtoolsHighlightTarget(target: HighlightElementPayload) { | ||
setDevtoolsTarget(target) | ||
}, | ||
openElementSourceCode( | ||
location: SourceLocation, | ||
element: SourceElementType<HTMLElement>, | ||
) { | ||
if (!targetIDE) return warn('Please set `targetIDE` it in useLocator options.') | ||
const projectPath = getProjectPath() | ||
if (!projectPath) return warn('projectPath is not set.') | ||
openSourceCode(targetIDE, { | ||
...location, | ||
projectPath, | ||
element, | ||
}) | ||
}, | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import type {KbdKey} from '@solid-primitives/keyboard' | ||
import type {TargetIDE, TargetURLFunction} from './find-components' | ||
|
||
export type {LocationAttr, LocatorComponent, TargetIDE, TargetURLFunction} from './find-components' | ||
|
||
export type LocatorOptions = { | ||
/** Choose in which IDE the component source code should be revealed. */ | ||
targetIDE?: false | TargetIDE | TargetURLFunction | ||
/** | ||
* Holding which key should enable the locator overlay? | ||
* @default 'Alt' | ||
*/ | ||
key?: false | KbdKey | ||
} | ||
|
||
// used by the transform | ||
export const WINDOW_PROJECTPATH_PROPERTY = '$sdt_projectPath' | ||
export const LOCATION_ATTRIBUTE_NAME = 'data-source-loc' |
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 could not make LocatorOptions DOM-agnostic without changing the api. targetIDE is fine, but key assumes the browser's implementation of keyboard keys