Skip to content

A lightweight Clip Studio Paint alternative with real-time multiplayer collaborative drawing. Built with Tauri and React.

1.9 KB
Raw Blame
import { LoadedDocument } from '../types/editor';
import {
  fromNativeLayerNodeMeta,
  openNativeDocumentBytes,
} from './nativeEngineBridge';
import { sanitizeDocumentStem } from './saveManager/fileNameUtils';
import { unwrapLegacySoraBytes } from './legacySoraMigrator';

/**
 * Universal zero-canvas file opener delegating `.ink`, legacy `.sora` v2, `.psd`/`.psb`,
 * `.clip`/`.csp`/`.lip`, and standard raster images (`.png`, `.jpg`, `.jpeg`, `.webp`,
 * `.bmp`, `.gif`, `.ico`, `.tif`, `.tiff`) directly into Rust `crates/ink-engine`.
 */
export async function openFile(file: File, docId?: string): Promise<LoadedDocument> {
  const dot = file.name.lastIndexOf('.');
  const ext = dot >= 0 ? file.name.slice(dot).toLowerCase() : '';
  const targetDocId = docId || `doc-${Date.now()}-${Math.random().toString(36).slice(2, 6)}`;
  const rawBytes = new Uint8Array(await file.arrayBuffer());

  const uploadBytes = ext === '.sora' ? unwrapLegacySoraBytes(rawBytes) : rawBytes;
  const uploadName = ext === '.sora' ? `${sanitizeDocumentStem(file.name, 'Untitled')}.ink` : file.name;

  const nativeRes = await openNativeDocumentBytes(targetDocId, uploadName, uploadBytes);
  const resolvedDocName = file.name
    ? sanitizeDocumentStem(file.name, sanitizeDocumentStem(nativeRes.doc.name, 'Untitled'))
    : sanitizeDocumentStem(nativeRes.doc.name, 'Untitled');

  return {
    name: resolvedDocName,
    dimensions: nativeRes.doc.dimensions,
    colorMode: nativeRes.doc.colorMode,
    layers: nativeRes.doc.layers.map(fromNativeLayerNodeMeta),
    previewDataUrl: nativeRes.previewDataUrl ?? nativeRes.doc.previewDataUrl ?? undefined,
    perspectiveRuler: (nativeRes.doc.perspectiveRuler as LoadedDocument['perspectiveRuler']) ?? null,
    animationTimeline: nativeRes.doc.animationTimeline as LoadedDocument['animationTimeline'],
    timelapse: (nativeRes.doc.timelapse as LoadedDocument['timelapse']) ?? null,
    workTimeMs: nativeRes.doc.workTimeMs,
  };
}