From 9a928c286861958f0a08f2c2d173d073972afa7f Mon Sep 17 00:00:00 2001 From: Johnny Date: Mon, 10 Aug 2026 22:53:07 +0800 Subject: [PATCH] feat: insert attachment images into memo content (#6172) --- .../MemoActionMenu/MemoShareImagePreview.tsx | 7 +- .../memoShareImagePreviewModel.ts | 6 +- .../MemoContent/MemoMarkdownRenderer.tsx | 7 +- web/src/components/MemoContent/index.tsx | 3 +- web/src/components/MemoContent/types.ts | 3 + .../MemoEditor/Editor/controller.ts | 39 ++- .../components/MemoEditor/Editor/editor.css | 83 ++++++ .../MemoEditor/Editor/extensions.ts | 13 +- .../components/MemoEditor/Editor/index.tsx | 4 +- .../MemoEditor/Editor/uploadAnchors.ts | 158 +++++++++++ .../MemoEditor/Toolbar/EditorToolbar.tsx | 28 +- .../MemoEditor/Toolbar/InsertMenu.tsx | 65 +++-- .../MemoEditor/components/EditorContent.tsx | 16 +- .../MemoEditor/components/EditorMetadata.tsx | 13 +- web/src/components/MemoEditor/hooks/index.ts | 3 +- .../MemoEditor/hooks/useAutoSave.ts | 52 ++-- .../MemoEditor/hooks/useFileUpload.ts | 4 +- .../MemoEditor/hooks/useInlineImageUpload.ts | 235 ++++++++++++++++ .../MemoEditor/hooks/useMemoInit.ts | 9 +- .../MemoEditor/hooks/useMemoSave.ts | 4 +- web/src/components/MemoEditor/index.tsx | 49 +++- .../MemoEditor/services/cacheService.ts | 65 ++++- .../MemoEditor/services/uploadService.ts | 27 +- .../MemoEditor/services/validationService.ts | 26 +- .../components/MemoEditor/state/actions.ts | 5 + .../components/MemoEditor/state/reducer.ts | 9 + web/src/components/MemoEditor/state/types.ts | 3 + .../components/MemoEditor/types/components.ts | 9 + .../MemoEditor/types/editorController.ts | 5 + .../Attachment/AttachmentListEditor.tsx | 104 ++++++- .../components/MemoPreview/MemoPreview.tsx | 11 +- .../MemoView/components/MemoBody.tsx | 10 +- .../PagedMemoList/memoCardHeight.ts | 3 +- web/src/contexts/InstanceContext.tsx | 5 + web/src/locales/en.json | 18 ++ web/src/locales/zh-Hans.json | 18 ++ web/src/locales/zh-Hant.json | 18 ++ web/src/utils/managed-attachment.ts | 255 ++++++++++++++++++ .../attachment-inline-placement.test.tsx | 108 ++++++++ .../editor-inline-image-validation.test.ts | 23 ++ web/tests/editor-upload-anchor.test.ts | 73 +++++ web/tests/inline-image-upload-files.test.ts | 42 +++ web/tests/insert-menu.test.tsx | 99 +++++++ web/tests/managed-attachment.test.ts | 158 +++++++++++ web/tests/memo-card-height.test.ts | 18 +- web/tests/memo-editor-cache.test.ts | 36 +++ web/tests/memo-editor-extensions.test.ts | 7 +- web/tests/memo-share-image.test.ts | 16 +- web/tests/use-auto-save.test.tsx | 55 +++- web/tests/use-inline-image-upload.test.tsx | 120 +++++++++ web/tests/use-memo-init.test.tsx | 62 +++++ 51 files changed, 2069 insertions(+), 140 deletions(-) create mode 100644 web/src/components/MemoEditor/Editor/uploadAnchors.ts create mode 100644 web/src/components/MemoEditor/hooks/useInlineImageUpload.ts create mode 100644 web/src/utils/managed-attachment.ts create mode 100644 web/tests/attachment-inline-placement.test.tsx create mode 100644 web/tests/editor-inline-image-validation.test.ts create mode 100644 web/tests/editor-upload-anchor.test.ts create mode 100644 web/tests/inline-image-upload-files.test.ts create mode 100644 web/tests/insert-menu.test.tsx create mode 100644 web/tests/managed-attachment.test.ts create mode 100644 web/tests/use-inline-image-upload.test.tsx create mode 100644 web/tests/use-memo-init.test.tsx diff --git a/web/src/components/MemoActionMenu/MemoShareImagePreview.tsx b/web/src/components/MemoActionMenu/MemoShareImagePreview.tsx index b29acc1e..09913345 100644 --- a/web/src/components/MemoActionMenu/MemoShareImagePreview.tsx +++ b/web/src/components/MemoActionMenu/MemoShareImagePreview.tsx @@ -39,7 +39,12 @@ const MemoShareImagePreview = forwardRef(({ w
- +
diff --git a/web/src/components/MemoActionMenu/memoShareImagePreviewModel.ts b/web/src/components/MemoActionMenu/memoShareImagePreviewModel.ts index 5d5b2c98..73391d9e 100644 --- a/web/src/components/MemoActionMenu/memoShareImagePreviewModel.ts +++ b/web/src/components/MemoActionMenu/memoShareImagePreviewModel.ts @@ -2,6 +2,7 @@ import { timestampDate } from "@bufbuild/protobuf/wkt"; import { separateAttachments } from "@/components/MemoMetadata/Attachment/attachmentHelpers"; import type { Memo } from "@/types/proto/api/v1/memo_service_pb"; import type { User } from "@/types/proto/api/v1/user_service_pb"; +import { filterInlineManagedAttachments } from "@/utils/managed-attachment"; import { type AttachmentVisualItem, buildAttachmentVisualItems, countLogicalAttachmentItems } from "@/utils/media-item"; import { getMemoSharePreviewAvatarUrl } from "./memoShareImage"; @@ -41,9 +42,10 @@ export const buildMemoShareImagePreviewModel = ({ timeStyle: "short", }); - const attachmentGroups = separateAttachments(memo.attachments); + const attachmentOnlyItems = filterInlineManagedAttachments(memo.content, memo.attachments); + const attachmentGroups = separateAttachments(attachmentOnlyItems); const visualItems = buildAttachmentVisualItems(attachmentGroups.visual); - const attachmentCount = countLogicalAttachmentItems(memo.attachments); + const attachmentCount = countLogicalAttachmentItems(attachmentOnlyItems); const nonVisualAttachmentCount = Math.max(attachmentCount - visualItems.length, 0); const footerBadges: MemoShareImageFooterBadge[] = nonVisualAttachmentCount > 0 ? [{ type: "attachment-summary", count: attachmentCount }] : []; diff --git a/web/src/components/MemoContent/MemoMarkdownRenderer.tsx b/web/src/components/MemoContent/MemoMarkdownRenderer.tsx index 18989e70..7159710e 100644 --- a/web/src/components/MemoContent/MemoMarkdownRenderer.tsx +++ b/web/src/components/MemoContent/MemoMarkdownRenderer.tsx @@ -4,7 +4,9 @@ import type { Components } from "react-markdown"; import ReactMarkdown from "react-markdown"; import { buildRehypePlugins, buildRemarkPlugins } from "@/components/MemoContent/pipeline"; import { isMentionElement, isTagElement, isTaskListItemElement } from "@/types/markdown"; +import type { Attachment } from "@/types/proto/api/v1/attachment_service_pb"; import { lazyWithReload } from "@/utils/lazy"; +import { resolveManagedAttachmentImageSource } from "@/utils/managed-attachment"; import { CodeBlock } from "./CodeBlock"; import { MarkdownRenderContext, rootMarkdownRenderContext } from "./MarkdownRenderContext"; import { Mention } from "./Mention"; @@ -17,6 +19,7 @@ import { TrustedIframe } from "./TrustedIframe"; export interface MemoMarkdownRendererProps { content: string; + attachments?: Attachment[]; resolvedMentionUsernames: Set; /** Resource name of the memo (e.g. `memos/abc123`), used to target footnote links at the detail page. */ memoName?: string; @@ -56,6 +59,7 @@ function getMentionUsername(node: Element, children?: ReactNode): string { export const MemoMarkdownRendererCore = ({ content, + attachments = [], resolvedMentionUsernames, memoName, compact, @@ -137,7 +141,7 @@ export const MemoMarkdownRendererCore = ({ }, code: ({ children, ...props }) => {children}, iframe: TrustedIframe, - img: (props) => , + img: ({ src, ...props }) => , pre: CodeBlock, table: ({ children, ...props }) => {children}
, thead: ({ children, ...props }) => {children}, @@ -182,6 +186,7 @@ export const MemoMarkdownRenderer = memo( MemoMarkdownRendererComponent, (previous, next) => previous.content === next.content && + previous.attachments === next.attachments && previous.memoName === next.memoName && previous.compact === next.compact && haveEqualResolvedMentions(previous.resolvedMentionUsernames, next.resolvedMentionUsernames), diff --git a/web/src/components/MemoContent/index.tsx b/web/src/components/MemoContent/index.tsx index 9fa7062c..177c73e6 100644 --- a/web/src/components/MemoContent/index.tsx +++ b/web/src/components/MemoContent/index.tsx @@ -9,7 +9,7 @@ import type { MemoContentProps } from "./types"; // the renderer (e.g. footnote links navigate to the detail page instead of scrolling, // since a collapsed card may hide the target). const MemoContent = (props: MemoContentProps) => { - const { className, contentClassName, content, onClick, onDoubleClick } = props; + const { className, contentClassName, content, attachments, onClick, onDoubleClick } = props; const resolvedMentionUsernames = useResolvedMentionUsernames(content); return ( @@ -36,6 +36,7 @@ const MemoContent = (props: MemoContentProps) => { > state.doc.toString().trim() === ""; @@ -22,14 +29,38 @@ export function createController(view: EditorView, formatting: FormattingControl }, insertMarkdown: (markdown) => { if (!markdown) return; - const { from, to } = view.state.selection.main; + // Insert at the caret without consuming the selection: callers live outside + // the editor (attachment list, audio recorder), and CodeMirror keeps its + // selection across blur, so replacing the range would delete text the user + // merely had highlighted. + const { head } = view.state.selection.main; const doc = view.state.doc.toString(); - const { prefix, suffix } = blockPad(doc.slice(0, from), doc.slice(to)); + const { prefix, suffix } = blockPad(doc.slice(0, head), doc.slice(head)); const insert = prefix + markdown + suffix; - const caret = from + insert.length; - view.dispatch({ changes: { from, to, insert }, selection: { anchor: caret }, scrollIntoView: true }); + view.dispatch({ changes: { from: head, insert }, selection: { anchor: head + insert.length }, scrollIntoView: true }); view.focus(); }, + createUploadAnchor: (descriptor, position) => createUploadAnchor(view, descriptor, position), + updateUploadAnchor: (descriptor) => setUploadAnchor(view, descriptor), + resolveUploadAnchor: (id, markdown) => { + const position = getUploadAnchorPosition(view.state, id); + if (position === undefined) return; + if (!markdown) { + cancelUploadAnchor(view, id); + return; + } + const doc = view.state.doc.toString(); + const { prefix, suffix } = blockPad(doc.slice(0, position), doc.slice(position)); + const insert = prefix + markdown + suffix; + view.dispatch({ + changes: { from: position, insert }, + effects: removeUploadAnchorEffect(id), + selection: { anchor: position + insert.length }, + scrollIntoView: true, + }); + view.focus(); + }, + cancelUploadAnchor: (id) => cancelUploadAnchor(view, id), getCursor: () => view.state.selection.main.head, setCursor: (position: number) => { const cursor = Math.min(Math.max(position, 0), view.state.doc.length); diff --git a/web/src/components/MemoEditor/Editor/editor.css b/web/src/components/MemoEditor/Editor/editor.css index de71c2ce..27de1845 100644 --- a/web/src/components/MemoEditor/Editor/editor.css +++ b/web/src/components/MemoEditor/Editor/editor.css @@ -127,3 +127,86 @@ color: inherit; } } +.cm-upload-anchor { + position: relative; + display: flex; + min-height: 2.25rem; + align-items: center; + gap: 0.375rem; + margin: 0.375rem 0; + padding-left: 1rem; + font-family: inherit; + font-size: 0.6875rem; +} + +.cm-upload-anchor-rail { + position: absolute; + inset-block: 0; + left: 0; + width: 2px; + border-radius: 999px; + background: var(--primary); +} + +.cm-upload-anchor-rail::after { + position: absolute; + top: 50%; + left: -3px; + width: 8px; + height: 8px; + border: 2px solid var(--card); + border-radius: 999px; + background: var(--primary); + content: ""; + transform: translateY(-50%); +} + +.cm-upload-anchor-badge { + display: inline-flex; + align-items: center; + gap: 0.375rem; + border-radius: calc(var(--radius) - 2px); + background: color-mix(in oklab, var(--primary) 8%, transparent); + padding: 0.25rem 0.5rem; + color: var(--primary); + font-weight: 500; +} + +.cm-upload-anchor[data-status="failed"] .cm-upload-anchor-badge { + background: color-mix(in oklab, var(--destructive) 8%, transparent); + color: var(--destructive); +} + +.cm-upload-anchor-spinner { + width: 0.75rem; + height: 0.75rem; + border: 1.5px solid currentColor; + border-right-color: transparent; + border-radius: 999px; + animation: spin 1s linear infinite; +} + +.cm-upload-anchor-actions { + display: inline-flex; + gap: 0.25rem; +} + +.cm-upload-anchor-action { + border-radius: calc(var(--radius) - 2px); + padding: 0.25rem 0.5rem; + color: var(--muted-foreground); + font-weight: 500; +} + +.cm-upload-anchor-action:hover, +.cm-upload-anchor-action:focus-visible { + background: var(--accent); + color: var(--accent-foreground); + outline: none; +} + +@media (prefers-reduced-motion: reduce) { + .cm-upload-anchor-spinner { + animation: none; + } +} diff --git a/web/src/components/MemoEditor/Editor/extensions.ts b/web/src/components/MemoEditor/Editor/extensions.ts index 5e098dfa..e3077d53 100644 --- a/web/src/components/MemoEditor/Editor/extensions.ts +++ b/web/src/components/MemoEditor/Editor/extensions.ts @@ -9,6 +9,7 @@ import { liftListItem, sinkListItem } from "./listIndent"; import { tagAutocomplete } from "./tagAutocomplete"; import { tagMentionDecorations } from "./tagMentionDecorations"; import { memoEditorTheme } from "./theme"; +import { uploadAnchorField } from "./uploadAnchors"; // Key bindings layered below the autocomplete keymap so the completion popup's // own Tab/Escape win while it is open. On a list item, Tab/Shift-Tab nest / @@ -30,7 +31,7 @@ const editorKeys: KeyBinding[] = [ export interface EditorExtensionsOptions { placeholder: string; onChange: (markdown: string) => void; - onFiles: (files: File[]) => void; + onFiles: (files: File[], position: number) => void; onUpdate: () => void; onSubmit: () => void; getTags: () => string[]; @@ -88,21 +89,23 @@ export function buildEditorExtensions({ EditorView.contentAttributes.of({ autocorrect: "on" }), placeholderCompartment.of(cmPlaceholder(placeholder)), EditorView.domEventHandlers({ - paste: (event) => { + paste: (event, view) => { const files = clipboardFiles(event); if (files.length === 0) return false; - onFiles(files); + onFiles(files, view.state.selection.main.head); return true; }, - drop: (event) => { + drop: (event, view) => { const files = Array.from(event.dataTransfer?.files ?? []); if (files.length === 0) return false; - onFiles(files); + const position = view.posAtCoords({ x: event.clientX, y: event.clientY }) ?? view.state.selection.main.head; + onFiles(files, position); return true; }, }), tagMentionDecorations, headingDecorations, + uploadAnchorField, // tagAutocomplete must precede the editing keymap so the completion popup's // Enter/Tab/arrow bindings win while it is open. tagAutocomplete(getTags), diff --git a/web/src/components/MemoEditor/Editor/index.tsx b/web/src/components/MemoEditor/Editor/index.tsx index 54359e96..1ce289d0 100644 --- a/web/src/components/MemoEditor/Editor/index.tsx +++ b/web/src/components/MemoEditor/Editor/index.tsx @@ -14,7 +14,7 @@ interface EditorProps { initialContent: string; placeholder: string; onContentChange: (content: string) => void; - onFiles: (files: File[]) => void; + onFiles: (files: File[], position: number) => void; /** Invoked by the in-editor save shortcut (Cmd/Ctrl+Enter). */ onSubmit: () => void; isFocusMode?: boolean; @@ -51,7 +51,7 @@ const Editor = forwardRef(function Editor(props: EditorProps, ref: React.Forward extensions: buildEditorExtensions({ placeholder, onChange: (md) => onChangeRef.current(md), - onFiles: (files) => onFilesRef.current(files), + onFiles: (files, position) => onFilesRef.current(files, position), onUpdate: () => listenersRef.current.forEach((l) => l()), onSubmit: () => onSubmitRef.current(), getTags: () => tagsRef.current, diff --git a/web/src/components/MemoEditor/Editor/uploadAnchors.ts b/web/src/components/MemoEditor/Editor/uploadAnchors.ts new file mode 100644 index 00000000..0a96e93c --- /dev/null +++ b/web/src/components/MemoEditor/Editor/uploadAnchors.ts @@ -0,0 +1,158 @@ +import { type EditorState, StateEffect, StateField } from "@codemirror/state"; +import { Decoration, type DecorationSet, EditorView, WidgetType } from "@codemirror/view"; + +export type UploadAnchorStatus = "uploading" | "failed"; + +export interface UploadAnchorDescriptor { + id: string; + status: UploadAnchorStatus; + completed: number; + total: number; + message: string; + retryLabel: string; + keepLabel: string; + onRetry?: () => void; + onKeepAttachments?: () => void; +} + +const addUploadAnchor = StateEffect.define<{ position: number; descriptor: UploadAnchorDescriptor }>(); +const updateUploadAnchor = StateEffect.define(); +const removeUploadAnchor = StateEffect.define(); + +class UploadAnchorWidget extends WidgetType { + constructor(readonly descriptor: UploadAnchorDescriptor) { + super(); + } + + // Every field below is rendered by toDOM — the labels as text, the callbacks as + // click listeners baked into the reused element — so all of them have to take + // part in the comparison. Returning true for a descriptor that differs only in + // its callbacks would leave the previous handlers wired to the old DOM. + eq(other: UploadAnchorWidget): boolean { + const [a, b] = [this.descriptor, other.descriptor]; + return ( + a.id === b.id && + a.status === b.status && + a.completed === b.completed && + a.total === b.total && + a.message === b.message && + a.retryLabel === b.retryLabel && + a.keepLabel === b.keepLabel && + a.onRetry === b.onRetry && + a.onKeepAttachments === b.onKeepAttachments + ); + } + + toDOM(): HTMLElement { + const root = document.createElement("div"); + root.className = "cm-upload-anchor"; + root.dataset.status = this.descriptor.status; + root.setAttribute("role", "status"); + root.setAttribute("aria-live", "polite"); + + const rail = document.createElement("span"); + rail.className = "cm-upload-anchor-rail"; + root.appendChild(rail); + + const badge = document.createElement("span"); + badge.className = "cm-upload-anchor-badge"; + if (this.descriptor.status === "uploading") { + const spinner = document.createElement("span"); + spinner.className = "cm-upload-anchor-spinner"; + spinner.setAttribute("aria-hidden", "true"); + badge.appendChild(spinner); + badge.append(this.descriptor.message); + } else { + badge.append(this.descriptor.message); + } + root.appendChild(badge); + + if (this.descriptor.status === "failed") { + const actions = document.createElement("span"); + actions.className = "cm-upload-anchor-actions"; + if (this.descriptor.onRetry) { + const retry = document.createElement("button"); + retry.type = "button"; + retry.className = "cm-upload-anchor-action"; + retry.textContent = this.descriptor.retryLabel; + retry.addEventListener("click", this.descriptor.onRetry); + actions.appendChild(retry); + } + if (this.descriptor.onKeepAttachments) { + const keep = document.createElement("button"); + keep.type = "button"; + keep.className = "cm-upload-anchor-action"; + keep.textContent = this.descriptor.keepLabel; + keep.addEventListener("click", this.descriptor.onKeepAttachments); + actions.appendChild(keep); + } + root.appendChild(actions); + } + + return root; + } + + ignoreEvent(): boolean { + return true; + } +} + +const anchorIdOf = (value: Decoration): string | undefined => + value.spec.widget instanceof UploadAnchorWidget ? value.spec.widget.descriptor.id : undefined; + +const findAnchorPosition = (decorations: DecorationSet, id: string): number | undefined => { + let position: number | undefined; + decorations.between(0, Number.MAX_SAFE_INTEGER, (from, _to, value) => { + if (anchorIdOf(value) === id) { + position = from; + } + }); + return position; +}; + +export const uploadAnchorField = StateField.define({ + create: () => Decoration.none, + update: (anchors, transaction) => { + let next = anchors.map(transaction.changes); + for (const effect of transaction.effects) { + if (effect.is(addUploadAnchor)) { + const position = Math.min(Math.max(effect.value.position, 0), transaction.state.doc.length); + next = next.update({ + add: [Decoration.widget({ widget: new UploadAnchorWidget(effect.value.descriptor), side: 1, block: true }).range(position)], + sort: true, + }); + } else if (effect.is(updateUploadAnchor)) { + const position = findAnchorPosition(next, effect.value.id); + if (position === undefined) continue; + next = next.update({ + filter: (_from, _to, value) => anchorIdOf(value) !== effect.value.id, + add: [Decoration.widget({ widget: new UploadAnchorWidget(effect.value), side: 1, block: true }).range(position)], + sort: true, + }); + } else if (effect.is(removeUploadAnchor)) { + next = next.update({ + filter: (_from, _to, value) => anchorIdOf(value) !== effect.value, + }); + } + } + return next; + }, + provide: (field) => EditorView.decorations.from(field), +}); + +export const createUploadAnchor = (view: EditorView, descriptor: UploadAnchorDescriptor, position?: number): void => { + view.dispatch({ effects: addUploadAnchor.of({ position: position ?? view.state.selection.main.head, descriptor }) }); +}; + +export const setUploadAnchor = (view: EditorView, descriptor: UploadAnchorDescriptor): void => { + view.dispatch({ effects: updateUploadAnchor.of(descriptor) }); +}; + +export const cancelUploadAnchor = (view: EditorView, id: string): void => { + view.dispatch({ effects: removeUploadAnchor.of(id) }); +}; + +export const getUploadAnchorPosition = (state: EditorState, id: string): number | undefined => + findAnchorPosition(state.field(uploadAnchorField, false) ?? Decoration.none, id); + +export const removeUploadAnchorEffect = (id: string): StateEffect => removeUploadAnchor.of(id); diff --git a/web/src/components/MemoEditor/Toolbar/EditorToolbar.tsx b/web/src/components/MemoEditor/Toolbar/EditorToolbar.tsx index ee9145fd..55205b10 100644 --- a/web/src/components/MemoEditor/Toolbar/EditorToolbar.tsx +++ b/web/src/components/MemoEditor/Toolbar/EditorToolbar.tsx @@ -1,5 +1,6 @@ import type { FC } from "react"; import { Button } from "@/components/ui/button"; +import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip"; import type { Location, Visibility } from "@/types/proto/api/v1/memo_service_pb"; import { useTranslate } from "@/utils/i18n"; import { validationService } from "../services"; @@ -15,6 +16,7 @@ export const EditorToolbar: FC = ({ onAudioRecorderClick, isFormattingToolbarVisible, onToggleFormattingToolbar, + onInsertImages, }) => { const t = useTranslate(); const { actions, dispatch } = useEditorContext(); @@ -22,10 +24,17 @@ export const EditorToolbar: FC = ({ // doesn't re-render the toolbar or the heavy InsertMenu it hosts. `valid` // flips only on empty↔non-empty / loading transitions, not per keystroke. const valid = useEditorSelector((s) => validationService.canSave(s).valid); + const blockedReason = useEditorSelector((s) => validationService.canSave(s).reason); + const blockedReasonDetail = useEditorSelector((s) => validationService.canSave(s).detail); const isSaving = useEditorSelector((s) => s.ui.isLoading.saving); const isUploading = useEditorSelector((s) => s.ui.isLoading.uploading); const location = useEditorSelector((s) => s.metadata.location); const visibility = useEditorSelector((s) => s.metadata.visibility); + const blockedMessage = valid + ? undefined + : blockedReason + ? t(blockedReason, blockedReasonDetail ? { url: blockedReasonDetail } : undefined) + : t("editor.validation.cannot-save"); const handleLocationChange = (next?: Location) => { dispatch(actions.setMetadata({ location: next })); @@ -44,6 +53,7 @@ export const EditorToolbar: FC = ({
= ({ onAudioRecorderClick={onAudioRecorderClick} isFormattingToolbarVisible={isFormattingToolbarVisible} onToggleFormattingToolbar={onToggleFormattingToolbar} + onInsertImages={onInsertImages} />
@@ -62,9 +73,20 @@ export const EditorToolbar: FC = ({ )} - + {!valid && !isSaving && blockedMessage ? ( + + }> + + + {blockedMessage} + + ) : ( + + )} ); diff --git a/web/src/components/MemoEditor/Toolbar/InsertMenu.tsx b/web/src/components/MemoEditor/Toolbar/InsertMenu.tsx index 7320c23e..ed6bbe99 100644 --- a/web/src/components/MemoEditor/Toolbar/InsertMenu.tsx +++ b/web/src/components/MemoEditor/Toolbar/InsertMenu.tsx @@ -1,6 +1,17 @@ import { uniqBy } from "lodash-es"; -import { CheckIcon, FileIcon, ImageIcon, LinkIcon, LoaderIcon, MapPinIcon, Maximize2Icon, MicIcon, PlusIcon, TypeIcon } from "lucide-react"; -import { useCallback, useEffect, useState } from "react"; +import { + CheckIcon, + ImageIcon, + LinkIcon, + LoaderIcon, + MapPinIcon, + Maximize2Icon, + MicIcon, + PaperclipIcon, + PlusIcon, + TypeIcon, +} from "lucide-react"; +import { useCallback, useEffect, useRef, useState } from "react"; import { LinkMemoDialog, LocationDialog } from "@/components/MemoMetadata"; import type { MapPoint } from "@/components/map/types"; import { useReverseGeocoding } from "@/components/map/useReverseGeocoding"; @@ -22,7 +33,7 @@ import type { LocalFile } from "../types/attachment"; const InsertMenu = (props: InsertMenuProps) => { const t = useTranslate(); - const { actions, dispatch } = useEditorContext(); + const { actions, dispatch, getState } = useEditorContext(); const relations = useEditorSelector((s) => s.metadata.relations); const { location: initialLocation, @@ -35,8 +46,10 @@ const InsertMenu = (props: InsertMenuProps) => { const [linkDialogOpen, setLinkDialogOpen] = useState(false); const [locationDialogOpen, setLocationDialogOpen] = useState(false); + const inlineImageInputRef = useRef(null); const { fileInputRef, selectingFlag, handleFileInputChange, handleUploadClick } = useFileUpload((newFiles: LocalFile[]) => { + if (getState().ui.isLoading.saving) return; newFiles.forEach((file) => dispatch(actions.addLocalFile(file))); }); @@ -80,6 +93,7 @@ const InsertMenu = (props: InsertMenuProps) => { }, [displayName, setPlaceholder]); const isUploading = selectingFlag || isUploadingProp; + const insertionDisabled = isUploading || props.isSaving; const handleOpenLinkDialog = useCallback(() => { setLinkDialogOpen(true); @@ -114,19 +128,30 @@ const InsertMenu = (props: InsertMenuProps) => { setLocationDialogOpen(false); }, [locationReset]); - const handleMediaUploadClick = useCallback(() => { - handleUploadClick("image/*,video/*"); - }, [handleUploadClick]); - - const handleFileUploadClick = useCallback(() => { + const handleAttachmentUploadClick = useCallback(() => { + if (getState().ui.isLoading.saving) return; handleUploadClick(); - }, [handleUploadClick]); + }, [getState, handleUploadClick]); + + const handleInlineImageUploadClick = useCallback(() => { + if (getState().ui.isLoading.saving) return; + inlineImageInputRef.current?.click(); + }, [getState]); + + const handleInlineImageInputChange = useCallback( + (event: React.ChangeEvent) => { + const files = Array.from(event.target.files ?? []); + if (files.length > 0) props.onInsertImages(files); + event.target.value = ""; + }, + [props.onInsertImages], + ); // Insert actions (add content). const insertItems = [ - { key: "media", label: t("attachment-library.tabs.media"), icon: ImageIcon, onClick: handleMediaUploadClick }, + { key: "attachment", label: t("editor.insert-menu.add-attachment"), icon: PaperclipIcon, onClick: handleAttachmentUploadClick }, + { key: "inline-image", label: t("editor.insert-menu.insert-image"), icon: ImageIcon, onClick: handleInlineImageUploadClick }, { key: "audio", label: t("editor.audio-recorder.trigger"), icon: MicIcon, onClick: props.onAudioRecorderClick }, - { key: "file", label: t("common.file"), icon: FileIcon, onClick: handleFileUploadClick }, { key: "link", label: t("editor.insert-menu.link-memo"), icon: LinkIcon, onClick: handleOpenLinkDialog }, { key: "location", label: t("editor.insert-menu.add-location"), icon: MapPinIcon, onClick: handleLocationClick }, ]; @@ -134,12 +159,12 @@ const InsertMenu = (props: InsertMenuProps) => { return ( <> - }> + }> {isUploading ? : } {insertItems.map((item) => ( - + {item.label} @@ -162,11 +187,21 @@ const InsertMenu = (props: InsertMenuProps) => { + + (({ placeholder, onSubmit }, ref) => { +export const EditorContent = forwardRef(({ placeholder, onSubmit, onFiles }, ref) => { const { actions, dispatch } = useEditorContext(); - const { createBlobUrl } = useBlobUrls(); const content = useEditorSelector((s) => s.content); const isFocusMode = useEditorSelector((s) => s.ui.isFocusMode); - const handleFiles = (files: File[]) => { - const localFiles: LocalFile[] = files.map((file) => ({ - file, - previewUrl: createBlobUrl(file), - origin: "upload", - })); - localFiles.forEach((localFile) => dispatch(actions.addLocalFile(localFile))); - }; - const handleContentChange = (content: string) => { dispatch(actions.updateContent(content)); }; @@ -44,7 +32,7 @@ export const EditorContent = forwardRef(({ placeholder={placeholder || ""} isFocusMode={isFocusMode} onContentChange={handleContentChange} - onFiles={handleFiles} + onFiles={onFiles} onSubmit={onSubmit} /> diff --git a/web/src/components/MemoEditor/components/EditorMetadata.tsx b/web/src/components/MemoEditor/components/EditorMetadata.tsx index da89d25e..65cb43d3 100644 --- a/web/src/components/MemoEditor/components/EditorMetadata.tsx +++ b/web/src/components/MemoEditor/components/EditorMetadata.tsx @@ -1,14 +1,20 @@ import type { FC } from "react"; import { AttachmentListEditor, LocationDisplayEditor, RelationListEditor } from "@/components/MemoMetadata"; +import { extractManagedAttachmentUIDs } from "@/utils/managed-attachment"; import { useEditorContext, useEditorSelector } from "../state"; import type { EditorMetadataProps } from "../types"; -export const EditorMetadata: FC = ({ memoName }) => { +export const EditorMetadata: FC = ({ memoName, uploadingLocalFileURLs, onInsertAttachments, onInsertLocalFiles }) => { const { actions, dispatch } = useEditorContext(); const attachments = useEditorSelector((s) => s.metadata.attachments); const localFiles = useEditorSelector((s) => s.localFiles); const relations = useEditorSelector((s) => s.metadata.relations); const location = useEditorSelector((s) => s.metadata.location); + const content = useEditorSelector((s) => s.content); + const placementActionsDisabled = useEditorSelector( + (s) => s.ui.isLoading.saving || s.ui.isLoading.uploading || s.ui.pendingInlineImageInsertions > 0, + ); + const inlineAttachmentUIDs = extractManagedAttachmentUIDs(content); return (
@@ -18,6 +24,11 @@ export const EditorMetadata: FC = ({ memoName }) => { onAttachmentsChange={(next) => dispatch(actions.setMetadata({ attachments: next }))} onLocalFilesChange={(next) => dispatch(actions.setLocalFiles(next))} onRemoveLocalFile={(previewUrl) => dispatch(actions.removeLocalFile(previewUrl))} + inlineAttachmentUIDs={inlineAttachmentUIDs} + onInsertAttachments={onInsertAttachments} + onInsertLocalFiles={onInsertLocalFiles} + placementActionsDisabled={placementActionsDisabled} + uploadingLocalFileURLs={uploadingLocalFileURLs} /> + left.content === right.content && + left.attachments.length === right.attachments.length && + left.attachments.every((attachment, index) => { + const other = right.attachments[index]; + return other !== undefined && equals(AttachmentSchema, attachment, other); + }); + /** - * Persists the editor's content to localStorage as a draft. Subscribes to the - * editor store directly for content rather than taking it as a prop, so the - * component that mounts this hook does not re-render on every keystroke. + * Persists the editor's content and already-uploaded attachments to localStorage + * as a draft. Subscribes to the editor store directly rather than taking draft + * state as props, so the component that mounts this hook does not re-render on + * every keystroke. */ export const useAutoSave = (username: string, cacheKey: string | undefined, enabled = true) => { const store = useEditorStore(); - const latestContentRef = useRef(store.getState().content); - const discardedContentRef = useRef(undefined); + const initialState = store.getState(); + const latestDraftRef = useRef({ content: initialState.content, attachments: initialState.metadata.attachments }); + const discardedDraftRef = useRef(undefined); useEffect(() => { if (!enabled) return; const key = cacheService.key(username, cacheKey); - const persist = (content: string) => { - latestContentRef.current = content; - if (discardedContentRef.current !== undefined && discardedContentRef.current !== content) { - discardedContentRef.current = undefined; + const persist = (draft: EditorDraft) => { + latestDraftRef.current = draft; + if (discardedDraftRef.current !== undefined && !sameDraft(discardedDraftRef.current, draft)) { + discardedDraftRef.current = undefined; } - cacheService.save(key, content); + cacheService.save(key, draft.content, draft.attachments); }; - // Persist the current content on mount/enable, then on every change. - persist(store.getState().content); + // Persist the current draft on mount/enable, then on every relevant change. + const state = store.getState(); + persist({ content: state.content, attachments: state.metadata.attachments }); return store.subscribe(() => { - const content = store.getState().content; - if (content !== latestContentRef.current) { - persist(content); + const nextState = store.getState(); + const draft = { content: nextState.content, attachments: nextState.metadata.attachments }; + if (!sameDraft(draft, latestDraftRef.current)) { + persist(draft); } }); }, [store, username, cacheKey, enabled]); @@ -39,11 +53,11 @@ export const useAutoSave = (username: string, cacheKey: string | undefined, enab const key = cacheService.key(username, cacheKey); const flushDraft = () => { - if (discardedContentRef.current === latestContentRef.current) { + if (discardedDraftRef.current && sameDraft(discardedDraftRef.current, latestDraftRef.current)) { return; } - cacheService.saveNow(key, latestContentRef.current); + cacheService.saveNow(key, latestDraftRef.current.content, latestDraftRef.current.attachments); }; const handleVisibilityChange = () => { if (document.visibilityState === "hidden") { @@ -65,7 +79,7 @@ export const useAutoSave = (username: string, cacheKey: string | undefined, enab const discardDraft = useCallback(() => { const key = cacheService.key(username, cacheKey); - discardedContentRef.current = latestContentRef.current; + discardedDraftRef.current = latestDraftRef.current; cacheService.clear(key); }, [username, cacheKey]); diff --git a/web/src/components/MemoEditor/hooks/useFileUpload.ts b/web/src/components/MemoEditor/hooks/useFileUpload.ts index 142968a9..5a03210e 100644 --- a/web/src/components/MemoEditor/hooks/useFileUpload.ts +++ b/web/src/components/MemoEditor/hooks/useFileUpload.ts @@ -30,7 +30,7 @@ export const useFileUpload = (onFilesSelected: (localFiles: LocalFile[]) => void if (fileInputRef.current) fileInputRef.current.value = ""; }; - const handleUploadClick = (accept = "*") => { + const handleUploadClick = (accept = "") => { if (!fileInputRef.current) { return; } @@ -47,7 +47,7 @@ export const useFileUpload = (onFilesSelected: (localFiles: LocalFile[]) => void }; }; -const pairAppleLivePhotoFiles = (localFiles: LocalFile[]): LocalFile[] => { +export const pairAppleLivePhotoFiles = (localFiles: LocalFile[]): LocalFile[] => { const stemMap = new Map(); for (const localFile of localFiles) { const stem = normalizeFilenameStem(localFile.file.name); diff --git a/web/src/components/MemoEditor/hooks/useInlineImageUpload.ts b/web/src/components/MemoEditor/hooks/useInlineImageUpload.ts new file mode 100644 index 00000000..1de4043b --- /dev/null +++ b/web/src/components/MemoEditor/hooks/useInlineImageUpload.ts @@ -0,0 +1,235 @@ +import { uniqBy } from "lodash-es"; +import { type RefObject, useCallback, useEffect, useRef, useState } from "react"; +import { toast } from "react-hot-toast"; +import type { Attachment } from "@/types/proto/api/v1/attachment_service_pb"; +import { isImage } from "@/utils/attachment"; +import { useTranslate } from "@/utils/i18n"; +import { buildManagedAttachmentMarkdown, canInlineAttachment } from "@/utils/managed-attachment"; +import { errorService, uploadService } from "../services"; +import { useEditorContext } from "../state"; +import type { LocalFile } from "../types/attachment"; +import type { EditorController } from "../types/editorController"; + +interface UploadEntry { + localFile: LocalFile; + attachment?: Attachment; +} + +interface UploadJob { + id: string; + entries: UploadEntry[]; + representativeIndexes: number[]; + active: boolean; +} + +interface InlineLocalFileSplit { + /** Files that become inline Markdown images; motion groups stay contiguous. */ + inline: LocalFile[]; + /** Indexes into `inline` of the file each reference is written for (the still of a motion group). */ + representativeIndexes: number[]; + /** Everything else, which stays in the attachment list. */ + attachments: LocalFile[]; +} + +/** Single source of truth for which picked files can be inlined and which file represents a motion group. */ +export const splitInlineLocalFiles = (localFiles: LocalFile[]): InlineLocalFileSplit => { + const inline: LocalFile[] = []; + const representativeIndexes: number[] = []; + const attachments: LocalFile[] = []; + const consumedGroups = new Set(); + + for (const localFile of localFiles) { + const groupID = localFile.motionMedia?.groupId; + if (!groupID) { + if (isImage(localFile.file.type)) { + representativeIndexes.push(inline.length); + inline.push(localFile); + } else { + attachments.push(localFile); + } + continue; + } + + if (consumedGroups.has(groupID)) continue; + consumedGroups.add(groupID); + const group = localFiles.filter((candidate) => candidate.motionMedia?.groupId === groupID); + const representative = group.find((candidate) => isImage(candidate.file.type)); + if (!representative) { + attachments.push(...group); + continue; + } + representativeIndexes.push(inline.length + group.indexOf(representative)); + inline.push(...group); + } + + return { inline, representativeIndexes, attachments }; +}; + +const createJob = (localFiles: LocalFile[]): UploadJob | undefined => { + const { inline, representativeIndexes } = splitInlineLocalFiles(localFiles); + if (inline.length === 0) return undefined; + return { + id: crypto.randomUUID(), + entries: inline.map((localFile) => ({ localFile })), + representativeIndexes, + active: false, + }; +}; + +export const useInlineImageUpload = (editorRef: RefObject) => { + const t = useTranslate(); + const { actions, dispatch, getState } = useEditorContext(); + const jobsRef = useRef(new Map()); + const disposedRef = useRef(false); + const [uploadingLocalFileURLs, setUploadingLocalFileURLs] = useState>(new Set()); + + /** Publishes everything derived from the live job map; call after any mutation of it. */ + const syncJobState = useCallback(() => { + const jobs = Array.from(jobsRef.current.values()); + const state = getState(); + if (state.ui.pendingInlineImageInsertions !== jobs.length) { + dispatch(actions.setPendingInlineImageInsertions(jobs.length)); + } + const isUploading = jobs.some((job) => job.active); + if (state.ui.isLoading.uploading !== isUploading) { + dispatch(actions.setLoading("uploading", isUploading)); + } + + const uploadingURLs = new Set( + jobs.flatMap((job) => + job.active ? job.entries.filter((entry) => !entry.attachment).map((entry) => entry.localFile.previewUrl) : [], + ), + ); + setUploadingLocalFileURLs((previous) => + previous.size === uploadingURLs.size && [...uploadingURLs].every((url) => previous.has(url)) ? previous : uploadingURLs, + ); + }, [actions, dispatch, getState]); + + const appendAttachment = useCallback( + (attachment: Attachment, localPreviewURL: string) => { + const state = getState(); + dispatch(actions.setMetadata({ attachments: uniqBy([...state.metadata.attachments, attachment], (item) => item.name) })); + if (state.localFiles.some((localFile) => localFile.previewUrl === localPreviewURL)) { + dispatch(actions.removeLocalFile(localPreviewURL)); + } + }, + [actions, dispatch, getState], + ); + + const finishJob = useCallback( + (job: UploadJob) => { + jobsRef.current.delete(job.id); + syncJobState(); + }, + [syncJobState], + ); + + const dismissJob = useCallback( + (id: string) => { + const job = jobsRef.current.get(id); + if (!job || job.active) return; + editorRef.current?.cancelUploadAnchor(id); + finishJob(job); + }, + [editorRef, finishJob], + ); + + const runJobRef = useRef<(id: string) => Promise>(async () => undefined); + const retryJob = useCallback((id: string) => void runJobRef.current(id), []); + + const descriptorFor = useCallback( + (job: UploadJob, status: "uploading" | "failed") => { + const completed = job.entries.filter((entry) => entry.attachment).length; + return { + id: job.id, + status, + completed, + total: job.entries.length, + message: t(status === "uploading" ? "editor.insert-menu.uploading-images" : "editor.insert-menu.image-upload-stopped", { + completed, + total: job.entries.length, + }), + retryLabel: t("editor.insert-menu.retry-image-upload"), + // Partially-uploaded jobs keep what landed; a job with nothing uploaded is just cancelled. + keepLabel: t(completed > 0 ? "editor.insert-menu.keep-as-attachments" : "common.cancel"), + onRetry: status === "failed" ? () => retryJob(job.id) : undefined, + onKeepAttachments: status === "failed" ? () => dismissJob(job.id) : undefined, + }; + }, + [dismissJob, retryJob, t], + ); + + const runJob = useCallback( + async (id: string) => { + const job = jobsRef.current.get(id); + if (!job || job.active || disposedRef.current) return; + job.active = true; + syncJobState(); + editorRef.current?.updateUploadAnchor(descriptorFor(job, "uploading")); + + let lastError: unknown; + for (const entry of job.entries) { + if (entry.attachment) continue; + try { + entry.attachment = await uploadService.uploadFile(entry.localFile); + if (disposedRef.current) return; + appendAttachment(entry.attachment, entry.localFile.previewUrl); + editorRef.current?.updateUploadAnchor(descriptorFor(job, "uploading")); + } catch (error) { + lastError = error; + } + } + + job.active = false; + if (disposedRef.current) return; + syncJobState(); + + if (job.entries.some((entry) => !entry.attachment)) { + editorRef.current?.updateUploadAnchor(descriptorFor(job, "failed")); + toast.error(errorService.getErrorMessage(lastError) || t("editor.insert-menu.image-upload-failed")); + return; + } + + const markdown = job.representativeIndexes + .map((index) => buildManagedAttachmentMarkdown(job.entries[index]!.attachment!)) + .join("\n\n"); + editorRef.current?.resolveUploadAnchor(job.id, markdown); + finishJob(job); + }, + [appendAttachment, descriptorFor, editorRef, finishJob, syncJobState, t], + ); + runJobRef.current = runJob; + + const insertLocalImages = useCallback( + (localFiles: LocalFile[], position?: number) => { + if (getState().ui.isLoading.saving) return; + const editor = editorRef.current; + const job = createJob(localFiles); + if (!editor || !job) return; + jobsRef.current.set(job.id, job); + syncJobState(); + editor.createUploadAnchor(descriptorFor(job, "uploading"), position); + void runJob(job.id); + }, + [descriptorFor, editorRef, getState, runJob, syncJobState], + ); + + const insertRemoteImages = useCallback( + (attachments: Attachment[]) => { + if (getState().ui.isLoading.saving) return; + const markdown = attachments.filter(canInlineAttachment).map(buildManagedAttachmentMarkdown).join("\n\n"); + editorRef.current?.insertMarkdown(markdown); + }, + [editorRef, getState], + ); + + useEffect(() => { + disposedRef.current = false; + return () => { + disposedRef.current = true; + jobsRef.current.clear(); + }; + }, []); + + return { insertLocalImages, insertRemoteImages, uploadingLocalFileURLs }; +}; diff --git a/web/src/components/MemoEditor/hooks/useMemoInit.ts b/web/src/components/MemoEditor/hooks/useMemoInit.ts index c1b6361d..c06bbe9d 100644 --- a/web/src/components/MemoEditor/hooks/useMemoInit.ts +++ b/web/src/components/MemoEditor/hooks/useMemoInit.ts @@ -37,9 +37,12 @@ export const useMemoInit = ({ cacheService.clear(key); dispatch(actions.initMemo(initialState)); } else { - const cachedContent = cacheService.load(key); - if (cachedContent) { - dispatch(actions.updateContent(cachedContent)); + const cachedDraft = cacheService.loadDraft(key); + if (cachedDraft.content) { + dispatch(actions.updateContent(cachedDraft.content)); + } + if (cachedDraft.attachments.length > 0) { + dispatch(actions.setMetadata({ attachments: cachedDraft.attachments })); } if (defaultVisibility !== undefined) { dispatch(actions.setMetadata({ visibility: defaultVisibility })); diff --git a/web/src/components/MemoEditor/hooks/useMemoSave.ts b/web/src/components/MemoEditor/hooks/useMemoSave.ts index 34fadc4a..98ca6be7 100644 --- a/web/src/components/MemoEditor/hooks/useMemoSave.ts +++ b/web/src/components/MemoEditor/hooks/useMemoSave.ts @@ -41,9 +41,9 @@ export function useMemoSave({ return useCallback(async () => { const state = getState(); - const { valid, reason } = validationService.canSave(state); + const { valid, reason, detail } = validationService.canSave(state); if (!valid) { - toast.error(reason || "Cannot save"); + toast.error(reason ? t(reason, detail ? { url: detail } : undefined) : t("editor.validation.cannot-save")); return; } diff --git a/web/src/components/MemoEditor/index.tsx b/web/src/components/MemoEditor/index.tsx index 69a460e9..15678007 100644 --- a/web/src/components/MemoEditor/index.tsx +++ b/web/src/components/MemoEditor/index.tsx @@ -10,7 +10,17 @@ import { useTranslate } from "@/utils/i18n"; import { convertVisibilityFromString } from "@/utils/memo"; import { AudioRecorderPanel, EditorContent, EditorMetadata, FocusModeOverlay, TimestampPopover } from "./components"; import { FOCUS_MODE_STYLES, FORMATTING_TOOLBAR_STORAGE_KEY } from "./constants"; -import { useAudioRecorder, useAutoSave, useFocusMode, useMemoInit, useMemoSave } from "./hooks"; +import { + pairAppleLivePhotoFiles, + splitInlineLocalFiles, + useAudioRecorder, + useAutoSave, + useBlobUrls, + useFocusMode, + useInlineImageUpload, + useMemoInit, + useMemoSave, +} from "./hooks"; import { cacheService, errorService, transcriptionService } from "./services"; import { EditorProvider, useEditorContext, useEditorSelector } from "./state"; import { EditorToolbar, FormattingToolbar } from "./Toolbar"; @@ -39,7 +49,7 @@ const MemoEditorImpl: React.FC = ({ const t = useTranslate(); const currentUser = useCurrentUser(); const editorRef = useRef(null); - const { actions, dispatch } = useEditorContext(); + const { actions, dispatch, getState } = useEditorContext(); // Subscribe only to the low-frequency slices this component renders from, so // typing (which changes content) does not re-render the editor shell and its // toolbar/metadata children. @@ -49,6 +59,8 @@ const MemoEditorImpl: React.FC = ({ const { aiSetting, fetchSetting } = useInstance(); const [isAudioRecorderOpen, setIsAudioRecorderOpen] = useState(false); const [isTranscribingAudio, setIsTranscribingAudio] = useState(false); + const { createBlobUrl } = useBlobUrls(); + const inlineImageUpload = useInlineImageUpload(editorRef); // Persisted preference: also show the formatting toolbar in normal mode. Focus // mode always shows it regardless; this only governs the non-focus layout. const [isFormattingToolbarVisible, setFormattingToolbarVisible] = useLocalStorage(FORMATTING_TOOLBAR_STORAGE_KEY, false); @@ -220,6 +232,29 @@ const MemoEditorImpl: React.FC = ({ void handleStartAudioRecording(); }; + const toLocalFiles = useCallback( + (files: File[]): LocalFile[] => + pairAppleLivePhotoFiles( + files.map((file) => ({ + file, + previewUrl: createBlobUrl(file), + origin: "upload", + })), + ), + [createBlobUrl], + ); + + /** Shared by the + menu (no position) and by editor paste/drop (drop position). */ + const handleInsertImages = useCallback( + (files: File[], position?: number) => { + if (getState().ui.isLoading.saving) return; + const { inline, attachments } = splitInlineLocalFiles(toLocalFiles(files)); + attachments.forEach((file) => dispatch(actions.addLocalFile(file))); + inlineImageUpload.insertLocalImages(inline, position); + }, + [actions, dispatch, getState, inlineImageUpload.insertLocalImages, toLocalFiles], + ); + const handleCancelAudioRecording = () => { setIsTranscribingAudio(false); audioRecorder.resetRecording(); @@ -285,7 +320,7 @@ const MemoEditorImpl: React.FC = ({ )} {/* Editor content grows to fill available space in focus mode */} - + {isAudioRecorderOpen && (audioRecorder.isBusy || isTranscribingAudio) && ( = ({ {/* Metadata and toolbar grouped together at bottom */}
- + = ({ onAudioRecorderClick={handleAudioRecorderClick} isFormattingToolbarVisible={isFormattingToolbarVisible} onToggleFormattingToolbar={handleToggleFormattingToolbar} + onInsertImages={handleInsertImages} />
diff --git a/web/src/components/MemoEditor/services/cacheService.ts b/web/src/components/MemoEditor/services/cacheService.ts index f4429aa2..8a4733b9 100644 --- a/web/src/components/MemoEditor/services/cacheService.ts +++ b/web/src/components/MemoEditor/services/cacheService.ts @@ -1,30 +1,64 @@ +import { fromJson, type JsonValue, toJson } from "@bufbuild/protobuf"; +import type { Attachment } from "@/types/proto/api/v1/attachment_service_pb"; +import { AttachmentSchema } from "@/types/proto/api/v1/attachment_service_pb"; + export const CACHE_DEBOUNCE_DELAY = 500; const pendingSaves = new Map>(); const cursors = new Map(); const STRUCTURED_CACHE_ENTRY_KIND = "memos.editor-cache"; -const STRUCTURED_CACHE_ENTRY_VERSION = 1; +const STRUCTURED_CACHE_ENTRY_VERSION = 2; -function deserializeContent(raw: string): string { +export interface EditorDraft { + content: string; + attachments: Attachment[]; +} + +function deserializeDraft(raw: string): EditorDraft { try { - const parsed = JSON.parse(raw) as { kind?: unknown; version?: unknown; content?: unknown }; + const parsed = JSON.parse(raw) as { kind?: unknown; version?: unknown; content?: unknown; attachments?: unknown }; + if (parsed.kind === STRUCTURED_CACHE_ENTRY_KIND && parsed.version === 1 && typeof parsed.content === "string") { + return { content: parsed.content, attachments: [] }; + } if ( parsed.kind === STRUCTURED_CACHE_ENTRY_KIND && parsed.version === STRUCTURED_CACHE_ENTRY_VERSION && typeof parsed.content === "string" ) { - return parsed.content; + const attachments = Array.isArray(parsed.attachments) + ? parsed.attachments.flatMap((value) => { + try { + return [fromJson(AttachmentSchema, value as JsonValue, { ignoreUnknownFields: true })]; + } catch { + return []; + } + }) + : []; + return { content: parsed.content, attachments }; } } catch { // Drafts have historically been stored as raw markdown strings. } - return raw; + return { content: raw, attachments: [] }; } -function writeEntry(key: string, content: string): void { - if (content.trim()) { - localStorage.setItem(key, content); +function serializeDraft(content: string, attachments: Attachment[]): string { + if (attachments.length === 0) { + return content; + } + + return JSON.stringify({ + kind: STRUCTURED_CACHE_ENTRY_KIND, + version: STRUCTURED_CACHE_ENTRY_VERSION, + content, + attachments: attachments.map((attachment) => toJson(AttachmentSchema, attachment)), + }); +} + +function writeEntry(key: string, content: string, attachments: Attachment[]): void { + if (content.trim() || attachments.length > 0) { + localStorage.setItem(key, serializeDraft(content, attachments)); } else { localStorage.removeItem(key); } @@ -35,7 +69,7 @@ export const cacheService = { return `${username}-${cacheKey || ""}`; }, - save: (key: string, content: string) => { + save: (key: string, content: string, attachments: Attachment[] = []) => { const pendingSave = pendingSaves.get(key); if (pendingSave) { window.clearTimeout(pendingSave); @@ -44,25 +78,30 @@ export const cacheService = { const timeoutId = window.setTimeout(() => { pendingSaves.delete(key); - writeEntry(key, content); + writeEntry(key, content, attachments); }, CACHE_DEBOUNCE_DELAY); pendingSaves.set(key, timeoutId); }, - saveNow: (key: string, content: string) => { + saveNow: (key: string, content: string, attachments: Attachment[] = []) => { const pendingSave = pendingSaves.get(key); if (pendingSave) { window.clearTimeout(pendingSave); pendingSaves.delete(key); } - writeEntry(key, content); + writeEntry(key, content, attachments); }, load(key: string): string { const raw = localStorage.getItem(key); - return raw ? deserializeContent(raw) : ""; + return raw ? deserializeDraft(raw).content : ""; + }, + + loadDraft(key: string): EditorDraft { + const raw = localStorage.getItem(key); + return raw ? deserializeDraft(raw) : { content: "", attachments: [] }; }, saveCursor(key: string, cursor: number): void { diff --git a/web/src/components/MemoEditor/services/uploadService.ts b/web/src/components/MemoEditor/services/uploadService.ts index 404eae58..1d4880c3 100644 --- a/web/src/components/MemoEditor/services/uploadService.ts +++ b/web/src/components/MemoEditor/services/uploadService.ts @@ -5,24 +5,27 @@ import { AttachmentSchema, MotionMediaSchema } from "@/types/proto/api/v1/attach import type { LocalFile } from "../types/attachment"; export const uploadService = { + async uploadFile(localFile: LocalFile): Promise { + const { file, motionMedia } = localFile; + const buffer = new Uint8Array(await file.arrayBuffer()); + return attachmentServiceClient.createAttachment({ + attachment: create(AttachmentSchema, { + filename: file.name, + size: BigInt(file.size), + type: file.type, + content: buffer, + motionMedia: motionMedia ? create(MotionMediaSchema, motionMedia) : undefined, + }), + }); + }, + async uploadFiles(localFiles: LocalFile[]): Promise { if (localFiles.length === 0) return []; const attachments: Attachment[] = []; for (const localFile of localFiles) { - const { file, motionMedia } = localFile; - const buffer = new Uint8Array(await file.arrayBuffer()); - const attachment = await attachmentServiceClient.createAttachment({ - attachment: create(AttachmentSchema, { - filename: file.name, - size: BigInt(file.size), - type: file.type, - content: buffer, - motionMedia: motionMedia ? create(MotionMediaSchema, motionMedia) : undefined, - }), - }); - attachments.push(attachment); + attachments.push(await uploadService.uploadFile(localFile)); } return attachments; diff --git a/web/src/components/MemoEditor/services/validationService.ts b/web/src/components/MemoEditor/services/validationService.ts index 4693e0cb..852da0d9 100644 --- a/web/src/components/MemoEditor/services/validationService.ts +++ b/web/src/components/MemoEditor/services/validationService.ts @@ -1,35 +1,49 @@ +import type { Translations } from "@/utils/i18n"; +import { findInvalidManagedAttachmentReferences } from "@/utils/managed-attachment"; import type { EditorState } from "../state"; export interface ValidationResult { valid: boolean; - reason?: string; + reason?: Translations; + detail?: string; } export const validationService = { canSave(state: EditorState): ValidationResult { // Cannot save while loading initial content if (state.ui.isLoading.loading) { - return { valid: false, reason: "Loading memo content" }; + return { valid: false, reason: "editor.validation.loading-content" }; } // Must have content, attachment, or local file if (!state.content.trim() && state.metadata.attachments.length === 0 && state.localFiles.length === 0) { - return { valid: false, reason: "Content, attachment, or file required" }; + return { valid: false, reason: "editor.validation.content-required" }; } // Cannot save while uploading if (state.ui.isLoading.uploading) { - return { valid: false, reason: "Wait for upload to complete" }; + return { valid: false, reason: "editor.validation.wait-for-upload" }; + } + + if (state.ui.pendingInlineImageInsertions > 0) { + return { valid: false, reason: "editor.validation.resolve-image-uploads" }; + } + + // The API rejects these outright, so catch them here rather than letting the + // save fail with an opaque error that doesn't say which URL is at fault. + const [invalidReference] = findInvalidManagedAttachmentReferences(state.content); + if (invalidReference) { + return { valid: false, reason: "editor.validation.unsupported-attachment-image-url", detail: invalidReference }; } // Cannot save while audio recorder is active if (state.recorderBusy) { - return { valid: false, reason: "Finish audio recording before saving" }; + return { valid: false, reason: "editor.validation.finish-audio-recording" }; } // Cannot save while already saving if (state.ui.isLoading.saving) { - return { valid: false, reason: "Save in progress" }; + return { valid: false, reason: "editor.validation.save-in-progress" }; } return { valid: true }; diff --git a/web/src/components/MemoEditor/state/actions.ts b/web/src/components/MemoEditor/state/actions.ts index 8c80fcac..5b9ee093 100644 --- a/web/src/components/MemoEditor/state/actions.ts +++ b/web/src/components/MemoEditor/state/actions.ts @@ -41,6 +41,11 @@ export const editorActions = { payload: { key, value }, }), + setPendingInlineImageInsertions: (value: number): EditorAction => ({ + type: "SET_PENDING_INLINE_IMAGE_INSERTIONS", + payload: value, + }), + setTimestamps: (timestamps: Partial): EditorAction => ({ type: "SET_TIMESTAMPS", payload: timestamps, diff --git a/web/src/components/MemoEditor/state/reducer.ts b/web/src/components/MemoEditor/state/reducer.ts index 31ea0ce8..962a1356 100644 --- a/web/src/components/MemoEditor/state/reducer.ts +++ b/web/src/components/MemoEditor/state/reducer.ts @@ -65,6 +65,15 @@ export function editorReducer(state: EditorState, action: EditorAction): EditorS }, }; + case "SET_PENDING_INLINE_IMAGE_INSERTIONS": + return { + ...state, + ui: { + ...state.ui, + pendingInlineImageInsertions: action.payload, + }, + }; + case "SET_TIMESTAMPS": return { ...state, diff --git a/web/src/components/MemoEditor/state/types.ts b/web/src/components/MemoEditor/state/types.ts index 861e2539..e89041b9 100644 --- a/web/src/components/MemoEditor/state/types.ts +++ b/web/src/components/MemoEditor/state/types.ts @@ -15,6 +15,7 @@ export interface EditorState { }; ui: { isFocusMode: boolean; + pendingInlineImageInsertions: number; isLoading: { saving: boolean; uploading: boolean; @@ -40,6 +41,7 @@ export type EditorAction = | { type: "SET_LOCAL_FILES"; payload: LocalFile[] } | { type: "TOGGLE_FOCUS_MODE" } | { type: "SET_LOADING"; payload: { key: LoadingKey; value: boolean } } + | { type: "SET_PENDING_INLINE_IMAGE_INSERTIONS"; payload: number } | { type: "SET_TIMESTAMPS"; payload: Partial } | { type: "SET_RECORDER_BUSY"; payload: boolean } | { type: "RESET" }; @@ -55,6 +57,7 @@ const defaultState: EditorState = { }, ui: { isFocusMode: false, + pendingInlineImageInsertions: 0, isLoading: { saving: false, uploading: false, diff --git a/web/src/components/MemoEditor/types/components.ts b/web/src/components/MemoEditor/types/components.ts index 37af5a86..c0dc935b 100644 --- a/web/src/components/MemoEditor/types/components.ts +++ b/web/src/components/MemoEditor/types/components.ts @@ -1,5 +1,7 @@ +import type { Attachment } from "@/types/proto/api/v1/attachment_service_pb"; import type { Location, Memo, Visibility } from "@/types/proto/api/v1/memo_service_pb"; import type { AudioRecorderStatus } from "../hooks/useAudioRecorder"; +import type { LocalFile } from "./attachment"; export interface MemoEditorProps { className?: string; @@ -29,6 +31,7 @@ export interface EditorContentProps { placeholder?: string; /** Invoked by the in-editor save shortcut (Cmd/Ctrl+Enter). */ onSubmit: () => void; + onFiles: (files: File[], position: number) => void; } export interface EditorToolbarProps { @@ -39,10 +42,14 @@ export interface EditorToolbarProps { /** Whether the formatting toolbar is shown in normal mode (persisted preference). */ isFormattingToolbarVisible: boolean; onToggleFormattingToolbar: () => void; + onInsertImages: (files: File[]) => void; } export interface EditorMetadataProps { memoName?: string; + uploadingLocalFileURLs: ReadonlySet; + onInsertAttachments: (attachments: Attachment[]) => void; + onInsertLocalFiles: (localFiles: LocalFile[]) => void; } export interface AudioRecorderPanelProps { @@ -69,6 +76,7 @@ export interface FocusModeExitButtonProps { export interface InsertMenuProps { isUploading?: boolean; + isSaving?: boolean; location?: Location; onLocationChange: (location?: Location) => void; onToggleFocusMode?: () => void; @@ -77,6 +85,7 @@ export interface InsertMenuProps { /** Persisted toggle for the normal-mode formatting toolbar. */ isFormattingToolbarVisible?: boolean; onToggleFormattingToolbar?: () => void; + onInsertImages: (files: File[]) => void; } export interface VisibilitySelectorProps { diff --git a/web/src/components/MemoEditor/types/editorController.ts b/web/src/components/MemoEditor/types/editorController.ts index 3829027f..00f9f413 100644 --- a/web/src/components/MemoEditor/types/editorController.ts +++ b/web/src/components/MemoEditor/types/editorController.ts @@ -1,3 +1,4 @@ +import type { UploadAnchorDescriptor } from "../Editor/uploadAnchors"; import type { ActiveFormatState, EditorCommandContext, EditorCommandId } from "../formatting/commands"; /** @@ -16,6 +17,10 @@ export interface EditorController { setMarkdown(markdown: string): void; /** Insert markdown at the cursor as its own block. */ insertMarkdown(markdown: string): void; + createUploadAnchor(descriptor: UploadAnchorDescriptor, position?: number): void; + updateUploadAnchor(descriptor: UploadAnchorDescriptor): void; + resolveUploadAnchor(id: string, markdown: string): void; + cancelUploadAnchor(id: string): void; getCursor(): number; setCursor(position: number): void; scrollToCursor(): void; diff --git a/web/src/components/MemoMetadata/Attachment/AttachmentListEditor.tsx b/web/src/components/MemoMetadata/Attachment/AttachmentListEditor.tsx index 9ca6dff5..6c396110 100644 --- a/web/src/components/MemoMetadata/Attachment/AttachmentListEditor.tsx +++ b/web/src/components/MemoMetadata/Attachment/AttachmentListEditor.tsx @@ -1,4 +1,14 @@ -import { ChevronDownIcon, ChevronUpIcon, FileAudioIcon, FileIcon, PaperclipIcon, PauseIcon, PlayIcon, XIcon } from "lucide-react"; +import { + ChevronDownIcon, + ChevronUpIcon, + FileAudioIcon, + FileIcon, + ImagePlusIcon, + PaperclipIcon, + PauseIcon, + PlayIcon, + XIcon, +} from "lucide-react"; import { type FC, type MouseEvent, useMemo, useRef, useState } from "react"; import type { AttachmentItem, LocalFile } from "@/components/MemoEditor/types/attachment"; import { getAudioRecordingTimeLabel, toAttachmentItems } from "@/components/MemoEditor/types/attachment"; @@ -9,30 +19,55 @@ import { cn } from "@/lib/utils"; import type { Attachment } from "@/types/proto/api/v1/attachment_service_pb"; import { formatFileSize, getFileTypeLabel } from "@/utils/format"; import { useTranslate } from "@/utils/i18n"; +import { canInlineAttachment, extractAttachmentUIDFromName } from "@/utils/managed-attachment"; import type { PreviewMediaItem } from "@/utils/media-item"; import { formatAudioTime, toggleAudioPlayback } from "./attachmentHelpers"; +const collectMembers = (byId: ReadonlyMap, memberIds: string[]): T[] => + memberIds.map((memberId) => byId.get(memberId)).filter((member): member is T => member !== undefined); + interface AttachmentListEditorProps { attachments: Attachment[]; localFiles?: LocalFile[]; onAttachmentsChange?: (attachments: Attachment[]) => void; onLocalFilesChange?: (localFiles: LocalFile[]) => void; onRemoveLocalFile?: (previewUrl: string) => void; + inlineAttachmentUIDs?: ReadonlySet; + onInsertAttachments?: (attachments: Attachment[]) => void; + onInsertLocalFiles?: (localFiles: LocalFile[]) => void; + placementActionsDisabled?: boolean; + uploadingLocalFileURLs?: ReadonlySet; } const AttachmentItemActions: FC<{ + placementAction?: { label: string; onClick: () => void; disabled?: boolean }; onRemove?: () => void; onMoveUp?: () => void; onMoveDown?: () => void; canMoveUp?: boolean; canMoveDown?: boolean; -}> = ({ onRemove, onMoveUp, onMoveDown, canMoveUp = true, canMoveDown = true }) => { +}> = ({ placementAction, onRemove, onMoveUp, onMoveDown, canMoveUp = true, canMoveDown = true }) => { const stopPropagation = (event: MouseEvent) => { event.stopPropagation(); }; return (
+ {placementAction && ( + + )} {onMoveUp && (