(doc: Document, docWindow: Window, plugin: CodeSpacePlugin)
| 517 | } |
| 518 | |
| 519 | function installEmbedObserverForDocument(doc: Document, docWindow: Window, plugin: CodeSpacePlugin) { |
| 520 | if (embedObserversByDoc.has(doc)) return; |
| 521 | if (!doc.body) { |
| 522 | // Popout windows may fire before body is ready; retry shortly. |
| 523 | docWindow.setTimeout(() => installEmbedObserverForDocument(doc, docWindow, plugin), 80); |
| 524 | return; |
| 525 | } |
| 526 | |
| 527 | const observer = new MutationObserver((mutations) => { |
| 528 | for (const mutation of mutations) { |
| 529 | for (const node of Array.from(mutation.removedNodes)) { |
| 530 | if (node.nodeType !== 1) continue; |
| 531 | |
| 532 | const elem = node as Element; |
| 533 | const removedEmbeds: HTMLElement[] = []; |
| 534 | |
| 535 | if (elem.classList.contains("file-embed")) { |
| 536 | removedEmbeds.push(elem as HTMLElement); |
| 537 | } else { |
| 538 | elem.querySelectorAll?.(".file-embed").forEach((e) => removedEmbeds.push(e as HTMLElement)); |
| 539 | } |
| 540 | |
| 541 | for (const embedEl of removedEmbeds) { |
| 542 | disposeCodeEmbed(embedEl); |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | for (const node of Array.from(mutation.addedNodes)) { |
| 547 | // Cross-window safe: use numeric constant instead of `node instanceof Element`. |
| 548 | if (node.nodeType !== 1) continue; |
| 549 | |
| 550 | const elem = node as Element; |
| 551 | const embeds: HTMLElement[] = []; |
| 552 | |
| 553 | if (elem.classList.contains("file-embed")) { |
| 554 | embeds.push(elem as HTMLElement); |
| 555 | } else { |
| 556 | // Look for both div.file-embed and span.file-embed |
| 557 | elem.querySelectorAll?.(".file-embed").forEach((e) => embeds.push(e as HTMLElement)); |
| 558 | } |
| 559 | |
| 560 | for (const embedEl of embeds) { |
| 561 | const sourcePath = resolveSourcePathForEmbed(embedEl, plugin); |
| 562 | // For ambiguous bare filenames, we need a real sourcePath; wait for the leaf to be ready. |
| 563 | if (!sourcePath) { |
| 564 | docWindow.setTimeout(() => { |
| 565 | const retrySourcePath = resolveSourcePathForEmbed(embedEl, plugin); |
| 566 | if (!retrySourcePath) return; |
| 567 | rememberSourcePath(embedEl, retrySourcePath); |
| 568 | scheduleProcessCodeEmbed(embedEl, plugin, retrySourcePath); |
| 569 | }, 120); |
| 570 | continue; |
| 571 | } |
| 572 | rememberSourcePath(embedEl, sourcePath); |
| 573 | scheduleProcessCodeEmbed(embedEl, plugin, sourcePath); |
| 574 | } |
| 575 | } |
| 576 | } |
no test coverage detected