mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-23 16:42:19 +00:00
fix(notes): avoid repeated code toolbar mutations
This commit is contained in:
1 parent
971f702c32
commit
a1f5181e58
2 files changed
+41
-1
No files matched your search
@@ -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(`
|
||||
<div id="container">
|
||||
<div class="_codeMirrorToolbar_test">
|
||||
<button class="_toolbarCodeBlockLanguageSelectTrigger_test"></button>
|
||||
<button id="delete"></button>
|
||||
</div>
|
||||
</div>
|
||||
`);
|
||||
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/);
|
||||
|
||||
@@ -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) =>
|
||||
|
||||
Reference in new issue
Block a user