Skip to content

Commit 721da6f

Browse files
committed
perf: use group extra to storage custom web panel html
and compatible with old meta webpanel storage
1 parent ed6c9f3 commit 721da6f

File tree

6 files changed

+83
-25
lines changed

6 files changed

+83
-25
lines changed

client/web/plugins/com.msgbyte.webview/src/group/GroupCustomWebPanelRender.tsx

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
1-
import React, { useEffect, useRef } from 'react';
1+
import React, { useEffect, useRef, useState } from 'react';
22
import { Translate } from '../translate';
33
import xss from 'xss';
4+
import { useWatch } from '@capital/common';
5+
import { GroupExtraDataPanel, TextArea } from '@capital/component';
6+
import styled from 'styled-components';
7+
8+
const EditModalContent = styled.div`
9+
padding: 10px;
10+
width: 80vw;
11+
height: 80vh;
12+
display: flex;
13+
flex-direction: column;
14+
overflow: hidden;
15+
16+
.main {
17+
flex: 1;
18+
overflow: hidden;
19+
20+
> textarea {
21+
height: 100%;
22+
resize: none;
23+
}
24+
}
25+
`;
426

527
function getInjectedStyle() {
628
try {
@@ -15,10 +37,9 @@ function getInjectedStyle() {
1537
}
1638
}
1739

18-
const GroupCustomWebPanelRender: React.FC<{ panelInfo: any }> = (props) => {
19-
const panelInfo = props.panelInfo;
40+
const GroupCustomWebPanelRender: React.FC<{ html: string }> = (props) => {
2041
const ref = useRef<HTMLIFrameElement>(null);
21-
const html = panelInfo?.meta?.html;
42+
const html = props.html;
2243

2344
useEffect(() => {
2445
if (!ref.current || !html) {
@@ -31,12 +52,56 @@ const GroupCustomWebPanelRender: React.FC<{ panelInfo: any }> = (props) => {
3152
doc.close();
3253
}, [html]);
3354

34-
if (!panelInfo) {
55+
if (!html) {
3556
return <div>{Translate.notfound}</div>;
3657
}
3758

3859
return <iframe ref={ref} className="w-full h-full" />;
3960
};
4061
GroupCustomWebPanelRender.displayName = 'GroupCustomWebPanelRender';
4162

42-
export default GroupCustomWebPanelRender;
63+
const GroupCustomWebPanelEditor: React.FC<{
64+
initValue: string;
65+
onChange: (html: string) => void;
66+
}> = React.memo((props) => {
67+
const [html, setHtml] = useState(() => props.initValue ?? '');
68+
69+
useWatch([html], () => {
70+
props.onChange(html);
71+
});
72+
73+
return <TextArea value={html} onChange={(e) => setHtml(e.target.value)} />;
74+
});
75+
GroupCustomWebPanelEditor.displayName = 'GroupCustomWebPanelEditor';
76+
77+
const GroupCustomWebPanel: React.FC<{ panelInfo: any }> = (props) => {
78+
return (
79+
<GroupExtraDataPanel
80+
names={['html']}
81+
render={(dataMap: Record<string, string>) => {
82+
return (
83+
<GroupCustomWebPanelRender
84+
html={dataMap['html'] ?? props.panelInfo?.meta?.html ?? ''}
85+
/>
86+
);
87+
}}
88+
renderEdit={(dataMap: Record<string, string>) => {
89+
return (
90+
<EditModalContent>
91+
<div>{Translate.editTip}</div>
92+
93+
<div className="main">
94+
<GroupCustomWebPanelEditor
95+
initValue={dataMap['html'] ?? props.panelInfo?.meta?.html ?? ''}
96+
onChange={(html) => (dataMap['html'] = html)}
97+
/>
98+
</div>
99+
</EditModalContent>
100+
);
101+
}}
102+
/>
103+
);
104+
};
105+
GroupCustomWebPanel.displayName = 'GroupCustomWebPanel';
106+
107+
export default GroupCustomWebPanel;

client/web/plugins/com.msgbyte.webview/src/index.tsx

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,7 @@ regGroupPanel({
3333
name: `${PLUGIN_NAME}/customwebpanel`,
3434
label: Translate.customwebpanel,
3535
provider: PLUGIN_NAME,
36-
extraFormMeta: [
37-
{
38-
type: 'textarea',
39-
name: 'html',
40-
label: Translate.htmlcode,
41-
placeholder: Translate.customwebpanelPlaceholder,
42-
},
43-
],
4436
render: Loadable(() => import('./group/GroupCustomWebPanelRender'), {
45-
componentName: 'com.msgbyte.webview:GroupCustomWebPanelRender',
37+
componentName: `${PLUGIN_NAME}:GroupCustomWebPanelRender`,
4638
}),
4739
});

client/web/plugins/com.msgbyte.webview/src/translate.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ export const Translate = {
66
'zh-CN': '自定义网页面板',
77
'en-US': 'Custom Webview Panel',
88
}),
9-
customwebpanelPlaceholder: localTrans({
10-
'zh-CN': '建议在第三方页面编辑完毕后粘贴到此处',
11-
'en-US': 'Recommended to paste it here after editing the third-party page',
12-
}),
139
notfound: localTrans({
1410
'zh-CN': '加载失败, 面板信息不存在',
1511
'en-US': 'Loading failed, panel info does not exist',
@@ -26,4 +22,8 @@ export const Translate = {
2622
'zh-CN': '在外部打开',
2723
'en-US': 'Open in extra',
2824
}),
25+
editTip: localTrans({
26+
'zh-CN': '使用html语法编辑, 关闭窗口自动保存',
27+
'en-US': 'Edit with html syntax, close the window and save automatically',
28+
}),
2929
};

client/web/src/plugin/common/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export {
6969
joinArray,
7070
useConverseMessageContext,
7171
loginWithToken,
72+
useWatch,
7273
} from 'tailchat-shared';
7374

7475
export { navigate } from '@/components/AppRouterApi';

client/web/src/plugin/common/reg.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export interface PluginGroupPanel {
8888
/**
8989
* 额外的表单数据, 用于创建面板时使用
9090
*/
91-
extraFormMeta: MetaFormFieldMeta[];
91+
extraFormMeta?: MetaFormFieldMeta[];
9292

9393
/**
9494
* 该面板如何渲染

website/docs/plugins/api/common.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ Register Group Panel
1111

1212
```typescript
1313
regGroupPanel({
14-
name: `com.msgbyte.webview/grouppanel`,
15-
label: 'web panel',
16-
provider: PLUGIN_NAME,
17-
extraFormMeta: [{ type: 'text', name: 'url', label: 'URL' }],
18-
render: GroupWebPanelRender,
14+
name: `com.msgbyte.webview/grouppanel`,
15+
label: 'web panel',
16+
provider: PLUGIN_NAME,
17+
extraFormMeta: [{ type: 'text', name: 'url', label: 'URL' }],
18+
render: GroupWebPanelRender,
1919
});
2020
```
2121

0 commit comments

Comments
 (0)