mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-17 01:56:27 +08:00
init: the first public commit for AFFiNE
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
import { FC, useState } from 'react';
|
||||
import { CreateView } from '@toeverything/framework/virgo';
|
||||
import {
|
||||
WrapperWithPendantAndDragDrop,
|
||||
useOnSelect,
|
||||
} from '@toeverything/components/editor-core';
|
||||
import { Upload } from '../../components/upload/upload';
|
||||
import { SourceView } from '../../components/source-view';
|
||||
import { LinkContainer } from '../../components/style-container';
|
||||
|
||||
const MESSAGES = {
|
||||
ADD_EMBED_LINK: 'Add embed link',
|
||||
};
|
||||
|
||||
type EmbedLinkView = CreateView;
|
||||
export const EmbedLinkView: FC<EmbedLinkView> = props => {
|
||||
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 (
|
||||
<WrapperWithPendantAndDragDrop editor={editor} block={block}>
|
||||
<LinkContainer>
|
||||
{embedLinkUrl ? (
|
||||
<SourceView
|
||||
block={block}
|
||||
editorElement={props.editorElement}
|
||||
isSelected={isSelect}
|
||||
viewType="embedLink"
|
||||
link={embedLinkUrl}
|
||||
/>
|
||||
) : (
|
||||
<Upload
|
||||
firstCreate={block.firstCreateFlag}
|
||||
uploadType={'embedLink'}
|
||||
savaLink={onEmbedLinkUrlChange}
|
||||
defaultAddBtnText={MESSAGES.ADD_EMBED_LINK}
|
||||
isSelected={isSelect}
|
||||
/>
|
||||
)}
|
||||
</LinkContainer>
|
||||
</WrapperWithPendantAndDragDrop>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,47 @@
|
||||
import {
|
||||
AsyncBlock,
|
||||
BaseView,
|
||||
SelectBlock,
|
||||
} from '@toeverything/framework/virgo';
|
||||
import { Protocol } from '@toeverything/datasource/db-service';
|
||||
import { EmbedLinkView } from './EmbedLinkView';
|
||||
|
||||
export class EmbedLinkBlock extends BaseView {
|
||||
public override selectable = true;
|
||||
public override activatable = false;
|
||||
type = Protocol.Block.Type.embedLink;
|
||||
View = EmbedLinkView;
|
||||
|
||||
override html2block(
|
||||
el: Element,
|
||||
parseEl: (el: Element) => any[]
|
||||
): any[] | null {
|
||||
const tag_name = el.tagName;
|
||||
if (tag_name === 'A' && el.parentElement?.childElementCount === 1) {
|
||||
return [
|
||||
{
|
||||
type: this.type,
|
||||
properties: {
|
||||
// TODO: Not sure what value to fill for name
|
||||
embedLink: {
|
||||
name: this.type,
|
||||
value: el.getAttribute('href'),
|
||||
},
|
||||
},
|
||||
children: [],
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
override async block2html(
|
||||
block: AsyncBlock,
|
||||
children: SelectBlock[],
|
||||
generateHtml: (el: any[]) => Promise<string>
|
||||
): Promise<string> {
|
||||
const figma_url = block.getProperty('embedLink')?.value;
|
||||
return `<p><a src=${figma_url}>${figma_url}</p>`;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user