|
| 1 | +import { |
| 2 | + FileBlockConfig, |
| 3 | + InlineContentSchema, |
| 4 | + PropSchema, |
| 5 | + StyleSchema, |
| 6 | + insertOrUpdateBlock, |
| 7 | +} from '@blocknote/core'; |
| 8 | +import { |
| 9 | + BlockTypeSelectItem, |
| 10 | + ReactCustomBlockRenderProps, |
| 11 | + ResizableFileBlockWrapper, |
| 12 | + createReactBlockSpec, |
| 13 | +} from '@blocknote/react'; |
| 14 | +import { TFunction } from 'i18next'; |
| 15 | +import React, { useState } from 'react'; |
| 16 | +import { css } from 'styled-components'; |
| 17 | + |
| 18 | +import { Box, Icon } from '@/components'; |
| 19 | + |
| 20 | +import { DocsBlockNoteEditor } from '../../types'; |
| 21 | + |
| 22 | +export const iframePropSchema: PropSchema & { |
| 23 | + caption: { |
| 24 | + default: ''; |
| 25 | + }; |
| 26 | + name: { |
| 27 | + default: ''; |
| 28 | + }; |
| 29 | +} = { |
| 30 | + url: { default: '' }, |
| 31 | + caption: { default: '' }, |
| 32 | + name: { default: '' }, |
| 33 | + showPreview: { default: true }, |
| 34 | + previewWidth: { type: 'number', default: undefined }, |
| 35 | +}; |
| 36 | + |
| 37 | +export const iframeBlockConfig = { |
| 38 | + type: 'embed' as const, |
| 39 | + propSchema: iframePropSchema, |
| 40 | + content: 'none', |
| 41 | + isFileBlock: true, |
| 42 | + fileBlockAccept: ['image/png'], |
| 43 | +} satisfies FileBlockConfig; |
| 44 | + |
| 45 | +export const IFrameViewer = ( |
| 46 | + props: ReactCustomBlockRenderProps< |
| 47 | + typeof iframeBlockConfig, |
| 48 | + InlineContentSchema, |
| 49 | + StyleSchema |
| 50 | + >, |
| 51 | +) => { |
| 52 | + const url = props.block.props.url; |
| 53 | + |
| 54 | + const [iframeError, setIframeError] = useState(false); |
| 55 | + if (!url) { |
| 56 | + return <Box>No URL provided for embed.</Box>; |
| 57 | + } |
| 58 | + |
| 59 | + return !iframeError ? ( |
| 60 | + <iframe |
| 61 | + src={url} |
| 62 | + className="bn-visual-media" |
| 63 | + style={{ |
| 64 | + height: '300px', |
| 65 | + }} |
| 66 | + allowFullScreen |
| 67 | + title="Embedded content" |
| 68 | + onError={() => setIframeError(true)} |
| 69 | + /> |
| 70 | + ) : ( |
| 71 | + <Box |
| 72 | + $css={css` |
| 73 | + color: #d32f2f; |
| 74 | + background: #fff3f3; |
| 75 | + border: 1px solid #f8d7da; |
| 76 | + border-radius: 6px; |
| 77 | + padding: 1rem; |
| 78 | + `} |
| 79 | + > |
| 80 | + <Icon iconName="error" $size="16px" /> This site cannot be embedded. It |
| 81 | + may not allow embedding in an iframe. |
| 82 | + </Box> |
| 83 | + ); |
| 84 | +}; |
| 85 | + |
| 86 | +export const IframeToExternalHTML = ( |
| 87 | + props: ReactCustomBlockRenderProps< |
| 88 | + typeof iframeBlockConfig, |
| 89 | + InlineContentSchema, |
| 90 | + StyleSchema |
| 91 | + >, |
| 92 | +) => ( |
| 93 | + <iframe |
| 94 | + src={props.block.props.url} |
| 95 | + className="bn-visual-media" |
| 96 | + style={{ |
| 97 | + position: 'absolute', |
| 98 | + top: 0, |
| 99 | + left: 0, |
| 100 | + width: '100%', |
| 101 | + height: '100%', |
| 102 | + border: 'none', |
| 103 | + }} |
| 104 | + allowFullScreen |
| 105 | + title="Embedded content" |
| 106 | + /> |
| 107 | +); |
| 108 | + |
| 109 | +export const IframeBlock = ( |
| 110 | + props: ReactCustomBlockRenderProps< |
| 111 | + typeof iframeBlockConfig, |
| 112 | + InlineContentSchema, |
| 113 | + StyleSchema |
| 114 | + >, |
| 115 | +) => { |
| 116 | + return ( |
| 117 | + <ResizableFileBlockWrapper |
| 118 | + {...(props as any)} // eslint-disable-line @typescript-eslint/no-explicit-any |
| 119 | + buttonText="Embed" |
| 120 | + buttonIcon={<Icon iconName="language" $size="18px" />} |
| 121 | + > |
| 122 | + <IFrameViewer {...props} /> |
| 123 | + </ResizableFileBlockWrapper> |
| 124 | + ); |
| 125 | +}; |
| 126 | + |
| 127 | +export const ReactEmbedBlock = createReactBlockSpec(iframeBlockConfig, { |
| 128 | + render: IframeBlock, |
| 129 | + parse: () => undefined, |
| 130 | + toExternalHTML: IframeToExternalHTML, |
| 131 | +}); |
| 132 | + |
| 133 | +export const getEmbedReactSlashMenuItems = ( |
| 134 | + editor: DocsBlockNoteEditor, |
| 135 | + t: TFunction<'translation', undefined>, |
| 136 | + group: string, |
| 137 | +) => [ |
| 138 | + { |
| 139 | + title: t('Embed'), |
| 140 | + onItemClick: () => { |
| 141 | + insertOrUpdateBlock(editor, { |
| 142 | + type: 'embed', |
| 143 | + }); |
| 144 | + }, |
| 145 | + aliases: ['embed', 'iframe', 'link'], |
| 146 | + group, |
| 147 | + icon: <Icon iconName="link" $size="18px" />, |
| 148 | + subtext: t('Add an embed block'), |
| 149 | + }, |
| 150 | +]; |
| 151 | + |
| 152 | +export const getEmbedFormattingToolbarItems = ( |
| 153 | + t: TFunction<'translation', undefined>, |
| 154 | +): BlockTypeSelectItem => ({ |
| 155 | + name: t('Embed'), |
| 156 | + type: 'embed', |
| 157 | + icon: () => <Icon iconName="link" $size="16px" />, |
| 158 | + isSelected: (block: any) => block.type === 'embed', |
| 159 | +}); |
0 commit comments