@alpha2-sh/editor

中文文档
0.5.3发布于 2026/11/05MITTypeScriptES Module构建来源已验证近 30 天下载 3102

Headless Project-based editing sessions and browser adapters for alpha2.

原始使用文档
文档目录6 节
  1. Install
  2. Documents and commands
  3. Browser bridge
  4. React
  5. Native export
  6. License

@alpha2-sh/editor

An editing SDK for alpha2: editing sessions over the engine's project document, with typed commands, validation and undo, and a browser bridge that previews, plays and exports the document with the alpha2 engine.

Install

npm install @alpha2-sh/editor alpha2

alpha2 is a peer dependency at the same version as this package. React is an optional peer, needed only for @alpha2-sh/editor/react (React 19.3 or a later 19). The browser requirements are alpha2's.

Documents and commands

The root import is the editing core. It loads no DOM, React, WebAssembly or audio, so it also runs in Node.js.

import { createEditor, createEmptyDocument, serializeDocument, parseDocument } from "@alpha2-sh/editor";

const editor = createEditor({ document: createEmptyDocument({ width: 1280, height: 720 }) });
const result = editor.dispatch({
  type: "track.add",
  track: { id: "video", name: "Video", kind: "video", clips: [] },
});
if (!result.ok) throw result.error; // an EditorError with a `code`
editor.undo();
editor.redo();
const json = serializeDocument(editor.document); // portable JSON; media bytes stay apart
const reopened = parseDocument(json);
editor.dispose();
  • An editor document holds an engine project (document.project), the editor's metadata, and a descriptor of each asset. serializeDocument and parseDocument write and read it as JSON; parseDocument also reads a plain engine project.
  • dispatch applies one Command (the typed union covers the project, tracks, clips, effects, transitions, caption cues, composition graphs, assets and markers); a transaction applies several and is checked once, by the document it ends with. A command the document's rules refuse returns { ok: false, error } and changes nothing. canExecute asks without applying.
  • Every change that commits is one undo step. The history keeps up to 120 steps and 64 MiB by default (createEditor({ historyLimit, historyByteLimit })).
  • beginGesture() previews a drag: each update(command) shows a candidate in editor.draft without touching the document or the history, and commit() adds one undo step (cancel() adds none).
  • subscribe, getSnapshot and subscribeSelector notify views of changes. A refusal is an EditorError, whose code says which check failed.

Browser bridge

@alpha2-sh/editor/browser connects a session to the engine in the browser: import, preview, playback with audio, and export to an MP4, MOV, MKV or WebM file through WebCodecs.

import { createEditor, createEmptyDocument } from "@alpha2-sh/editor";
import { createBrowserBridge } from "@alpha2-sh/editor/browser";

const editor = createEditor({ document: createEmptyDocument({ width: 1280, height: 720 }) });
const bridge = createBrowserBridge({
  editor,
  // URLs of alpha2/wasm/alpha2_wasm_bg.wasm, alpha2/wasm/alpha2_audio_worklet.wasm and
  // alpha2/audio-worklet-processor, as the application's bundler serves them.
  runtimeAssets: { wasmUrl, audioWasmUrl, workletUrl },
});
await bridge.bindPreview(canvas);

const { assetIds } = await bridge.importFiles(files); // from an <input type="file">
const trackId = crypto.randomUUID();
const placed = editor.dispatch({
  type: "transaction",
  commands: [
    { type: "track.add", track: { id: trackId, kind: "video", name: "Video", clips: [] } },
    {
      type: "clip.insert",
      trackId,
      clip: {
        id: crypto.randomUUID(),
        source_id: assetIds[0],
        timeline_range: { start: 0, end: 120000 },
        source_range: { start: 0, end: 120000 },
        time_map: { type: "forward" },
      },
    },
  ],
});
if (!placed.ok) throw placed.error;
await bridge.seek(0);

