context menu for file tree

This commit is contained in:
2026-02-26 10:40:38 +10:00
parent ee09163a7d
commit 13f5386420
8 changed files with 410 additions and 41 deletions

View File

@@ -1,5 +1,5 @@
import { readdir, readFile, writeFile, mkdir, unlink } from 'fs/promises'
import { join } from 'path'
import { readdir, readFile, writeFile, mkdir, unlink, rename as fsRename, rm } from 'fs/promises'
import { join, dirname } from 'path'
import type { FileNode, RevisionMeta } from '../renderer/types/editor'
const DRAFT_ROOT =
@@ -215,3 +215,37 @@ export async function deleteRevision(filePath: string, revisionId: string): Prom
const revPath = join(REVISIONS_DIR, slug, `${revisionId}.json`)
await unlink(revPath)
}
// ─── File tree mutations ──────────────────────────────────────────────────────
export async function renameFileOrDir(oldPath: string, newName: string): Promise<string> {
assertInDraftRoot(oldPath)
const isFile = oldPath.endsWith('.md')
const newPath = join(dirname(oldPath), isFile ? `${newName}.md` : newName)
assertInDraftRoot(newPath)
await fsRename(oldPath, newPath)
return newPath
}
export async function deleteFileOrDir(targetPath: string): Promise<void> {
assertInDraftRoot(targetPath)
await rm(targetPath, { recursive: true, force: true })
}
export async function createMarkdownFile(parentPath: string, name: string): Promise<string> {
const dir = parentPath === '__root__' ? DRAFT_ROOT : parentPath
assertInDraftRoot(dir)
const filePath = join(dir, `${name}.md`)
assertInDraftRoot(filePath)
await writeFile(filePath, '', 'utf-8')
return filePath
}
export async function createSubdirectory(parentPath: string, name: string): Promise<string> {
const parent = parentPath === '__root__' ? DRAFT_ROOT : parentPath
assertInDraftRoot(parent)
const newDir = join(parent, name)
assertInDraftRoot(newDir)
await mkdir(newDir, { recursive: true })
return newDir
}