Skip to content

Commit 8453574

Browse files
authored
feat: support miniprogram (#120)
1 parent a6fcd73 commit 8453574

File tree

38 files changed

+499
-230
lines changed

38 files changed

+499
-230
lines changed

.github/workflows/build_docker.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ name: Create and publish a dev Docker image
1111

1212
on:
1313
push:
14-
branches: ["main"]
14+
branches: ["main","feat/miniprogram"]
1515

1616
env:
1717
REGISTRY: ghcr.io

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
},
1818
"devDependencies": {
1919
"@babel/eslint-parser": "^7.19.1",
20-
"@huolala-tech/page-spy": "^1.4.11",
20+
"@huolala-tech/page-spy": "^1.5.2",
2121
"@mdx-js/rollup": "^2.3.0",
2222
"@types/lodash-es": "^4.17.7",
2323
"@types/mdx": "^2.0.4",
@@ -38,7 +38,7 @@
3838
"less": "^4.1.2",
3939
"lint-staged": "^12.3.3",
4040
"prettier": "^2.5.1",
41-
"typescript": "^4.7.4",
41+
"typescript": "^5.3.3",
4242
"vite": "^4.0.0",
4343
"vite-plugin-svgr": "^2.4.0",
4444
"yorkie": "^2.0.0"

scripts/public-files.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ root=$(pwd)
55
# SDK
66
target_sdk="${root}/public/page-spy"
77
mkdir -p "$target_sdk"
8-
cp "${root}/node_modules/@huolala-tech/page-spy/dist/index.min.js" "${root}/public/page-spy/index.min.js"
8+
cp "${root}/node_modules/@huolala-tech/page-spy/dist/web/index.min.js" "${root}/public/page-spy/index.min.js"
99

1010
# source-map
1111
target_sourcemap="${root}/public/source-map"

src/assets/image/miniprogram.svg

Lines changed: 8 additions & 0 deletions
Loading

src/assets/image/web-h5.svg

Lines changed: 8 additions & 0 deletions
Loading

src/assets/locales/en.json

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,18 @@
2626
"no-frames": "No valid error information found"
2727
},
2828
"inject": {
29-
"load-script": "Load a <script> in the testing project;",
30-
"init-instance": "<0>Next, configure (optional) and initialize; check </0><1>configuration options;</1>",
29+
"web": {
30+
"title": "WEB",
31+
"load-sdk": "Load a <script> in the test project;",
32+
"init-sdk": "<0>Then, initialize it and check the</0> <1>configuration options</1>:"
33+
},
34+
"miniprogram": {
35+
"title": "Mini Program",
36+
"request-host": "Add the page-spy server domain to the whitelist of the miniprogram http and websocket requests. Note that the miniprogram requires https and wss protocols, except for the development environment.",
37+
"install-sdk": "First, install dependencies in the project:",
38+
"init-sdk": "<0>For cross platform frameworks like uniApp and Taro,import the SDK in the entry file and initialize it,check the </0> <1>configuration options</1>:",
39+
"init-sdk-native": "For native miniprogram, you can not use node_modules directly, you have to copy the SDK file to your project path and import them:"
40+
},
3141
"end": "That's ALL.",
3242
"start-debug": "Start debugging by clicking the <1>Connections</1> menu at the top!"
3343
},

src/assets/locales/ja.json

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,18 @@
2626
"no-frames": "有効なエラー情報が見つかりませんでした"
2727
},
2828
"inject": {
29-
"load-script": "テストプロジェクトに <script> を読み込む;",
30-
"init-instance": "<0>次に、構成(オプション)して初期化します; </0><1>構成オプションを確認します。</1>",
29+
"web": {
30+
"title": "WEB",
31+
"load-sdk": "テストプロジェクトで <script> をロードする;",
32+
"init-sdk": "<0>次に、初期化し、</0> <1>構成オプション</1> を確認します:"
33+
},
34+
"miniprogram": {
35+
"title": "Mini Program",
36+
"request-host": "page-spy サービスドメインを小プログラムのhttp、websocketリクエストのホワイトリストに入力します。開発環境以外では、小プログラムはhttpsとwssプロトコルの使用を強制しますので注意してください。",
37+
"install-sdk": "まず、プロジェクトに依存関係をインストールします:",
38+
"init-sdk": "<0>クロスプラットフォームのミニプログラムフレームワーク(uniApp、Taroなど)については、エントリーファイルでSDKをインポートし、インスタンス化します。</0> <1>構成オプション</1> を確認します:",
39+
"init-sdk-native": "もし原生のミニプログラムであれば、node_modulesのパスを直接使用することができないため、パッケージ内のファイルを手動でプロジェクトディレクトリにコピーしてインポートする必要があります。"
40+
},
3141
"end": "以上がすべてです。",
3242
"start-debug": "<1>接続</1>メニューからトップに移動し、デバッグを開始します!"
3343
},

