-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Inject DevTools Script in Browser Extension #2778
Merged
thegreatercurve
merged 6 commits into
facebook:main
from
MLH-Fellowship:inject-devtools-script
Aug 9, 2022
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2f21955
move devtools code to injected script
noi5e 8fdc069
correct typescript errors
noi5e 05cef8b
write cloneInto type
noi5e 5fc5b28
add injected script to web_accessible_resources
noi5e 2f3c540
fix highlighting bug
noi5e 81e0e1a
wrapper function for cloneInto
noi5e 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
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
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,100 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
import {EditorState} from 'lexical'; | ||
import {LexicalHTMLElement, LexicalKey} from 'packages/lexical-devtools/types'; | ||
|
||
let editorDOMNode: LexicalHTMLElement | null, editorKey: string | null; | ||
|
||
// the existing editorState.toJSON() does not contain lexicalKeys | ||
// therefore, we have a custom serializeEditorState helper | ||
const serializeEditorState = (editorState: EditorState) => { | ||
const nodeMap = Object.fromEntries(editorState._nodeMap); // convert from Map structure to JSON-friendly object | ||
return {nodeMap}; | ||
}; | ||
|
||
const postEditorState = (editorState: EditorState) => { | ||
const serializedEditorState = serializeEditorState(editorState); | ||
const data = {editorState: serializedEditorState}; | ||
document.dispatchEvent(new CustomEvent('editorStateUpdate', {detail: data})); | ||
}; | ||
|
||
document.addEventListener('loadEditorState', function (e) { | ||
// query document for Lexical editor instance. | ||
// TODO: add support multiple Lexical editors within the same page | ||
editorDOMNode = document.querySelectorAll( | ||
'div[data-lexical-editor]', | ||
)[0] as LexicalHTMLElement; | ||
const editor = editorDOMNode.__lexicalEditor; | ||
editorKey = editorDOMNode.__lexicalEditor._key; // dynamically generated upon each pageload, we need this to find DOM nodes for highlighting | ||
|
||
const initialEditorState = editor.getEditorState(); | ||
postEditorState(initialEditorState); | ||
|
||
editor.registerUpdateListener(({editorState}) => { | ||
postEditorState(editorState); | ||
}); | ||
}); | ||
|
||
// append highlight overlay <div> to document | ||
const highlightOverlay = document.createElement('div'); | ||
highlightOverlay.style.position = 'absolute'; | ||
highlightOverlay.style.background = 'rgba(119, 182, 255, 0.5)'; | ||
highlightOverlay.style.border = '1px dashed #77b6ff'; | ||
document.body.appendChild(highlightOverlay); | ||
|
||
// functions to highlight/dehighlight DOM nodes onHover of DevTools nodes | ||
document.addEventListener('highlight', (evt: CustomEvent) => { | ||
highlight(evt.detail.lexicalKey); | ||
}); | ||
|
||
document.addEventListener('dehighlight', function () { | ||
dehighlight(); | ||
}); | ||
|
||
// depth first search to find DOM node with lexicalKey | ||
const findDOMNode = ( | ||
node: LexicalHTMLElement, | ||
targetKey: string, | ||
): LexicalHTMLElement | null => { | ||
// each DOM node has its Lexical key stored at a particular key eg. DOMNode[__lexicalKey_pzely] | ||
const key = ('__lexicalKey_' + editorKey) as LexicalKey; | ||
|
||
if (targetKey === 'root') { | ||
return editorDOMNode; | ||
} | ||
|
||
if (node[key] && node[key] === targetKey) { | ||
return node; | ||
} | ||
|
||
for (let i = 0; i < node.childNodes.length; i++) { | ||
const child = node.childNodes[i] as LexicalHTMLElement; | ||
const childResults = findDOMNode(child, targetKey); | ||
if (childResults) return childResults; | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
const highlight = (targetKey: string) => { | ||
if (editorDOMNode) { | ||
const node = findDOMNode(editorDOMNode, targetKey); | ||
if (node) { | ||
const {width, height, top, left} = node.getBoundingClientRect(); | ||
highlightOverlay.style.width = width + 'px'; | ||
highlightOverlay.style.height = height + 'px'; | ||
highlightOverlay.style.top = top + window.scrollY + 'px'; | ||
highlightOverlay.style.left = left + window.scrollX + 'px'; | ||
highlightOverlay.style.display = 'block'; | ||
} | ||
} | ||
}; | ||
|
||
const dehighlight = () => { | ||
highlightOverlay.style.display = 'none'; | ||
}; |
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
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.
For the
highlight
CustomEvent
. I'm unsure if adeclare global
is what I'm supposed to be doing here. @thegreatercurveThere 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.
You can do, it will just get referenced in this module scope either way.