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

Video

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

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

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

	// Optional: an Upload button, and pasted or dropped video files uploaded.
	const video = createVideoPlugin({
		upload: async (file) => {
			const body = new FormData();
			body.append('file', file);
			const response = await fetch('/api/uploads', { method: 'POST', body });
			return (await response.json()).url;
		}
	});
</script>

<Edytor plugins={[video]} />

A slash menu then lists “Video” under Media. Without upload, files are linked only. A YouTube or Vimeo link is not a video file: it plays in an embed.

Try it

Click “Embed a video” to link a video file or upload one: the upload is kept in this tab as an object URL. The editor is local to this page: a reload resets it.

Customize

createMediaEmptyPlugin({ empty }) draws the empty slot, here as a film slate with a drop zone, a link field and a file picker; the player is the default one.

<script lang="ts">
	import {
		Edytor,
		createMediaEmptyPlugin,
		createVideoPlugin,
		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();

	// Every empty media block drawn with the snippet below.
	const slot = createMediaEmptyPlugin({ empty });
	// Uploads stay in this tab: the file's object URL.
	const video = createVideoPlugin({
		upload: async (file, report) => {
			report.progress(1);
			return URL.createObjectURL(file);
		}
	});

	let over = $state(false);
</script>

<!-- A film slate: a drop zone with a link field under it. -->
{#snippet empty(panel: MediaEmptyController)}
	{#if panel.pending?.status === 'uploading'}
		<div class="slate" role="status">
			<span class="title">Loading the reel…</span>
			<progress value={panel.pending.progress ?? undefined} max="1"></progress>
		</div>
	{:else if panel.readonly}
		<div class="slate"><span class="title">{panel.icon} No {panel.kind} yet</span></div>
	{:else}
		<!-- svelte-ignore a11y_no_static_element_interactions -->
		<div
			class="slate"
			class:over
			ondragenter={() => (over = true)}
			ondragleave={() => (over = false)}
			ondrop={() => (over = false)}
		>
			<span class="clapper" aria-hidden="true">{panel.icon}</span>
			<span class="title">Roll a {panel.kind}</span>
			<form
				onsubmit={(event) => {
					event.preventDefault();
					panel.embed();
				}}
			>
				<input
					aria-label="Link to a {panel.kind} file"
					placeholder="https://…/clip.mp4"
					bind:value={panel.draft}
					{@attach panel.field}
				/>
				<button type="submit">Action</button>
			</form>
			{#if panel.canUpload}
				<label class="pick">
					or choose a file
					<input
						type="file"
						accept={panel.accept}
						hidden
						onchange={(event) => {
							const picked = event.currentTarget.files?.[0];
							if (picked) void panel.upload(picked);
						}}
					/>
				</label>
			{/if}
			{#if panel.failed}
				<small role="alert">Cut: that is not a {panel.kind} file.</small>
			{/if}
		</div>
	{/if}
{/snippet}

<Edytor {value} plugins={[video, slot, ...plugins]} aria-label="Video demo" />

<style>
	.slate {
		display: flex;
		flex-direction: column;
		align-items: center;
		gap: 8px;
		padding: 18px 16px;
		border-radius: 10px;
		background: repeating-linear-gradient(
			135deg,
			#1f2328 0 14px,
			#2a2f36 14px 28px
		);
		color: #e6edf3;
		font: 13px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
		outline: 2px dashed transparent;
		outline-offset: -8px;
	}
	.slate.over {
		outline-color: #febc2e;
	}
	.clapper {
		font-size: 28px;
	}
	.title {
		font-weight: 600;
		letter-spacing: 0.08em;
		text-transform: uppercase;
	}
	form {
		display: flex;
		flex-wrap: wrap;
		justify-content: center;
		gap: 6px;
		width: 100%;
	}
	input:not([type='file']) {
		box-sizing: border-box;
		flex: 1;
		min-width: 180px;
		max-width: 320px;
		padding: 6px 10px;
		border: 1px solid #3d444d;
		border-radius: 8px;
		background: #151b23;
		color: inherit;
		font: inherit;
		outline: none;
	}
	button,
	.pick {
		padding: 6px 12px;
		border: 0;
		border-radius: 8px;
		background: #febc2e;
		color: #1f2328;
		font: inherit;
		font-weight: 600;
		cursor: pointer;
	}
	.pick {
		background: transparent;
		color: #febc2e;
		text-decoration: underline;
	}
	small {
		color: #ff7b72;
	}
</style>

Options

createVideoPlugin(options) takes a VideoPluginOptions: 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, it has an Upload (accept="video/*") and pasted and dropped video files become video 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
video figure { src: string } Video (🎬, group Media, keywords movie, mp4, clip)

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, preload="metadata" and playsinline.

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

Clipboard

  • Copying exports <figure><video src="…" controls></video><figcaption>caption</figcaption></figure>.
  • Pasting HTML imports a video 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 video is dropped.

Styling

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

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

Was this page helpful?