Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
feat(editor): add drag-and-drop visual overlay and file type validation
Naiyuan Qing301a4a3882bb
diff --git a/apps/web/features/editor/content-editor.css b/apps/web/features/editor/content-editor.cssindex a58931c72e..f1390a31ab 100644--- a/apps/web/features/editor/content-editor.css+++ b/apps/web/features/editor/content-editor.css@@ -388,3 +388,23 @@0%, 100% { opacity: 0.5; }50% { opacity: 0.3; }}++/* Drag-and-drop overlay */+.editor-drop-overlay {+ position: absolute;+ inset: 0;+ display: flex;+ align-items: center;+ justify-content: center;+ border: 2px dashed hsl(var(--primary));+ border-radius: var(--radius);+ background: hsl(var(--primary) / 0.05);+ z-index: 10;+ pointer-events: none;+}++.editor-drop-overlay p {+ font-size: 0.875rem;+ font-weight: 500;+ color: hsl(var(--primary));+}diff --git a/apps/web/features/editor/content-editor.tsx b/apps/web/features/editor/content-editor.tsxindex 3e125115b6..1da31bd424 100644--- a/apps/web/features/editor/content-editor.tsx+++ b/apps/web/features/editor/content-editor.tsx@@ -30,6 +30,7 @@ import {useEffect,useImperativeHandle,useRef,+ useState,} from "react";import { useEditor, EditorContent } from "@tiptap/react";import { cn } from "@/lib/utils";@@ -82,6 +83,7 @@ const ContentEditor = forwardRef<ContentEditorRef, ContentEditorProps>(},ref,) {+ const [dragOver, setDragOver] = useState(false);const debounceRef = useRef<ReturnType<typeof setTimeout>>(undefined);const onUpdateRef = useRef(onUpdate);const onSubmitRef = useRef(onSubmit);@@ -195,7 +197,31 @@ const ContentEditor = forwardRef<ContentEditorRef, ContentEditorProps>(if (!editor) return null;- return <EditorContent editor={editor} />;+ return (+ <div+ className={cn("relative", dragOver && "editor-drag-over")}+ onDragEnter={(e) => {+ e.preventDefault();+ if (editable && e.dataTransfer.types.includes("Files"))+ setDragOver(true);+ }}+ onDragOver={(e) => {+ e.preventDefault();+ }}+ onDragLeave={(e) => {+ if (!e.currentTarget.contains(e.relatedTarget as Node))+ setDragOver(false);+ }}+ onDrop={() => setDragOver(false)}+ >+ <EditorContent editor={editor} />+ {dragOver && (+ <div className="editor-drop-overlay">+ <p>Drop files to upload</p>+ </div>+ )}+ </div>+ );},);diff --git a/apps/web/features/editor/extensions/file-upload.ts b/apps/web/features/editor/extensions/file-upload.tsindex d6469fd4b6..d1e36ebbf4 100644--- a/apps/web/features/editor/extensions/file-upload.ts+++ b/apps/web/features/editor/extensions/file-upload.ts@@ -1,6 +1,8 @@import { Extension } from "@tiptap/core";import { Plugin, PluginKey } from "@tiptap/pm/state";+import { toast } from "sonner";import type { UploadResult } from "@/shared/hooks/use-file-upload";+import { isAllowedFileType } from "@/shared/constants/upload";// eslint-disable-next-line @typescript-eslint/no-explicit-anyfunction removeImageBySrc(editor: any, src: string) {@@ -88,6 +90,10 @@ export function createFileUploadExtension(const handler = onUploadFileRef.current;if (!handler) return false;for (const file of Array.from(files)) {+ if (!isAllowedFileType(file.type, file.name)) {+ toast.error(`Unsupported file type: ${file.name}`);+ continue;+ }await uploadAndInsertFile(editor, file, handler);}return true;diff --git a/apps/web/shared/constants/__tests__/upload.test.ts b/apps/web/shared/constants/__tests__/upload.test.tsindex 389ccbb1b7..4f0cc2b3dc 100644--- a/apps/web/shared/constants/__tests__/upload.test.ts+++ b/apps/web/shared/constants/__tests__/upload.test.ts@@ -52,6 +52,25 @@ describe("upload constants", () => {expect(isAllowedFileType("Text/Plain")).toBe(true);});+ it("rejects empty MIME type without filename", () => {+ expect(isAllowedFileType("")).toBe(false);+ });++ it("falls back to extension when MIME type is empty", () => {+ expect(isAllowedFileType("", "main.go")).toBe(true);+ expect(isAllowedFileType("", "lib.rs")).toBe(true);+ expect(isAllowedFileType("", "config.toml")).toBe(true);+ expect(isAllowedFileType("", "data.yaml")).toBe(true);+ expect(isAllowedFileType("", "script.py")).toBe(true);+ expect(isAllowedFileType("", "notes.md")).toBe(true);+ });++ it("rejects unknown extension even with fallback", () => {+ expect(isAllowedFileType("", "video.mp4")).toBe(false);+ expect(isAllowedFileType("", "archive.zip")).toBe(false);+ expect(isAllowedFileType("", "noext")).toBe(false);+ });+it("exports MAX_FILE_SIZE as 100MB", () => {expect(MAX_FILE_SIZE).toBe(100 * 1024 * 1024);});diff --git a/apps/web/shared/constants/upload.ts b/apps/web/shared/constants/upload.tsindex cf27f73147..ee320dae8d 100644--- a/apps/web/shared/constants/upload.ts+++ b/apps/web/shared/constants/upload.ts@@ -31,46 +31,63 @@ export const ALLOWED_MIME_PATTERNS: readonly string[] = ["application/json",];+/**+ * Extra file extensions for the HTML accept attribute.+ * Needed because browsers don't always map these extensions to MIME types.+ */+const EXTRA_EXTENSIONS = [+ ".md", ".markdown", ".csv", ".json",+ ".ts", ".tsx", ".js", ".jsx",+ ".py", ".go", ".rs", ".rb", ".java",+ ".c", ".cpp", ".h", ".sql",+ ".yaml", ".yml", ".toml", ".xml", ".sh",+ ".ipynb",+];+/*** HTML accept attribute value for <input type="file">.- * Must mirror ALLOWED_MIME_PATTERNS for browser-level filtering.+ * Derived from ALLOWED_MIME_PATTERNS + extra extensions.*/export const FILE_INPUT_ACCEPT = [- "image/png",- "image/jpeg",- "image/gif",- "image/webp",- "image/svg+xml",- "application/pdf",- "text/*",- "application/json",- ".md",- ".markdown",- ".json",- ".csv",- ".ts",- ".tsx",- ".js",- ".jsx",- ".py",- ".go",- ".rs",- ".rb",- ".java",- ".c",- ".cpp",- ".h",- ".sql",- ".yaml",- ".yml",- ".toml",- ".xml",- ".sh",- ".ipynb",+ ...ALLOWED_MIME_PATTERNS,+ ...EXTRA_EXTENSIONS,].join(",");-/** Check if a MIME type matches our allowed patterns. */-export function isAllowedFileType(mimeType: string): boolean {+/**+ * Extension-to-MIME fallback for files where `file.type` is empty.+ * Browsers often report "" for .go, .rs, .toml, .yaml, etc.+ */+const EXTENSION_MIME_MAP: Record<string, string> = {+ ".md": "text/markdown",+ ".markdown": "text/markdown",+ ".txt": "text/plain",+ ".csv": "text/csv",+ ".json": "application/json",+ ".html": "text/html",+ ".htm": "text/html",+ ".xml": "text/xml",+ ".svg": "image/svg+xml",+ ".ts": "text/x-typescript",+ ".tsx": "text/x-typescript",+ ".js": "text/javascript",+ ".jsx": "text/javascript",+ ".py": "text/x-python",+ ".go": "text/x-go",+ ".rs": "text/x-rust",+ ".rb": "text/x-ruby",+ ".java": "text/x-java",+ ".c": "text/x-c",+ ".cpp": "text/x-c++",+ ".h": "text/x-c",+ ".sql": "text/x-sql",+ ".yaml": "text/yaml",+ ".yml": "text/yaml",+ ".toml": "text/x-toml",+ ".sh": "text/x-sh",+ ".ipynb": "application/json",+};++function matchesMimePattern(mimeType: string): boolean {const ct = mimeType.toLowerCase();return ALLOWED_MIME_PATTERNS.some((pattern) => {if (pattern.endsWith("/*")) {@@ -79,3 +96,23 @@ export function isAllowedFileType(mimeType: string): boolean {return ct === pattern;});}++/**+ * Check if a file is an allowed upload type.+ * Uses MIME type when available, falls back to extension for files+ * where the browser reports an empty type (e.g. .go, .rs, .toml).+ */+export function isAllowedFileType(mimeType: string, filename?: string): boolean {+ if (mimeType && matchesMimePattern(mimeType)) return true;++ // Fallback: infer MIME from extension when browser reports empty type+ if (filename) {+ const ext = filename.lastIndexOf(".") >= 0+ ? filename.slice(filename.lastIndexOf(".")).toLowerCase()+ : "";+ const inferred = EXTENSION_MIME_MAP[ext];+ if (inferred && matchesMimePattern(inferred)) return true;+ }++ return false;+}diff --git a/apps/web/shared/hooks/use-file-upload.ts b/apps/web/shared/hooks/use-file-upload.tsindex 0134cc2d9b..c6ef546734 100644--- a/apps/web/shared/hooks/use-file-upload.ts+++ b/apps/web/shared/hooks/use-file-upload.ts@@ -22,7 +22,7 @@ export function useFileUpload() {const upload = useCallback(async (file: File, ctx?: UploadContext): Promise<UploadResult | null> => {- if (!isAllowedFileType(file.type)) {+ if (!isAllowedFileType(file.type, file.name)) {throw new Error(`Unsupported file type: ${file.type || file.name.split(".").pop()}`,);