-
Notifications
You must be signed in to change notification settings - Fork 99
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3440,7 +3440,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) { | ||||||||||||||||
|
|
@@ -3449,6 +3452,62 @@ 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'); | ||||||||||||||||
|
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.
Wrapping image elements with Useful? React with 👍 / 👎. |
||||||||||||||||
|
|
||||||||||||||||
| if (sanitized.protocol === 'http' || sanitized.protocol === 'https') { | ||||||||||||||||
| anchor.target = '_blank'; | ||||||||||||||||
| anchor.rel = 'noopener noreferrer'; | ||||||||||||||||
| } | ||||||||||||||||
| if (hyperlink.tooltip) { | ||||||||||||||||
| anchor.title = hyperlink.tooltip; | ||||||||||||||||
| } | ||||||||||||||||
|
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): HTMLElement { | ||||||||||||||||
| try { | ||||||||||||||||
| const lookup = this.blockLookup.get(fragment.blockId); | ||||||||||||||||
|
|
@@ -4941,7 +5000,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) | ||||||||||||||||
|
|
@@ -4996,10 +5055,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'); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| /** | ||||||||||||||||
|
|
||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -313,6 +313,18 @@ export function imageNodeToBlock( | |
| ...(rotation !== undefined && { rotation }), | ||
| ...(flipH !== undefined && { flipH }), | ||
| ...(flipV !== undefined && { flipV }), | ||
| // Image hyperlink from OOXML a:hlinkClick | ||
| ...(() => { | ||
|
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. same parsing logic exists in |
||
| const hlAttr = isPlainObject(attrs.hyperlink) ? attrs.hyperlink : undefined; | ||
| if (hlAttr && typeof hlAttr.url === 'string' && hlAttr.url.trim()) { | ||
| const hyperlink: { url: string; tooltip?: string } = { url: hlAttr.url as string }; | ||
| if (typeof hlAttr.tooltip === 'string' && (hlAttr.tooltip as string).trim()) { | ||
| hyperlink.tooltip = hlAttr.tooltip as string; | ||
| } | ||
| return { hyperlink }; | ||
| } | ||
| return {}; | ||
| })(), | ||
| }; | ||
| } | ||
|
|
||
|
|
||
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.
layoutis created here but never used — the tests build their own. can be removed.