OpenReel Video - Professional browser-based video editor. Open source CapCut alternative. 100% browser-based, no installation, no cloud uploads, no watermarks.
TypeScript
fix(editor): fix script view dialog and project JSON validation
Valics Lehel66521e58d3ae
diff --git a/apps/web/src/components/editor/ScriptViewDialog.tsx b/apps/web/src/components/editor/ScriptViewDialog.tsxindex 64ac2c86..ab6ba107 100644--- a/apps/web/src/components/editor/ScriptViewDialog.tsx+++ b/apps/web/src/components/editor/ScriptViewDialog.tsx@@ -1,4 +1,4 @@-import { useState, useCallback, useMemo } from "react";+import { useState, useCallback, useMemo, useRef } from "react";import {Copy,Download,@@ -18,10 +18,6 @@ import {DialogTitle,DialogDescription,Button,- Tabs,- TabsList,- TabsTrigger,- TabsContent,} from "@openreel/ui";import { useProjectStore } from "../../stores/project-store";import { createProjectSerializer, createStorageEngine } from "@openreel/core";@@ -39,10 +35,13 @@ export const ScriptViewDialog: React.FC<ScriptViewDialogProps> = ({onClose,}) => {const { project } = useProjectStore();+ const [activeTab, setActiveTab] = useState<"export" | "import">("export");const [importJson, setImportJson] = useState("");const [validation, setValidation] = useState<ValidationResult | null>(null);const [isValidating, setIsValidating] = useState(false);const [copySuccess, setCopySuccess] = useState(false);+ const [isDragging, setIsDragging] = useState(false);+ const fileInputRef = useRef<HTMLInputElement>(null);const storage = useMemo(() => createStorageEngine(), []);const serializer = useMemo(() => createProjectSerializer(storage), [storage]);@@ -70,13 +69,86 @@ export const ScriptViewDialog: React.FC<ScriptViewDialogProps> = ({const url = URL.createObjectURL(blob);const a = document.createElement("a");a.href = url;- a.download = `${project?.name || "project"}-script.json`;+ a.download = `${project?.name || "project"}.json`;document.body.appendChild(a);a.click();document.body.removeChild(a);URL.revokeObjectURL(url);}, [exportedJson, project?.name]);+ const processImportJson = useCallback(+ (jsonString: string) => {+ setImportJson(jsonString);+ setValidation(null);+ // Auto-validate+ try {+ const result = serializer.validateProjectJson(jsonString);+ setValidation(result);+ } catch (error) {+ setValidation({+ valid: false,+ errors: [+ `Validation error: ${error instanceof Error ? error.message : "Unknown error"}`,+ ],+ warnings: [],+ });+ }+ },+ [serializer],+ );++ const handleFileUpload = useCallback(+ (file: File) => {+ const reader = new FileReader();+ reader.onload = (e) => {+ const content = e.target?.result;+ if (typeof content === "string") {+ processImportJson(content);+ }+ };+ reader.readAsText(file);+ },+ [processImportJson],+ );++ const handleFileInputChange = useCallback(+ (e: React.ChangeEvent<HTMLInputElement>) => {+ const file = e.target.files?.[0];+ if (file) handleFileUpload(file);+ // Reset so same file can be selected again+ e.target.value = "";+ },+ [handleFileUpload],+ );++ const handleDragOver = useCallback((e: React.DragEvent) => {+ e.preventDefault();+ setIsDragging(true);+ }, []);++ const handleDragLeave = useCallback((e: React.DragEvent) => {+ e.preventDefault();+ setIsDragging(false);+ }, []);++ const handleDrop = useCallback(+ (e: React.DragEvent) => {+ e.preventDefault();+ setIsDragging(false);+ const file = e.dataTransfer.files[0];+ if (file && file.type === "application/json") {+ handleFileUpload(file);+ } else if (file) {+ setValidation({+ valid: false,+ errors: ["Please upload a .json file"],+ warnings: [],+ });+ }+ },+ [handleFileUpload],+ );+const handleValidate = useCallback(() => {setIsValidating(true);try {@@ -120,167 +192,232 @@ export const ScriptViewDialog: React.FC<ScriptViewDialogProps> = ({return (<Dialog open onOpenChange={(open) => !open && onClose()}>- <DialogContent className="max-w-6xl max-h-[90vh] p-0 gap-0 bg-background-secondary border-border overflow-hidden flex flex-col">+ <DialogContent className="max-w-4xl p-0 gap-0 bg-background-secondary border-border overflow-hidden flex flex-col" style={{ height: "70vh" }}><DialogHeader className="p-4 border-b border-border space-y-0"><div className="flex items-center gap-3"><FileCode size={20} className="text-primary" /><div><DialogTitle className="text-lg font-semibold text-text-primary">- Script View+ Project JSON</DialogTitle><DialogDescription className="text-xs text-text-muted">- View and import project JSON+ Export or import project as JSON</DialogDescription></div></div></DialogHeader>- <Tabs defaultValue="view" className="flex-1 flex flex-col overflow-hidden">- <TabsList className="flex gap-1 p-2 border-b border-border bg-transparent rounded-none justify-start">- <TabsTrigger- value="view"- className="px-4 py-2 rounded-lg text-sm font-medium data-[state=active]:bg-background-tertiary data-[state=active]:text-text-primary text-text-secondary hover:text-text-primary hover:bg-background-elevated"- >- View & Export- </TabsTrigger>- <TabsTrigger- value="import"- className="px-4 py-2 rounded-lg text-sm font-medium data-[state=active]:bg-background-tertiary data-[state=active]:text-text-primary text-text-secondary hover:text-text-primary hover:bg-background-elevated"- >- Import- </TabsTrigger>- </TabsList>+ {/* Tab buttons */}+ <div className="flex gap-1 p-2 border-b border-border">+ <button+ onClick={() => setActiveTab("export")}+ className={`px-4 py-2 rounded-lg text-sm font-medium transition-colors ${+ activeTab === "export"+ ? "bg-background-tertiary text-text-primary"+ : "text-text-secondary hover:text-text-primary hover:bg-background-elevated"+ }`}+ >+ Export JSON+ </button>+ <button+ onClick={() => setActiveTab("import")}+ className={`px-4 py-2 rounded-lg text-sm font-medium transition-colors ${+ activeTab === "import"+ ? "bg-background-tertiary text-text-primary"+ : "text-text-secondary hover:text-text-primary hover:bg-background-elevated"+ }`}+ >+ Import+ </button>+ </div>- <TabsContent value="view" className="flex-1 flex flex-col overflow-hidden mt-0">- <div className="flex gap-2 p-3 border-b border-border">- <Button variant="outline" size="sm" onClick={handleCopy}>- {copySuccess ? (- <>- <CheckCircle2 size={16} className="text-primary" />- Copied!- </>- ) : (- <>- <Copy size={16} />- Copy- </>- )}- </Button>- <Button variant="outline" size="sm" onClick={handleDownload}>- <Download size={16} />- Download- </Button>- <div className="flex-1" />- <a- href="/llm.txt"- download="openreel-llm-documentation.txt"- className="flex items-center gap-2 px-3 py-2 bg-primary/10 hover:bg-primary/20 text-primary rounded-lg text-sm transition-colors"- >- <FileCode size={16} />- Download LLM.txt- </a>- </div>+ {/* Tab content */}+ <div className="flex-1 flex flex-col overflow-hidden">+ {activeTab === "export" && (+ <>+ {exportedJson ? (+ <>+ <div className="flex gap-2 p-3 border-b border-border">+ <Button variant="outline" size="sm" onClick={handleCopy}>+ {copySuccess ? (+ <>+ <CheckCircle2 size={16} className="text-primary" />+ Copied!+ </>+ ) : (+ <>+ <Copy size={16} />+ Copy+ </>+ )}+ </Button>+ <Button variant="outline" size="sm" onClick={handleDownload}>+ <Download size={16} />+ Download JSON+ </Button>+ </div>- <div className="flex-1 overflow-auto custom-scrollbar p-4">- <div className="rounded-lg overflow-hidden border border-border">- <SyntaxHighlighter- language="json"- style={vs2015}- showLineNumbers- customStyle={{- margin: 0,- padding: "1rem",- background: "#1e1e1e",- fontSize: "12px",- }}- >- {exportedJson}- </SyntaxHighlighter>- </div>- </div>- </TabsContent>+ <div className="flex-1 overflow-auto custom-scrollbar p-4">+ <div className="rounded-lg overflow-hidden border border-border">+ <SyntaxHighlighter+ language="json"+ style={vs2015}+ showLineNumbers+ customStyle={{+ margin: 0,+ padding: "1rem",+ background: "#1e1e1e",+ fontSize: "12px",+ }}+ >+ {exportedJson}+ </SyntaxHighlighter>+ </div>+ </div>+ </>+ ) : (+ <div className="flex-1 flex flex-col items-center justify-center gap-3 p-8 text-center">+ <FileCode size={40} className="text-text-muted" />+ <p className="text-sm text-text-secondary">+ No project data to export.+ </p>+ </div>+ )}+ </>+ )}- <TabsContent value="import" className="flex-1 flex flex-col gap-4 p-4 overflow-auto mt-0">- <div className="flex-1 flex flex-col gap-2">- <label className="text-sm font-medium text-text-secondary">- Paste JSON- </label>- <textarea- value={importJson}- onChange={(e) => setImportJson(e.target.value)}- placeholder="Paste project JSON here..."- className="flex-1 p-3 bg-background-tertiary border border-border rounded-lg text-text-primary font-mono text-xs resize-none focus:outline-none focus:border-primary"+ {activeTab === "import" && (+ <div className="flex-1 flex flex-col gap-4 p-4 overflow-auto">+ {/* File upload drop zone */}+ <input+ ref={fileInputRef}+ type="file"+ accept=".json,application/json"+ onChange={handleFileInputChange}+ className="hidden"/>- </div>-- <Button- variant="outline"- onClick={handleValidate}- disabled={!importJson || isValidating}- >- {isValidating ? "Validating..." : "Validate"}- </Button>+ <div+ onDragOver={handleDragOver}+ onDragLeave={handleDragLeave}+ onDrop={handleDrop}+ onClick={() => fileInputRef.current?.click()}+ className={`flex flex-col items-center justify-center gap-3 p-8 border-2 border-dashed rounded-xl cursor-pointer transition-colors ${+ isDragging+ ? "border-primary bg-primary/10"+ : "border-border hover:border-text-muted hover:bg-background-tertiary"+ }`}+ >+ <Upload+ size={32}+ className={+ isDragging ? "text-primary" : "text-text-muted"+ }+ />+ <div className="text-center">+ <p className="text-sm text-text-primary font-medium">+ {isDragging+ ? "Drop JSON file here"+ : "Drop a JSON file here or click to browse"}+ </p>+ <p className="text-xs text-text-muted mt-1">+ Accepts .json project files+ </p>+ </div>+ </div>- {validation && (- <div className="space-y-2">- {validation.valid && (- <div className="flex items-center gap-2 p-3 bg-primary/10 border border-primary/30 rounded-lg">- <CheckCircle2 size={16} className="text-primary" />- <span className="text-sm text-primary">- Valid JSON - Ready to import- </span>- </div>- )}+ {/* Show loaded file info */}+ {importJson && (+ <div className="flex items-center gap-2 p-3 bg-background-tertiary border border-border rounded-lg">+ <FileCode size={16} className="text-text-secondary" />+ <span className="text-sm text-text-primary flex-1">+ {importJson.length.toLocaleString()} characters loaded+ </span>+ <Button+ variant="outline"+ size="sm"+ onClick={() => {+ setImportJson("");+ setValidation(null);+ }}+ >+ Clear+ </Button>+ <Button+ variant="outline"+ size="sm"+ onClick={handleValidate}+ disabled={isValidating}+ >+ {isValidating ? "Validating..." : "Re-validate"}+ </Button>+ </div>+ )}- {validation.errors.length > 0 && (- <div className="p-3 bg-error/10 border border-error/30 rounded-lg space-y-1">- <div className="flex items-center gap-2 text-error font-medium text-sm">- <AlertCircle size={16} />- Errors+ {/* Validation results */}+ {validation && (+ <div className="space-y-2">+ {validation.valid && (+ <div className="flex items-center gap-2 p-3 bg-primary/10 border border-primary/30 rounded-lg">+ <CheckCircle2 size={16} className="text-primary" />+ <span className="text-sm text-primary">+ Valid project JSON — ready to import+ </span></div>- <ul className="list-disc list-inside text-xs text-error/80 space-y-0.5">- {validation.errors.map((err, i) => (- <li key={i}>{err}</li>- ))}- </ul>- </div>- )}+ )}- {validation.warnings.length > 0 && (- <div className="p-3 bg-warning/10 border border-warning/30 rounded-lg space-y-1">- <div className="flex items-center gap-2 text-warning font-medium text-sm">- <AlertTriangle size={16} />- Warnings+ {validation.errors.length > 0 && (+ <div className="p-3 bg-error/10 border border-error/30 rounded-lg space-y-1">+ <div className="flex items-center gap-2 text-error font-medium text-sm">+ <AlertCircle size={16} />+ Errors+ </div>+ <ul className="list-disc list-inside text-xs text-error/80 space-y-0.5">+ {validation.errors.map((err, i) => (+ <li key={i}>{err}</li>+ ))}+ </ul></div>- <ul className="list-disc list-inside text-xs text-warning/80 space-y-0.5">- {validation.warnings.map((warning, i) => (- <li key={i}>{warning}</li>- ))}- </ul>- </div>- )}+ )}- {validation.missingAssets &&- validation.missingAssets.length > 0 && (- <div className="p-3 bg-background-tertiary border border-border rounded-lg space-y-1">- <div className="text-sm font-medium text-text-secondary">- Missing Assets ({validation.missingAssets.length})+ {validation.warnings.length > 0 && (+ <div className="p-3 bg-warning/10 border border-warning/30 rounded-lg space-y-1">+ <div className="flex items-center gap-2 text-warning font-medium text-sm">+ <AlertTriangle size={16} />+ Warnings</div>- <p className="text-xs text-text-muted">- These assets will be imported as placeholders and can- be replaced later.- </p>+ <ul className="list-disc list-inside text-xs text-warning/80 space-y-0.5">+ {validation.warnings.map((warning, i) => (+ <li key={i}>{warning}</li>+ ))}+ </ul></div>)}- </div>- )}- <Button onClick={handleImport} disabled={!validation?.valid}>- <Upload size={16} />- Import Project- </Button>- </TabsContent>- </Tabs>+ {validation.missingAssets &&+ validation.missingAssets.length > 0 && (+ <div className="p-3 bg-background-tertiary border border-border rounded-lg space-y-1">+ <div className="text-sm font-medium text-text-secondary">+ Missing Assets ({validation.missingAssets.length})+ </div>+ <p className="text-xs text-text-muted">+ These assets will be imported as placeholders and can+ be replaced later.+ </p>+ </div>+ )}+ </div>+ )}++ {/* Import button */}+ {importJson && (+ <Button onClick={handleImport} disabled={!validation?.valid}>+ <Upload size={16} />+ Import Project+ </Button>+ )}+ </div>+ )}+ </div></DialogContent></Dialog>);diff --git a/apps/web/src/components/editor/Toolbar.tsx b/apps/web/src/components/editor/Toolbar.tsxindex 3f0e4b95..644e643b 100644--- a/apps/web/src/components/editor/Toolbar.tsx+++ b/apps/web/src/components/editor/Toolbar.tsx@@ -929,7 +929,7 @@ export const Toolbar: React.FC = () => {</button></TooltipTrigger><TooltipContent>- <p>Script View - View/Import JSON</p>+ <p>Project JSON - Export/Import</p></TooltipContent></Tooltip>diff --git a/packages/core/src/storage/project-serializer.ts b/packages/core/src/storage/project-serializer.tsindex 137cc313..40b96911 100644--- a/packages/core/src/storage/project-serializer.ts+++ b/packages/core/src/storage/project-serializer.ts@@ -154,7 +154,17 @@ export class ProjectSerializer {for (const track of project.timeline.tracks) {if (track.clips) {for (const clip of track.clips) {- if (clip.mediaId && !mediaIds.has(clip.mediaId)) {+ const isVirtualClip =+ clip.mediaId &&+ (clip.mediaId.startsWith("text-") ||+ clip.mediaId.startsWith("shape-") ||+ clip.mediaId.startsWith("svg-") ||+ clip.mediaId.startsWith("sticker-"));+ if (+ clip.mediaId &&+ !isVirtualClip &&+ !mediaIds.has(clip.mediaId)+ ) {result.errors.push(`Clip ${clip.id} references non-existent mediaId: ${clip.mediaId}`,);