Blocknote custom slash menu (#4517)

blocknote v12, cleaned up blockschema & specs, added custom slash menu
This commit is contained in:
brendanlaschke
2024-03-20 08:38:05 +01:00
committed by GitHub
parent 35d41e38c8
commit 017b09ba35
11 changed files with 308 additions and 183 deletions

View File

@ -1,11 +1,4 @@
import { ChangeEvent, useRef } from 'react';
import {
BlockFromConfig,
BlockNoteEditor,
InlineContentSchema,
PropSchema,
StyleSchema,
} from '@blocknote/core';
import { createReactBlockSpec } from '@blocknote/react';
import styled from '@emotion/styled';
import { isNonEmptyString } from '@sniptt/guards';
@ -19,31 +12,10 @@ import { AttachmentIcon } from '../files/components/AttachmentIcon';
import { AttachmentType } from '../files/types/Attachment';
import { getFileType } from '../files/utils/getFileType';
import { blockSpecs } from './blockSpecs';
export const filePropSchema = {
// File url
url: {
default: '' as string,
},
name: {
default: '' as string,
},
fileType: {
default: 'Other' as AttachmentType,
},
} satisfies PropSchema;
const StyledFileInput = styled.input`
display: none;
`;
const FileBlockConfig = {
type: 'file' as const,
propSchema: filePropSchema,
content: 'none' as const,
};
const StyledFileLine = styled.div`
align-items: center;
display: flex;
@ -66,75 +38,82 @@ const StyledUploadFileContainer = styled.div`
gap: ${({ theme }) => theme.spacing(2)};
`;
const FileBlockRenderer = ({
block,
editor,
}: {
block: BlockFromConfig<
typeof FileBlockConfig,
InlineContentSchema,
StyleSchema
>;
editor: BlockNoteEditor<typeof blockSpecs, InlineContentSchema, StyleSchema>;
}) => {
const inputFileRef = useRef<HTMLInputElement>(null);
const handleUploadAttachment = async (file: File) => {
if (isUndefinedOrNull(file)) {
return '';
}
const fileUrl = await editor.uploadFile?.(file);
editor.updateBlock(block.id, {
props: {
...block.props,
...{ url: fileUrl, fileType: getFileType(file.name), name: file.name },
export const FileBlock = createReactBlockSpec(
{
type: 'file',
propSchema: {
url: {
default: '' as string,
},
});
};
const handleUploadFileClick = () => {
inputFileRef?.current?.click?.();
};
const handleFileChange = (e: ChangeEvent<HTMLInputElement>) => {
if (isDefined(e.target.files)) handleUploadAttachment?.(e.target.files[0]);
};
if (isNonEmptyString(block.props.url)) {
return (
<AppThemeProvider>
<StyledFileLine>
<AttachmentIcon
attachmentType={block.props.fileType as AttachmentType}
></AttachmentIcon>
<StyledLink href={block.props.url} target="__blank">
{block.props.name}
</StyledLink>
</StyledFileLine>
</AppThemeProvider>
);
}
return (
<AppThemeProvider>
<StyledUploadFileContainer>
<StyledFileInput
ref={inputFileRef}
onChange={handleFileChange}
type="file"
/>
<Button onClick={handleUploadFileClick} title="Upload File"></Button>
</StyledUploadFileContainer>
</AppThemeProvider>
);
};
export const FileBlock = createReactBlockSpec(FileBlockConfig, {
render: (block) => {
return (
<FileBlockRenderer
block={block.block}
editor={block.editor}
></FileBlockRenderer>
);
name: {
default: '' as string,
},
fileType: {
default: 'Other' as AttachmentType,
},
},
content: 'none',
},
});
{
render: ({ block, editor }) => {
// eslint-disable-next-line react-hooks/rules-of-hooks
const inputFileRef = useRef<HTMLInputElement>(null);
const handleUploadAttachment = async (file: File) => {
if (isUndefinedOrNull(file)) {
return '';
}
const fileUrl = await editor.uploadFile?.(file);
editor.updateBlock(block.id, {
props: {
...block.props,
...{
url: fileUrl,
fileType: getFileType(file.name),
name: file.name,
},
},
});
};
const handleUploadFileClick = () => {
inputFileRef?.current?.click?.();
};
const handleFileChange = (e: ChangeEvent<HTMLInputElement>) => {
if (isDefined(e.target.files))
handleUploadAttachment?.(e.target.files[0]);
};
if (isNonEmptyString(block.props.url)) {
return (
<AppThemeProvider>
<StyledFileLine>
<AttachmentIcon
attachmentType={block.props.fileType as AttachmentType}
></AttachmentIcon>
<StyledLink href={block.props.url} target="__blank">
{block.props.name}
</StyledLink>
</StyledFileLine>
</AppThemeProvider>
);
}
return (
<AppThemeProvider>
<StyledUploadFileContainer>
<StyledFileInput
ref={inputFileRef}
onChange={handleFileChange}
type="file"
/>
<Button
onClick={handleUploadFileClick}
title="Upload File"
></Button>
</StyledUploadFileContainer>
</AppThemeProvider>
);
},
},
);