perf: use group extra to storage custom web panel html

and compatible with old meta webpanel storage
pull/109/head
moonrailgun 2 years ago
parent ed6c9f3a8f
commit 721da6f34c

@ -1,6 +1,28 @@
import React, { useEffect, useRef } from 'react';
import React, { useEffect, useRef, useState } from 'react';
import { Translate } from '../translate';
import xss from 'xss';
import { useWatch } from '@capital/common';
import { GroupExtraDataPanel, TextArea } from '@capital/component';
import styled from 'styled-components';
const EditModalContent = styled.div`
padding: 10px;
width: 80vw;
height: 80vh;
display: flex;
flex-direction: column;
overflow: hidden;
.main {
flex: 1;
overflow: hidden;
> textarea {
height: 100%;
resize: none;
}
}
`;
function getInjectedStyle() {
try {
@ -15,10 +37,9 @@ function getInjectedStyle() {
}
}
const GroupCustomWebPanelRender: React.FC<{ panelInfo: any }> = (props) => {
const panelInfo = props.panelInfo;
const GroupCustomWebPanelRender: React.FC<{ html: string }> = (props) => {
const ref = useRef<HTMLIFrameElement>(null);
const html = panelInfo?.meta?.html;
const html = props.html;
useEffect(() => {
if (!ref.current || !html) {
@ -31,7 +52,7 @@ const GroupCustomWebPanelRender: React.FC<{ panelInfo: any }> = (props) => {
doc.close();
}, [html]);
if (!panelInfo) {
if (!html) {
return <div>{Translate.notfound}</div>;
}
@ -39,4 +60,48 @@ const GroupCustomWebPanelRender: React.FC<{ panelInfo: any }> = (props) => {
};
GroupCustomWebPanelRender.displayName = 'GroupCustomWebPanelRender';
export default GroupCustomWebPanelRender;
const GroupCustomWebPanelEditor: React.FC<{
initValue: string;
onChange: (html: string) => void;
}> = React.memo((props) => {
const [html, setHtml] = useState(() => props.initValue ?? '');
useWatch([html], () => {
props.onChange(html);
});
return <TextArea value={html} onChange={(e) => setHtml(e.target.value)} />;
});
GroupCustomWebPanelEditor.displayName = 'GroupCustomWebPanelEditor';
const GroupCustomWebPanel: React.FC<{ panelInfo: any }> = (props) => {
return (
<GroupExtraDataPanel
names={['html']}
render={(dataMap: Record<string, string>) => {
return (
<GroupCustomWebPanelRender
html={dataMap['html'] ?? props.panelInfo?.meta?.html ?? ''}
/>
);
}}
renderEdit={(dataMap: Record<string, string>) => {
return (
<EditModalContent>
<div>{Translate.editTip}</div>
<div className="main">
<GroupCustomWebPanelEditor
initValue={dataMap['html'] ?? props.panelInfo?.meta?.html ?? ''}
onChange={(html) => (dataMap['html'] = html)}
/>
</div>
</EditModalContent>
);
}}
/>
);
};
GroupCustomWebPanel.displayName = 'GroupCustomWebPanel';
export default GroupCustomWebPanel;

@ -33,15 +33,7 @@ regGroupPanel({
name: `${PLUGIN_NAME}/customwebpanel`,
label: Translate.customwebpanel,
provider: PLUGIN_NAME,
extraFormMeta: [
{
type: 'textarea',
name: 'html',
label: Translate.htmlcode,
placeholder: Translate.customwebpanelPlaceholder,
},
],
render: Loadable(() => import('./group/GroupCustomWebPanelRender'), {
componentName: 'com.msgbyte.webview:GroupCustomWebPanelRender',
componentName: `${PLUGIN_NAME}:GroupCustomWebPanelRender`,
}),
});

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

@ -69,6 +69,7 @@ export {
joinArray,
useConverseMessageContext,
loginWithToken,
useWatch,
} from 'tailchat-shared';
export { navigate } from '@/components/AppRouterApi';

@ -88,7 +88,7 @@ export interface PluginGroupPanel {
/**
* , 使
*/
extraFormMeta: MetaFormFieldMeta[];
extraFormMeta?: MetaFormFieldMeta[];
/**
*

Loading…
Cancel
Save