追加测试server的逻辑

This commit is contained in:
xu_yanfeng
2024-12-09 21:37:07 +08:00
parent 52fe01c01b
commit bacf6817ad
6 changed files with 86 additions and 26 deletions

View File

@@ -1,19 +1,22 @@
import CCP from "cc-plugin/src/ccp/entry-render";
import { Msg, Page, PluginEvent } from "../../core/types";
import { TestClient, testServer, TestServer } from "./test/server";
export type BackgroundCallback = (data: PluginEvent, sender: any) => void;
class ConnectBackground {
class ConnectBackground implements TestClient {
connect: chrome.runtime.Port | null = null;
constructor() {
this._initConnect();
}
private _initConnect() {
if (chrome && chrome.runtime) {
if (CCP.Adaptation.Env.isChromeRuntime) {
this.connect = chrome.runtime.connect({ name: Page.Devtools });
this.connect.onDisconnect.addListener(() => {
console.log(`%c[Connect-Dis]`, "color:red;")
this.connect = null;
})
} else {
testServer.add(this);
}
}
/**
@@ -28,16 +31,20 @@ class ConnectBackground {
});
}
}
testMessage(data: PluginEvent) {
recv(event: PluginEvent): void {
this.doCallback(event);
}
doCallback(data: PluginEvent) {
if (this.callback) {
this.callback(data, null);
}
}
sendMsgToContentScript(msg: Msg, data?: any) {
if (!chrome || !chrome.devtools) {
return;
if (CCP.Adaptation.Env.isChromeDevtools) {
this.postMessageToBackground(msg, data);
} else {
testServer.recv(msg, data);
}
this.postMessageToBackground(msg, data);
}
postMessageToBackground(msg: Msg, data?: any) {
if (this.connect) {

View File

@@ -259,6 +259,12 @@ export class Group {
}
export interface NodeInfoData {
uuid: string;// 节点的uuid
/**
* 节点的uuid
*/
uuid: string;
/**
* 组件数据
*/
group: Group[];
}

View File

@@ -23,7 +23,7 @@
</slot>
</CCInput>
<div class="treeList">
<CCTree :value="treeData"></CCTree>
<CCTree :value="treeData" @node-click="handleNodeClick"></CCTree>
<!-- <el-tree
:data="treeData"
ref="tree"
@@ -78,10 +78,10 @@ import Bus, { BusMsg } from "./bus";
import { connectBackground } from "./connectBackground";
import { EngineData, FrameDetails, Info, NodeInfoData, ObjectData, ObjectItemRequestData, TreeData } from "./data";
import { appStore, RefreshType } from "./store";
import Test from "./test.vue";
ccui.components.CCAd;
import Test from "./test/test.vue";
import properties from "./ui/propertys.vue";
import SettingsVue from "./ui/settings.vue";
ccui.components.CCAd;
const { CCTree, CCFootBar, CCDialog, CCInput, CCButton, CCInputNumber, CCSelect, CCButtonGroup, CCCheckBox, CCColor, CCDivider } = ccui.components;
interface FrameInfo {
label: string;

View File

@@ -0,0 +1,46 @@
import { Msg, Page, PluginEvent } from "../../../core/types";
import { NodeInfoData, TreeData } from "../data";
export class TestClient {
recv(event: PluginEvent) {
}
}
export class TestServer {
private clients: TestClient[] = [];
add(client: TestClient) {
this.clients.push(client);
}
recv(msg: string, data: any) {
switch (msg) {
case Msg.NodeInfo: {
console.log(data);
const ret: NodeInfoData = {
uuid: "1",
group: []
};
const event = new PluginEvent(Page.Background, Page.Devtools, Msg.NodeInfo, ret);
this.send(event);
break;
}
case Msg.TreeInfo: {
const data: TreeData = {
id: "1",
text: "root",
active: true,
children: [],
};
const event = new PluginEvent(Page.Inject, Page.Devtools, Msg.TreeInfo, data);
this.send(event);
break;
}
default:
break;
}
}
send(event: PluginEvent) {
this.clients.forEach((client) => {
client.recv(event)
});
}
}
export const testServer = new TestServer();

View File

@@ -11,9 +11,10 @@
import ccui from "@xuyanfeng/cc-ui";
import { ITreeData } from "@xuyanfeng/cc-ui/types/cc-tree/const";
import { defineComponent, ref } from "vue";
import { Msg, Page, PluginEvent } from "../../core/types";
import { connectBackground } from "./connectBackground";
import { TreeData } from "./data";
import { Msg, Page, PluginEvent } from "../../../core/types";
import { connectBackground } from "../connectBackground";
import { TreeData } from "../data";
import { TestServer } from "./server";
const { CCButton, CCSection } = ccui.components;
export default defineComponent({
name: "test",
@@ -40,7 +41,7 @@ export default defineComponent({
children: [],
};
const event = new PluginEvent(Page.Inject, Page.Devtools, Msg.TreeInfo, data);
connectBackground.testMessage(event);
connectBackground.doCallback(event);
},
};
},