⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.
TypeScript honoissue-managementissue-trackerjira-alternativekanbanlinear-alternativemcpproject-managementreactself-hostedtypescript
fix(web): keep task description editor alive across task switches (#1581)
ab1921309b2a3fa02722
diff --git a/apps/web/src/components/task/task-description.test.tsx b/apps/web/src/components/task/task-description.test.tsxnew file mode 100644index 000000000..0df6ebeb4--- /dev/null+++ b/apps/web/src/components/task/task-description.test.tsx@@ -0,0 +1,83 @@+import { render } from "@testing-library/react";+import { describe, expect, it, vi } from "vitest";+import TaskDescription from "./task-description";++const mocks = vi.hoisted(() => ({+ useEditor: vi.fn(),+ t: (key: string) => key,+}));++vi.mock("@tiptap/react", () => ({+ useEditor: mocks.useEditor,+ EditorContent: () => null,+ NodeViewWrapper: ({ children }: { children: unknown }) => children,+ ReactNodeViewRenderer: () => () => null,+}));+vi.mock("@tiptap/react/menus", () => ({ BubbleMenu: () => null }));+vi.mock("./extensions/attachment-card", () => ({+ AttachmentCard: { configure: () => ({}) },+}));+vi.mock("./extensions/embed-block", () => ({+ EmbedBlock: { configure: () => ({}) },+}));+vi.mock("./extensions/kaneo-issue-link", () => ({+ KaneoIssueLink: { configure: () => ({}) },+}));+vi.mock("./extensions/mermaid-block", () => ({+ MermaidBlock: { configure: () => ({}) },+}));+vi.mock("./extensions/shiki-code-block", () => ({+ ShikiCodeBlock: { configure: () => ({}) },+}));+vi.mock("./extensions/task-item-with-checkbox", () => ({+ TaskItemWithCheckbox: { configure: () => ({}) },+}));+vi.mock("react-i18next", () => ({+ useTranslation: () => ({ t: mocks.t }),+ initReactI18next: { type: "3rdParty", init: () => {} },+}));+vi.mock("@/hooks/queries/task/use-get-task", () => ({+ default: () => ({ data: undefined }),+}));+vi.mock("@/hooks/mutations/task/use-update-task-description", () => ({+ useUpdateTaskDescription: () => ({ mutateAsync: vi.fn() }),+}));+vi.mock("@/hooks/use-workspace-permission", () => ({+ useWorkspacePermission: () => ({ canManageTasks: () => true }),+}));+vi.mock("@/lib/toast", () => ({+ toast: {+ error: vi.fn(),+ success: vi.fn(),+ loading: vi.fn(),+ dismiss: vi.fn(),+ },+}));+vi.mock("@/lib/upload-task-image", () => ({ uploadTaskImage: vi.fn() }));+vi.mock("@/lib/shiki-highlighter", () => ({+ getSharedShikiHighlighter: () => Promise.resolve({}),+}));++describe("TaskDescription", () => {+ it("keeps the useEditor deps stable when the task id changes", () => {+ const depsPerRender: unknown[][] = [];+ mocks.useEditor.mockImplementation((_options: unknown, deps: unknown[]) => {+ depsPerRender.push(deps);+ return null;+ });++ const { rerender } = render(<TaskDescription taskId="task-a" />);+ rerender(<TaskDescription taskId="task-b" />);+ rerender(<TaskDescription taskId="task-c" />);++ expect(depsPerRender.length).toBeGreaterThanOrEqual(3);++ const [first, ...rest] = depsPerRender;+ for (const deps of rest) {+ expect(deps).toHaveLength(first.length);+ deps.forEach((dep, index) => {+ expect(dep).toBe(first[index]);+ });+ }+ });+});diff --git a/apps/web/src/components/task/task-description.tsx b/apps/web/src/components/task/task-description.tsxindex 77244db9c..f3f600a6b 100644--- a/apps/web/src/components/task/task-description.tsx+++ b/apps/web/src/components/task/task-description.tsx@@ -9,7 +9,7 @@ import TableRow from "@tiptap/extension-table-row";import TaskList from "@tiptap/extension-task-list";import { Markdown } from "@tiptap/markdown";import { Fragment, Slice } from "@tiptap/pm/model";-import { TextSelection } from "@tiptap/pm/state";+import { EditorState, TextSelection } from "@tiptap/pm/state";import { EditorContent, useEditor } from "@tiptap/react";import { BubbleMenu } from "@tiptap/react/menus";import StarterKit from "@tiptap/starter-kit";@@ -40,7 +40,14 @@ import {Underline as UnderlineIcon,} from "lucide-react";import type { MouseEvent as ReactMouseEvent } from "react";-import { useCallback, useEffect, useMemo, useRef, useState } from "react";+import {+ useCallback,+ useEffect,+ useLayoutEffect,+ useMemo,+ useRef,+ useState,+} from "react";import { useTranslation } from "react-i18next";import { bundledLanguages, type Highlighter } from "shiki";import { Button } from "@/components/ui/button";@@ -119,6 +126,31 @@ function formatMarkdown(markdown: string) {.replace(/\n{2,}$/g, "\n");}+function resetUndoHistory(editor: Editor) {+ const { schema, plugins, doc } = editor.state;+ editor.view.updateState(+ EditorState.create({+ schema,+ plugins,+ doc,+ }),+ );+}++function setContentWithoutUndoStep(editor: Editor, content: string) {+ editor+ .chain()+ .command(({ tr }) => {+ tr.setMeta("addToHistory", false);+ return true;+ })+ .setContent(content, {+ emitUpdate: false,+ contentType: "markdown",+ })+ .run();+}+type EmbedComposerState = {mode: "choice" | "input";url: string;@@ -274,12 +306,14 @@ export default function TaskDescription({ taskId }: TaskDescriptionProps) {const imageInputRef = useRef<HTMLInputElement | null>(null);const dragDepthRef = useRef(0);const taskRef = useRef(task);+ const taskIdRef = useRef(taskId);const updateTaskRef = useRef(updateTaskDescription);const activeTaskIdRef = useRef<string | null>(null);const lastEditorRef = useRef<Editor | null>(null);const pendingImageInsertRef = useRef<{editor: Editor;range?: SlashRange;+ taskId: string;} | null>(null);const hasHydratedRef = useRef(false);const isSyncingExternalContentRef = useRef(false);@@ -306,10 +340,11 @@ export default function TaskDescription({ taskId }: TaskDescriptionProps) {} | null>(null);const slashMenuRef = useRef<SlashMenuState | null>(null);- useEffect(() => {+ useLayoutEffect(() => {taskRef.current = task;+ taskIdRef.current = taskId;updateTaskRef.current = updateTaskDescription;- }, [task, updateTaskDescription]);+ }, [task, taskId, updateTaskDescription]);const shikiSupportedLanguages = useMemo(() => new Set([...Object.keys(bundledLanguages), "text"]),@@ -399,16 +434,23 @@ export default function TaskDescription({ taskId }: TaskDescriptionProps) {return;}+ const uploadTaskId = taskIdRef.current;const loadingToast = toast.loading(t("tasks:detail.editor.upload.loading"),);try {const uploadedAsset = await uploadTaskImage({- taskId,+ taskId: uploadTaskId,surface: "description",file,});++ if (activeEditor.isDestroyed || taskIdRef.current !== uploadTaskId) {+ toast.dismiss(loadingToast);+ return;+ }+insertUploadedAsset(activeEditor, uploadedAsset, range);toast.dismiss(loadingToast);@@ -426,13 +468,13 @@ export default function TaskDescription({ taskId }: TaskDescriptionProps) {);}},- [insertUploadedAsset, t, taskId],+ [insertUploadedAsset, t],);const openImagePicker = useCallback((activeEditor?: Editor | null, range?: SlashRange) => {pendingImageInsertRef.current = activeEditor- ? { editor: activeEditor, range }+ ? { editor: activeEditor, range, taskId: taskIdRef.current }: null;imageInputRef.current?.click();},@@ -960,7 +1002,7 @@ export default function TaskDescription({ taskId }: TaskDescriptionProps) {);useEffect(() => {- if (!editor) return;+ if (!editor || editor.isDestroyed) return;if (lastEditorRef.current !== editor) {hasHydratedRef.current = false;lastEditorRef.current = editor;@@ -977,10 +1019,10 @@ export default function TaskDescription({ taskId }: TaskDescriptionProps) {if (!hasHydratedRef.current) {isSyncingExternalContentRef.current = true;latestSyncedMarkdownRef.current = incomingMarkdown;- editor.commands.setContent(incomingMarkdown, {- emitUpdate: false,- contentType: "markdown",- });+ setContentWithoutUndoStep(editor, incomingMarkdown);+ if (isTaskChanged) {+ resetUndoHistory(editor);+ }hasHydratedRef.current = true;requestAnimationFrame(() => {isSyncingExternalContentRef.current = false;@@ -993,10 +1035,7 @@ export default function TaskDescription({ taskId }: TaskDescriptionProps) {isSyncingExternalContentRef.current = true;latestSyncedMarkdownRef.current = incomingMarkdown;- editor.commands.setContent(incomingMarkdown, {- emitUpdate: false,- contentType: "markdown",- });+ setContentWithoutUndoStep(editor, incomingMarkdown);requestAnimationFrame(() => {isSyncingExternalContentRef.current = false;});@@ -1271,7 +1310,7 @@ export default function TaskDescription({ taskId }: TaskDescriptionProps) {const copyHoveredCodeBlock = useCallback(async () => {if (!editor || !hoveredCodeBlock) return;const node = editor.state.doc.nodeAt(hoveredCodeBlock.nodePos);- if (!node || node.type.name !== "codeBlock") return;+ if (node?.type.name !== "codeBlock") return;const content = node.textContent || "";if (!content) return;@@ -1356,17 +1395,20 @@ export default function TaskDescription({ taskId }: TaskDescriptionProps) {className="sr-only"onChange={(event) => {const file = event.target.files?.[0];+ event.target.value = "";if (!file) return;const pendingInsert = pendingImageInsertRef.current;pendingImageInsertRef.current = null;+ if (!pendingInsert || pendingInsert.taskId !== taskIdRef.current) {+ return;+ }+void handleAssetFileUpload(file,- pendingInsert?.editor,- pendingInsert?.range,+ pendingInsert.editor,+ pendingInsert.range,);-- event.target.value = "";}}/>{editor && hoveredCodeBlock && (