-
Notifications
You must be signed in to change notification settings - Fork 4
ENG-1565 Convert image to node via hover icon #936
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
trangdoan982
wants to merge
8
commits into
main
Choose a base branch
from
eng-1565-convert-image-to-node-via-hover-icon
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+192
−35
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
66051df
ENG-1565 Convert image to node via hover icon
trangdoan982 0bfabe8
revert prettier reformatting of style.css, keep only new CSS
trangdoan982 4c5f86d
some changes
trangdoan982 9be050f
some changes
trangdoan982 fed9419
address PR comments
trangdoan982 deaa646
Update apps/obsidian/src/utils/imageEmbedHoverIcon.ts
trangdoan982 7c38103
fix
trangdoan982 da18638
address PR comments
trangdoan982 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| import { | ||
| type PluginValue, | ||
| EditorView, | ||
| ViewPlugin, | ||
| ViewUpdate, | ||
| } from "@codemirror/view"; | ||
| import { setIcon, TFile } from "obsidian"; | ||
| import type DiscourseGraphPlugin from "~/index"; | ||
| import { | ||
| isImageFile, | ||
| openConvertImageToNodeModal, | ||
| } from "~/utils/editorMenuUtils"; | ||
|
|
||
| const ICON_CLASS = "dg-image-convert-icon"; | ||
|
|
||
| const resolveImageFile = ( | ||
| embedEl: HTMLElement, | ||
| plugin: DiscourseGraphPlugin, | ||
| ): TFile | null => { | ||
| const src = embedEl.getAttribute("src"); | ||
| if (!src) return null; | ||
|
|
||
| const activeFile = plugin.app.workspace.getActiveFile(); | ||
| if (!activeFile) return null; | ||
|
|
||
| const resolved = plugin.app.metadataCache.getFirstLinkpathDest( | ||
| src, | ||
| activeFile.path, | ||
| ); | ||
| if (!resolved || !isImageFile(resolved)) return null; | ||
|
|
||
| return resolved; | ||
| }; | ||
|
|
||
| const createConvertIcon = ( | ||
| embedEl: HTMLElement, | ||
| plugin: DiscourseGraphPlugin, | ||
| ): HTMLButtonElement => { | ||
| const btn = document.createElement("button"); | ||
| btn.className = `${ICON_CLASS} absolute z-[2] right-[42px] w-[26px] h-[26px] flex cursor-[var(--cursor)] border-none opacity-0 pointer-events-none group-hover:opacity-100 group-hover:pointer-events-auto`; | ||
| btn.style.cssText = ` | ||
| top: var(--size-2-2); | ||
| padding: var(--size-2-2) var(--size-2-3); | ||
| color: var(--text-muted); | ||
| background-color: var(--background-primary); | ||
| `; | ||
| btn.title = "Convert to node"; | ||
| setIcon(btn, "file-input"); | ||
|
|
||
| btn.addEventListener("click", (e) => { | ||
| e.stopPropagation(); | ||
| e.preventDefault(); | ||
|
|
||
| const imageFile = resolveImageFile(embedEl, plugin); | ||
| if (!imageFile) return; | ||
|
|
||
| openConvertImageToNodeModal({ plugin, imageFile }); | ||
| }); | ||
|
|
||
| return btn; | ||
| }; | ||
|
|
||
| const processContainer = ( | ||
| container: HTMLElement, | ||
| plugin: DiscourseGraphPlugin, | ||
| ): void => { | ||
| const embeds = container.querySelectorAll<HTMLElement>( | ||
| ".internal-embed.image-embed", | ||
| ); | ||
|
|
||
| for (const embedEl of embeds) { | ||
| if (embedEl.querySelector(`.${ICON_CLASS}`)) continue; | ||
|
|
||
| const imageFile = resolveImageFile(embedEl, plugin); | ||
| if (!imageFile) continue; | ||
|
|
||
| embedEl.classList.add("group", "relative"); | ||
| embedEl.appendChild(createConvertIcon(embedEl, plugin)); | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * CodeMirror ViewPlugin that adds a "Convert to node" hover icon | ||
| * on embedded images in the live-preview editor. | ||
| */ | ||
| export const createImageEmbedHoverExtension = ( | ||
| plugin: DiscourseGraphPlugin, | ||
| ): ViewPlugin<PluginValue> => { | ||
| return ViewPlugin.fromClass( | ||
| class { | ||
| private dom: HTMLElement; | ||
| private observer: MutationObserver; | ||
|
|
||
| constructor(view: EditorView) { | ||
| this.dom = view.dom; | ||
| processContainer(view.dom, plugin); | ||
|
|
||
| // Obsidian renders embeds asynchronously after doc changes, | ||
| // so we need a MutationObserver to catch newly added image embeds. | ||
| this.observer = new MutationObserver((mutations) => { | ||
| const hasRelevantMutation = mutations.some((m) => | ||
| Array.from(m.addedNodes).some( | ||
| (n) => | ||
| n instanceof HTMLElement && | ||
| !n.classList.contains(ICON_CLASS) && | ||
| (n.matches(".internal-embed.image-embed") || | ||
| n.querySelector(".internal-embed.image-embed")), | ||
| ), | ||
| ); | ||
| if (hasRelevantMutation) { | ||
| processContainer(this.dom, plugin); | ||
| } | ||
| }); | ||
| this.observer.observe(this.dom, { | ||
| childList: true, | ||
| subtree: true, | ||
| }); | ||
| } | ||
|
|
||
| update(update: ViewUpdate): void { | ||
| if (update.docChanged || update.viewportChanged) { | ||
| processContainer(update.view.dom, plugin); | ||
| } | ||
| } | ||
|
|
||
| destroy(): void { | ||
| this.observer.disconnect(); | ||
| const icons = this.dom.querySelectorAll(`.${ICON_CLASS}`); | ||
| icons.forEach((icon) => icon.remove()); | ||
| } | ||
| }, | ||
| ); | ||
| }; | ||
devin-ai-integration[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔴 Hardcoded inline styles via
style.cssTextviolates AGENTS.md ruleThe button created in
createConvertIconsets multiple CSS properties viabtn.style.cssTextat lines 41–46. The mandatory rule inapps/obsidian/AGENTS.md:100states: "Do not hardcode styles inline — use CSS classes and Obsidian's CSS variables." While the values reference CSS variables, the delivery mechanism is inlinestyle.cssText, which directly violates this rule. These properties should be moved to a CSS class in the plugin's stylesheet (e.g.,styles.cssorsrc/styles/).Prompt for agents
Was this helpful? React with 👍 or 👎 to provide feedback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these styles are here to make sure we matches Obsidian styling choices. All these vars are interpretable by Obsidian