Skip to content
Edytor
Esc
↑↓navigate↵open⌘Jpreview
On this page

Audio

The audio plugin adds Notion's audio block, an audio file in the browser's player, linked or uploaded through your upload function.

createAudioPlugin() adds Notion’s audio block: an “Embed audio” panel (your empty snippet, createMediaEmptyPlugin({ empty })) until the block has a source, then the browser’s player (<audio controls>), with an editable caption. It is not a default plugin: list it.

<script lang="ts">
	import { Edytor, createAudioPlugin } from 'edytor';
</script>

<Edytor plugins={[createAudioPlugin()]} />

A slash menu then lists “Audio” under Media. Without upload (createAudioPlugin({ upload })), audio files are linked only.

Try it

Click the empty audio block (“Embed audio”) to link a sound or pick one from your computer (it plays from this tab only). The editor is local to this page: a reload resets it.

Customize

The empty slot is your empty snippet (createMediaEmptyPlugin({ empty })), so it can look like anything. Here, a dashed drop zone with a link field and an Upload button.

<script lang="ts">
	import {
		Edytor,
		createAudioPlugin,
		createMediaEmptyPlugin,
		type JSONDoc,
		type MediaEmptyController,
		type Plugin
	} from 'edytor';

	// The page's document and the rest of your editor's plugins.
	let { value, plugins = [] }: { value?: JSONDoc; plugins?: Plugin[] } = $props();

	// The empty slot of every media block, drawn by the snippet below.
	const empty = createMediaEmptyPlugin({ empty: slot });
	// Uploads stay in this tab: the file's object URL.
	const audio = createAudioPlugin({
		upload: async (file, report) => {
			report.progress(1);
			return URL.createObjectURL(file);
		}
	});
</script>

<!-- A drop zone: one big target, the link field below it. -->
{#snippet slot(slot: MediaEmptyController)}
	{#if slot.pending?.status === 'uploading'}
		<div class="zone" role="status">
			<progress value={slot.pending.progress ?? undefined} max="1"></progress>
			Uploading…
		</div>
	{:else if slot.readonly}
		<div class="zone">{slot.icon} Nothing here yet</div>
	{:else}
		<div class="zone">
			<span class="icon" aria-hidden="true">{slot.icon}</span>
			<strong>Add a sound</strong>
			<div class="row">
				<input
					placeholder="https://…/clip.mp3"
					aria-label="Audio link"
					bind:value={slot.draft}
					{@attach slot.field}
				/>
				<button type="button" onclick={() => slot.embed()}>Link</button>
				{#if slot.canUpload}
					<label class="pick">
						Upload
						<input
							type="file"
							accept={slot.accept}
							hidden
							onchange={(event) => {
								const picked = event.currentTarget.files?.[0];
								if (picked) void slot.upload(picked);
							}}
						/>
					</label>
				{/if}
			</div>
			{#if slot.failed}
				<small>That did not work. Try another file or link.</small>
			{/if}
		</div>
	{/if}
{/snippet}

<Edytor {value} plugins={[audio, empty, ...plugins]} aria-label="Audio demo" />

<style>
	.zone {
		display: flex;
		flex-direction: column;
		align-items: center;
		gap: 8px;
		padding: 20px 16px;
		border: 2px dashed #c9c5f0;
		border-radius: 14px;
		background: #f6f5ff;
		color: #4b44a8;
		font-size: 14px;
	}
	.icon {
		font-size: 28px;
	}
	.row {
		display: flex;
		flex-wrap: wrap;
		justify-content: center;
		gap: 6px;
		width: 100%;
	}
	input:not([type='file']) {
		flex: 1;
		min-width: 180px;
		max-width: 320px;
		height: 30px;
		padding: 0 10px;
		border: 1px solid #c9c5f0;
		border-radius: 999px;
		background: #fff;
		font: inherit;
		outline: none;
	}
	button,
	.pick {
		height: 30px;
		padding: 0 14px;
		border: 0;
		border-radius: 999px;
		background: #5b52d6;
		color: #fff;
		font: inherit;
		line-height: 30px;
		cursor: pointer;
	}
	.pick {
		background: #fff;
		color: #5b52d6;
		box-shadow: inset 0 0 0 1px #5b52d6;
	}
	small {
		color: #c0392b;
	}
</style>

Options

createAudioPlugin(options) takes an AudioPluginOptions: upload, an Uploader that stores a file and answers its URL. The empty block is drawn by the view’s media empty plugin (Menus and handles). With upload, the empty block has an Upload (accept="audio/*") and pasted and dropped audio files become audio blocks (Pasted and dropped files); the answer is stored only when the block is still there, of its kind and without a source: a source a collaborator set while the upload ran stays. While it uploads, the block shows the file’s name and size with the progress.

The kind

Kind Element Data Preset
audio figure { src: string } Audio (🎵, group Media, keywords sound, music, mp3)

A source is accepted only when it is an http:/https: URL with a host or a blob: URL of one (safeMediaSrc), never data: or a script URL; any other stored value shows no player. The player has controls and preload="metadata".

The block is void like the image, and its only text is the caption. The command id is block.audio. Its role is in defaultSemantics. In a readonly view the empty block is a passive placeholder.

Clipboard

  • Copying exports <figure><audio src="…" controls></audio><figcaption>caption</figcaption></figure>.
  • Pasting HTML imports an audio with a safe src, or a source child with one (bare, or in a figure whose figcaption becomes the caption). Without the plugin, a pasted audio is dropped.

Styling

Selector Matches
[data-edytor-type='audio'] The figure
[data-edytor-audio] The wrapper of the audio

The empty state uses the shared media selectors (see Embed).

Was this page helpful?