Skip to content

Commit

Permalink
chore: separate code from chrome (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zyie authored May 30, 2024
1 parent 02fa42c commit d1e905b
Show file tree
Hide file tree
Showing 35 changed files with 94 additions and 51 deletions.
17 changes: 13 additions & 4 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
"./packages/*"
],
"scripts": {
"clean": "rimraf .upload && rimraf packages/*/dist",
"postinstall": "patch-package",
"lint": "eslint --ext .ts,.tsx . && prettier --check .",
"lint:fix": "eslint --ext .ts,.tsx . --fix && prettier --write .",
"start": "npm run --ws start --if-present",
"start:local": "npm run start --workspace @devtool/local --if-present",
"start:chrome": "npm run start --workspace @devtool/chrome & npm run start --workspace @devtool/example",
"prebuild": "npm run clean",
"build": "npm run --ws build --if-present",
"postbuild": "mkdir -p .upload && cd ./packages/devtool-chrome && zip -r chrome.zip ./dist/chrome/* && mv chrome.zip ../../.upload/",
"watch": "npm run --ws watch --if-present",
Expand Down
2 changes: 1 addition & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"start": "npm run watch"
},
"devDependencies": {
"pixi.js": "8.1.4"
"pixi.js": "8.1.5"
},
"peerDependencies": {
"pixi.js": "^7 || ^8"
Expand Down
5 changes: 4 additions & 1 deletion packages/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
/// <reference path="../global.d.ts" />
import { Devtools, DevtoolsAPI } from './types';

export * from './propertyPlugin';
export * from './nodeTrackerPlugin';
export * from './treePlugin';
export * from './types';

export async function initDevtools(opts: DevtoolsAPI) {
Expand All @@ -11,7 +14,7 @@ export async function initDevtools(opts: DevtoolsAPI) {
throw new Error('You must provide either an app or a renderer and stage');
}
if (castOpts.app) {
castOpts.renderer = castOpts.app.renderer as import('pixi.js').Renderer;
castOpts.renderer = castOpts.app.renderer;
castOpts.stage = castOpts.app.stage;
}

Expand Down
6 changes: 6 additions & 0 deletions packages/api/src/nodeTrackerPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Container } from 'pixi.js';

export interface NodeTrackerPlugin {
trackNode: (container: Container, state: Record<string, number>) => boolean;
getKeys: () => string[];
}
24 changes: 24 additions & 0 deletions packages/api/src/propertyPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { Container } from 'pixi.js';

type PropertyPanelData = {
value: any;
prop: string;
entry: {
section: string;
label?: string;
type: 'boolean' | 'number' | 'range' | 'select' | 'text' | 'button' | 'vector2' | 'vectorX' | 'color';
options?: any;
onChange: (value: string | number | boolean) => void;
};
};

type NoOnChange = Omit<PropertyPanelData['entry'], 'onChange'>;
export type Props = Omit<PropertyPanelData, 'entry'> & { entry: NoOnChange };
export type PropsData = Omit<PropertyPanelData, 'entry'> & { entry: NoOnChange };

export interface PropertyPlugin {
updateProps(container: Container): PropsData[];
setValue(container: Container, prop: string, value: any): void;
containsProperty(prop: string): boolean;
props: Props[];
}
12 changes: 12 additions & 0 deletions packages/api/src/treePlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Container } from 'pixi.js';

export interface TreePlugin {
/** to implement */
onRename: (container: Container, newName: string) => void;
onDeleted: (container: Container) => void;
onCreate: (container: Container) => void;
onSwap: (container: Container, newIndex: number) => void;
/** to implement */

getName: (container: Container, data: any) => { name: string; suffix: string };
}
38 changes: 6 additions & 32 deletions packages/api/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,12 @@
import type { Container, Renderer, Application } from 'pixi.js';
import { PropertyPlugin } from './propertyPlugin';
import { NodeTrackerPlugin } from './nodeTrackerPlugin';

type PropertyPanelData = {
value: any;
prop: string;
entry: {
section: string;
label?: string;
type: 'boolean' | 'number' | 'range' | 'select' | 'text' | 'button' | 'vector2' | 'vectorX' | 'color';
options?: any;
onChange: (value: string | number | boolean) => void;
};
};

export interface NodeTrackerPlugin {
trackNode: (container: Container, state: Record<string, number>) => boolean;
getKeys: () => string[];
}

type NoOnChange = Omit<PropertyPanelData['entry'], 'onChange'>;
export type Props = Omit<PropertyPanelData, 'entry'> & { entry: NoOnChange };
export type PropsData = Omit<PropertyPanelData, 'entry'> & { entry: NoOnChange };

export interface PropertyPlugin {
updateProps(container: Container): PropsData[];
setValue(container: Container, prop: string, value: any): void;
containsProperty(prop: string): boolean;
props: Props[];
}

