From 72275137835d303381aaa7c300d9526da676de70 Mon Sep 17 00:00:00 2001 From: ammarahm-ed Date: Thu, 18 May 2023 21:14:26 +0500 Subject: [PATCH] editor: enable attachment previews for files --- packages/editor/src/toolbar/tools/attachment.tsx | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/editor/src/toolbar/tools/attachment.tsx b/packages/editor/src/toolbar/tools/attachment.tsx index cb1599df3..c09de5154 100644 --- a/packages/editor/src/toolbar/tools/attachment.tsx +++ b/packages/editor/src/toolbar/tools/attachment.tsx @@ -61,10 +61,23 @@ export function DownloadAttachment(props: ToolProps) { /> ); } +const previewableFileExtensions = ["pdf"]; +function canPreviewAttachment(attachment: Attachment) { + if (!attachment) return false; + const extension = attachment.filename?.split(".").pop(); + if (!extension) return false; + + return previewableFileExtensions.indexOf(extension) > -1; +} export function PreviewAttachment(props: ToolProps) { const { editor } = props; - if (!editor.isActive("image")) return null; + const attachmentNode = + findSelectedNode(editor, "attachment") || findSelectedNode(editor, "image"); + const attachment = (attachmentNode?.attrs || {}) as Attachment; + + if (!editor.isActive("image") && !canPreviewAttachment(attachment)) + return null; return (