feat(blogeditor): enable corner drag-resize handles for inline and figure images

This commit is contained in:
Jakub Zych
2026-11-27 14:58:07 +01:00
parent ccd8dc85aa
commit 260a86b686
5 changed files with 178 additions and 35 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -342,6 +342,68 @@ golem15-journal-blogeditor .ProseMirror img {
height: auto;
}
golem15-journal-blogeditor .ProseMirror [data-resize-container] {
max-width: 100%;
vertical-align: bottom;
}
golem15-journal-blogeditor .ProseMirror [data-resize-wrapper] {
max-width: 100%;
line-height: 0;
}
golem15-journal-blogeditor .ProseMirror [data-resize-wrapper] img {
display: block;
max-width: 100%;
height: auto !important;
}
golem15-journal-blogeditor .ProseMirror [data-resize-container].ProseMirror-selectednode [data-resize-wrapper],
golem15-journal-blogeditor .ProseMirror [data-resize-container][data-resize-state="true"] [data-resize-wrapper] {
outline: 2px solid var(--c-ring);
}
golem15-journal-blogeditor .ProseMirror [data-resize-handle] {
width: 10px;
height: 10px;
background: var(--c-ring, #2563eb);
border: 2px solid var(--c-surface, #ffffff);
border-radius: 2px;
opacity: 0;
transition: opacity 120ms ease;
z-index: 2;
}
golem15-journal-blogeditor .ProseMirror [data-resize-wrapper]:hover [data-resize-handle],
golem15-journal-blogeditor .ProseMirror [data-resize-container].ProseMirror-selectednode [data-resize-handle],
golem15-journal-blogeditor .ProseMirror [data-resize-container][data-resize-state="true"] [data-resize-handle] {
opacity: 1;
}
golem15-journal-blogeditor .ProseMirror [data-resize-handle="top-left"] {
top: -5px;
left: -5px;
cursor: nwse-resize;
}
golem15-journal-blogeditor .ProseMirror [data-resize-handle="top-right"] {
top: -5px;
right: -5px;
cursor: nesw-resize;
}
golem15-journal-blogeditor .ProseMirror [data-resize-handle="bottom-left"] {
bottom: -5px;
left: -5px;
cursor: nesw-resize;
}
golem15-journal-blogeditor .ProseMirror [data-resize-handle="bottom-right"] {
bottom: -5px;
right: -5px;
cursor: nwse-resize;
}
golem15-journal-blogeditor .ProseMirror table {
border-collapse: collapse;
width: 100%;

View File

@@ -1,4 +1,4 @@
import { Extension, Mark, Node, mergeAttributes, type AnyExtension } from '@tiptap/core'
import { Extension, Mark, Node, ResizableNodeView, mergeAttributes, type AnyExtension } from '@tiptap/core'
import type { DOMOutputSpec } from '@tiptap/pm/model'
import Image from '@tiptap/extension-image'
import { Table, TableCell, TableHeader, TableRow } from '@tiptap/extension-table'
@@ -489,6 +489,48 @@ export const FigureImage = Node.create({
renderHTML({ HTMLAttributes }) {
return ['img', HTMLAttributes]
},
addNodeView() {
if (typeof document === 'undefined') return null
return ({ node, getPos, HTMLAttributes, editor }) => {
const el = document.createElement('img')
el.draggable = false
for (const [k, v] of Object.entries(HTMLAttributes)) {
if (v != null && k !== 'width' && k !== 'height') el.setAttribute(k, String(v))
}
return new ResizableNodeView({
element: el,
editor,
node,
getPos,
onResize: (width, height) => {
el.style.width = `${width}px`
el.style.height = `${height}px`
},
onCommit: (width, height) => {
const pos = getPos()
if (typeof pos !== 'number') return
const cur = editor.state.doc.nodeAt(pos)
if (!cur) return
editor.view.dispatch(editor.state.tr.setNodeMarkup(pos, undefined, { ...cur.attrs, width, height }))
},
onUpdate: (updatedNode) => {
if (updatedNode.type !== node.type) return false
for (const key of ['src', 'alt', 'title'] as const) {
const val = updatedNode.attrs[key]
if (val != null) el.setAttribute(key, String(val))
else el.removeAttribute(key)
}
return true
},
options: {
directions: ['top-left', 'top-right', 'bottom-left', 'bottom-right'],
min: { width: 48, height: 48 },
preserveAspectRatio: true,
},
})
}
},
})
/** Figcaption is the editable caption of a figure. */
@@ -599,7 +641,17 @@ export function preservationExtensions(labels: Labels = labelsFor('en'), heading
},
}),
...(levels.includes(1) ? [] : [HeadingOneAsTwo]),
SizedImage.configure({ inline: true, allowBase64: false }),
SizedImage.configure({
inline: true,
allowBase64: false,
resize: {
enabled: true,
directions: ['top-left', 'top-right', 'bottom-left', 'bottom-right'],
minWidth: 48,
minHeight: 48,
alwaysPreserveAspectRatio: true,
},
}),
PlainTable,
TableRow,
PlainTableHeader,

View File

@@ -141,5 +141,34 @@ describe('Media Library picker (Phase 37, D-05)', () => {
expect(el.value).toContain('width="1200"')
expect(el.value).toContain('height="800"')
expect(changes.length).toBeGreaterThan(0)
expect(el.querySelectorAll('[data-resize-handle]').length).toBe(4)
})
it('renders corner resize handles on captioned figure images inserted from Media Library', () => {
const { el } = mount('<p>Intro</p>')
const picks: CustomEvent[] = []
el.addEventListener('summer-media-pick', (event) => picks.push(event as CustomEvent))
click(button(el, 'mediaLibrary'))
const { id } = picks[0]!.detail as { id: string }
el.dispatchEvent(
new CustomEvent('summer-media-pick-result', {
detail: {
id,
item: {
url: '/media/abc123456789/reishi.jpg',
alt: 'Reishi',
caption: 'Reishi mushroom',
width: 800,
height: 600,
},
},
}),
)
expect(el.value).toContain('<figure>')
expect(el.value).toContain('<figcaption>Reishi mushroom</figcaption>')
expect(el.querySelectorAll('figure [data-resize-handle]').length).toBe(4)
})
})