mirror of https://github.com/msgbyte/tailchat
parent
93985b02fb
commit
92d5e39cfc
@ -1,3 +0,0 @@
|
||||
const PLUGIN_NAME = 'ReactNative支持';
|
||||
|
||||
console.log(`Plugin ${PLUGIN_NAME} is loaded`);
|
@ -1,8 +1,32 @@
|
||||
// @ts-nocheck
|
||||
|
||||
/**
|
||||
* 生成注入到Webview中的js代码
|
||||
*/
|
||||
export function generateInjectScript() {
|
||||
// console.log(require('../../../dist/plugins/com.msgbyte.env.rn/index.js'));
|
||||
export function generateInstallPluginScript() {
|
||||
/**
|
||||
* manifest copy from:
|
||||
* com.msgbyte.env.rn/manifest.json
|
||||
*/
|
||||
const inner = `function main() {
|
||||
window.tailchat
|
||||
.installPlugin({
|
||||
label: 'ReactNative支持',
|
||||
name: 'com.msgbyte.env.rn',
|
||||
url: '/plugins/com.msgbyte.env.rn/index.js',
|
||||
version: '0.0.0',
|
||||
author: 'moonrailgun',
|
||||
description: '在Tailchat添加对ReactNative环境的支持',
|
||||
requireRestart: true,
|
||||
});
|
||||
}`;
|
||||
|
||||
const raw = `(${inner})()`;
|
||||
return raw;
|
||||
}
|
||||
|
||||
return `alert(JSON.stringify(window.tailchat))`;
|
||||
export function generatePostMessageScript() {
|
||||
return `window.postMessage = function (data) {
|
||||
window.ReactNativeWebView.postMessage(JSON.stringify(data));
|
||||
};`;
|
||||
}
|
||||
|
@ -0,0 +1,22 @@
|
||||
import type WebView from 'react-native-webview';
|
||||
import { generateInstallPluginScript } from '.';
|
||||
import { useUIStore } from '../../store/ui';
|
||||
|
||||
export function handleTailchatMessage(
|
||||
type: string,
|
||||
payload: any,
|
||||
webview: WebView
|
||||
) {
|
||||
console.log('onMessage receive:', type, payload);
|
||||
|
||||
if (type === 'init') {
|
||||
webview.injectJavaScript(generateInstallPluginScript());
|
||||
return;
|
||||
}
|
||||
|
||||
if (type === 'loadColorScheme') {
|
||||
// 设置颜色
|
||||
useUIStore.getState().setColorScheme(payload);
|
||||
return;
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
import { create } from 'zustand';
|
||||
import { immer } from 'zustand/middleware/immer';
|
||||
import { persist } from 'zustand/middleware';
|
||||
import { zustandRNStorage } from '../lib/utils/storage';
|
||||
|
||||
interface UIStoreState {
|
||||
colorScheme: 'dark' | 'light' | 'auto';
|
||||
setColorScheme: (colorScheme: 'dark' | 'light' | 'auto' | string) => void;
|
||||
}
|
||||
|
||||
export const useUIStore = create<UIStoreState>()(
|
||||
persist(
|
||||
immer((set) => ({
|
||||
colorScheme: 'dark',
|
||||
setColorScheme: (colorScheme) => {
|
||||
if (colorScheme === 'dark') {
|
||||
set({
|
||||
colorScheme: 'dark',
|
||||
});
|
||||
} else if (colorScheme === 'light') {
|
||||
set({
|
||||
colorScheme: 'light',
|
||||
});
|
||||
} else {
|
||||
set({
|
||||
colorScheme: 'auto',
|
||||
});
|
||||
}
|
||||
},
|
||||
})),
|
||||
{
|
||||
name: 'ui',
|
||||
storage: zustandRNStorage,
|
||||
partialize: (state) => ({ colorScheme: state.colorScheme }),
|
||||
}
|
||||
)
|
||||
);
|
@ -0,0 +1,16 @@
|
||||
import { sharedEvent } from '@capital/common';
|
||||
|
||||
const PLUGIN_NAME = 'ReactNative支持';
|
||||
|
||||
console.log(`Plugin ${PLUGIN_NAME} is loaded`);
|
||||
|
||||
sharedEvent.on('loadColorScheme', (colorScheme: string) => {
|
||||
window.postMessage(
|
||||
{
|
||||
_isTailchat: true,
|
||||
type: 'loadColorScheme',
|
||||
payload: colorScheme,
|
||||
},
|
||||
'*'
|
||||
);
|
||||
});
|
@ -0,0 +1,21 @@
|
||||
/**
|
||||
* 消息生命周期
|
||||
*/
|
||||
interface MessageEventMap {
|
||||
init: undefined; // 初始化阶段
|
||||
loaded: undefined; // 插件加载完毕
|
||||
}
|
||||
|
||||
export function postMessageEvent<T extends keyof MessageEventMap>(
|
||||
eventType: T,
|
||||
eventData?: MessageEventMap[T]
|
||||
) {
|
||||
window.postMessage(
|
||||
{
|
||||
_isTailchat: true,
|
||||
type: eventType,
|
||||
payload: eventData,
|
||||
},
|
||||
'*'
|
||||
);
|
||||
}
|
Loading…
Reference in New Issue