Skip to content

Canvix — Free & open-source graphic design tool. A community-built Canva alternative with a drag-and-drop editor, templates, shapes, text, images & one-click export. Works on desktop, tablet & mobile.

24.4 KB
Raw Blame
'use client'

import { useCallback, useEffect, useRef, useState } from 'react'
import dynamic from 'next/dynamic'
import { X, Keyboard, Eye } from 'lucide-react'
import { useAppStore } from '@/store/app-store'
import { useEditorStore, type PanelId } from '@/store/editor-store'
import { readPastedElements, capturePastedImage, fileFromClipboard } from '@/lib/clipboard/system-clipboard'
import { TopBar } from './TopBar'
import { LeftRail } from './LeftRail'
import { ContextToolbar } from './PropertiesBar'
import { PageBar } from './PageBar'
import { ZoomControls } from './ZoomControls'
import { PreviewOverlay } from './PreviewOverlay'
import { PresenterOverlay } from './PresenterOverlay'
import { TemplatesPanel } from './panels/TemplatesPanel'
import { ElementsPanel } from './panels/ElementsPanel'
import { TextPanel } from './panels/TextPanel'
import { UploadsPanel } from './panels/UploadsPanel'
import { PhotosPanel } from './panels/PhotosPanel'
import { VideosPanel } from './panels/VideosPanel'
import { AudioPanel } from './panels/AudioPanel'
import { BrandPanel } from './panels/BrandPanel'
import { ToolsPanel } from './panels/ToolsPanel'
import { ProjectsPanel } from './panels/ProjectsPanel'
import { AppsPanel } from './panels/AppsPanel'
import { BackgroundPanel } from './panels/BackgroundPanel'
import { LayersPanel } from './panels/LayersPanel'
import { captureThumbnail, canvasBridge } from './canvas/canvas-bridge'
import { CropDialog } from './CropDialog'
import { useIsMobile } from '@/hooks/use-mobile'
import { useCollab } from '@/hooks/use-collab'
import { useComments, markAllCommentsRead } from '@/hooks/use-comments'
import { CommentsPanel } from './CommentsPanel'
import { AnimatePanel } from './AnimatePanel'
import { Button } from '@/components/ui/button'
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog'
import { toast } from '@/hooks/use-toast'
import type { ImageElement, PageData } from '@/lib/types'
import { ensureDocumentFonts } from '@/lib/fonts/loader'

const CanvasStage = dynamic(() => import('./canvas/CanvasStage'), { ssr: false })

// v0.13 (P4): hidden plugin service iframes — mounted only when the
// open design actually contains plugin elements and the install
// list says the plugin is enabled. Flag-off instances fetch once,
// get 503, and never download the frame chunk at all.
const PluginServiceLayer = dynamic(() => import('./apps/PluginServiceLayer').then((m) => m.PluginServiceLayer), { ssr: false })

const PANEL_TITLES: Record<Exclude<PanelId, null>, string> = {
  templates: 'Templates',
  elements: 'Elements',
  text: 'Text',
  brand: 'Brand',
  uploads: 'Uploads',
  photos: 'Photos',
  videos: 'Videos',
  audio: 'Audio',
  tools: 'Tools',
  projects: 'Projects',
  apps: 'Apps',
  background: 'Background',
  layers: 'Layers',
}

const SHORTCUTS: [string, string][] = [
  ['Ctrl + Z / Ctrl + Shift + Z', 'Undo / redo'],
  ['Ctrl + S', 'Save design'],
  ['Ctrl + Alt + S', 'Save a version snapshot'],
  ['Ctrl + D', 'Duplicate selection'],
  ['Ctrl + C / X / V', 'Copy / cut / paste elements'],
  ['Ctrl + G / Ctrl + Shift + G', 'Group / ungroup selection'],
  ['Ctrl + A', 'Select all elements'],
  ['Ctrl + L', 'Lock / unlock selection'],
  ['Ctrl + ] / [', 'Bring forward / send backward'],
  ['Shift + Ctrl + ] / [', 'Bring to front / send to back'],
  ['Delete / Backspace', 'Delete selection'],
  ['Arrows (+ Shift ×10)', 'Nudge selection'],
  ['+ / −', 'Zoom in / out'],
  ['Space + drag', 'Pan the canvas'],
  ['Drag on empty canvas', 'Marquee (box) select'],
  ['Double-click text', 'Edit text'],
  ['Double-click layer name', 'Rename layer'],
  ['V', 'Select tool'],
  ['Shift + R', 'Show / hide rulers'],
  ['Drag from a ruler', 'Pull out a guide line'],
  ['Escape', 'Deselect / close'],
  ['Ctrl + mouse wheel', 'Zoom at cursor'],
]