interface DevtoolApp {
export interface DevtoolApp extends DevtoolPixi {
app: Application;
}

interface DevtoolRenderer {
export interface DevtoolRenderer extends DevtoolPixi {
renderer: Renderer;
stage: Container;
}
Expand All @@ -45,5 +19,5 @@ interface DevtoolPixi {
};
}

export type DevtoolsAPI = (DevtoolApp | DevtoolRenderer) & DevtoolPixi;
export type Devtools = Partial<DevtoolApp & DevtoolRenderer & DevtoolPixi>;
export type DevtoolsAPI = DevtoolApp | DevtoolRenderer;
export type Devtools = Partial<DevtoolApp & DevtoolRenderer>;
8 changes: 8 additions & 0 deletions packages/backend/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@devtool/backend",
"version": "0.0.0",
"private": true,
"type": "module",
"scripts": {},
"dependencies": {}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { DevtoolMessage } from '@devtool/frontend/types';
import { convertPostMessage } from '../../messageUtils';
import { PixiDevtools } from '../pixi';

// Constants
Expand Down Expand Up @@ -46,7 +45,6 @@ function tryDetectPixi(foundCallback: () => void) {
if (pixiDetectionResult === DevtoolMessage.active) {
stopPolling();
foundCallback();
window.postMessage(convertPostMessage(DevtoolMessage.active, {}), '*');
}
} catch (error) {
// If an error occurred, stop polling
Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion packages/devtool-chrome/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"start": "npm run watch"
},
"dependencies": {
"@devtool/frontend": "*"
"@devtool/frontend": "*",
"@devtool/backend": "*"
}
}
8 changes: 6 additions & 2 deletions packages/devtool-chrome/src/inject/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Note: this file is compiled to `dist/content-inject/index.js` and is used by the content script

import { PixiDevtools } from './pixi';
import { pollPixi } from './utils/poller';
import { PixiDevtools } from '@devtool/backend/pixi';
import { pollPixi } from '@devtool/backend/utils/poller';
import { convertPostMessage } from '../messageUtils';
import { DevtoolMessage } from '@devtool/frontend/types';

function attach() {
const renderer = PixiDevtools.renderer;
Expand All @@ -15,6 +17,8 @@ function attach() {
},
});
}

window.postMessage(convertPostMessage(DevtoolMessage.active, {}), '*');
}

pollPixi(attach);
1 change: 1 addition & 0 deletions packages/devtool-chrome/vite.chrome.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default defineConfig((config) => {
resolve: {
alias: {
'@devtool/frontend': path.resolve(process.cwd(), '../../packages/frontend/src/'),
'@devtool/backend': path.resolve(process.cwd(), '../../packages/backend/src/'),
},
},
plugins: [
Expand Down
1 change: 1 addition & 0 deletions packages/devtool-chrome/vite.inject.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default defineConfig((config) => {
resolve: {
alias: {
'@devtool/frontend': path.resolve(process.cwd(), '../../packages/frontend/src/'),
'@devtool/backend': path.resolve(process.cwd(), '../../packages/backend/src/'),
},
},
root: resolve(__dirname, 'src/'),
Expand Down
3 changes: 2 additions & 1 deletion packages/devtool-local/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"start": "npm run watch"
},
"dependencies": {
"@devtool/frontend": "*"
"@devtool/frontend": "*",
"@devtool/backend": "*"
}
}
2 changes: 1 addition & 1 deletion packages/devtool-local/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import App from '@devtool/frontend/App';
import type { BridgeFn } from '@devtool/frontend/lib/utils';
import React from 'react';
import ReactDOM from 'react-dom/client';
import { PixiDevtools } from '../../devtool-chrome/src/inject/pixi';
import { PixiDevtools } from '@devtool/backend/pixi';
import scene from './scene.ts';
import * as PIXI from 'pixi.js';

Expand Down
1 change: 1 addition & 0 deletions packages/devtool-local/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default defineConfig({
resolve: {
alias: {
'@devtool/frontend': path.resolve(process.cwd(), '../../packages/frontend/src/'),
'@devtool/backend': path.resolve(process.cwd(), '../../packages/backend/src/'),
},
},
});
9 changes: 3 additions & 6 deletions packages/frontend/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useEffect } from 'react';
import { PrismAsyncLight as SyntaxHighlighter } from 'react-syntax-highlighter';
import ts from 'react-syntax-highlighter/dist/esm/languages/prism/typescript';
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
import { dracula } from 'react-syntax-highlighter/dist/esm/styles/prism';

import { create } from 'zustand';
Expand All @@ -15,8 +14,6 @@ import { ScenePanel } from './pages/scene/ScenePanel';
import { sceneStateSlice } from './pages/scene/state';
import { DevtoolState } from './types';

SyntaxHighlighter.registerLanguage('typescript', ts);

const tabComponents = {
Scene: <ScenePanel />,
Assets: <AssetsPanel />,
Expand Down Expand Up @@ -153,7 +150,7 @@ initDevtools({
<div className="p-4">
<p>1. Using the window.__PIXI_DEVTOOLS__ global variable</p>
<div className="relative p-4">
<SyntaxHighlighter language="typescript" style={dracula}>
<SyntaxHighlighter language="javascript" style={dracula}>
{windowString}
</SyntaxHighlighter>
<CopyToClipboardButton data={windowString} />
Expand All @@ -170,7 +167,7 @@ initDevtools({
<CopyToClipboardButton data={npmInstallString} />
</div>
<div className="relative p-4 pt-0">
<SyntaxHighlighter language="typescript" style={dracula}>
<SyntaxHighlighter language="javascript" style={dracula}>
{npmString}
</SyntaxHighlighter>
<CopyToClipboardButton data={npmString} />
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"baseUrl": "./",
"paths": {
"@devtool/frontend/*": ["packages/frontend/src/*"],
"@devtool/backend/*": ["packages/backend/src/*"],
"@pixi/devtools": ["packages/api/src/index.ts"]
}
},
Expand Down

0 comments on commit d1e905b

Please sign in to comment.