Skip to content

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

feat(mobile): show image upload placeholders

tianma-ifbec731ffc6e3
diff --git a/apps/mobile/app.json b/apps/mobile/app.json
index c249e573..5f26a446 100644
--- a/apps/mobile/app.json
+++ b/apps/mobile/app.json
@@ -3,7 +3,7 @@
"name": "EdgeEver",
"slug": "edgeever",
"scheme": "edgeever",
- "version": "0.4.15",
+ "version": "0.4.16",
"runtimeVersion": {
"policy": "appVersion"
},
@@ -25,7 +25,7 @@
},
"android": {
"package": "org.edgeever.mobile",
- "versionCode": 21,
+ "versionCode": 22,
"adaptiveIcon": {
"backgroundColor": "#d1fae5",
"backgroundImage": "./assets/icon.png",
diff --git a/apps/mobile/package.json b/apps/mobile/package.json
index 32e5bfd5..069a21a8 100644
--- a/apps/mobile/package.json
+++ b/apps/mobile/package.json
@@ -1,6 +1,6 @@
{
"name": "@edgeever/mobile",
- "version": "0.4.15",
+ "version": "0.4.16",
"private": true,
"type": "module",
"main": "expo-router/entry",
diff --git a/apps/mobile/src/components/LocalTiptapEditor.tsx b/apps/mobile/src/components/LocalTiptapEditor.tsx
index ea59f508..f9df845c 100644
--- a/apps/mobile/src/components/LocalTiptapEditor.tsx
+++ b/apps/mobile/src/components/LocalTiptapEditor.tsx
@@ -16,6 +16,11 @@ import {
} from "@edgeever/shared/mobile-editor";
import { useDOMImperativeHandle, type DOMImperativeFactory, type DOMProps } from "expo/dom";
import { useCallback, useEffect, useMemo, useRef, type ReactNode, type Ref } from "react";
+import {
+ createMobileImageUploadPlaceholderSource,
+ isMobileImageUploadPlaceholderSource,
+ stripMobileImageUploadPlaceholders,
+} from "../lib/mobile-image-upload-placeholder";
type EditorDoc = TiptapDoc;
@@ -49,11 +54,14 @@ type LocalTiptapEditorProps = {
};
const CHANGE_IDLE_MS = 500;
+const TRANSIENT_IMAGE_UPLOAD_META = "edgeeverImageUploadPlaceholder";
const ignoreSearchResult = async () => undefined;
export default function LocalTiptapEditor(props: LocalTiptapEditorProps) {
const startedAtRef = useRef(performance.now());
const changeTimerRef = useRef<number | null>(null);
+ const imageUploadInFlightRef = useRef(false);
+ const imageUploadSequenceRef = useRef(0);
const onChangeRef = useRef(props.onChange);
const onLoadResourceRef = useRef(props.onLoadResource);
const onPickImageRef = useRef(props.onPickImage);
@@ -66,8 +74,8 @@ export default function LocalTiptapEditor(props: LocalTiptapEditorProps) {
onReadyRef.current = props.onReady;
onSearchResultRef.current = props.onSearchResult ?? ignoreSearchResult;
const protectedImageExtension = useMemo(
- () => createProtectedImageExtension(props.baseUrl, (source) => onLoadResourceRef.current(source)),
- [props.baseUrl]
+ () => createProtectedImageExtension(props.baseUrl, props.locale, (source) => onLoadResourceRef.current(source)),
+ [props.baseUrl, props.locale]
);
const editor = useEditor({
@@ -83,13 +91,16 @@ export default function LocalTiptapEditor(props: LocalTiptapEditorProps) {
editorProps: {
attributes: getMobileEditorInputAttributes("edgeever-editor-content"),
},
- onUpdate: ({ editor: activeEditor }) => {
+ onUpdate: ({ editor: activeEditor, transaction }) => {
+ if (transaction.getMeta(TRANSIENT_IMAGE_UPLOAD_META)) {
+ return;
+ }
if (changeTimerRef.current !== null) {
window.clearTimeout(changeTimerRef.current);
}
changeTimerRef.current = window.setTimeout(() => {
changeTimerRef.current = null;
- void onChangeRef.current(normalizeImageSources(activeEditor.getJSON() as EditorDoc, props.baseUrl));
+ void onChangeRef.current(getPersistableEditorDoc(activeEditor.getJSON() as EditorDoc, props.baseUrl));
}, CHANGE_IDLE_MS);
},
});
@@ -102,7 +113,7 @@ export default function LocalTiptapEditor(props: LocalTiptapEditorProps) {
window.clearTimeout(changeTimerRef.current);
changeTimerRef.current = null;
}
- void onChangeRef.current(normalizeImageSources(editor.getJSON() as EditorDoc, props.baseUrl));
+ void onChangeRef.current(getPersistableEditorDoc(editor.getJSON() as EditorDoc, props.baseUrl));
}, [editor, props.baseUrl]);
const search = useCallback((query: DOMValue, requestedIndex: DOMValue) => {
@@ -202,12 +213,38 @@ export default function LocalTiptapEditor(props: LocalTiptapEditorProps) {
});
const insertImage = async () => {
- if (!editor) {
+ if (!editor || imageUploadInFlightRef.current) {
return;
}
- const image = await onPickImageRef.current();
- if (image) {
- editor.chain().focus().setImage({ alt: image.alt, src: resolveUrl(image.url, props.baseUrl) }).run();
+
+ imageUploadInFlightRef.current = true;
+ imageUploadSequenceRef.current += 1;
+ const placeholderSource = createMobileImageUploadPlaceholderSource(
+ `${Date.now()}-${imageUploadSequenceRef.current}`
+ );
+ insertImageUploadPlaceholder(
+ editor,
+ placeholderSource,
+ props.locale === "en-US" ? "Uploading image…" : "图片上传中…"
+ );
+
+ try {
+ const image = await onPickImageRef.current();
+ if (image) {
+ replaceImageUploadPlaceholder(
+ editor,
+ placeholderSource,
+ resolveUrl(image.url, props.baseUrl),
+ image.alt
+ );
+ } else {
+ removeImageUploadPlaceholder(editor, placeholderSource);
+ }
+ } catch (error) {
+ removeImageUploadPlaceholder(editor, placeholderSource);
+ throw error;
+ } finally {
+ imageUploadInFlightRef.current = false;
}
};
@@ -367,6 +404,9 @@ const normalizeImageSources = (doc: EditorDoc, baseUrl: string) => {
return mapImageSources(doc, (source) => source.startsWith(`${normalizedBaseUrl}/`) ? source.slice(normalizedBaseUrl.length) : source);
};
+const getPersistableEditorDoc = (doc: EditorDoc, baseUrl: string) =>
+ normalizeImageSources(stripMobileImageUploadPlaceholders(doc), baseUrl);
+
const normalizeProtectedResourceSource = (source: string, baseUrl: string) => {
const normalizedBaseUrl = baseUrl.replace(/\/+$/, "");
const relativeSource = source.startsWith(`${normalizedBaseUrl}/`) ? source.slice(normalizedBaseUrl.length) : source;
@@ -380,9 +420,31 @@ const resolveUrl = (source: string, baseUrl: string) => {
return `${baseUrl.replace(/\/+$/, "")}${source}`;
};
-const createProtectedImageExtension = (baseUrl: string, loadResource: (source: string) => Promise<string | null>) => Image.extend({
+const createProtectedImageExtension = (
+ baseUrl: string,
+ locale: "zh-CN" | "en-US",
+ loadResource: (source: string) => Promise<string | null>
+) => Image.extend({
addNodeView() {
return ({ node }) => {
+ if (isMobileImageUploadPlaceholderSource(node.attrs.src)) {
+ const placeholder = document.createElement("div");
+ placeholder.className = "edgeever-image-upload-placeholder";
+ placeholder.contentEditable = "false";
+ placeholder.setAttribute("role", "status");
+ placeholder.setAttribute("aria-live", "polite");
+
+ const spinner = document.createElement("span");
+ spinner.className = "edgeever-image-upload-spinner";
+ spinner.setAttribute("aria-hidden", "true");
+ placeholder.append(spinner, locale === "en-US" ? "Uploading image…" : "图片上传中…");
+
+ return {
+ dom: placeholder,
+ update: (updatedNode) => isMobileImageUploadPlaceholderSource(updatedNode.attrs.src),
+ };
+ }
+
const image = document.createElement("img");
const imageType = node.type;
let requestId = 0;
@@ -444,6 +506,70 @@ const createProtectedImageExtension = (baseUrl: string, loadResource: (source: s
inline: false,
});
+type TiptapEditor = NonNullable<ReturnType<typeof useEditor>>;
+type ImageUploadPlaceholderMatch = { nodeSize: number; pos: number };
+
+const findImageUploadPlaceholder = (
+ editor: TiptapEditor,
+ source: string
+): ImageUploadPlaceholderMatch | null => {
+ let match: ImageUploadPlaceholderMatch | null = null;
+ editor.state.doc.descendants((node, pos) => {
+ if (node.type.name === "image" && node.attrs.src === source) {
+ match = { nodeSize: node.nodeSize, pos };
+ return false;
+ }
+ });
+ return match as ImageUploadPlaceholderMatch | null;
+};
+
+const insertImageUploadPlaceholder = (editor: TiptapEditor, source: string, alt: string) => {
+ const imageType = editor.schema.nodes.image;
+ if (!imageType) {
+ return;
+ }
+ editor.chain().focus().command(({ tr, dispatch }) => {
+ tr.replaceSelectionWith(imageType.create({ alt, src: source }));
+ tr.setMeta(TRANSIENT_IMAGE_UPLOAD_META, true);
+ dispatch?.(tr);
+ return true;
+ }).run();
+};
+
+const replaceImageUploadPlaceholder = (
+ editor: TiptapEditor,
+ placeholderSource: string,
+ imageSource: string,
+ alt: string
+) => {
+ const match = findImageUploadPlaceholder(editor, placeholderSource);
+ if (!match) {
+ return;
+ }
+ editor.chain().command(({ tr, dispatch }) => {
+ const node = tr.doc.nodeAt(match.pos);
+ if (!node) {
+ return false;
+ }
+ tr.setNodeMarkup(match.pos, node.type, { ...node.attrs, alt, src: imageSource });
+ dispatch?.(tr);
+ return true;
+ }).run();
+};
+
+const removeImageUploadPlaceholder = (editor: TiptapEditor, source: string) => {
+ const match = findImageUploadPlaceholder(editor, source);
+ if (!match) {
+ return;
+ }
+ editor.chain().command(({ tr, dispatch }) => {
+ tr.delete(match.pos, match.pos + match.nodeSize);
+ tr.setMeta(TRANSIENT_IMAGE_UPLOAD_META, true);
+ dispatch?.(tr);
+ return true;
+ }).run();
+};
+
const getEditorStyles = (theme: "light" | "dark") => `
:root { color-scheme: ${theme}; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; }
* { box-sizing: border-box; }
@@ -465,5 +591,8 @@ const getEditorStyles = (theme: "light" | "dark") => `
.edgeever-editor-content code { border-radius: 4px; padding: 2px 4px; background: ${theme === "dark" ? "#1e293b" : "#f1f5f9"}; }
.edgeever-editor-content pre code { padding: 0; background: transparent; }
.edgeever-editor-content img { display: block; max-width: 100%; height: auto; margin: 14px auto; border-radius: 10px; }
+ .edgeever-image-upload-placeholder { display: flex; min-height: 112px; margin: 14px 0; align-items: center; justify-content: center; gap: 10px; border: 1px dashed ${theme === "dark" ? "#475569" : "#cbd5e1"}; border-radius: 10px; background: ${theme === "dark" ? "#1e293b" : "#f8fafc"}; color: ${theme === "dark" ? "#cbd5e1" : "#64748b"}; font-size: 14px; }
+ .edgeever-image-upload-spinner { width: 18px; height: 18px; border: 2px solid ${theme === "dark" ? "#475569" : "#cbd5e1"}; border-top-color: #0f766e; border-radius: 999px; animation: edgeever-image-upload-spin 0.8s linear infinite; }
+ @keyframes edgeever-image-upload-spin { to { transform: rotate(360deg); } }
.edgeever-editor-content hr { margin: 24px 0; border: 0; border-top: 1px solid #cbd5e1; }
`;
diff --git a/apps/mobile/src/lib/mobile-image-upload-placeholder.test.ts b/apps/mobile/src/lib/mobile-image-upload-placeholder.test.ts
new file mode 100644
index 00000000..a5b0a710
--- /dev/null
+++ b/apps/mobile/src/lib/mobile-image-upload-placeholder.test.ts
@@ -0,0 +1,38 @@
+import { expect, test } from "bun:test";
+import {
+ createMobileImageUploadPlaceholderSource,
+ stripMobileImageUploadPlaceholders,
+} from "./mobile-image-upload-placeholder";
+
+test("removes transient image upload placeholders before a note is persisted", () => {
+ const placeholderSource = createMobileImageUploadPlaceholderSource("upload-1");
+ const result = stripMobileImageUploadPlaceholders({
+ type: "doc",
+ content: [
+ { type: "paragraph", content: [{ type: "text", text: "before" }] },
+ { type: "image", attrs: { alt: "图片上传中…", src: placeholderSource } },
+ { type: "image", attrs: { alt: "done", src: "/api/v1/resources/res_1" } },
+ ],
+ });
+
+ expect(result).toEqual({
+ type: "doc",
+ content: [
+ { type: "paragraph", content: [{ type: "text", text: "before" }] },
+ { type: "image", attrs: { alt: "done", src: "/api/v1/resources/res_1" } },
+ ],
+ });
+});
+
+test("keeps an empty editor valid when its only node is an upload placeholder", () => {
+ expect(stripMobileImageUploadPlaceholders({
+ type: "doc",
+ content: [{
+ type: "image",
+ attrs: { src: createMobileImageUploadPlaceholderSource("upload-2") },
+ }],
+ })).toEqual({
+ type: "doc",
+ content: [{ type: "paragraph" }],
+ });
+});
diff --git a/apps/mobile/src/lib/mobile-image-upload-placeholder.ts b/apps/mobile/src/lib/mobile-image-upload-placeholder.ts
new file mode 100644
index 00000000..c603309b
--- /dev/null
+++ b/apps/mobile/src/lib/mobile-image-upload-placeholder.ts
@@ -0,0 +1,28 @@
+import type { TiptapDoc, TiptapNode } from "@edgeever/shared";
+
+const IMAGE_UPLOAD_PLACEHOLDER_PREFIX = "edgeever-image-upload://";
+
+export const createMobileImageUploadPlaceholderSource = (id: string) =>
+ `${IMAGE_UPLOAD_PLACEHOLDER_PREFIX}${id}`;
+
+export const isMobileImageUploadPlaceholderSource = (source: unknown) =>
+ typeof source === "string" && source.startsWith(IMAGE_UPLOAD_PLACEHOLDER_PREFIX);
+
+export const stripMobileImageUploadPlaceholders = (doc: TiptapDoc): TiptapDoc => {
+ const stripNodes = (nodes: TiptapNode[]): TiptapNode[] => nodes.flatMap((node) => {
+ if (node.type === "image" && isMobileImageUploadPlaceholderSource(node.attrs?.src)) {
+ return [];
+ }
+
+ return [{
+ ...node,
+ ...(node.content ? { content: stripNodes(node.content as TiptapNode[]) } : {}),
+ }];
+ });
+
+ const content = stripNodes(doc.content);
+ return {
+ ...doc,
+ content: content.length > 0 ? content : [{ type: "paragraph" }],
+ };
+};
diff --git a/bun.lock b/bun.lock
index 68cb01c6..b999beef 100644
--- a/bun.lock
+++ b/bun.lock
@@ -71,7 +71,7 @@
},
"apps/extension": {
"name": "@edgeever/extension",
- "version": "0.1.0",
+ "version": "0.1.1",
"dependencies": {
"@mozilla/readability": "^0.6.0",
"turndown": "^7.2.4",
@@ -79,7 +79,7 @@
},
"apps/mobile": {
"name": "@edgeever/mobile",
- "version": "0.4.15",
+ "version": "0.4.16",
"dependencies": {
"@edgeever/client": "workspace:*",
"@edgeever/shared": "workspace:*",