Files
AFFiNE-Mirror/libs/components/editor-blocks/src/blocks/embed-link/EmbedLinkView.tsx
T
2022-08-26 17:26:36 +08:00

58 lines
1.9 KiB
TypeScript

import {
BlockPendantProvider,
useOnSelect,
} from '@toeverything/components/editor-core';
import { CreateView } from '@toeverything/framework/virgo';
import { useState } from 'react';
import { SourceView } from '../../components/source-view';
import { LinkContainer } from '../../components/style-container';
import { Upload } from '../../components/upload/upload';
const MESSAGES = {
ADD_EMBED_LINK: 'Add embed link',
};
type EmbedLinkView = CreateView;
export const EmbedLinkView = (props: EmbedLinkView) => {
const { block, editor } = props;
const [isSelect, setIsSelect] = useState(false);
const [embedLinkUrl, setEmbedLinkUrl] = useState<string>(
block.getProperty('embedLink')?.value
);
useOnSelect(block.id, (isSelect: boolean) => {
setIsSelect(isSelect);
});
const onEmbedLinkUrlChange = async (link: string) => {
const DEMO_URL = 'https://affine.pro/';
const value = link ? link : DEMO_URL;
setEmbedLinkUrl(value);
block.setProperty('embedLink', { value: value, name: 'embedLink' });
};
return (
<BlockPendantProvider editor={editor} block={block}>
<LinkContainer>
{embedLinkUrl ? (
<SourceView
block={block}
isSelected={isSelect}
viewType="embedLink"
link={embedLinkUrl}
/>
) : (
<Upload
firstCreate={block.firstCreateFlag}
uploadType={'embedLink'}
savaLink={onEmbedLinkUrlChange}
defaultAddBtnText={MESSAGES.ADD_EMBED_LINK}
isSelected={isSelect}
/>
)}
</LinkContainer>
</BlockPendantProvider>
);
};