|
1 | | -import type { Editor } from '../../externals.js'; |
| 1 | +import type { Editor, ProseMirrorNode } from '../../externals.js'; |
| 2 | +import { NodeSelection } from '../../externals.js'; |
2 | 3 | import { UmbTiptapToolbarElementApiBase } from '../tiptap-toolbar-element-api-base.js'; |
3 | 4 | import { getGuidFromUdi, imageSize } from '@umbraco-cms/backoffice/utils'; |
4 | 5 | import { ImageCropModeModel } from '@umbraco-cms/backoffice/external/backend-api'; |
@@ -41,36 +42,67 @@ export default class UmbTiptapToolbarMediaPickerToolbarExtensionApi extends UmbT |
41 | 42 |
|
42 | 43 | override async execute(editor: Editor) { |
43 | 44 | const currentTarget = editor.getAttributes('image'); |
44 | | - const figure = editor.getAttributes('figure'); |
| 45 | + const currentMediaUdi = this.#extractMediaUdi(currentTarget); |
| 46 | + const currentAltText = currentTarget?.alt; |
| 47 | + const currentCaption = this.#extractCaption(editor.state.selection); |
45 | 48 |
|
46 | | - let currentMediaUdi: string | undefined = undefined; |
47 | | - if (currentTarget?.['data-udi']) { |
48 | | - currentMediaUdi = getGuidFromUdi(currentTarget['data-udi']); |
49 | | - } |
| 49 | + await this.#updateImageWithMetadata(editor, currentMediaUdi, currentAltText, currentCaption); |
| 50 | + } |
50 | 51 |
|
51 | | - let currentAltText: string | undefined = undefined; |
52 | | - if (currentTarget?.alt) { |
53 | | - currentAltText = currentTarget.alt; |
54 | | - } |
| 52 | + async #updateImageWithMetadata( |
| 53 | + editor: Editor, |
| 54 | + currentMediaUdi: string | undefined, |
| 55 | + currentAltText: string | undefined, |
| 56 | + currentCaption: string | undefined, |
| 57 | + ) { |
| 58 | + const mediaGuid = await this.#getMediaGuid(currentMediaUdi); |
| 59 | + if (!mediaGuid) return; |
55 | 60 |
|
56 | | - let currentCaption: string | undefined = undefined; |
57 | | - if (figure?.figcaption) { |
58 | | - currentCaption = figure.figcaption; |
59 | | - } |
| 61 | + const media = await this.#showMediaCaptionAltText(mediaGuid, currentAltText, currentCaption); |
| 62 | + if (!media) return; |
60 | 63 |
|
61 | | - const selection = await this.#openMediaPicker(currentMediaUdi); |
62 | | - if (!selection?.length) return; |
| 64 | + this.#insertInEditor(editor, mediaGuid, media); |
| 65 | + } |
63 | 66 |
|
64 | | - const mediaGuid = selection[0]; |
| 67 | + #extractMediaUdi(imageAttributes: Record<string, unknown>): string | undefined { |
| 68 | + return imageAttributes?.['data-udi'] ? getGuidFromUdi(imageAttributes['data-udi'] as string) : undefined; |
| 69 | + } |
65 | 70 |
|
66 | | - if (!mediaGuid) { |
67 | | - throw new Error('No media selected'); |
| 71 | + #extractCaption(selection: unknown): string | undefined { |
| 72 | + if (!(selection instanceof NodeSelection)) return undefined; |
| 73 | + if (selection.node.type.name !== 'figure') return undefined; |
| 74 | + |
| 75 | + return this.#findFigcaptionText(selection.node); |
| 76 | + } |
| 77 | + |
| 78 | + #findFigcaptionText(figureNode: ProseMirrorNode): string | undefined { |
| 79 | + let caption: string | undefined; |
| 80 | + figureNode.descendants((child) => { |
| 81 | + if (child.type.name === 'figcaption') { |
| 82 | + caption = child.textContent || undefined; |
| 83 | + return false; // Stop searching |
| 84 | + } |
| 85 | + return true; // Continue searching |
| 86 | + }); |
| 87 | + return caption; |
| 88 | + } |
| 89 | + |
| 90 | + async #getMediaGuid(currentMediaUdi?: string): Promise<string | undefined> { |
| 91 | + if (currentMediaUdi) { |
| 92 | + // Image already exists, go directly to edit alt text/caption |
| 93 | + return currentMediaUdi; |
68 | 94 | } |
69 | 95 |
|
70 | | - const media = await this.#showMediaCaptionAltText(mediaGuid, currentAltText, currentCaption); |
71 | | - if (!media) return; |
| 96 | + // No image selected, open media picker |
| 97 | + const selection = await this.#openMediaPicker(); |
| 98 | + if (!selection?.length) return undefined; |
72 | 99 |
|
73 | | - this.#insertInEditor(editor, mediaGuid, media); |
| 100 | + const selectedGuid = selection[0]; |
| 101 | + if (!selectedGuid) { |
| 102 | + throw new Error('No media selected'); |
| 103 | + } |
| 104 | + |
| 105 | + return selectedGuid; |
74 | 106 | } |
75 | 107 |
|
76 | 108 | async #openMediaPicker(currentMediaUdi?: string) { |
|
0 commit comments