const artifact = await bridge.startExport({ container: "webm" }).result;
// artifact.kind "file": a temporary file in the origin's private storage; release it once downloaded.
if (artifact.kind === "file") await artifact.release();

await bridge.dispose();
await editor.dispose();
  • Import checks each file with the engine (media, fonts, .cube LUTs, SRT and WebVTT captions through importCaptions) and adds it to the document in one undoable step. importSequence imports numbered PNG or OpenEXR files as one image sequence. A source whose provider reads no file, such as a LaTeX formula ({ format: "latex", latex, font_size }), is kept in the project itself: putProviderSource adds or edits one in one undoable step, and it draws in preview and both exports with no file to relink.
  • bindPreview draws on a canvas. With runtimeAssets.workerFactory, a worker running the @alpha2-sh/editor/preview-worker entry renders the preview off the main thread; without one, or when the worker fails, the main thread does. hashWorkerFactory (@alpha2-sh/editor/hash-worker) and extensionsWorkerFactory (@alpha2-sh/editor/extensions-worker, with extensionsWasmUrl and extensionModuleUrls such as { pdf }) move file hashing and SVG, animated image, Lottie, OpenEXR and PDF reading into workers too.
  • seek, play and pause move the playhead; with audio, playback follows the audio clock. Audio preview needs a cross-origin isolated page; editing and video preview do not.
  • startExport returns a job with result, progress (subscribe) and cancel. container (mp4 by default, mov, mkv or webm) names the file and decides the codecs: an absent videoCodec or audioCodec is the container's default (H.264 and AAC, or VP9 and Opus for WebM, with Opus where a browser cannot encode AAC in MP4 or MKV), and one the container does not offer is refused. The output goes to a temporary file in the origin's private storage by default (release() deletes it), to a file the user picked (output: { kind: "file", handle }), or to memory (output: { kind: "memory", maxBytes }). Whether the browser encodes a codec is the browser's to say: bridge.videoEncoders() asks ahead, and an export with a codec the browser does not encode is refused before it renders.

React

@alpha2-sh/editor/react has EditorProvider, useEditor, useEditorSelector(selector, equal?) and EditorPreview, which binds a bridge's preview to a canvas it makes:

<EditorProvider editor={editor}>
  <EditorPreview bridge={bridge} onError={reportError} />
</EditorProvider>

The application owns the session: unmounting the provider does not dispose it.

Native export

@alpha2-sh/editor/native-client (createNativeClient({ baseUrl })) exports a document on an HTTP export service that the application runs, for example on top of @alpha2-sh/node. It uploads the document's media (bridge.nativeAssets) and follows the job to a downloadable file.

License

MIT, copyright Alpha2 contributors.

版本与发布记录

@alpha2-sh/editor 共发布 19 个版本,首次发布于 2026/10/20;过去 12 个月发布了 19 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.5.3latest2026/11/052 天前
0.5.22026/11/043 天前
0.5.12026/11/034 天前
0.5.02026/11/016 天前
0.4.62026/10/309 天前
0.4.52026/10/2810 天前

关于 @alpha2-sh/editor 的常见问题

@alpha2-sh/editor 是什么?

Headless Project-based editing sessions and browser adapters for alpha2.

如何安装 @alpha2-sh/editor?

在项目目录运行 npm install @alpha2-sh/editor,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:alpha2(0.5.3)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@alpha2-sh/editor 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@alpha2-sh/editor 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@alpha2-sh/editor 可以商用吗?

@alpha2-sh/editor 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@alpha2-sh/editor 的下载量有多少?

根据 npm 官方下载统计,@alpha2-sh/editor 在 2026/10/06 – 2026/11/04 这 30 天内被下载 3,102 次,日均约 103 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@alpha2-sh/editor 还在维护吗?

最新版本 0.5.3 发布于 2026/11/05(2 天前);至今共发布 19 个版本,其中过去 12 个月发布了 19 个;首次发布于 2026/10/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。