AI-native design editor. Open-source Figma alternative.
import { createEventHook } from '@vueuse/core'
import type { Editor, EditorState } from '@open-pencil/core'
import { exportFigFile } from '@open-pencil/core/io/formats/fig'
import { createAutosave } from '@/app/document/autosave'
import { createDocumentChanges } from '@/app/document/io/changes'
import {
documentFileName,
documentNameFromFigPath,
downloadNameFromPath,
figDownloadName,
isFigArchiveName
} from '@/app/document/io/names'
import { createSaveActions } from '@/app/document/io/save/actions'
import { createDocumentSourceState } from '@/app/document/io/source/state'
import type { DocumentSourceAccess } from '@/app/document/io/types'
import { createDocumentRecovery } from '@/app/document/recovery'
import { recoveryEnabled } from '@/app/document/recovery/preferences'
import { storageLocationOf } from '@/app/integrations/storage/location'
import type { StorageDocumentBinding, StorageLocation } from '@/app/integrations/storage/types'
import { cacheRecentFileCover, drawHomeCardCover } from '@/app/recent-files'
import { createCanvasId } from '@/app/storage/id'
import { isTauri } from '@/app/tauri/env'
import { trackDocumentFormat } from '@/app/telemetry'
type DocumentSourceState = EditorState & {
documentName: string
autosaveEnabled: boolean
}
export { createDocumentSourceState }
type DocumentSourceOptions = DocumentSourceAccess & {
editor: Editor
state: DocumentSourceState
stopWatchingFile: () => void
startWatchingFile: () => Promise<void>
getRenderer: () => Editor['renderer']
}
export function createDocumentSourceActions({
editor,
state,
stopWatchingFile,
startWatchingFile,
getFileHandle,
setFileHandle,
getFilePath,
setFilePath,
getDownloadName,
setDownloadName,
getStorageBinding,
setStorageBinding,
hasWritableSource,
setSourceIdentity,
getSavedVersion,
setSavedVersion,
setLastWriteTime,
getRenderer
}: DocumentSourceOptions) {
const changes = createDocumentChanges(editor)
async function saveAndTrack(save: () => Promise<boolean>) {
const revision = changes.capture()
const saved = await save()
if (saved) changes.markSaved(revision)
return saved
}
function buildFigFile() {
const renderer = getRenderer()
return exportFigFile(editor.graph, {
ck: renderer?.ck,
renderer: renderer ?? undefined,
pageId: state.currentPageId,
documentName: state.documentName
})
}
/**
* A write changes the file's time, which keys its Home preview, so the sharp cover is drawn
* again for it; without it Home falls back to the small cover inside the file. An edit made
* while `version` was being written is not in the file, so the cover is left to the next save.
*/
function recacheRecentCover(version: number) {
const path = getFilePath()
const renderer = getRenderer()
if (!path || !renderer || !isFigArchiveName(path)) return
if (changes.capture() !== version) return
void cacheRecentFileCover(path, editor.graph, renderer).catch((error: unknown) => {
console.warn('[Recent files] Could not cache the preview after saving', error)
})
}
function buildRecoveryFigFile() {
return exportFigFile(editor.graph, {
pageId: state.currentPageId,
documentName: state.documentName
})
}
const recovery = createDocumentRecovery({
state,
version: changes.capture,
isEnabled: () => recoveryEnabled.value,
buildFigFile: buildRecoveryFigFile,
buildCover: () => {
const renderer = getRenderer()
return renderer ? drawHomeCardCover(editor.graph, renderer) : null
},
hasWritableSource
})
const { saveFigFile, saveFigFileAs, writeFile } = createSaveActions({
state,
version: changes.capture,
buildFigFile,
getFilePath,
setFilePath,
getFileHandle,
setFileHandle,
getDownloadName,
setDownloadName,
getStorageBinding,
setStorageBinding,
hasWritableSource,
setSourceIdentity,
setSavedVersion,
setLastWriteTime,
startWatchingFile: () => {
void startWatchingFile()
},
onWriteSuccess: (version) => {
recacheRecentCover(version)
return recovery.markProtectedVersion(version)
},
onDownloadSuccess: (version) => recovery.markProtectedVersion(version)
})
const autosave = createAutosave({
state,
version: changes.capture,
getSavedVersion,
hasWritableSource,
saveCurrentDocument: async (version) => {
const revision = changes.capture()
const data = await buildFigFile()
if (await writeFile(data, version)) changes.markSaved(revision)
}
})
function markDocumentSaved() {
changes.markSaved()
setSavedVersion(changes.capture())
}
// Only names the person gives; a save's provisional name and the room's own are no rename.
const renamed = createEventHook<string>()
function renameDocument(name: string) {
const trimmed = name.trim()
if (state.hostRestrictions.readOnly || !trimmed || trimmed === state.documentName) return
state.documentName = trimmed
// Storage keeps the name with the document; a file's name is its path, which saving leaves as is.
if (getStorageBinding()) changes.markChanged()
void renamed.trigger(trimmed)
}
function setDocumentSource(
fileName: string,
sourceFormat: string,
handle?: FileSystemFileHandle,
path?: string
) {
stopWatchingFile()
setStorageBinding(null)
const isFig = sourceFormat === 'fig'
setFileHandle(isFig ? (handle ?? null) : null)
setFilePath(isFig ? (path ?? null) : null)
setDownloadName(figDownloadName(fileName, sourceFormat))
setSourceIdentity({ handle: handle ?? null, path: path ?? null })
markDocumentSaved()
void recovery.markProtectedVersion(changes.capture())
trackDocumentFormat('document_opened', sourceFormat)
if (isFig && (handle || path)) {
void startWatchingFile()
}
}
function setStorageDocumentSource(binding: StorageDocumentBinding, documentName: string) {
stopWatchingFile()
setFileHandle(null)
setFilePath(null)
setDownloadName(documentFileName(documentName, state.documentKind))
setSourceIdentity({ handle: null, path: null })
setStorageBinding(binding)
state.documentName = documentName
state.autosaveEnabled = true
markDocumentSaved()
void recovery.markProtectedVersion(changes.capture())
}
function setPlannedFilePath(path: string) {
stopWatchingFile()
setStorageBinding(null)
setFileHandle(null)
setFilePath(path)
const downloadName = downloadNameFromPath(path)
setDownloadName(downloadName)
state.documentName = documentNameFromFigPath(downloadName)
}
let retargeting = false
/**
* Save to a new target; when the write fails, the document keeps the source it had. A second
* retarget while one is in flight is refused, so a failure never restores another save's target.
*/
async function saveToNewTarget(planTarget: () => void): Promise<boolean> {
if (retargeting) return false
retargeting = true
const previous = {
filePath: getFilePath(),
fileHandle: getFileHandle(),
storageBinding: getStorageBinding(),
downloadName: getDownloadName(),
documentName: state.documentName
}
const restore = () => {
setStorageBinding(previous.storageBinding)
setFileHandle(previous.fileHandle)
setFilePath(previous.filePath)
setDownloadName(previous.downloadName)
state.documentName = previous.documentName
if (previous.filePath || previous.fileHandle) void startWatchingFile()
}
planTarget()
try {
const saved = await saveAndTrack(saveFigFile)
if (!saved) restore()
return saved
} catch (error) {
restore()
throw error
} finally {
retargeting = false
}
}
async function saveFigFileToPath(path: string): Promise<boolean> {
// Only the desktop app writes paths; a browser would fall back to a download.
if (!isTauri()) return false
const saved = await saveToNewTarget(() => setPlannedFilePath(path))
if (saved) void startWatchingFile()
return saved
}
/**
* Hand the document to `write`, such as an MCP server saving it to a path a browser cannot
* reach, and count it saved as a download is. Where the document saves next does not change.
*/
async function saveFigFileCopy(write: (data: Uint8Array) => Promise<void>): Promise<boolean> {
const version = changes.capture()
await write(await buildFigFile())
await recovery.markProtectedVersion(version)
return true
}
/** Upload the document to storage as a new stored document and keep editing it there. */
async function saveFigFileToStorage(location: StorageLocation): Promise<boolean> {
const saved = await saveToNewTarget(() => {
stopWatchingFile()
setFileHandle(null)
setFilePath(null)
setDownloadName(documentFileName(state.documentName, state.documentKind))
setStorageBinding({ ...storageLocationOf(location), documentId: createCanvasId() })
})
if (saved) {
setSourceIdentity({ handle: null, path: null })
state.autosaveEnabled = true
}
return saved
}
function startWatchingCurrentFile() {
void startWatchingFile()
}
function disposeDocumentIO() {
changes.dispose()
stopWatchingFile()
autosave.disposeAutosave()
recovery.disposeRecovery()
}
return {
renameDocument,
onDocumentRenamed: renamed.on,
setDocumentSource,
setStorageDocumentSource,
setPlannedFilePath,
saveFigFileToPath,
saveFigFileCopy: (write: (data: Uint8Array) => Promise<void>) =>
saveAndTrack(() => saveFigFileCopy(write)),
saveFigFileToStorage,
startWatchingCurrentFile,
disposeDocumentIO,
saveFigFile: () => saveAndTrack(saveFigFile),
saveFigFileAs: () => saveAndTrack(saveFigFileAs),
hasUnsavedChanges: changes.hasUnsavedChanges,
markDocumentSaved,
markDocumentChanged: changes.markChanged,
adoptChanges: changes.adopt,
getStorageBinding,
getRecoveryId: () => recovery.getRecoveryId(),
adoptRecoverySnapshot: (id: string) => {
changes.markChanged()
return recovery.adoptRecoverySnapshot(id)
},
persistRecoveryNow: () => recovery.persistNow(),
discardRecovery: () => recovery.discardRecovery()
}
}