-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
15 changed files
with
163 additions
and
14 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
};`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }), | ||
} | ||
) | ||
); |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}, | ||
'*' | ||
); | ||
}); |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}, | ||
'*' | ||
); | ||
} |