feat: iframe block (#1225)

This commit is contained in:
chenos 2022-12-09 23:29:54 +08:00 committed by GitHub
parent f4ee872a16
commit 0480b57db5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 143 additions and 1 deletions

View File

@ -34,6 +34,7 @@
"mathjs": "^10.6.0", "mathjs": "^10.6.0",
"react-beautiful-dnd": "^13.1.0", "react-beautiful-dnd": "^13.1.0",
"react-big-calendar": "^0.38.7", "react-big-calendar": "^0.38.7",
"react-iframe": "~1.8.5",
"react-contenteditable": "^3.3.6", "react-contenteditable": "^3.3.6",
"react-drag-listview": "^0.1.9", "react-drag-listview": "^0.1.9",
"react-helmet": "^6.1.0", "react-helmet": "^6.1.0",

View File

@ -510,4 +510,5 @@ export default {
"Selector": "Selector", "Selector": "Selector",
"Inner": "Inner", "Inner": "Inner",
"Search and select collection": "Search and select collection", "Search and select collection": "Search and select collection",
'Please fill in the iframe URL': 'Please fill in the iframe URL',
} }

View File

@ -626,4 +626,5 @@ export default {
"Selector": "选择器", "Selector": "选择器",
"Inner": "里面", "Inner": "里面",
"Search and select collection": "搜索并选择数据表", "Search and select collection": "搜索并选择数据表",
'Please fill in the iframe URL': '请填写嵌入的地址',
} }

View File

@ -0,0 +1,66 @@
import { ISchema, useField, useFieldSchema } from '@formily/react';
import React from 'react';
import { useTranslation } from 'react-i18next';
import { GeneralSchemaDesigner, SchemaSettings } from '../../../schema-settings';
import { useDesignable } from '../../hooks';
export const IframeDesigner = () => {
const field = useField();
const fieldSchema = useFieldSchema();
const { t } = useTranslation();
const { dn } = useDesignable();
return (
<GeneralSchemaDesigner>
<SchemaSettings.ModalItem
title={t('Edit iframe')}
schema={
{
type: 'object',
title: t('Edit chart'),
properties: {
url: {
title: t('URL'),
type: 'string',
default: fieldSchema?.['x-component-props']?.['url'],
'x-decorator': 'FormItem',
'x-component': 'Input',
required: true,
},
height: {
title: t('Height'),
type: 'string',
default: fieldSchema?.['x-component-props']?.height || '60vh',
'x-decorator': 'FormItem',
'x-component': 'Input',
required: true,
},
},
} as ISchema
}
// {{ fetchData(api, { url: 'chartData:get' }) }}
onSubmit={async ({ url, height }) => {
field.componentProps.url = url;
field.componentProps.height = height;
const componentProps = fieldSchema['x-component-props'] || {};
componentProps['url'] = url;
componentProps['height'] = height;
fieldSchema['x-component-props'] = componentProps;
dn.emit('patch', {
schema: {
'x-uid': fieldSchema['x-uid'],
'x-component-props': componentProps,
},
});
}}
/>
<SchemaSettings.Divider />
<SchemaSettings.Remove
removeParentsIfNoChildren
breakRemoveOn={{
'x-component': 'Grid',
}}
/>
</GeneralSchemaDesigner>
);
};

View File

@ -0,0 +1,39 @@
import { observer } from '@formily/react';
import { Card } from 'antd';
import React from 'react';
import { useTranslation } from 'react-i18next';
import RIframe from 'react-iframe';
import type { IIframe } from 'react-iframe/types';
import { IframeDesigner } from './Iframe.Designer';
function isNumeric(str: string) {
if (typeof str !== 'string') return false; // we only process strings!
return (
!isNaN(str as any) && // use type coercion to parse the _entirety_ of the string (`parseFloat` alone does not do this)...
!isNaN(parseFloat(str))
); // ...and ensure strings of whitespace fail
}
export const Iframe: any = observer((props: IIframe) => {
const { url, height, ...others } = props;
const { t } = useTranslation();
if (!url) {
return <Card style={{ marginBottom: 24 }}>{t('Please fill in the iframe URL')}</Card>;
}
return (
<RIframe
url={url}
width="100%"
display="block"
position="relative"
styles={{
height: isNumeric(height) ? `${height}px` : height,
marginBottom: '24px',
border: 0,
}}
{...others}
/>
);
});
Iframe.Designer = IframeDesigner;

View File

@ -1,3 +1,4 @@
export * from './dnd-context'; export * from './dnd-context';
export * from './iframe';
export * from './sortable-item'; export * from './sortable-item';

View File

@ -164,6 +164,12 @@ export const BlockInitializers = {
title: '{{t("Markdown")}}', title: '{{t("Markdown")}}',
component: 'MarkdownBlockInitializer', component: 'MarkdownBlockInitializer',
}, },
{
key: 'iframe',
type: 'item',
title: '{{t("Iframe")}}',
component: 'IframeBlockInitializer',
},
{ {
key: 'auditLogs', key: 'auditLogs',
type: 'item', type: 'item',

View File

@ -0,0 +1,25 @@
import { FormOutlined } from '@ant-design/icons';
import React from 'react';
import { useTranslation } from 'react-i18next';
import { SchemaInitializer } from '../SchemaInitializer';
export const IframeBlockInitializer = (props) => {
const { insert } = props;
const { t } = useTranslation();
return (
<SchemaInitializer.Item
{...props}
icon={<FormOutlined />}
onClick={() => {
insert({
type: 'void',
'x-designer': 'Iframe.Designer',
'x-decorator': 'BlockItem',
'x-component': 'Iframe',
'x-component-props': {},
});
}}
/>
);
};

View File

@ -11,12 +11,13 @@ export * from './CreateSubmitActionInitializer';
export * from './CustomizeActionInitializer'; export * from './CustomizeActionInitializer';
export * from './CustomizeBulkEditActionInitializer'; export * from './CustomizeBulkEditActionInitializer';
export * from './DataBlockInitializer'; export * from './DataBlockInitializer';
export * from './DestroyActionInitializer';
export * from './DeleteEventActionInitializer'; export * from './DeleteEventActionInitializer';
export * from './DestroyActionInitializer';
export * from './DetailsBlockInitializer'; export * from './DetailsBlockInitializer';
export * from './FilterActionInitializer'; export * from './FilterActionInitializer';
export * from './FormBlockInitializer'; export * from './FormBlockInitializer';
export * from './G2PlotInitializer'; export * from './G2PlotInitializer';
export * from './IframeBlockInitializer';
export * from './InitializerWithSwitch'; export * from './InitializerWithSwitch';
export * from './KanbanBlockInitializer'; export * from './KanbanBlockInitializer';
export * from './MarkdownBlockInitializer'; export * from './MarkdownBlockInitializer';
@ -37,3 +38,4 @@ export * from './TableSelectorInitializer';
export * from './UpdateActionInitializer'; export * from './UpdateActionInitializer';
export * from './UpdateSubmitActionInitializer'; export * from './UpdateSubmitActionInitializer';
export * from './ViewActionInitializer'; export * from './ViewActionInitializer';