export default function Editor() {
  const pendingDesign = useAppStore((s) => s.pendingDesign)
  const goDashboard = useAppStore((s) => s.goDashboard)
  const loadDesign = useEditorStore((s) => s.loadDesign)
  const designId = useEditorStore((s) => s.designId)
  const readOnly = useEditorStore((s) => s.readOnly)
  const panel = useEditorStore((s) => s.panel)
  const setPanel = useEditorStore((s) => s.setPanel)
  const dirty = useEditorStore((s) => s.dirty)
  const saving = useEditorStore((s) => s.saving)
  const version = useEditorStore((s) => s.version)
  const isMobile = useIsMobile()
  const [shortcutsOpen, setShortcutsOpen] = useState(false)
  const selectedIds = useEditorStore((s) => s.selectedIds)
  const commentsOpen = useEditorStore((s) => s.commentsOpen)
  const animateOpen = useEditorStore((s) => s.animateOpen)
  const setAnimateOpen = useEditorStore((s) => s.setAnimateOpen)

  // v0.3.1: image crop dialog (opened from the toolbar or context menu)
  const cropTargetId = useEditorStore((s) => s.cropTargetId)
  const closeCrop = useEditorStore((s) => s.closeCrop)
  const cropPages = useEditorStore((s) => s.pages)
  const cropPageIdx = useEditorStore((s) => s.currentPage)
  const cropElement = cropTargetId
    ? (cropPages[cropPageIdx]?.elements.find((e) => e.id === cropTargetId && e.type === 'image') as ImageElement | undefined)
    : undefined

  const loadedRef = useRef(false)

  // ── v0.5: real-time collaboration session for the open design ──
  useCollab()

  // ── v0.5: comments (live refresh) ──
  useComments()

  // ── load pending design (from dashboard) ───────────────────
  useEffect(() => {
    if (loadedRef.current) return
    loadedRef.current = true
    if (pendingDesign) {
      loadDesign(pendingDesign)
    } else if (!designId) {
      // no design to open → back home
      goDashboard()
    }
  }, [])

  // ── v0.17 (Wave 2): on-demand webfonts ─────────────────────
  // The catalog is 369 families but only the historic 63 preload with
  // first paint. Scan the open document (and any later mutation —
  // template applies, pastes) so every used family renders correctly.
  useEffect(() => {
    ensureDocumentFonts(cropPages)
  }, [cropPages])

  // ── autosave ───────────────────────────────────────────────
  // v0.9: view-only designs (owned by another account) never autosave —
  // the API would refuse with 403 anyway; skipping keeps the console
  // clean and the “saved” indicator honest.
  const persistOfflineDoc = useCallback(async () => {
    const state = useEditorStore.getState()
    if (!state.designId || state.readOnly) return
    try {
      const { putDoc } = await import('@/lib/collab/offline-store')
      const { getActiveCollabSession } = await import('@/hooks/use-collab')
      await putDoc({
        designId: state.designId,
        name: state.designName,
        width: state.width,
        height: state.height,
        pages: state.pages,
        baseUpdatedAt: state.baseUpdatedAt,
        seq: getActiveCollabSession()?.lastSeq ?? 0,
        savedAt: Date.now(),
      })
    } catch {
      /* IndexedDB unavailable — offline durability is best-effort */
    }
  }, [])

  const save = useCallback(
    async (withThumbnail: boolean) => {
      const state = useEditorStore.getState()
      if (!state.designId || state.saving || state.readOnly) return
      state.markSaving()
      try {
        let thumbnail: string | null = null
        if (withThumbnail) {
          thumbnail = (await canvasBridge.captureThumbnail?.()) ?? (await captureThumbnail().catch(() => null))
        }
        const res = await fetch(`/api/designs/${state.designId}`, {
          method: 'PUT',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({
            name: state.designName,
            pages: state.pages,
            baseUpdatedAt: state.baseUpdatedAt,
            ...(thumbnail ? { thumbnail } : {}),
            // v0.32.1: the apply flow's pending contract rides THIS save —
            // the server normalizes it ('reject' mode: over-limit is
            // refused, never weakened) and verifies it against the
            // saved pages (proof, not a claim)
            ...(state.aiPendingProvenance ? { aiProvenance: state.aiPendingProvenance } : {}),
          }),
        })
        if (!res.ok) throw new Error('save failed')
        const saved = (await res.json()) as {
          updatedAt?: string
          merged?: boolean
          pages?: PageData[]
          aiProvenance?: unknown
          aiProvenanceRejected?: string
        }
        // v0.32.1: the contract was stored or explicitly rejected —
        // either way the pending payload is settled (a rejection is
        // surfaced, never silently retried into a weakened store)
        if (saved.aiProvenance !== undefined || saved.aiProvenanceRejected !== undefined) {
          if (saved.aiProvenanceRejected) {
            toast({
              title: 'AI contract not saved',
              description: saved.aiProvenanceRejected.slice(0, 160),
              variant: 'destructive',
            })
          }
          useEditorStore.getState().setAIPendingProvenance(null)
        }
        // v0.10: the server merged our pages with concurrent edits — adopt
        // the merged result (applying through the merge gate is a no-op for
        // our own content and pulls in the kept remote parts)
        if (saved.merged && Array.isArray(saved.pages) && saved.pages.length) {
          useEditorStore.getState().applyRemoteOp({
            seq: 0,
            id: `save-merge-${Date.now()}`,
            designId: state.designId,
            actorId: 'server-merge',
            kind: 'pages:replace',
            payload: { pages: saved.pages },
            createdAt: new Date().toISOString(),
          })
        }
        useEditorStore.getState().setBaseUpdatedAt(saved.updatedAt ?? null)
        useEditorStore.getState().markSaved()
      } catch (err) {
        console.error('Autosave failed', err)
        useEditorStore.setState({ saving: false })
      }
      // v0.10: keep the offline mirror fresh whether the save landed or not
      void persistOfflineDoc()
    },
    [persistOfflineDoc]
  )

  // debounced autosave on changes
  useEffect(() => {
    if (!dirty || !designId) return
    const t = setTimeout(() => void save(true), 1600)
    return () => clearTimeout(t)
  }, [dirty, designId, version, save])

  // v0.10: offline mirror — persist the working document (debounced) on
  // every content mutation so a crash/reload/offline spell loses nothing
  useEffect(() => {
    if (!dirty || !designId) return
    const t = setTimeout(() => void persistOfflineDoc(), 700)
    return () => clearTimeout(t)
  }, [dirty, designId, version, persistOfflineDoc])

  // save when leaving the editor
  useEffect(() => {
    return () => {
      const state = useEditorStore.getState()
      if (state.designId && (state.dirty || state.saving)) void save(false)
    }
  }, [])

  // ── keyboard shortcuts ─────────────────────────────────────
  useEffect(() => {
    const isTyping = () => {
      const el = document.activeElement
      if (!el) return false
      const tag = el.tagName
      return tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT' || (el as HTMLElement).isContentEditable
    }

    const onKey = (e: KeyboardEvent) => {
      const mod = e.ctrlKey || e.metaKey
      const state = useEditorStore.getState()

      if (e.key === 'Escape') {
        state.clearSelection()
        return
      }
      if (isTyping()) return

      // v0.3.1: Shift+R toggles rulers & guides
      if (!mod && e.shiftKey && e.key.toLowerCase() === 'r') {
        e.preventDefault()
        state.toggleRulers()
        return
      }
      // canva: V switches back to the select tool
      if (!mod && !e.shiftKey && !e.altKey && e.key.toLowerCase() === 'v') {
        state.setTool('select')
        return
      }
      // v0.3.1: Ctrl+Alt+S saves a version snapshot
      if (mod && e.altKey && (e.code === 'KeyS' || e.key.toLowerCase() === 's')) {
        e.preventDefault()
        state.saveVersion()
        toast({ title: 'Version saved' })
        return
      }

      if (mod && e.key.toLowerCase() === 'z') {
        e.preventDefault()
        if (e.shiftKey) state.redo()
        else state.undo()
        return
      }
      if (mod && e.key.toLowerCase() === 'y') {
        e.preventDefault()
        state.redo()
        return
      }
      if (mod && e.key.toLowerCase() === 's') {
        e.preventDefault()
        void save(true)
        toast({ title: 'Design saved' })
        return
      }
      if (mod && e.key.toLowerCase() === 'd') {
        e.preventDefault()
        state.duplicateSelection()
        return
      }
      if (mod && e.key.toLowerCase() === 'c') {
        state.copySelection()
        return
      }
      if (mod && e.key.toLowerCase() === 'x') {
        state.copySelection()
        state.deleteSelection()
        return
      }
      if (mod && e.key.toLowerCase() === 'v') {
        state.pasteClipboard()
        return
      }
      if (mod && e.key.toLowerCase() === 'g') {
        e.preventDefault()
        if (e.shiftKey) state.ungroupSelection()
        else state.groupSelection()
        return
      }
      if (mod && e.key.toLowerCase() === 'a') {
        e.preventDefault()
        const els = state.pages[state.currentPage].elements.filter((el) => !el.locked)
        state.setSelection(els.map((el) => el.id))
        return
      }
      if (mod && e.key.toLowerCase() === 'l') {
        e.preventDefault()
        const hasLocked = state.pages[state.currentPage].elements.some((el) => state.selectedIds.includes(el.id) && el.locked)
        state.setLock(state.selectedIds, !hasLocked)
        return
      }
      if (mod && (e.key === ']' || e.key === '[')) {
        e.preventDefault()
        for (const id of state.selectedIds) state.moveLayer(id, e.key === ']' ? (e.shiftKey ? 'front' : 'up') : (e.shiftKey ? 'back' : 'down'))
        return
      }
      if (e.key === 'Delete' || e.key === 'Backspace') {
        if (state.selectedIds.length) {
          e.preventDefault()
          state.deleteSelection()
        }
        return
      }
      // nudge
      if (e.key.startsWith('Arrow') && state.selectedIds.length) {
        e.preventDefault()
        const step = e.shiftKey ? 10 : 1
        const dx = e.key === 'ArrowLeft' ? -step : e.key === 'ArrowRight' ? step : 0
        const dy = e.key === 'ArrowUp' ? -step : e.key === 'ArrowDown' ? step : 0
        const patch = {}
        for (const id of state.selectedIds) {
          const el = state.pages[state.currentPage].elements.find((el) => el.id === id)
          if (el) {
            state.updateElementsLive([id], { x: el.x + dx, y: el.y + dy })
          }
        }
        void patch
        return
      }
      if (e.key === '+' || e.key === '=') state.zoomIn()
      if (e.key === '-') state.zoomOut()
    }

    window.addEventListener('keydown', onKey)
    return () => window.removeEventListener('keydown', onKey)
  }, [save])

  // ── v0.14 (D2): the paste pipeline ─────────────────────────
  // ONE native handler (the keydown path can't read clipboardData):
  //   1. an OS image file (screenshots, "copy image") → image element
  //      through the uploads-library rail (D1), embed as fallback
  //   2. a Canvix envelope (custom MIME, or the text/plain fallback
  //      Firefox writes) → sanitized elements — cross-tab copy
  //   3. otherwise the internal clipboard (same-tab latency path)
  // Read-only designs never paste; typing surfaces keep their default.
  useEffect(() => {
    const isTyping = () => {
      const el = document.activeElement
      if (!el) return false
      const tag = el.tagName
      return tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT' || (el as HTMLElement).isContentEditable
    }
    const onPaste = (e: ClipboardEvent) => {
      if (isTyping()) return
      const state = useEditorStore.getState()
      if (state.readOnly) return
      const dt = e.clipboardData
      if (!dt) return

      // read EVERYTHING synchronously — the DataTransfer is neutered
      // the moment this handler returns (async continuations see '');
      // for files that means starting the byte read inside the handler
      const img = capturePastedImage(dt)
      const els = readPastedElements(dt)

      // 1. OS image → the media-store library rail (D1)
      if (img) {
        e.preventDefault()
        void (async () => {
          const file = await fileFromClipboard(img)
          const { uploadImageToLibrary, addImageFromFile, addImageFromSrc } = await import('@/components/editor/add-element')
          try {
            const { src, w, h } = await uploadImageToLibrary(file)
            addImageFromSrc(src, w, h)
          } catch {
            // library rail failed (quota/offline) → document embed
            await addImageFromFile(file).catch(() => undefined)
          }
        })()
        return
      }
      // 2. Canvix envelope (cross-tab / cross-window copy)
      if (els.length) {
        e.preventDefault()
        state.pasteElements(els)
        return
      }
      // 3. internal clipboard — the same-tab fast path
      if (state.hasInternalClipboard()) {
        e.preventDefault()
        state.pasteClipboard()
      }
    }
    window.addEventListener('paste', onPaste)
    return () => window.removeEventListener('paste', onPaste)
  }, [])

  // ── drag & drop image files onto the canvas ───────────────
  useEffect(() => {
    const onDragOver = (e: DragEvent) => {
      if (e.dataTransfer?.types.includes('Files')) {
        e.preventDefault()
        e.dataTransfer.dropEffect = 'copy'
      }
    }
    const onDrop = async (e: DragEvent) => {
      const files = e.dataTransfer?.files
      if (!files?.length) return
      e.preventDefault()
      const images = Array.from(files).filter((f) => f.type.startsWith('image/'))
      if (!images.length) return
      const { addImageFromFile, fillFrameAt, readImageFile } = await import('./add-element')
      const { canvasBridge } = await import('./canvas/canvas-bridge')
      let ok = 0
      let filled = 0
      for (const file of images) {
        try {
          // v0.6: dropping over a frame fills the frame instead of adding an image
          const { src, w, h } = await readImageFile(file)
          const pageX = (e.clientX - canvasBridge.pan.x) / canvasBridge.zoom
          const pageY = (e.clientY - canvasBridge.pan.y) / canvasBridge.zoom
          if (fillFrameAt(pageX, pageY, src, w, h)) {
            filled += 1
            ok += 1
            continue
          }
          await addImageFromFile(file)
          ok += 1
        } catch { /* ignore */ }
      }
      if (filled) toast({ title: `Frame filled with ${filled} image${filled > 1 ? 's' : ''}` })
      else if (ok) toast({ title: `${ok} image${ok > 1 ? 's' : ''} added` })
    }
    window.addEventListener('dragover', onDragOver)
    window.addEventListener('drop', onDrop)
    return () => {
      window.removeEventListener('dragover', onDragOver)
      window.removeEventListener('drop', onDrop)
    }
  }, [])

  // ── render panel ───────────────────────────────────────────
  const renderPanel = () => {
    switch (panel) {
      case 'templates': return <TemplatesPanel />
      case 'elements': return <ElementsPanel />
      case 'text': return <TextPanel />
      case 'uploads': return <UploadsPanel />
      case 'photos': return <PhotosPanel />
      case 'videos': return <VideosPanel />
      case 'audio': return <AudioPanel />
      case 'brand': return <BrandPanel />
      case 'tools': return <ToolsPanel />
      case 'projects': return <ProjectsPanel />
      case 'apps': return <AppsPanel />
      case 'background': return <BackgroundPanel />
      case 'layers': return <LayersPanel />
      default: return null
    }
  }

  const hasPanel = panel !== null

  return (
    <div className="h-screen w-full flex flex-col bg-[#0F1015] overflow-hidden">
      <TopBar onSave={() => save(true)} onShortcuts={() => setShortcutsOpen(true)} />
      {readOnly && (
        <div className="flex items-center justify-center gap-2 bg-[#3B2A00] border-b border-[#F59E0B]/25 px-4 py-1.5 text-[12px] text-[#FBBF24]" role="status">
          <Eye size={13} /> View-only — this design belongs to another account. Sign in as its owner to edit it.
        </div>
      )}

      <div className="flex flex-1 min-h-0">
        {/* desktop rail */}
        <div className="hidden md:flex">
          <LeftRail vertical />
        </div>

        {/* v0.5: comments panel — replaces the tool panel while open */}
        {commentsOpen ? (
          <aside
            className={
              isMobile
                ? 'fixed inset-x-0 bottom-14 top-14 z-40 bg-[#16181D] shadow-2xl flex flex-col'
                : 'w-[340px] bg-[#16181D] border-r border-white/[0.07] flex flex-col shrink-0'
            }
          >
            <CommentsPanel />
          </aside>
        ) : (
          <>
        {/* panel — desktop: sidebar; mobile: overlay sheet */}
        {hasPanel && (
          <aside
            className={
              isMobile
                ? 'fixed inset-x-0 bottom-14 top-14 z-40 bg-[#16181D] shadow-2xl flex flex-col'
                : 'w-[320px] bg-[#16181D] border-r border-white/[0.07] flex flex-col shrink-0'
            }
          >
            {isMobile && (
              <div className="flex items-center justify-between px-4 py-3 border-b border-white/[0.07] shrink-0">
                <h2 className="font-bold text-sm text-white">{PANEL_TITLES[panel]}</h2>
                <Button variant="ghost" size="icon" className="rounded-full text-white hover:bg-white/10 hover:text-white" onClick={() => setPanel(null)} aria-label="Close panel">
                  <X size={16} />
                </Button>
              </div>
            )}
            <div className="flex-1 min-h-0">{renderPanel()}</div>
          </aside>
        )}
          </>
        )}

        {/* canvas workspace */}
        <main className="flex-1 relative min-w-0 min-h-0" aria-label="Design canvas">
          <CanvasStage />
          <PageBar />
          <ZoomControls />
          {/* plugin renderers for elements on the canvas (hidden iframes) */}
          <PluginServiceLayer />
        </main>

        {/* v0.5: Animate panel — canva-style right side panel */}
        {animateOpen && (
          <aside className="w-[300px] bg-[#16181D] border-l border-white/[0.07] flex flex-col shrink-0 hidden md:flex">
            <AnimatePanel onClose={() => setAnimateOpen(false)} />
          </aside>
        )}
      </div>

      {/* mobile contextual action bar (canva mobile: bottom bar on selection) */}
      {isMobile && selectedIds.length > 0 && (
        <div className="fixed inset-x-0 bottom-14 z-40">
          <ContextToolbar variant="mobile" />
        </div>
      )}

      {/* mobile rail */}
      <div className="md:hidden border-t border-white/[0.07]">
        <LeftRail vertical={false} />
      </div>

      {/* save indicator (mobile) */}
      {isMobile && saving && (
        <div className="fixed top-16 left-1/2 -translate-x-1/2 z-50 text-xs bg-[#7630D7] text-white px-3 py-1 rounded-full">
          Saving…
        </div>
      )}

      <PreviewOverlay />
      {/* v0.18 (Wave 3): present with speaker notes */}
      <PresenterOverlay />

      {/* v0.3.1: image crop dialog */}
      {cropElement && (
        <CropDialog element={cropElement} open onOpenChange={(o) => { if (!o) closeCrop() }} />
      )}

      <Dialog open={shortcutsOpen} onOpenChange={setShortcutsOpen}>
        <DialogContent className="rounded-[28px] bg-[#16181D] border-white/10 text-white sm:max-w-sm">
          <DialogHeader>
            <DialogTitle className="flex items-center gap-2 text-lg font-bold">
              <Keyboard size={18} className="text-[#02C0CC]" /> Keyboard shortcuts
            </DialogTitle>
          </DialogHeader>
          <div className="divide-y divide-white/[0.07]">
            {SHORTCUTS.map(([keys, label]) => (
              <div key={keys} className="flex items-center justify-between py-2">
                <span className="text-[13px] text-white/70">{label}</span>
                <kbd className="text-[11px] font-semibold bg-white/[0.08] border border-white/10 rounded-md px-2 py-1">{keys}</kbd>
              </div>
            ))}
          </div>
        </DialogContent>
      </Dialog>
    </div>
  )
}