src/assets/locales/ko.json

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,18 @@
2626
"no-frames": "유효한 오류 정보를 찾을 수 없습니다"
2727
},
2828
"inject": {
29-
"load-script": "테스트 프로젝트에 <script>를 로드합니다.",
30-
"init-instance": "<0>다음으로, 구성 (옵션) 및 초기화; 확인 </0><1>구성 옵션;</1>",
29+
"web": {
30+
"title": "WEB",
31+
"load-sdk": "테스트 프로젝트에 <script>를 로드하십시오;",
32+
"init-sdk": "<0>그런 다음 초기화하고</0> <1>구성 옵션</1>을 확인하십시오:"
33+
},
34+
"miniprogram": {
35+
"title": "Mini Program",
36+
"request-host": "\"page-spy\" 서비스 도메인을 미니 프로그램의 HTTP 및 WebSocket 요청 화이트리스트에 추가하십시오. 개발 환경을 제외하고는 미니 프로그램에서는 HTTPS 및 WSS 프로토콜을 의무적으로 사용해야 합니다.",
37+
"install-sdk": "먼저 프로젝트에 종속성을 설치하십시오:",
38+
"init-sdk": "<0>크로스 플랫폼 미니 프로그램 프레임워크인 uniApp 및 Taro와 같은 경우, 엔트리 파일에서 SDK를 가져와 인스턴스화합니다.</0> <1>구성 옵션</1>을 확인하십시오:",
39+
"init-sdk-native": "원래의 미니 프로그램이라면 node_modules의 경로를 직접 사용할 수 없으므로, 패키지 내 파일을 수동으로 프로젝트 디렉토리에 복사하고 가져와야 합니다."
40+
},
3141
"end": "모두 여기까지입니다.",
3242
"start-debug": "<1>연결</1> 메뉴에서 상단으로 이동하여 디버그를 시작하세요!"
3343
},

src/assets/locales/zh.json

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,18 @@
2626
"no-frames": "未找到有效的错误信息"
2727
},
2828
"inject": {
29-
"load-script": "在测试项目中加载一个 <script>;",
30-
"init-instance": "<0>紧接着,配置(可选的)并初始化;查看 </0><1>配置项;</1>",
29+
"web": {
30+
"title": "WEB",
31+
"load-sdk": "在测试项目中加载一个 <script>;",
32+
"init-sdk": "<0>紧接着初始化,查看</0> <1>配置项</1>:"
33+
},
34+
"miniprogram": {
35+
"title": "小程序",
36+
"request-host": "将 page-spy 服务域名填入小程序的 http、websocket 请求白名单中。注意除了开发环境,小程序强制要求使用 https 和 wss 协议。",
37+
"install-sdk": "首先在项目中安装依赖:",
38+
"init-sdk": "<0>对于跨端小程序框架(uniApp, Taro 等),在入口文件中引入 SDK 并实例化,查看</0> <1>配置项</1>:",
39+
"init-sdk-native": "如果是原生小程序,由于无法直接使用 node_modules 路径,需要手动将包中的文件复制到项目目录中引入:"
40+
},
3141
"end": "以上就是全部。",
3242
"start-debug": "从顶部的 <1>房间列表</1> 菜单进入并开始调试!"
3343
},

src/components/CodeBlock/index.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,22 @@ import { Space } from 'antd';
55
import { CheckOutlined } from '@ant-design/icons';
66
import sh from '@/utils/shiki-highlighter';
77
import { useAsyncEffect } from 'ahooks';
8+
import type { Lang } from 'shiki';
89

910
interface Props {
1011
code: string;
12+
lang?: Lang;
1113
showCopy?: boolean;
1214
}
1315

