fix: bugs

This commit is contained in:
Kilu 2024-06-29 20:02:56 +08:00
parent e5fd455e93
commit a5a8e8f125
5 changed files with 84 additions and 0 deletions

View File

@ -32,6 +32,7 @@ export enum BlockType {
OutlineBlock = 'outline',
TableBlock = 'table',
TableCell = 'table/cell',
LinkPreview = 'link_preview',
}
export enum InlineBlockType {
@ -79,6 +80,10 @@ export interface MathEquationBlockData extends BlockData {
formula?: string;
}
export interface LinkPreviewBlockData extends BlockData {
url?: string;
}
export enum ImageType {
Local = 0,
Internal = 1,

View File

@ -0,0 +1,68 @@
import { EditorElementProps, LinkPreviewNode } from '@/components/editor/editor.type';
import axios from 'axios';
import React, { forwardRef, memo, useEffect, useState } from 'react';
export const LinkPreview = memo(
forwardRef<HTMLDivElement, EditorElementProps<LinkPreviewNode>>(({ node, children, ...attributes }, ref) => {
const [data, setData] = useState<{
image: { url: string };
title: string;
description: string;
} | null>(null);
const url = node.data.url;
useEffect(() => {
if (!url) return;
setData(null);
void (async () => {
try {
const response = await axios.get(`https://api.microlink.io/?url=${url}`);
if (response.data.statusCode !== 200) return;
const data = response.data.data;
setData(data);
} catch (error) {
// don't do anything
}
})();
}, [url]);
return (
<div
onClick={() => {
window.open(url, '_blank');
}}
{...attributes}
ref={ref}
className={`link-preview-block relative w-full cursor-pointer`}
>
<div>
{data ? (
<div
className={
'container-bg flex w-full cursor-pointer select-none items-center gap-4 rounded border border-line-divider bg-fill-list-active p-3'
}
>
<img
src={data.image.url}
alt={data.title}
className={'container h-full w-40 rounded bg-cover bg-center'}
/>
<div className={'flex flex-col justify-center gap-2'}>
<div className={'text-base font-bold text-text-title'}>{data.title}</div>
<div className={'text-sm text-text-caption'}>{data.description}</div>
</div>
</div>
) : (
node.data.url
)}
</div>
<div ref={ref} className={'absolute left-0 top-0 h-full w-full caret-transparent'}>
{children}
</div>
</div>
);
})
);
export default LinkPreview;

View File

@ -0,0 +1 @@
export * from './LinkPreview';

View File

@ -6,6 +6,7 @@ import { DatabaseBlock } from '@/components/editor/components/blocks/database';
import { DividerNode } from '@/components/editor/components/blocks/divider';
import { Heading } from '@/components/editor/components/blocks/heading';
import { ImageBlock } from '@/components/editor/components/blocks/image';
import { LinkPreview } from '@/components/editor/components/blocks/link-preview';
import { MathEquation } from '@/components/editor/components/blocks/math-equation';
import { NumberedList } from '@/components/editor/components/blocks/numbered-list';
import { Outline } from '@/components/editor/components/blocks/outline';
@ -74,6 +75,8 @@ export const Element = memo(
case BlockType.BoardBlock:
case BlockType.CalendarBlock:
return DatabaseBlock;
case BlockType.LinkPreview:
return LinkPreview;
default:
return UnSupportedBlock;
}

View File

@ -17,6 +17,7 @@ import {
BlockId,
BlockData,
DatabaseNodeData,
LinkPreviewBlockData,
} from '@/application/collab.type';
import { HTMLAttributes } from 'react';
import { Element } from 'slate';
@ -91,6 +92,12 @@ export interface CalloutNode extends BlockNode {
data: CalloutBlockData;
}
export interface LinkPreviewNode extends BlockNode {
type: BlockType.LinkPreview;
blockId: string;
data: LinkPreviewBlockData;
}
export interface MathEquationNode extends BlockNode {
type: BlockType.EquationBlock;
blockId: string;