Skip to content

Commit 81ab15f

Browse files
rtibblesbotclaude
andcommitted
Keep the resize handle under the pointer for aligned images
End-aligned images put the handle on their inline-start edge, the one that moves; centered images grow by twice the drag distance. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 71e5b22 commit 81ab15f

2 files changed

Lines changed: 69 additions & 15 deletions

File tree

‎contentcuration/contentcuration/frontend/shared/views/TipTapEditor/TipTapEditor/components/image/ImageNodeView.vue‎

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<div
55
ref="containerRef"
66
class="image-node-view"
7-
:class="{ 'is-selected': selected && editor.isEditable }"
7+
:class="{ 'is-selected': selected && editor.isEditable, 'is-end-aligned': isEndAligned }"
88
:style="{ width: styleWidth }"
99
>
1010
<img
@@ -191,19 +191,20 @@
191191
return null;
192192
};
193193
194-
const isHandleRtl = () => getComputedStyle(resizeHandleRef.value).direction === 'rtl';
194+
const isEndAligned = computed(() => props.node.attrs.textAlign === 'right');
195+
196+
const isHandleOnLeft = () =>
197+
(getComputedStyle(resizeHandleRef.value).direction === 'rtl') !== isEndAligned.value;
195198
196199
const onResizeStart = startEvent => {
197-
const isRtl = isHandleRtl();
200+
const growthPerPixel =
201+
(isHandleOnLeft() ? -1 : 1) * (props.node.attrs.textAlign === 'center' ? 2 : 1);
198202
const startX = startEvent.clientX;
199203
const startWidth = width.value || containerRef.value.offsetWidth;
200204
201205
const onMouseMove = moveEvent => {
202206
const deltaX = moveEvent.clientX - startX;
203-
204-
const newWidth = isRtl
205-
? startWidth - deltaX // In RTL, moving right should decrease width
206-
: startWidth + deltaX; // In LTR, moving right should increase width
207+
const newWidth = startWidth + growthPerPixel * deltaX;
207208
208209
const clampedWidth = Math.max(minWidth, newWidth);
209210
@@ -241,14 +242,13 @@
241242
const currentWidth = width.value || containerRef.value.offsetWidth;
242243
let newWidth = currentWidth;
243244
244-
// Invert keyboard controls for RTL
245-
const isRtl = isHandleRtl();
246-
const rightKey = isRtl ? 'ArrowLeft' : 'ArrowRight';
247-
const leftKey = isRtl ? 'ArrowRight' : 'ArrowLeft';
245+
const handleOnLeft = isHandleOnLeft();
246+
const growKey = handleOnLeft ? 'ArrowLeft' : 'ArrowRight';
247+
const shrinkKey = handleOnLeft ? 'ArrowRight' : 'ArrowLeft';
248248
249-
if (event.key === rightKey) {
249+
if (event.key === growKey) {
250250
newWidth = currentWidth + step;
251-
} else if (event.key === leftKey) {
251+
} else if (event.key === shrinkKey) {
252252
newWidth = currentWidth - step;
253253
} else if (event.key === 'Escape' || event.key === 'Enter') {
254254
// Use ref instead of DOM query
@@ -315,6 +315,7 @@
315315
removeImage,
316316
editImage,
317317
isCompact,
318+
isEndAligned,
318319
onResizeKeyDown,
319320
onImageLoad,
320321
};
@@ -370,6 +371,16 @@
370371
cursor: nesw-resize /* rtl:ignore */;
371372
}
372373
374+
.is-end-aligned .resize-handle {
375+
inset-inline-start: -6px;
376+
inset-inline-end: auto;
377+
cursor: nesw-resize /* rtl:ignore */;
378+
}
379+
380+
.is-end-aligned .resize-handle:dir(rtl) {
381+
cursor: nwse-resize /* rtl:ignore */;
382+
}
383+
373384
.image-node-view.is-selected .resize-handle,
374385
.resize-handle:focus {
375386
opacity: 1;

‎contentcuration/contentcuration/frontend/shared/views/TipTapEditor/__tests__/ImageNodeView.spec.js‎

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import { fireEvent, render, screen, waitFor } from '@testing-library/vue';
22
import VueRouter from 'vue-router';
33
import TipTapEditor from '../TipTapEditor/TipTapEditor.vue';
44

5-
async function renderImage() {
5+
async function renderImage(align) {
6+
const alignAttr = align ? ` data-text-align="${align}"` : '';
67
render(TipTapEditor, {
78
props: {
8-
value: '<img src="x" width="300">',
9+
value: `<img src="x" width="300"${alignAttr}>`,
910
mode: 'edit',
1011
format: 'html',
1112
imageProcessor: { ACCEPTED_MIME_TYPES: ['image/png'] },
@@ -61,6 +62,48 @@ describe('ImageNodeView resize', () => {
6162
await waitFor(() => expect(slider).toHaveAttribute('aria-valuenow', '310'));
6263
});
6364

65+
it('shrinks an end-aligned image when dragged to the right in LTR content', async () => {
66+
const slider = await renderImage('right');
67+
68+
await dragRight(slider);
69+
70+
await waitFor(() => expect(slider).toHaveAttribute('aria-valuenow', '280'));
71+
});
72+
73+
it('grows an end-aligned image when dragged to the right in RTL content', async () => {
74+
const slider = await renderImage('right');
75+
slider.style.direction = 'rtl';
76+
77+
await dragRight(slider);
78+
79+
await waitFor(() => expect(slider).toHaveAttribute('aria-valuenow', '320'));
80+
});
81+
82+
it('grows an end-aligned image on ArrowLeft in LTR content', async () => {
83+
const slider = await renderImage('right');
84+
85+
await fireEvent.keyDown(slider, { key: 'ArrowLeft' });
86+
87+
await waitFor(() => expect(slider).toHaveAttribute('aria-valuenow', '310'));
88+
});
89+
90+
it('grows an end-aligned image on ArrowRight in RTL content', async () => {
91+
const slider = await renderImage('right');
92+
slider.style.direction = 'rtl';
93+
94+
await fireEvent.keyDown(slider, { key: 'ArrowRight' });
95+
96+
await waitFor(() => expect(slider).toHaveAttribute('aria-valuenow', '310'));
97+
});
98+
99+
it('grows a centered image by twice the drag distance', async () => {
100+
const slider = await renderImage('center');
101+
102+
await dragRight(slider);
103+
104+
await waitFor(() => expect(slider).toHaveAttribute('aria-valuenow', '340'));
105+
});
106+
64107
it('follows the content direction, not the page direction', async () => {
65108
window.isRTL = true;
66109
document.documentElement.dir = 'rtl';

0 commit comments

Comments
 (0)