Skip to content

Commit 95536b7

Browse files
authored
Update Browser Extension's Vite Config (#2540)
* messaging from background ➡️ content script; chrome + edge troubleshooting * display editorState changes in React UI * whittle away TS errors * clean up folder structure * change background + content scripts to ts files * rename devtools.js to .ts; debug in Chrome/Edge * add ES6-style import in browser extension * revert ES6-style import in content/background scripts This reverts commit 84680f310dc7b38bcafea18fde5ba83a537671ab. * remove linter disables * convert types to any
1 parent 42d2ca7 commit 95536b7

File tree

14 files changed

+72
-56
lines changed

14 files changed

+72
-56
lines changed

packages/lexical-devtools/public/background.js

Lines changed: 0 additions & 33 deletions
This file was deleted.

packages/lexical-devtools/public/devtools.html

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/lexical-devtools/public/manifest.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@
33
"description": "Developer Tools for Lexical",
44
"version": "0.0",
55
"manifest_version": 2,
6-
"devtools_page": "devtools.html",
6+
"devtools_page": "src/devtools/index.html",
77
"browser_action": {
8-
"default_popup": "popup.html",
8+
"default_popup": "src/popup/index.html",
99
"default_title": "Lexical Developer Tools"
1010
},
1111
"icons": {
1212
"16": "favicon-16x16.png",
1313
"48": "favicon-32x32.png"
1414
},
1515
"background": {
16-
"scripts": ["background.js"]
16+
"scripts": ["src/background/index.js"]
1717
},
1818
"content_scripts": [
1919
{
2020
"matches": ["<all_urls>"],
21-
"js": ["content.js"]
21+
"js": ["src/content/index.js"]
2222
}
2323
],
2424
"permissions": ["activeTab"]
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
*/
8+
// Create messaging connection to send editorState updates to Lexical DevTools App.
9+
const ports = {}; // Each tab will have a separate messaging port for the devTools app & the inspectedWindow's content script, eg. { tabId: { reactPort, contentScriptPort } }
10+
11+
// The Lexical DevTools React UI sends a message to initialize the port.
12+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
13+
chrome.runtime.onConnect.addListener(function (port: any) {
14+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
15+
port.onMessage.addListener((message: any) => {
16+
if (message.name === 'init' && message.type === 'FROM_APP') {
17+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
18+
(ports as any)[message.tabId].react = port;
19+
return;
20+
}
21+
22+
if (message.name === 'init' && message.type === 'FROM_CONTENT') {
23+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
24+
(ports as any)[port.sender.tab.id] = {};
25+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
26+
(ports as any)[port.sender.tab.id].content = port;
27+
return;
28+
}
29+
30+
if (message.name === 'editor-update') {
31+
const tabId = port.sender.tab.id;
32+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
33+
(ports as any)[tabId].react.postMessage({
34+
editorState: message.editorState,
35+
});
36+
}
37+
});
38+
});

packages/lexical-devtools/public/content.js renamed to packages/lexical-devtools/src/content/index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*
77
*/
8-
'use strict';
9-
10-
// eslint-disable-next-line no-undef
11-
let port = chrome.runtime.connect();
8+
const port = chrome.runtime.connect();
129

1310
port.postMessage({
1411
name: 'init',
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<script type="module" src="./index.ts"></script>

packages/lexical-devtools/public/devtools.js renamed to packages/lexical-devtools/src/devtools/index.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@
55
* LICENSE file in the root directory of this source tree.
66
*
77
*/
8-
9-
'use strict';
10-
118
// Create the panel which appears within the browser's DevTools, loading the Lexical DevTools App within index.html.
12-
// eslint-disable-next-line no-undef
13-
chrome.devtools.panels.create('Lexical', 'favicon-32x32.png', '../index.html');
9+
chrome.devtools.panels.create(
10+
'Lexical',
11+
'/../../favicon-32x32.png',
12+
'/src/panel/index.html',
13+
);
1414

1515
// Use devtools.inspectedWindow.eval() to get editorState updates. For more info on security concerns:
1616
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Content_scripts
1717
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/devtools/inspectedWindow/eval
18-
// eslint-disable-next-line no-undef
1918
chrome.devtools.inspectedWindow
2019
// Attach a registerUpdateListener within the window to subscribe to changes in editorState.
2120
// After the initial registration, all editorState updates are done via browser.runtime.onConnect & window.postMessage

packages/lexical-devtools/src/App.css renamed to packages/lexical-devtools/src/panel/components/App/index.css

File renamed without changes.

packages/lexical-devtools/src/App.tsx renamed to packages/lexical-devtools/src/panel/components/App/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66
*
77
*/
88

9-
import './App.css';
9+
import './index.css';
1010

1111
import * as React from 'react';
1212
import {useEffect, useRef, useState} from 'react';
1313

14-
import logo from './images/lexical-white.png';
14+
import logo from '../../../images/lexical-white.png';
1515

1616
function App(): JSX.Element {
1717
const [count, setCount] = useState<number>(0);
1818
// eslint-disable-next-line @typescript-eslint/no-unused-vars
1919
const [editorState, setEditorState] = useState<object>({});
20-
const port = useRef();
20+
const port = useRef<object>({});
2121

2222
useEffect(() => {
2323
// create and initialize the messaging port to receive editorState updates

packages/lexical-devtools/src/main.css renamed to packages/lexical-devtools/src/panel/components/main/index.css

File renamed without changes.

0 commit comments

Comments
 (0)