import React, { useContext } from 'react';
import { Col, message, Spin, Typography, Button, Tooltip } from 'antd';
import { ButtonProps } from 'antd/es/button';
import { ExportOutlined } from '@ant-design/icons';
import { IUiApi } from 'umi-types';
import styles from './index.module.less';
import HighlightedText from './HighlightedText';
import getInsertPosition, { PositionData } from './getInsertPosition';
import Context from '../UIApiContext';
import { Block, AddBlockParams, Resource } from '../../../data.d';
import ImageLoad from './ImageLoad';
import ImagePreview from './ImagePreview';
/**
* 子区块 的 tag
* @param
*/
export const Meats: React.FC<{
item: Block;
keyword?: string;
}> = ({ item, keyword }) => (
{item.tags &&
item.tags.map((tag: string) => (
))}
);
export interface BlockItemProps {
type: Resource['blockType'];
addingBlock?: Block;
item: Block;
disabled?: boolean;
loading?: boolean;
onShowModal?: (Block: Block, option: AddBlockParams) => void;
onHideModal?: () => void;
keyword?: string;
selectedTag?: string;
}
/**
* 将绝对路径转化为相对路径
* @param api
* @param filenamePath
*/
export const getPathFromFilename = async (api: IUiApi, filenamePath: string): Promise => {
const { data } = (await api.callRemote({
type: 'org.umi.block.getRelativePagesPath',
payload: {
path: filenamePath,
},
})) as {
data: string;
};
// /Users/userName/code/test/umi-block-test/src/page(s)/xxx/index.ts
// or /Users/userName/code/test/umi-pro/src/page(s)/xxx.js
// -> /xxx
return data.replace(/(index)?((\.js?)|(\.tsx?)|(\.jsx?))$/, '');
};
/**
* 打开 modal 框之前的一些处理
* @param api umi ui 的 api 全局对象
* @param param 参数
*/
const onBeforeOpenModal = async (api, { item, type, onShowModal }) => {
try {
await api.callRemote({
type: 'org.umi.block.checkIfCanAdd',
payload: {
item,
type,
},
});
} catch (e) {
message.error(e.message);
return;
}
if (api.isMini() && type === 'block') {
// umi ui 中区块有自己独有的打开方式
const position = (await getInsertPosition(api).catch(e => {
message.error(e.message);
})) as PositionData;
const targetPath = await getPathFromFilename(api, position.filename);
const option = {
path: targetPath,
index: position.index,
blockTarget: targetPath,
};
onShowModal(item, option);
return;
}
onShowModal(item, {});
};
interface ToolTipAddButtonProps extends ButtonProps {
disabledTitle?: string;
}
const ToolTipAddButton: React.FC = ({
disabledTitle,
disabled,
children,
...reset
}) => {
if (disabled) {
return (
);
}
return (
);
};
const BlockItem: React.FC = ({
type,
item,
loading = false,
disabled,
onShowModal,
keyword,
selectedTag,
}) => {
const { api } = useContext(Context);
const { intl } = api;
const isMini = api.isMini();
return (
{
if (loading) {
onShowModal(item, {});
}
}}
>
onBeforeOpenModal(api, {
type,
item,
onShowModal,
})
}
>
{loading
? intl({ id: 'org.umi.ui.blocks.list.viewlog' })
: intl({ id: 'org.umi.ui.blocks.list.add' })}
{item.description && !isMini && (
)}
{!isMini && selectedTag === '' &&
}
);
};
export default BlockItem;