(embedEl: HTMLElement, plugin: CodeSpacePlugin)
| 478 | } |
| 479 | |
| 480 | function resolveSourcePathForEmbed(embedEl: HTMLElement, plugin: CodeSpacePlugin): string { |
| 481 | const rememberedSourcePath = getRememberedSourcePath(embedEl); |
| 482 | if (rememberedSourcePath) return rememberedSourcePath; |
| 483 | |
| 484 | const ancestorSourcePath = resolveSourcePathFromAncestors(embedEl, plugin); |
| 485 | if (ancestorSourcePath) { |
| 486 | rememberSourcePath(embedEl, ancestorSourcePath); |
| 487 | return ancestorSourcePath; |
| 488 | } |
| 489 | |
| 490 | // Use the containing leaf's file path (works across popout windows) instead of activeFile. |
| 491 | try { |
| 492 | let matchedPath = ""; |
| 493 | |
| 494 | plugin.app.workspace.iterateAllLeaves((leaf) => { |
| 495 | if (matchedPath) return; |
| 496 | const view = leaf.view as unknown as { file?: TFile; containerEl?: HTMLElement } | null; |
| 497 | const containerEl = view?.containerEl; |
| 498 | if (containerEl && containerEl.contains(embedEl)) { |
| 499 | const filePath = view?.file?.path; |
| 500 | if (filePath) { |
| 501 | matchedPath = filePath; |
| 502 | } |
| 503 | } |
| 504 | }); |
| 505 | |
| 506 | if (matchedPath) { |
| 507 | rememberSourcePath(embedEl, matchedPath); |
| 508 | return matchedPath; |
| 509 | } |
| 510 | } catch { |
| 511 | // Ignore and fall back. |
| 512 | } |
| 513 | |
| 514 | const activePath = plugin.app.workspace.getActiveFile()?.path ?? ""; |
| 515 | if (activePath) rememberSourcePath(embedEl, activePath); |
| 516 | return activePath; |
| 517 | } |
| 518 | |
| 519 | function installEmbedObserverForDocument(doc: Document, docWindow: Window, plugin: CodeSpacePlugin) { |
| 520 | if (embedObserversByDoc.has(doc)) return; |
no test coverage detected