AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.
HTML ai-agentcloudflarecloudflare-d1cloudflare-r2cloudflare-workersdockerelectronevernoteevernote-alternativeknowledge-basemcpnote-takingobsidian-alternativeoffline-firstpersonal-knowledge-basepwaself-hostedsqlitetiptapweb-clipper
fix(editor): show image controls only after explicit selection
tianma-if8b4aad60507c
diff --git a/apps/web/src/components/editor/ResizableImage.tsx b/apps/web/src/components/editor/ResizableImage.tsxindex 243b97dc..d975e843 100644--- a/apps/web/src/components/editor/ResizableImage.tsx+++ b/apps/web/src/components/editor/ResizableImage.tsx@@ -16,6 +16,7 @@ import { createMarkdownImagePasteRule } from "@/lib/markdown-image-paste";import { cn } from "@/lib/utils";import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip";import { isImageInGallery } from "./image-editing-context";+import { createImageSelectionPlugin, imageSelectionKey } from "./image-selection";export type ImageMenuRequestDetail = {element: HTMLElement;@@ -51,14 +52,15 @@ const ResizableImageNodeView = ({const width = previewWidth ?? nodeWidth;const explicitAlignment = parseImageAlignment(node.attrs.alignment);const alignment = explicitAlignment ?? "left";- const { editable, inGallery } = useEditorState({+ const { editable, inGallery, explicitlySelected } = useEditorState({editor,selector: ({ editor: activeEditor }) => ({editable: activeEditor.isEditable,inGallery: isImageInGallery(activeEditor.state.doc, getPos()),+ explicitlySelected: imageSelectionKey.getState(activeEditor.state) === getPos(),}),});- const showSizeControls = editable && selected && !inGallery;+ const showSizeControls = editable && selected && explicitlySelected && !inGallery;const alt = typeof node.attrs.alt === "string" ? node.attrs.alt : "";const title = typeof node.attrs.title === "string" ? node.attrs.title : "";const src = typeof node.attrs.src === "string" ? node.attrs.src : "";@@ -235,11 +237,10 @@ const ResizableImageNodeView = ({<NodeViewWrapperref={wrapperRef}as="figure"- className={cn("edgeever-image-node", selected && "is-selected")}+ className={cn("edgeever-image-node", showSizeControls && "is-selected")}style={{ width: `${width}%`, ...(explicitAlignment ? imageAlignmentMargins(explicitAlignment) : {}) }}data-width={width}data-image-align={alignment}- onMouseEnter={showSizeControls ? undefined : requestImageMenu}onMouseLeave={hideImageMenu}onContextMenu={(event: MouseEvent<HTMLElement>) => {event.preventDefault();@@ -260,7 +261,10 @@ const ResizableImageNodeView = ({// A positioned figure does not occupy the full block wrapper. Select// its node explicitly instead of relying on coordinate hit testing.event.preventDefault();- editor.chain().focus().setNodeSelection(position).run();+ editor.chain().focus().setNodeSelection(position).command(({ tr }) => {+ tr.setMeta(imageSelectionKey, position);+ return true;+ }).run();}}onDoubleClick={(event) => {event.preventDefault();@@ -347,6 +351,9 @@ const ResizableImageNodeView = ({};export const ResizableImage = Image.extend({+ addProseMirrorPlugins() {+ return [...(this.parent?.() ?? []), createImageSelectionPlugin()];+ },addPasteRules() {return [createMarkdownImagePasteRule(this.type)];},diff --git a/apps/web/src/components/editor/editor-pane-helpers.ts b/apps/web/src/components/editor/editor-pane-helpers.tsindex f08c0dba..71199a38 100644--- a/apps/web/src/components/editor/editor-pane-helpers.ts+++ b/apps/web/src/components/editor/editor-pane-helpers.ts@@ -96,6 +96,9 @@ export const resetEditorDocument = (editor: Editor, content: TiptapDoc) => {doc: editor.schema.nodeFromJSON(content),plugins: editor.state.plugins,}));+ // updateState bypasses TipTap's transaction subscribers. Refresh node-view+ // controls even when the new note reuses the same image and selection.+ editor.view.dispatch(editor.state.tr.setMeta("addToHistory", false));releaseHtmlMediaSources({querySelectorAll: (selector)=>previousMedia.filter(node=>node.matches(selector) && !root.contains(node)),});diff --git a/apps/web/src/components/editor/image-selection.test.mjs b/apps/web/src/components/editor/image-selection.test.mjsnew file mode 100644index 00000000..432189e7--- /dev/null+++ b/apps/web/src/components/editor/image-selection.test.mjs@@ -0,0 +1,51 @@+import { describe, expect, test } from "bun:test";+import { getSchema } from "@tiptap/core";+import StarterKit from "@tiptap/starter-kit";+import Image from "@tiptap/extension-image";+import { EditorState, NodeSelection, TextSelection } from "@tiptap/pm/state";+import { createImageSelectionPlugin, imageSelectionKey } from "./image-selection";++const schema = getSchema([StarterKit, Image]);+const image = () => schema.nodes.image.create({ src: "photo.png" });+const createState = (content = [image()]) => EditorState.create({+ doc: schema.nodes.doc.create(null, content),+ plugins: [createImageSelectionPlugin()],+});+const clickImage = (state, position = 0) => state.apply(state.tr+ .setSelection(NodeSelection.create(state.doc, position))+ .setMeta(imageSelectionKey, position));++describe("explicit image selection", () => {+ test("opening an image-only or image-first note does not activate image controls", () => {+ for (const content of [[image()], [image(), schema.nodes.paragraph.create()]]) {+ const state = createState(content);+ expect(state.selection).toBeInstanceOf(NodeSelection);+ expect(imageSelectionKey.getState(state)).toBeNull();+ expect(imageSelectionKey.getState(state.apply(state.tr))).toBeNull();+ }+ });++ test("click activates an already default-selected image and survives size changes", () => {+ let state = clickImage(createState());+ expect(imageSelectionKey.getState(state)).toBe(0);+ state = state.apply(state.tr.setNodeMarkup(0, undefined, { src: "resized.png" }));+ expect(imageSelectionKey.getState(state)).toBe(0);+ });++ test("switching notes resets activation even when reusing identical content and plugins", () => {+ const state = clickImage(createState());+ const next = EditorState.create({ doc: state.doc, plugins: state.plugins });+ expect(next.selection).toBeInstanceOf(NodeSelection);+ expect(imageSelectionKey.getState(next)).toBeNull();+ });++ test("moving to text or another image requires clicking the image again", () => {+ let state = clickImage(createState([image(), image(), schema.nodes.paragraph.create()]));+ state = state.apply(state.tr.setSelection(NodeSelection.create(state.doc, 1)));+ expect(imageSelectionKey.getState(state)).toBeNull();+ state = clickImage(state, 1);+ expect(imageSelectionKey.getState(state)).toBe(1);+ state = state.apply(state.tr.setSelection(TextSelection.create(state.doc, 3)));+ expect(imageSelectionKey.getState(state)).toBeNull();+ });+});diff --git a/apps/web/src/components/editor/image-selection.ts b/apps/web/src/components/editor/image-selection.tsnew file mode 100644index 00000000..e0d03aea--- /dev/null+++ b/apps/web/src/components/editor/image-selection.ts@@ -0,0 +1,21 @@+import { NodeSelection, Plugin, PluginKey } from "@tiptap/pm/state";++// ProseMirror may select the first image while creating a document. Keep the+// user's explicit image selection separate from that required default selection.+export const imageSelectionKey = new PluginKey<number | null>("explicitImageSelection");++export const createImageSelectionPlugin = () => new Plugin<number | null>({+ key: imageSelectionKey,+ state: {+ init: () => null,+ apply(transaction, previous) {+ const requested = transaction.getMeta(imageSelectionKey) as number | undefined;+ const position = requested ?? (previous === null ? null : transaction.mapping.map(previous));+ const selection = transaction.selection;+ return position !== null && selection instanceof NodeSelection &&+ selection.node.type.name === "image" && selection.from === position+ ? position+ : null;+ },+ },+});diff --git a/tests/e2e/image-alignment.spec.ts b/tests/e2e/image-alignment.spec.tsindex ea2ee6b3..8ff89498 100644--- a/tests/e2e/image-alignment.spec.ts+++ b/tests/e2e/image-alignment.spec.ts@@ -48,6 +48,8 @@ test("positions individual images and preserves alignment through resize, undo,const first = images.nth(0);const second = images.nth(1);await expect(images).toHaveCount(2);+ await expect(first).not.toHaveClass(/is-selected/);+ await expect(rich.locator(".edgeever-image-controls")).toHaveCount(0);await expectPosition(first, "left");await selectImage(first);await first.getByRole("button", { name: "Center image", exact: true }).click();