From ab900ae5da7de3199e3af401ba5e80fe95440147 Mon Sep 17 00:00:00 2001 From: Abdullah Atta Date: Tue, 24 Feb 2026 09:36:47 +0500 Subject: [PATCH] editor: various fixes and improvements related to audio widget --- packages/editor/src/extensions/audio/audio.ts | 4 +- .../editor/src/extensions/audio/component.tsx | 73 ++++++++++++------- .../editor/src/toolbar/tools/attachment.tsx | 16 ++-- packages/editor/styles/styles.css | 5 +- 4 files changed, 63 insertions(+), 35 deletions(-) diff --git a/packages/editor/src/extensions/audio/audio.ts b/packages/editor/src/extensions/audio/audio.ts index 3dd952bf0..137add4b4 100644 --- a/packages/editor/src/extensions/audio/audio.ts +++ b/packages/editor/src/extensions/audio/audio.ts @@ -39,8 +39,6 @@ declare module "@tiptap/core" { export const AudioNode = Node.create({ name: "audio", - content: "", - marks: "", draggable: true, priority: 51, @@ -71,7 +69,7 @@ export const AudioNode = Node.create({ parseHTML() { return [ { - tag: "audio[data-hash][data-mime^='audio/']" + tag: "audio" } ]; }, diff --git a/packages/editor/src/extensions/audio/component.tsx b/packages/editor/src/extensions/audio/component.tsx index f0f90765e..88f552c60 100644 --- a/packages/editor/src/extensions/audio/component.tsx +++ b/packages/editor/src/extensions/audio/component.tsx @@ -28,39 +28,27 @@ import { DesktopOnly } from "../../components/responsive/index.js"; import { toBlobURL, revokeBloburl } from "../../utils/downloader.js"; import { formatBytes } from "@notesnook/common"; +const SAMPLE_AUDIO = toBlobURL( + "data:audio/wav;base64,UklGRiQAAABXQVZFZm10IBAAAAABAAEAQB8AAIA+AAACABAAZGF0YQAAAAA=", + "other", + "audio/wav", + "sample-audio" +); + export function AudioComponent(props: ReactNodeViewProps) { const { editor, node, selected } = props; const { filename, size, progress, mime, hash } = node.attrs; const elementRef = useRef(); const [isDragging, setIsDragging] = useState(false); - const [audioSrc, setAudioSrc] = useState(); - - useEffect(() => { - if (editor.storage?.getAttachmentData && hash) { - editor.storage - .getAttachmentData({ - type: "file", - hash - }) - .then((data: string | undefined) => { - if (data) { - const url = toBlobURL(data, "other", mime, hash); - if (url) { - setAudioSrc(url); - } - } - }) - .catch(console.error); - } - }, [editor.storage, hash, mime]); + const [error, setError] = useState(); useEffect(() => { return () => { - if (audioSrc && hash) { + if (hash) { revokeBloburl(hash); } }; - }, [audioSrc, hash]); + }, [hash]); return ( ) { onDragStart={() => setIsDragging(true)} onDragEnd={() => setIsDragging(false)} data-drag-handle + data-drag-image > @@ -120,7 +109,9 @@ export function AudioComponent(props: ReactNodeViewProps) { {progress ? `${progress}%` : formatBytes(size, 1)} - {audioSrc && ( + {error ? ( + {error} + ) : ( ) { } }} > - )} diff --git a/packages/editor/src/toolbar/tools/attachment.tsx b/packages/editor/src/toolbar/tools/attachment.tsx index 23fb3bcd0..91813a29d 100644 --- a/packages/editor/src/toolbar/tools/attachment.tsx +++ b/packages/editor/src/toolbar/tools/attachment.tsx @@ -23,6 +23,7 @@ import { MoreTools } from "../components/more-tools.js"; import { useToolbarLocation } from "../stores/toolbar-store.js"; import { findSelectedNode } from "../../utils/prosemirror.js"; import { Attachment } from "../../extensions/attachment/index.js"; +import { Editor } from "../../types.js"; export function AttachmentSettings(props: ToolProps) { const { editor } = props; @@ -52,9 +53,7 @@ export function DownloadAttachment(props: ToolProps) { title={props.title} toggled={false} onClick={() => { - const attachmentNode = - findSelectedNode(editor, "attachment") || - findSelectedNode(editor, "image"); + const attachmentNode = findAttachmentNode(editor); const attachment = (attachmentNode?.attrs || {}) as Attachment; editor.storage.downloadAttachment?.(attachment); @@ -65,8 +64,7 @@ export function DownloadAttachment(props: ToolProps) { export function PreviewAttachment(props: ToolProps) { const { editor } = props; - const attachmentNode = - findSelectedNode(editor, "attachment") || findSelectedNode(editor, "image"); + const attachmentNode = findAttachmentNode(editor); const attachment = (attachmentNode?.attrs || {}) as Attachment; if (!editor.isActive("image") && !canPreviewAttachment(attachment)) @@ -117,3 +115,11 @@ function canPreviewAttachment(attachment: Attachment) { return previewableFileExtensions.indexOf(extension) > -1; } + +function findAttachmentNode(editor: Editor) { + return ( + findSelectedNode(editor, "attachment") || + findSelectedNode(editor, "image") || + findSelectedNode(editor, "audio") + ); +} diff --git a/packages/editor/styles/styles.css b/packages/editor/styles/styles.css index 03f8eff50..dc2971556 100644 --- a/packages/editor/styles/styles.css +++ b/packages/editor/styles/styles.css @@ -131,8 +131,9 @@ } .ProseMirror > div.codeblock-view-content-wrap, -.ProseMirror > div.taskList-view-content-wrap, -.ProseMirror > div.math-block.math-node { +.ProseMirror > div.math-block.math-node, +.ProseMirror > div.audio-view-content-wrap, +.ProseMirror > div.taskList-view-content-wrap { margin-top: 1em; margin-bottom: 1em; }