feat(media): unify uploads onto modules/album, add batched srcset enrichment and Media Library picker

This commit is contained in:
Jakub Zych
2026-11-27 13:26:59 +01:00
parent f61c90a326
commit ccd8dc85aa
14 changed files with 781 additions and 79 deletions

File diff suppressed because one or more lines are too long

View File

@@ -13,6 +13,8 @@ export const TAG = 'golem15-journal-blogeditor'
export const CHANGE_EVENT = 'summer-change'
export const UPLOAD_EVENT = 'summer-upload'
export const UPLOAD_RESULT_EVENT = 'summer-upload-result'
export const MEDIA_PICK_EVENT = 'summer-media-pick'
export const MEDIA_PICK_RESULT_EVENT = 'summer-media-pick-result'
/** The image types the picker offers and the editor uploads (D-18). */
export const IMAGE_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp']
@@ -110,6 +112,7 @@ export class BlogEditorElement extends HTMLElement {
#options: EditorOptions = readOptions(null)
#showingSource = false
#pending = new Map<string, { name: string; choice: ImageChoice; timer: ReturnType<typeof setTimeout> }>()
#pendingMediaPick = new Set<string>()
/** The picked files waiting for their image form (J4). */
#queue: File[] = []
/** Whether this element shows the preview pane (per instance, off by default). */
@@ -172,6 +175,7 @@ export class BlogEditorElement extends HTMLElement {
openLink: () => this.#openLink(),
openEmbed: () => this.#openEmbed(),
pickImage: () => this.#fileInput?.click(),
pickFromMediaLibrary: () => this.#pickFromMediaLibrary(),
insertRawHtml: () => this.#insertRawHtml(),
showSource: (on) => this.showSource(on),
togglePreview: () => this.showPreview(!this.#previewOn),
@@ -263,6 +267,7 @@ export class BlogEditorElement extends HTMLElement {
)
this.addEventListener(UPLOAD_RESULT_EVENT, this.#onUploadResult)
this.addEventListener(MEDIA_PICK_RESULT_EVENT, this.#onMediaPickResult)
this.addEventListener(EDIT_HTML_EVENT, this.#onEditHtml)
const initial = this.value
@@ -301,9 +306,11 @@ export class BlogEditorElement extends HTMLElement {
disconnectedCallback(): void {
this.removeEventListener(UPLOAD_RESULT_EVENT, this.#onUploadResult)
this.removeEventListener(MEDIA_PICK_RESULT_EVENT, this.#onMediaPickResult)
this.removeEventListener(EDIT_HTML_EVENT, this.#onEditHtml)
for (const { timer } of this.#pending.values()) clearTimeout(timer)
this.#pending.clear()
this.#pendingMediaPick.clear()
clearTimeout(this.#previewTimer)
this.#previewTimer = undefined
this.#outline?.stop()
@@ -723,6 +730,50 @@ export class BlogEditorElement extends HTMLElement {
if (this.#imageForm) this.#imageForm.root.hidden = true
this.#showImageForm()
}
// Media Library Picker (Phase 37, D-05) -------------------------------------
#pickFromMediaLibrary(): void {
if (!this.#editor?.isEditable) return
const id = uploadRequestId()
this.#pendingMediaPick.add(id)
this.dispatchEvent(
new CustomEvent(MEDIA_PICK_EVENT, {
bubbles: true,
composed: true,
detail: { id, mode: 'image', defaultFolder: 'journal', showAlt: true },
}),
)
}
#onMediaPickResult = (event: Event): void => {
const detail = (
event as CustomEvent<{
id?: unknown
cancelled?: unknown
item?: { url?: unknown; alt?: unknown; caption?: unknown; width?: unknown; height?: unknown }
}>
).detail ?? {}
const id = typeof detail.id === 'string' ? detail.id : ''
if (!this.#pendingMediaPick.delete(id)) return
if (detail.cancelled === true || !detail.item) return
const url = typeof detail.item.url === 'string' ? detail.item.url.trim() : ''
if (!url || !isPolicyUrl(url)) return
const editor = this.#editor
if (!editor) return
const alt = typeof detail.item.alt === 'string' ? detail.item.alt : ''
const caption = typeof detail.item.caption === 'string' ? detail.item.caption.trim() : ''
const width = typeof detail.item.width === 'number' && detail.item.width > 0 ? String(detail.item.width) : null
const height = typeof detail.item.height === 'number' && detail.item.height > 0 ? String(detail.item.height) : null
if (caption) {
editor.chain().focus().setFigure({ src: url, alt, caption, width, height }).run()
} else {
const attrs: Record<string, unknown> = { src: url, alt }
if (width) attrs.width = width
if (height) attrs.height = height
editor.chain().focus().setImage(attrs as { src: string; alt?: string }).run()
}
}
}
function isImage(file: File): boolean {

View File

@@ -222,7 +222,7 @@ declare module '@tiptap/core' {
}
figure: {
/** Inserts an image with a caption as figure, img and figcaption. */
setFigure: (attributes: { src: string; alt: string; caption: string }) => ReturnType
setFigure: (attributes: { src: string; alt: string; caption: string; width?: number | string | null; height?: number | string | null }) => ReturnType
}
}
}
@@ -452,9 +452,12 @@ export const Figure = Node.create({
addCommands() {
return {
setFigure:
({ src, alt, caption }) =>
({ src, alt, caption, width, height }) =>
({ commands }) => {
const content: Record<string, unknown>[] = [{ type: 'figureImage', attrs: { src, alt } }]
const imgAttrs: Record<string, unknown> = { src, alt }
if (width != null && width !== '') imgAttrs.width = String(width)
if (height != null && height !== '') imgAttrs.height = String(height)
const content: Record<string, unknown>[] = [{ type: 'figureImage', attrs: imgAttrs }]
if (caption) content.push({ type: 'figcaption', content: [{ type: 'text', text: caption }] })
return commands.insertContent({ type: this.name, content })
},
@@ -572,6 +575,17 @@ export const HeadingOneAsTwo = Node.create({
* the preservation nodes and marks. headingLevels are the offered heading
* levels (J1): without 1, an h1 is parsed as a level 2 heading.
*/
/** Inline image with optional width/height attributes preserved for Media Library picks. */
const SizedImage = Image.extend({
addAttributes() {
return {
...this.parent?.(),
width: { default: null },
height: { default: null },
}
},
})
export function preservationExtensions(labels: Labels = labelsFor('en'), headingLevels: readonly number[] = DEFAULT_HEADING_LEVELS): AnyExtension[] {
const levels = schemaHeadingLevels(headingLevels)
return [
@@ -585,7 +599,7 @@ export function preservationExtensions(labels: Labels = labelsFor('en'), heading
},
}),
...(levels.includes(1) ? [] : [HeadingOneAsTwo]),
Image.configure({ inline: true, allowBase64: false }),
SizedImage.configure({ inline: true, allowBase64: false }),
PlainTable,
TableRow,
PlainTableHeader,

View File

@@ -28,6 +28,7 @@ export interface Labels {
deleteColumn: string
deleteTable: string
image: string
mediaLibrary: string
embed: string
rawHtml: string
undo: string
@@ -102,6 +103,7 @@ const en: Labels = {
deleteColumn: 'Delete column',
deleteTable: 'Delete table',
image: 'Image',
mediaLibrary: 'Choose from Media Library',
embed: 'Embed video',
rawHtml: 'HTML block',
undo: 'Undo',
@@ -173,6 +175,7 @@ const pl: Labels = {
deleteColumn: 'Usuń kolumnę',
deleteTable: 'Usuń tabelę',
image: 'Obraz',
mediaLibrary: 'Wybierz z biblioteki mediów',
embed: 'Osadź wideo',
rawHtml: 'Blok HTML',
undo: 'Cofnij',

View File

@@ -8,6 +8,7 @@ export interface ToolbarHost {
openLink: () => void
openEmbed: () => void
pickImage: () => void
pickFromMediaLibrary: () => void
insertRawHtml: () => void
showSource: (on: boolean) => void
/** Switches the preview pane on or off. */
@@ -91,6 +92,7 @@ const GROUPS: ButtonSpec[][] = [
],
[
{ command: 'image', glyph: 'Image', label: 'image', run: (_e, h) => h.pickImage(), host: true },
{ command: 'mediaLibrary', glyph: 'Library', label: 'mediaLibrary', run: (_e, h) => h.pickFromMediaLibrary(), host: true },
{ command: 'embed', glyph: 'Video', label: 'embed', run: (_e, h) => h.openEmbed(), host: true },
{ command: 'rawHtml', glyph: 'HTML', label: 'rawHtml', run: (_e, h) => h.insertRawHtml(), host: true },
],