From a1f5181e58e72e2bf522cc5ccb768b41329f96fb Mon Sep 17 00:00:00 2001
From: bincxz <16399091+binaricat@users.noreply.github.com>
Date: Mon, 24 Aug 2026 21:55:11 +0800
Subject: [PATCH] fix(notes): avoid repeated code toolbar mutations
---
.../notes/InlineMarkdownEditor.test.tsx | 41 ++++++++++++++++++-
components/notes/InlineMarkdownEditor.tsx | 1 +
2 files changed, 41 insertions(+), 1 deletion(-)
diff --git a/components/notes/InlineMarkdownEditor.test.tsx b/components/notes/InlineMarkdownEditor.test.tsx
index 01b5b7c4e..2963118c7 100644
--- a/components/notes/InlineMarkdownEditor.test.tsx
+++ b/components/notes/InlineMarkdownEditor.test.tsx
@@ -1,8 +1,10 @@
import test from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
+import { JSDOM } from "jsdom";
import {
+ annotateNoteCodeBlockDeleteButtons,
createNoteDecorationMutationScheduler,
getHostPickerTriggerRange,
getNoteDecorationMutationDelay,
@@ -632,8 +634,45 @@ test("annotateNoteCodeBlockCopyButtons treats a CSS-hidden toolbar as absent so
assert.match(source, /wrapper\.appendChild\(firstButton\)/);
});
-test("annotateNoteCodeBlockDeleteButtons swaps the delete icon to a stroke SVG", () => {
+test("annotateNoteCodeBlockDeleteButtons swaps the delete icon only once", () => {
const source = readFileSync(new URL("./InlineMarkdownEditor.tsx", import.meta.url), "utf8");
+ const dom = new JSDOM(`
+
+ `);
+ const previousHTMLElement = Object.getOwnPropertyDescriptor(globalThis, "HTMLElement");
+ Object.defineProperty(globalThis, "HTMLElement", {
+ configurable: true,
+ value: dom.window.HTMLElement,
+ });
+
+ try {
+ const container = dom.window.document.querySelector("#container") as HTMLElement;
+ const deleteButton = dom.window.document.querySelector("#delete") as HTMLButtonElement;
+ annotateNoteCodeBlockDeleteButtons(container);
+ const installedIcon = deleteButton.firstElementChild;
+ assert.equal(deleteButton.dataset.noteCodeDelete, "true");
+ assert.equal(installedIcon?.nodeName.toLowerCase(), "svg");
+ const observer = new dom.window.MutationObserver(() => {});
+ observer.observe(deleteButton, { attributes: true, childList: true, subtree: true });
+
+ annotateNoteCodeBlockDeleteButtons(container);
+
+ assert.equal(observer.takeRecords().length, 0);
+ assert.equal(deleteButton.firstElementChild, installedIcon);
+ observer.disconnect();
+ } finally {
+ if (previousHTMLElement) {
+ Object.defineProperty(globalThis, "HTMLElement", previousHTMLElement);
+ } else {
+ delete (globalThis as { HTMLElement?: unknown }).HTMLElement;
+ }
+ dom.window.close();
+ }
assert.match(source, /export const annotateNoteCodeBlockDeleteButtons/);
assert.match(source, /data-note-code-delete/);
diff --git a/components/notes/InlineMarkdownEditor.tsx b/components/notes/InlineMarkdownEditor.tsx
index 885f63330..fe16767a0 100644
--- a/components/notes/InlineMarkdownEditor.tsx
+++ b/components/notes/InlineMarkdownEditor.tsx
@@ -780,6 +780,7 @@ export const annotateNoteCodeBlockCopyButtons = (
export const annotateNoteCodeBlockDeleteButtons = (container: HTMLElement): void => {
container.querySelectorAll('[class*="_codeMirrorToolbar_"]').forEach((toolbar) => {
if (!(toolbar instanceof HTMLElement)) return;
+ if (toolbar.querySelector("[data-note-code-delete]")) return;
const deleteButton = Array.from(toolbar.querySelectorAll("button")).find(
(button) =>