-
Notifications
You must be signed in to change notification settings - Fork 105
fix: support hyperlinks on DrawingML images (a:hlinkClick) #2552
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
iguit0
wants to merge
2
commits into
superdoc-dev:main
Choose a base branch
from
iguit0:fix/drawingml-image-hyperlinks
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.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3668,7 +3668,10 @@ export class DomPainter { | |||||||||||||||
| if (filters.length > 0) { | ||||||||||||||||
| img.style.filter = filters.join(' '); | ||||||||||||||||
| } | ||||||||||||||||
| fragmentEl.appendChild(img); | ||||||||||||||||
|
|
||||||||||||||||
| // Wrap in anchor when block has a DrawingML hyperlink (a:hlinkClick) | ||||||||||||||||
| const imageChild = this.buildImageHyperlinkAnchor(img, block.hyperlink, 'block'); | ||||||||||||||||
| fragmentEl.appendChild(imageChild); | ||||||||||||||||
|
|
||||||||||||||||
| return fragmentEl; | ||||||||||||||||
| } catch (error) { | ||||||||||||||||
|
|
@@ -3677,6 +3680,65 @@ export class DomPainter { | |||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| /** | ||||||||||||||||
| * Optionally wrap an image element in an anchor for DrawingML hyperlinks (a:hlinkClick). | ||||||||||||||||
| * | ||||||||||||||||
| * When `hyperlink` is present and its URL passes sanitization, returns an | ||||||||||||||||
| * `<a class="superdoc-link">` wrapping `imageEl`. The existing EditorInputManager | ||||||||||||||||
| * click-delegation on `a.superdoc-link` handles both viewing-mode navigation and | ||||||||||||||||
| * editing-mode event dispatch automatically, with no extra wiring needed here. | ||||||||||||||||
| * | ||||||||||||||||
| * When `hyperlink` is absent or the URL fails sanitization the original element | ||||||||||||||||
| * is returned unchanged. | ||||||||||||||||
| * | ||||||||||||||||
| * @param imageEl - The image element (img or span wrapper) to potentially wrap. | ||||||||||||||||
| * @param hyperlink - Hyperlink metadata from the ImageBlock/ImageRun, or undefined. | ||||||||||||||||
| * @param display - CSS display value for the anchor: 'block' for fragment images, | ||||||||||||||||
| * 'inline-block' for inline runs. | ||||||||||||||||
| */ | ||||||||||||||||
| private buildImageHyperlinkAnchor( | ||||||||||||||||
| imageEl: HTMLElement, | ||||||||||||||||
| hyperlink: { url: string; tooltip?: string } | undefined, | ||||||||||||||||
| display: 'block' | 'inline-block', | ||||||||||||||||
| ): HTMLElement { | ||||||||||||||||
| if (!hyperlink?.url || !this.doc) return imageEl; | ||||||||||||||||
|
|
||||||||||||||||
| const sanitized = sanitizeHref(hyperlink.url); | ||||||||||||||||
| if (!sanitized?.href) return imageEl; | ||||||||||||||||
|
|
||||||||||||||||
| const anchor = this.doc.createElement('a'); | ||||||||||||||||
| anchor.href = sanitized.href; | ||||||||||||||||
| anchor.classList.add('superdoc-link'); | ||||||||||||||||
|
|
||||||||||||||||
| if (sanitized.protocol === 'http' || sanitized.protocol === 'https') { | ||||||||||||||||
| anchor.target = '_blank'; | ||||||||||||||||
| anchor.rel = 'noopener noreferrer'; | ||||||||||||||||
| } | ||||||||||||||||
| if (hyperlink.tooltip) { | ||||||||||||||||
| const tooltipResult = encodeTooltip(hyperlink.tooltip); | ||||||||||||||||
| if (tooltipResult?.text) { | ||||||||||||||||
| anchor.title = tooltipResult.text; | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. text links run the tooltip through
Suggested change
|
||||||||||||||||
|
|
||||||||||||||||
| // Accessibility: explicit role and keyboard focus (mirrors applyLinkAttributes for text links) | ||||||||||||||||
| anchor.setAttribute('role', 'link'); | ||||||||||||||||
| anchor.setAttribute('tabindex', '0'); | ||||||||||||||||
|
|
||||||||||||||||
| if (display === 'block') { | ||||||||||||||||
| anchor.style.cssText = 'display: block; width: 100%; height: 100%; cursor: pointer;'; | ||||||||||||||||
| } else { | ||||||||||||||||
| // inline-block preserves the image's layout box inside a paragraph line | ||||||||||||||||
| anchor.style.display = 'inline-block'; | ||||||||||||||||
| anchor.style.lineHeight = '0'; | ||||||||||||||||
| anchor.style.cursor = 'pointer'; | ||||||||||||||||
| anchor.style.verticalAlign = imageEl.style.verticalAlign || 'bottom'; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| anchor.appendChild(imageEl); | ||||||||||||||||
| return anchor; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| private renderDrawingFragment( | ||||||||||||||||
| fragment: DrawingFragment, | ||||||||||||||||
| context: FragmentRenderContext, | ||||||||||||||||
|
|
@@ -5246,7 +5308,7 @@ export class DomPainter { | |||||||||||||||
| this.applySdtDataset(wrapper, run.sdt); | ||||||||||||||||
| if (run.dataAttrs) applyRunDataAttributes(wrapper, run.dataAttrs); | ||||||||||||||||
| wrapper.appendChild(img); | ||||||||||||||||
| return wrapper; | ||||||||||||||||
| return this.buildImageHyperlinkAnchor(wrapper, run.hyperlink, 'inline-block'); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| // Apply PM position tracking for cursor placement (only on img when not wrapped) | ||||||||||||||||
|
|
@@ -5301,10 +5363,10 @@ export class DomPainter { | |||||||||||||||
| this.applySdtDataset(wrapper, run.sdt); | ||||||||||||||||
|
|
||||||||||||||||
| wrapper.appendChild(img); | ||||||||||||||||
| return wrapper; | ||||||||||||||||
| return this.buildImageHyperlinkAnchor(wrapper, run.hyperlink, 'inline-block'); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| return img; | ||||||||||||||||
| return this.buildImageHyperlinkAnchor(img, run.hyperlink, 'inline-block'); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| /** | ||||||||||||||||
|
|
||||||||||||||||
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.
Wrapping image elements with
a.superdoc-linkcausesEditorInputManager.#handlePointerDownto take the link fast-path (closest('a.superdoc-link')) and return before the inline/block image selection paths run (#handleInlineImageClick/#handleFragmentClick). As a result, in editable documents, clicking a hyperlinked image no longer produces image node selection or resize-overlay activation, so linked images become effectively non-selectable by pointer.Useful? React with 👍 / 👎.
Uh oh!
There was an error while loading. Please reload this page.
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.
@iguit0 this is a good catch — clicking a linked image in edit mode opens the link instead of selecting the image. the link handler runs first and skips the image selection path entirely.