Files
zsviczian 8dbd9ad7ad Fix embed controls and add optional empty-tab drawing creation
Prefer physical table ownership for wrapped embeds and reset relative mobile toolbar offsets. Add independent ribbon and empty-tab action preferences using the canonical creation/settings pipelines, with maintained translations and deterministic cleanup.

Toggle pen to selection on a second unmodified P at the plugin capture boundary. Cancel incomplete Extras APIs cleanly while preserving version gates; the incomplete #3051 reporter cause remains unconfirmed. Pair native video coverage with fork PR #484's two-condition iframe fix.

Validation: 781 plugin source cases, five bundle checks, development/production builds, focused lint, CSS and inventory pass. Fork source/browser/type/audit evidence and inherited-test limitations are documented. Native CLI testing is explicitly paused at maintainer request. Versions and dependencies remain unchanged. Refs #3049, #3047, #3044, #3048 and #3051.
2026-11-14 08:30:51 +01:00

63 lines
5.0 KiB
JavaScript

/** Exercise the real bridge's scheduling with public CM ranges and controlled DOM observers. */
import assert from "node:assert/strict";
import test from "node:test";
import { EditorState, StateField } from "@codemirror/state";
import * as cmView from "@codemirror/view";
import { sourceHarness } from "./sourceHarness.mjs";
for (const tableEmbed of [false, true]) for (const selfEmbed of [false, true]) test((tableEmbed ? "table embeds with internal-embed wrappers: " : "") + (selfEmbed
? "drawing Markdown self-embeds retain controls and the same lifecycle guards"
: "invalid render generations cannot remount controls or starve the renderer debounce"), async () => {
const pending = [], originalQueue = globalThis.queueMicrotask, observers = [];
let generation = true, loaded = 0, unloaded = 0, retireWhileMapping = false;
globalThis.queueMicrotask = callback => pending.push(callback);
const drawing = { path: "Map.excalidraw.md" }, file = selfEmbed ? drawing : { path: "Source.md" };
const token = tableEmbed ? "![[Map.excalidraw.md\\|180]]" : "![[Map.excalidraw.md|180]]";
const text = tableEmbed ? `| Drawing |\n| --- |\n| ${token} |` : token;
class MarkdownView { mode = "source"; file = file; getMode() { return this.mode; } }
const info = new MarkdownView(), sourceContainer = { style: { display: "" } };
const editorInfoField = StateField.define({ create: () => info, update: value => value });
const editorLivePreviewField = StateField.define({ create: () => true, update: value => value });
const doc = { defaultView: { MutationObserver: class {
constructor(callback) { this.callback = callback; observers.push(this); }
observe(target) { this.connected = true; (this.targets ??= []).push(target); }
disconnect() { this.connected = false; }
} } };
const frame = { isConnected: true, ownerDocument: doc }, parent = { childNodes: [] };
const native = { parentNode: parent, closest: () => null, contains: el => el === frame };
const root = { parentNode: parent, closest: () => null, contains: el => el === frame };
const table = { closest: () => root, rows: [{ cells: [{}] }, { cells: [{}] }] };
const row = { parentElement: { parentElement: table }, rowIndex: 1 };
const cell = { closest: () => table, parentElement: row, colSpan: 1, rowSpan: 1, cellIndex: 0 };
parent.childNodes.push(tableEmbed ? root : native);
const contentDOM = { querySelectorAll: () => [image], contains: el => el === frame };
const image = { parentElement: frame, closest: selector => ({ ".internal-embed": native, ".cm-content": contentDOM,
"table.table-editor": tableEmbed ? table : null, "td, th": tableEmbed ? cell : null })[selector] ?? null };
const actions = { target: drawing.path, isCurrent: () => generation };
const mutation = () => { for (const observer of observers) if (observer.connected) observer.callback(); };
const graph = sourceHarness({
obsidian: { editorInfoField, editorLivePreviewField, MarkdownView },
"@codemirror/view": { ...cmView, ViewPlugin: { fromClass: value => value } },
"../managers/DrawingEmbedControls": { DrawingEmbedControls: class { load() { loaded++; mutation(); } unload() { unloaded++; mutation(); } } },
"../managers/drawingEmbedActions": { getDrawingEmbedActions: () => actions },
});
try {
const { createDrawingEmbedExtension } = await graph.importModule("src/core/editor/drawingEmbedExtension.ts");
const plugin = { isExcalidrawFile: value => value === drawing, app: { metadataCache: { getFirstLinkpathDest: () => drawing } } };
const bridge = createDrawingEmbedExtension(plugin);
const view = { dom: { ownerDocument: doc, closest: () => sourceContainer }, contentDOM, state: EditorState.create({ doc: text + "\n", extensions: [editorInfoField, editorLivePreviewField] }),
posAtDOM(el, offset) { if (retireWhileMapping) generation = false; return el === parent && offset === 1 ? text.length + 1 : 0; } };
const drain = () => { for (let i = 0; pending.length && i < 10; i++) pending.shift()(); assert.equal(pending.length, 0, "unbounded remount/mutation loop"); };
new bridge.extension(view); drain(); assert.equal(loaded, 1);
assert.ok(observers[0].targets.includes(sourceContainer), "source visibility changes must schedule ownership sync");
info.mode = "preview"; mutation(); drain(); assert.equal(loaded, 1); assert.equal(unloaded, 1);
info.mode = "source"; sourceContainer.style.display = "none"; mutation(); drain(); assert.equal(loaded, 1);
sourceContainer.style.display = ""; mutation(); drain(); assert.equal(loaded, 2);
generation = false; mutation(); drain(); assert.equal(loaded, 2); assert.equal(unloaded, 2);
generation = true; mutation(); drain(); assert.equal(loaded, 3);
generation = false; mutation(); drain();
generation = true; retireWhileMapping = true; mutation(); drain(); assert.equal(loaded, 3, "source invalidated during DOM mapping was mounted");
bridge.dispose(); drain(); assert.ok(observers.every(observer => !observer.connected));
} finally { graph.close(); globalThis.queueMicrotask = originalQueue; }
});