Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

Add mobile Tiptap image upload

yoma35d2bc9f9012
diff --git a/apps/web/src/mobile-edit.tsx b/apps/web/src/mobile-edit.tsx
index 22ded351..59979a12 100644
--- a/apps/web/src/mobile-edit.tsx
+++ b/apps/web/src/mobile-edit.tsx
@@ -4,7 +4,7 @@ import { EditorContent, useEditor } from "@tiptap/react";
import StarterKit from "@tiptap/starter-kit";
import Image from "@tiptap/extension-image";
import Placeholder from "@tiptap/extension-placeholder";
-import { docToMarkdown, emptyDoc, markdownToDoc, type MemoDetail, type TiptapDoc } from "@edgeever/shared";
+import { docToMarkdown, emptyDoc, markdownToDoc, type MemoDetail, type Resource, type TiptapDoc } from "@edgeever/shared";
import "./styles/mobile-markdown-editor.css";
const AUTO_SAVE_DELAY_MS = 1200;
@@ -17,6 +17,10 @@ type MemoResponse = {
memo: MemoDetail;
};
+type ResourceResponse = {
+ resource: Resource;
+};
+
type MobileDraft = {
title: string;
tagsText: string;
@@ -24,7 +28,7 @@ type MobileDraft = {
updatedAt: string;
};
-type SaveState = "loading" | "idle" | "dirty" | "saving" | "saved" | "error" | "local-draft" | "leaving";
+type SaveState = "loading" | "idle" | "dirty" | "saving" | "saved" | "uploading" | "error" | "local-draft" | "leaving";
const getParams = () => new URLSearchParams(window.location.hash ? window.location.hash.slice(1) : window.location.search);
@@ -38,8 +42,9 @@ const safeReturnPath = (value: string | null) => (value?.startsWith("/") ? value
const requestJson = async <T,>(path: string, init?: RequestInit): Promise<T> => {
const headers = new Headers(init?.headers);
+ const isFormData = init?.body instanceof FormData;
- if (!headers.has("Content-Type")) {
+ if (!isFormData && !headers.has("Content-Type")) {
headers.set("Content-Type", "application/json");
}
@@ -61,6 +66,16 @@ const requestJson = async <T,>(path: string, init?: RequestInit): Promise<T> =>
return response.json() as Promise<T>;
};
+const uploadResource = async (memoId: string, file: File) => {
+ const form = new FormData();
+ form.append("file", file);
+
+ return requestJson<ResourceResponse>(`/api/v1/memos/${encodeURIComponent(memoId)}/resources`, {
+ method: "POST",
+ body: form,
+ });
+};
+
const normalizeDoc = (memo: MemoDetail): TiptapDoc => {
if (memo.contentJson && typeof memo.contentJson === "object") {
return memo.contentJson as TiptapDoc;
@@ -95,6 +110,7 @@ const MobileTiptapEditorApp = () => {
const initialFocusTimerRef = useRef<number | null>(null);
const currentSavePromiseRef = useRef<Promise<boolean> | null>(null);
const lastSavedSnapshotRef = useRef("");
+ const imageInputRef = useRef<HTMLInputElement | null>(null);
const setSaveStateStable = useCallback((nextState: SaveState) => {
if (saveStateRef.current === nextState) {
@@ -319,6 +335,32 @@ const MobileTiptapEditorApp = () => {
scheduleMetadataSave();
};
+ const handleImageUpload = async (file?: File | null) => {
+ const currentMemo = memoRef.current;
+ if (!currentMemo || !editor || !file) {
+ return;
+ }
+
+ setError(null);
+ setSaveStateStable("uploading");
+
+ try {
+ const { resource } = await uploadResource(currentMemo.id, file);
+ editor
+ .chain()
+ .focus()
+ .setImage({
+ src: resource.url,
+ alt: resource.filename || file.name,
+ title: resource.filename || file.name,
+ })
+ .run();
+ } catch (uploadError) {
+ setError(uploadError instanceof Error ? uploadError.message : "图片上传失败");
+ setSaveStateStable("error");
+ }
+ };
+
const focusEditorAfterLoad = useCallback(() => {
if (!editor) {
return;
@@ -457,20 +499,26 @@ const MobileTiptapEditorApp = () => {
? "加载中"
: saveState === "saving"
? "保存中"
- : saveState === "dirty"
- ? "未保存"
- : saveState === "saved"
- ? "已保存"
- : saveState === "local-draft"
- ? "本地草稿"
- : saveState === "leaving"
- ? "返回中"
- : saveState === "error"
- ? "保存失败"
- : "已保存";
+ : saveState === "uploading"
+ ? "上传中"
+ : saveState === "dirty"
+ ? "未保存"
+ : saveState === "saved"
+ ? "已保存"
+ : saveState === "local-draft"
+ ? "本地草稿"
+ : saveState === "leaving"
+ ? "返回中"
+ : saveState === "error"
+ ? "保存失败"
+ : "已保存";
const statusClassName =
- saveState === "error" ? "error" : saveState === "dirty" || saveState === "saving" || saveState === "leaving" ? "active" : "";
+ saveState === "error"
+ ? "error"
+ : saveState === "dirty" || saveState === "saving" || saveState === "uploading" || saveState === "leaving"
+ ? "active"
+ : "";
const fallbackMarkdown = memo ? docToMarkdown(contentJsonRef.current) : "";
@@ -509,6 +557,23 @@ const MobileTiptapEditorApp = () => {
onChange={(event) => handleTagsChange(event.target.value)}
/>
+ <div className="mobile-editor-tool-row">
+ <button type="button" disabled={!memo || saveState === "loading" || saveState === "uploading"} onClick={() => imageInputRef.current?.click()}>
+ 图片
+ </button>
+ </div>
+ <input
+ ref={imageInputRef}
+ type="file"
+ accept="image/png,image/jpeg,image/gif,image/webp,image/avif"
+ hidden
+ onChange={(event) => {
+ const file = event.target.files?.[0];
+ event.target.value = "";
+ void handleImageUpload(file);
+ }}
+ />
+
<div className="edgeever-mobile-tiptap-editor">
<EditorContent editor={editor} />
</div>
diff --git a/apps/web/src/styles/mobile-markdown-editor.css b/apps/web/src/styles/mobile-markdown-editor.css
index aeed635c..34c0e5b5 100644
--- a/apps/web/src/styles/mobile-markdown-editor.css
+++ b/apps/web/src/styles/mobile-markdown-editor.css
@@ -48,7 +48,8 @@ input {
}
.mobile-editor-back,
-.mobile-editor-done {
+.mobile-editor-done,
+.mobile-editor-tool-row button {
border: 0;
border-radius: 999px;
background: transparent;
@@ -79,7 +80,8 @@ input {
color: #fff;
}
-.mobile-editor-done:disabled {
+.mobile-editor-done:disabled,
+.mobile-editor-tool-row button:disabled {
background: #e2e8f0;
color: #64748b;
}
@@ -148,6 +150,21 @@ input {
color: #94a3b8;
}
+.mobile-editor-tool-row {
+ display: flex;
+ align-items: center;
+ gap: 8px;
+}
+
+.mobile-editor-tool-row button {
+ border: 1px solid #cbd5e1;
+ background: #fff;
+ color: #334155;
+ font-size: 14px;
+ min-height: 34px;
+ padding: 6px 12px;
+}
+
.edgeever-mobile-tiptap-editor {
min-height: calc(100dvh - 14rem);
border-top: 1px solid #f1f5f9;