メインコンテンツへ移動

Worker ファイルツリー

この例の構成

見出しへのリンク

このページはモックではなく、Worker ベースの入れ子ファイルツリーをそのまま見せています。docs ページが host DOM と pointer hit-testing を担当し、remote Vue app はツリー構造、inline 編集状態、drag ロジックを専用 Web Worker の中で保持します。

この例は次の 3 つの要素で構成されています。

  • preview 領域に HostedTree をマウントする host runtime
  • フォルダー、ファイル、inline アクションを描画し、RemoteSortableEvent を処理する remote Worker app
  • 編集や drop のたびに現在の nested remote state を表示する、ツリー下の live snapshot
  • ノードは handle からドラッグして兄弟ノードの並び替えや別フォルダーへの移動を行います。
  • inline で名前を変更し、ファイルやフォルダーを追加し、現在のツリーからノードを削除できます。
  • ツリーをリセット を使うと、初期の workspace 構造に戻せます。

専用 Worker で動く sortable かつ編集可能なファイルツリー

ファイルとフォルダーを handle からドラッグし、フォルダー間でノードを移動し、inline で名前を編集し、新しい項目を作成して、必要に応じて workspace を初期状態に戻してください。

専用 Worker runtime を起動しています...

このツリーは、ライブラリの公開 runtime API が使うのと同じ host/worker transport モデルで接続されています。

import type { Channel } from '@omnicajs/vue-remote/host'
import { createEndpoint, fromWebWorker } from '@remote-ui/rpc'
import {
createApp,
h,
} from 'vue'
import {
HostedTree,
createProvider,
createReceiver,
} from '@omnicajs/vue-remote/host'
interface SandboxWorkerApi {
release (): void;
run (channel: Channel, labels: SortableFileTreeSandboxLabels): Promise<void>;
}
export interface SortableFileTreeSandboxLabels {
addFileLabel: string;
addFolderLabel: string;
booting: string;
cancelLabel: string;
collapseLabel: string;
deleteLabel: string;
dragCanceledAction: string;
draggingAction: string;
emptyFolderLabel: string;
expandLabel: string;
failed: string;
fileAddedAction: string;
fileKindLabel: string;
folderAddedAction: string;
folderKindLabel: string;
initialAction: string;
movedAction: string;
newFileName: string;
newFolderName: string;
ready: string;
removedAction: string;
renameLabel: string;
renamedAction: string;
resetLabel: string;
rootEyebrow: string;
saveLabel: string;
snapshotLabel: string;
snapshotUnavailable?: string;
unsupported: string;
}
export interface SortableFileTreeSandboxElements {
rootElement: HTMLElement;
}
export interface SortableFileTreeSandboxHandle {
destroy (): Promise<void>;
}
export const mountSortableFileTreeSandbox = async (
elements: SortableFileTreeSandboxElements,
labels: SortableFileTreeSandboxLabels
): Promise<SortableFileTreeSandboxHandle> => {
const { rootElement } = elements
if (typeof Worker === 'undefined') {
rootElement.textContent = labels.unsupported
return {
async destroy () {},
}
}
rootElement.textContent = labels.booting
const receiver = createReceiver()
const provider = createProvider()
const host = createApp({
render: () => h(HostedTree, { provider, receiver }),
})
const worker = new Worker(new URL('./remote.worker.ts', import.meta.url), {
type: 'module',
})
const endpoint = createEndpoint<SandboxWorkerApi>(fromWebWorker(worker))
let destroyed = false
const destroy = async () => {
if (destroyed) {
return
}
destroyed = true
try {
await endpoint.call.release()
} catch {
// Worker teardown should not block page cleanup.
} finally {
endpoint.terminate()
worker.terminate()
host.unmount()
rootElement.innerHTML = ''
}
}
try {
host.mount(rootElement)
await endpoint.call.run(receiver.receive, labels)
} catch (error) {
await destroy()
rootElement.textContent = `${labels.failed}: ${error instanceof Error ? error.message : String(error)}`
}
return {
destroy,
}
}