14-
export const CodeBlock = ({ code, showCopy = true }: Props) => {
16+
export const CodeBlock = ({ code, lang = 'html', showCopy = true }: Props) => {
1517
const [codeContent, setCodeContent] = useState('');
1618
useAsyncEffect(async () => {
1719
const highlighter = await sh.get({
18-
lang: 'html',
20+
lang,
1921
});
2022
const content = highlighter.codeToHtml(code, {
21-
lang: 'html',
23+
lang,
2224
});
2325
setCodeContent(content);
2426
}, [code]);
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import { ReactComponent as WebSvg } from '@/assets/image/web-h5.svg';
2+
import { ReactComponent as MiniprogramSvg } from '@/assets/image/miniprogram.svg';
3+
import { ReactNode, useMemo, type ComponentType } from 'react';
4+
import { Trans, useTranslation } from 'react-i18next';
5+
import { Link } from 'react-router-dom';
6+
import type { Lang } from 'shiki';
7+
import { CodeBlock } from '@/components/CodeBlock';
8+
9+
export type PlatformName = 'web' | 'miniprogram';
10+
11+
export const PLATFORMS: { name: PlatformName; icon: ComponentType }[] = [
12+
{
13+
name: 'web',
14+
icon: WebSvg,
15+
},
16+
{
17+
name: 'miniprogram',
18+
icon: MiniprogramSvg,
19+
},
20+
];
21+
22+
interface Props {
23+
platform: PlatformName;
24+
onCloseModal: () => void;
25+
}
26+
27+
export const IntegrationWithPlatform = ({ platform, onCloseModal }: Props) => {
28+
const { t } = useTranslation();
29+
const deployPath = (location.host + location.pathname).replace(/\/+$/, '');
30+
const steps = useMemo(() => {
31+
const stepsWithPlatform = {
32+
web: [
33+
{
34+
title: t('inject.web.load-sdk'),
35+
code: `<script crossorigin="anonymous" src="${location.protocol}//${deployPath}/page-spy/index.min.js"></script>`,
36+
},
37+
{
38+
title: (
39+
<Trans i18nKey="inject.web.init-sdk">
40+
<span>slot-0</span>
41+
<a
42+
href="https://github.com/HuolalaTech/page-spy?tab=readme-ov-file#web"
43+
target="_blank"
44+
>
45+
slot-1
46+
</a>
47+
</Trans>
48+
),
49+
code: `<script>
50+
window.$pageSpy = new PageSpy();
51+
</script>`,
52+
},
53+
],
54+
miniprogram: [
55+
{
56+
title: t('inject.miniprogram.install-sdk'),
57+
code: `yarn add @huolala-tech/page-spy@latest`,
58+
lang: 'bash',
59+
},
60+
{
61+
title: t('inject.miniprogram.request-host'),
62+
code: `https://${window.location.host}\nwss://${window.location.host}`,
63+
},
64+
{
65+
title: (
66+
<Trans i18nKey="inject.miniprogram.init-sdk">
67+
<span>slot-0</span>
68+
<a
69+
href="https://github.com/HuolalaTech/page-spy?tab=readme-ov-file#mini-program"
70+
target="_blank"
71+
>
72+
slot-1
73+
</a>
74+
</Trans>
75+
),
76+
code: `// @huolala-tech/page-spy v1.5.x or upper version. \nimport PageSpy from '@huolala-tech/page-spy/miniprogram';\n\nnew PageSpy({
77+
api: '${deployPath}',
78+
})`,
79+
lang: 'js',
80+
},
81+
{
82+
title: t('inject.miniprogram.init-sdk-native'),
83+
code: `import PageSpy from './your/path/page-spy.js';\n\nnew PageSpy({
84+
api: '${deployPath}',
85+
})`,
86+
lang: 'js',
87+
},
88+
],
89+
};
90+
return [
91+
...stepsWithPlatform[platform],
92+
{
93+
title: (
94+
<span>
95+
{t('inject.end')}{' '}
96+
<Trans i18nKey="inject.start-debug">
97+
Start debugging by clicking the
98+
<Link to="/room-list" onClickCapture={onCloseModal}>
99+
Connections
100+
</Link>{' '}
101+
menu at the top!
102+
</Trans>
103+
</span>
104+
),
105+
code: '',
106+
},
107+
] as { title: ReactNode; code: string; lang?: Lang }[];
108+
}, [onCloseModal, platform, t]);
109+
110+
return (
111+
<div className="platform-integratio">
112+
{steps.map(({ title, code, lang = 'html' }, index) => {
113+
return (
114+
<div className="inject-steps" key={index}>
115+
<p className="inject-steps__title">
116+
{index + 1}. {title}
117+
</p>
118+
<CodeBlock code={code} lang={lang} />
119+
</div>
120+
);
121+
})}
122+
</div>
123+
);
124+
};

0 commit comments

Comments
 (0)