Skip to content

Commit

Permalink
feat(@lexical/devtools): Added DevTools panel + shared state
Browse files Browse the repository at this point in the history
  • Loading branch information
StyleT committed Mar 20, 2024
1 parent b1caaee commit dbd547b
Show file tree
Hide file tree
Showing 12 changed files with 214 additions and 10 deletions.
73 changes: 72 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions packages/lexical-devtools/entrypoints/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@
* LICENSE file in the root directory of this source tree.
*
*/
import store from '../store';

export default defineBackground(() => {
// eslint-disable-next-line no-console
console.log('Hello from Lexical DevTools extension content script.', {
id: browser.runtime.id,
});

// listen state changes
store.subscribe((state) => {
// console.log(state);
});
});
36 changes: 36 additions & 0 deletions packages/lexical-devtools/entrypoints/devtools-panel/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* 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 * as React from 'react';

import lexicalLogo from '/lexical.svg';

import useStore from '../../store';

function App() {
const counter = useStore((state) => state.counter);
const increase = useStore((state) => state.increase);

return (
<>
<div>
<a href="https://lexical.dev" target="_blank">
<img src={lexicalLogo} className="logo" alt="Lexical logo" />
</a>
</div>
<div className="card">
<button onClick={() => increase(1)}>count is {counter}</button>
<p>
Edit <code>src/App.tsx</code> and save to test HMR
</p>
</div>
</>
);
}

export default App;
11 changes: 11 additions & 0 deletions packages/lexical-devtools/entrypoints/devtools-panel/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body>
<div id="root"></div>
<script type="module" src="./main.tsx"></script>
</body>
</html>
18 changes: 18 additions & 0 deletions packages/lexical-devtools/entrypoints/devtools-panel/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* 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 React from 'react';
import ReactDOM from 'react-dom/client';

import App from './App.tsx';

ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<App />
</React.StrictMode>,
);
8 changes: 8 additions & 0 deletions packages/lexical-devtools/entrypoints/devtools/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script type="module" src="./main.ts"></script>
</head>
</html>
14 changes: 14 additions & 0 deletions packages/lexical-devtools/entrypoints/devtools/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* 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.
*
*/

// Create the panel which appears within the browser devtools
browser.devtools.panels.create(
'Lexical',
'icon/128.png',
'devtools-panel.html',
);
8 changes: 5 additions & 3 deletions packages/lexical-devtools/entrypoints/popup/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
*/
import './App.css';

import {useState} from 'react';
import * as React from 'react';

import lexicalLogo from '/lexical.svg';

import useStore from '../../store';

function App() {
const [count, setCount] = useState(0);
const counter = useStore((state) => state.counter);
const increase = useStore((state) => state.increase);

return (
<>
Expand All @@ -23,7 +25,7 @@ function App() {
</a>
</div>
<div className="card">
<button onClick={() => setCount((c) => c + 1)}>count is {count}</button>
<button onClick={() => increase(1)}>count is {counter}</button>
<p>
Edit <code>src/App.tsx</code> and save to test HMR
</p>
Expand Down
15 changes: 10 additions & 5 deletions packages/lexical-devtools/entrypoints/popup/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ import './style.css';
import React from 'react';
import ReactDOM from 'react-dom/client';

import {storeReadyPromise} from '../../store';
import App from './App.tsx';

ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<App />
</React.StrictMode>,
);
storeReadyPromise
.then(() =>
ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<App />
</React.StrictMode>,
),
)
.catch(console.error);
4 changes: 3 additions & 1 deletion packages/lexical-devtools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
},
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
"react-dom": "^18.2.0",
"webext-zustand": "0.2.0",
"zustand": "^4.5.1"
},
"devDependencies": {
"@types/react": "^18.2.46",
Expand Down
24 changes: 24 additions & 0 deletions packages/lexical-devtools/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* 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 {wrapStore} from 'webext-zustand';
import {create} from 'zustand';

interface ExtensionState {
counter: number;
increase: (by: number) => void;
}

export const useExtensionStore = create<ExtensionState>()((set) => ({
counter: 0,
increase: (by) => set((state) => ({counter: state.counter + by})),
}));

export const storeReadyPromise = wrapStore(useExtensionStore);

export default useExtensionStore;
6 changes: 6 additions & 0 deletions packages/lexical-devtools/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// To be fixed in https://github.com/sinanbekar/webext-zustand/pull/3
declare module 'webext-zustand' {
declare const wrapStore: <T>(
store: import('zustand').StoreApi<T>,
) => Promise<void>;
}

0 comments on commit dbd547b

Please sign in to comment.