Skip to content

Commit 4965747

Browse files
rtibblesbotclaude
andcommitted
Keep Save and delete inside an open chip's editor boundary
A click on a control marked within the editor's `data-editor-boundary` ancestor no longer counts as outside, so Save and delete run their own handlers instead of being pre-empted by the capture-phase close. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 3857a5e commit 4965747

6 files changed

Lines changed: 412 additions & 18 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
import { fireEvent, render, screen, waitFor, within } from '@testing-library/vue';
2+
import userEvent from '@testing-library/user-event';
3+
import Vue from 'vue';
4+
import VueRouter from 'vue-router';
5+
import EditableChipList from '../index.vue';
6+
import { qtiEditorStrings } from '../../../qtiEditorStrings';
7+
import { stubProseMirrorLayout } from 'shared/utils/testing';
8+
import { getTipTapEditorStrings } from 'shared/views/TipTapEditor/TipTapEditor/TipTapEditorStrings';
9+
10+
// jsdom defines `ontouchstart`, which would put the editor in its touch layout.
11+
jest.mock('shared/utils/browserInfo', () => ({ isTouchDevice: false }));
12+
13+
const {
14+
distractorsLabel$,
15+
addDistractorBtn$,
16+
editDistractorLabel$,
17+
deleteDistractorBtn$,
18+
saveChipBtn$,
19+
} = qtiEditorStrings;
20+
const { TipTapEditorLabel$, insertImage$, closeModal$, textFormatOptions$ } =
21+
getTipTapEditorStrings();
22+
23+
const CHIPS = [
24+
{ id: 'choice_a', content: '<p>Antonio</p>' },
25+
{ id: 'choice_b', content: '<p>Prospero</p>' },
26+
];
27+
28+
const LABELS = {
29+
addLabel: addDistractorBtn$(),
30+
listLabel: distractorsLabel$(),
31+
chipLabel: number => editDistractorLabel$({ number }),
32+
deleteLabel: number => deleteDistractorBtn$({ number }),
33+
};
34+
35+
const Host = {
36+
components: { EditableChipList },
37+
template: `
38+
<EditableChipList
39+
:chips="chips"
40+
:addMode="addMode"
41+
v-bind="labels"
42+
@add-chip="html => chips.push({ id: 'choice_new', content: html })"
43+
@update-chip="(index, html) => chips.splice(index, 1, { ...chips[index], content: html })"
44+
@remove-chip="index => chips.splice(index, 1)"
45+
/>
46+
`,
47+
props: {
48+
addMode: { type: String, default: 'button' },
49+
initialChips: { type: Array, default: () => CHIPS },
50+
},
51+
data() {
52+
return { chips: [...this.initialChips], labels: LABELS };
53+
},
54+
};
55+
56+
const list = () => screen.getByRole('list', { name: distractorsLabel$() });
57+
const chipTexts = () =>
58+
within(list())
59+
.queryAllByRole('listitem')
60+
.map(item => item.textContent.trim());
61+
const button = name => screen.getByRole('button', { name });
62+
const editButton = number => button(editDistractorLabel$({ number }));
63+
const openEditor = () => screen.queryByRole('textbox', { name: TipTapEditorLabel$() });
64+
65+
async function writeIn(user, opener) {
66+
await user.click(opener);
67+
await waitFor(() => expect(openEditor()).toContainElement(document.activeElement));
68+
await user.keyboard('!');
69+
}
70+
71+
describe('EditableChipList save', () => {
72+
stubProseMirrorLayout();
73+
74+
let consoleError;
75+
76+
beforeEach(() => {
77+
consoleError = jest.spyOn(console, 'error');
78+
});
79+
80+
afterEach(() => {
81+
try {
82+
expect(consoleError).not.toHaveBeenCalled();
83+
} finally {
84+
consoleError.mockRestore();
85+
}
86+
});
87+
88+
const renderHost = (addMode, initialChips) => {
89+
const user = userEvent.setup();
90+
render(Host, { props: { addMode, initialChips }, routes: new VueRouter() });
91+
return user;
92+
};
93+
94+
describe.each(['button', 'region'])('in %s mode', addMode => {
95+
it('closes an open chip on a click on its save button, keeping the edit', async () => {
96+
const user = renderHost(addMode);
97+
await writeIn(user, editButton(2));
98+
99+
await user.click(button(saveChipBtn$()));
100+
101+
await waitFor(() => expect(editButton(2)).toHaveFocus());
102+
expect(openEditor()).toBeNull();
103+
expect(chipTexts()).toEqual(['Antonio', 'Prospero!']);
104+
});
105+
106+
it('closes an open chip on Enter on its save button, keeping the edit', async () => {
107+
const user = renderHost(addMode);
108+
await writeIn(user, editButton(2));
109+
110+
button(saveChipBtn$()).focus();
111+
await user.keyboard('{Enter}');
112+
113+
await waitFor(() => expect(editButton(2)).toHaveFocus());
114+
expect(openEditor()).toBeNull();
115+
expect(chipTexts()).toEqual(['Antonio', 'Prospero!']);
116+
});
117+
118+
it('closes and removes an open chip on a click on its delete button', async () => {
119+
const user = renderHost(addMode);
120+
await writeIn(user, editButton(2));
121+
122+
await user.click(button(deleteDistractorBtn$({ number: 2 })));
123+
124+
expect(openEditor()).toBeNull();
125+
expect(chipTexts()).toEqual(['Antonio']);
126+
});
127+
128+
it('keeps an open chip open when its image dialog closes, and saves it after', async () => {
129+
const user = renderHost(addMode);
130+
await writeIn(user, editButton(2));
131+
132+
await user.click(within(openEditor()).getByRole('button', { name: insertImage$() }));
133+
// A browser runs Vue's render between a native click's listeners, unmounting the
134+
// dialog before the click bubbles out of it; jsdom renders only after dispatch.
135+
const closeButton = await screen.findByRole('button', { name: closeModal$() });
136+
closeButton.addEventListener('click', () => closeButton.remove());
137+
await user.click(closeButton);
138+
139+
expect(openEditor()).not.toBeNull();
140+
await user.click(button(saveChipBtn$()));
141+
142+
await waitFor(() => expect(editButton(2)).toHaveFocus());
143+
expect(openEditor()).toBeNull();
144+
expect(chipTexts()).toEqual(['Antonio', 'Prospero!']);
145+
});
146+
147+
describe.each([
148+
['the gap beside the closed chips', list],
149+
["the open chip's message line", () => document.querySelector('li.is-editing .is-reserved')],
150+
])('on a click on %s', (_, target) => {
151+
it('closes an open chip, keeping the edit, without opening another', async () => {
152+
const user = renderHost(addMode);
153+
await writeIn(user, editButton(2));
154+
155+
await user.click(target());
156+
157+
await waitFor(() => expect(openEditor()).toBeNull());
158+
expect(chipTexts()).toEqual(['Antonio', 'Prospero!']);
159+
});
160+
161+
it('only dismisses an open toolbar menu, keeping the chip open', async () => {
162+
const user = renderHost(addMode);
163+
await writeIn(user, editButton(2));
164+
const formatButton = within(openEditor()).getByRole('button', {
165+
name: textFormatOptions$(),
166+
});
167+
await user.click(formatButton);
168+
await waitFor(() => expect(formatButton).toHaveAttribute('aria-expanded', 'true'));
169+
170+
await user.click(target());
171+
172+
await waitFor(() => expect(formatButton).toHaveAttribute('aria-expanded', 'false'));
173+
expect(openEditor()).not.toBeNull();
174+
});
175+
});
176+
177+
describe('with a blank chip open', () => {
178+
const BLANK_SECOND = [
179+
{ id: 'choice_a', content: '<p>Antonio</p>' },
180+
{ id: 'choice_b', content: '' },
181+
{ id: 'choice_c', content: '<p>Caliban</p>' },
182+
{ id: 'choice_d', content: '<p>Miranda</p>' },
183+
];
184+
185+
const openBlank = async addMode => {
186+
const user = renderHost(addMode, BLANK_SECOND);
187+
await user.click(editButton(2));
188+
await waitFor(() => expect(openEditor()).toContainElement(document.activeElement));
189+
return user;
190+
};
191+
192+
it('removes only it on a click on its delete button', async () => {
193+
const user = await openBlank(addMode);
194+
195+
await user.click(button(deleteDistractorBtn$({ number: 2 })));
196+
197+
expect(openEditor()).toBeNull();
198+
expect(chipTexts()).toEqual(['Antonio', 'Caliban', 'Miranda']);
199+
});
200+
201+
it('removes a later chip whose delete button is clicked', async () => {
202+
const user = await openBlank(addMode);
203+
204+
await user.click(button(deleteDistractorBtn$({ number: 3 })));
205+
206+
expect(openEditor()).toBeNull();
207+
expect(chipTexts()).toEqual(['Antonio', 'Miranda']);
208+
});
209+
210+
it('opens the chip that is clicked', async () => {
211+
const user = await openBlank(addMode);
212+
213+
await user.click(editButton(3));
214+
215+
await waitFor(() => expect(openEditor()).toHaveTextContent('Caliban'));
216+
expect(chipTexts()).toHaveLength(3);
217+
});
218+
});
219+
220+
it('throws a written draft away on a click on its discard button', async () => {
221+
const user = renderHost(addMode);
222+
await writeIn(user, button(addDistractorBtn$()));
223+
224+
await user.click(button(deleteDistractorBtn$({ number: 3 })));
225+
226+
expect(openEditor()).toBeNull();
227+
expect(chipTexts()).toEqual(['Antonio', 'Prospero']);
228+
});
229+
});
230+
231+
it('adds a region-mode draft on save without opening another', async () => {
232+
const user = renderHost('region');
233+
await writeIn(user, button(addDistractorBtn$()));
234+
235+
await user.click(button(saveChipBtn$()));
236+
237+
await waitFor(() => expect(button(addDistractorBtn$())).toHaveFocus());
238+
expect(openEditor()).toBeNull();
239+
expect(chipTexts()).toEqual(['Antonio', 'Prospero', '!']);
240+
});
241+
242+
describe('in region mode, rendering between listeners as a browser does', () => {
243+
beforeEach(() => {
244+
Vue.config.async = false;
245+
});
246+
247+
afterEach(() => {
248+
Vue.config.async = true;
249+
});
250+
251+
it.each([
252+
['a chip', () => editButton(2), () => document.querySelector('.pool-content')],
253+
['a draft', () => button(addDistractorBtn$()), () => document.querySelector('.pool-content')],
254+
['a draft', () => button(addDistractorBtn$()), list],
255+
])('closes %s on a click in the box without opening a draft', async (_, opener, target) => {
256+
const user = renderHost('region');
257+
await writeIn(user, opener());
258+
259+
await user.click(target());
260+
261+
expect(openEditor()).toBeNull();
262+
});
263+
});
264+
265+
it('adds a draft on a save click that moves no focus', async () => {
266+
const user = renderHost('region');
267+
await writeIn(user, button(addDistractorBtn$()));
268+
269+
await fireEvent.click(button(saveChipBtn$()));
270+
271+
expect(openEditor()).toBeNull();
272+
await waitFor(() => expect(chipTexts()).toEqual(['Antonio', 'Prospero', '!']));
273+
});
274+
});

‎contentcuration/contentcuration/frontend/shared/views/QTIEditor/components/EditableChipList/index.vue‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
<ul
1515
class="chip-list"
1616
:aria-label="listLabel"
17+
data-editor-boundary
18+
@click="onListClick"
1719
>
1820
<li
1921
v-for="(chip, index) in chips"
@@ -49,7 +51,7 @@
4951
:tabindex="-1"
5052
class="editor"
5153
@update="html => onChipUpdate(index, html)"
52-
@minimize="close"
54+
@minimize="onMinimize"
5355
/>
5456
<div
5557
v-if="isChipOpen(index)"
@@ -102,6 +104,7 @@
102104
<div
103105
v-if="isDraftOpen"
104106
class="draft-row"
107+
data-editor-boundary
105108
@click.stop
106109
>
107110
<div class="draft-editor">
@@ -116,7 +119,7 @@
116119
:tabindex="-1"
117120
class="editor"
118121
@update="onDraftUpdate"
119-
@minimize="close"
122+
@minimize="onMinimize"
120123
/>
121124
<div class="save-row">
122125
<KButton
@@ -168,6 +171,7 @@
168171
import ValidationMessage from '../ValidationMessage/index.vue';
169172
import { qtiEditorStrings } from '../../qtiEditorStrings';
170173
import TipTapEditor from 'shared/views/TipTapEditor/TipTapEditor/TipTapEditor';
174+
import { isPopupDismissal } from 'shared/views/TipTapEditor/TipTapEditor/composables/useClickOutside';
171175
import EditorImageProcessor from 'shared/views/TipTapEditor/TipTapEditor/services/imageService';
172176
import { hasRichTextContent } from 'shared/views/TipTapEditor/TipTapEditor/utils/comparisonKey';
173177
@@ -281,7 +285,15 @@
281285
emit('open');
282286
}
283287
284-
function onRegionClick() {
288+
let closingClick = null;
289+
290+
function onMinimize(clickOutside) {
291+
closingClick = clickOutside;
292+
close();
293+
}
294+
295+
function onRegionClick(event) {
296+
if (event === closingClick) return;
285297
if (firstBlankIndex.value === -1) {
286298
openDraft();
287299
} else {
@@ -334,6 +346,14 @@
334346
}
335347
}
336348
349+
function onListClick(event) {
350+
if (!open.value || isDraftOpen.value || isPopupDismissal(event)) return;
351+
const path = event.composedPath();
352+
if (chipRegions.value.some(region => path.includes(region.$el))) return;
353+
event.stopPropagation();
354+
close();
355+
}
356+
337357
async function onDiscard(event) {
338358
event.stopPropagation();
339359
open.value = null;
@@ -362,11 +382,13 @@
362382
close,
363383
openChip,
364384
openDraft,
385+
onMinimize,
365386
onRegionClick,
366387
onChipUpdate,
367388
onDraftUpdate,
368389
onRemove,
369390
onSave,
391+
onListClick,
370392
onDiscard,
371393
saveChipBtn$,
372394
};

‎contentcuration/contentcuration/frontend/shared/views/TipTapEditor/TipTapEditor/TipTapEditor.vue‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,12 +245,12 @@
245245
}
246246
});
247247
248-
const minimize = () => {
248+
const minimize = clickOutside => {
249249
// Toolbar buttons suppress blur to keep the caret, so content written since
250250
// the last blur is still unsynced. Flush it first: a parent acting on the
251251
// close would otherwise read the content as it stood before that edit.
252252
emitContentUpdate();
253-
emit('minimize');
253+
emit('minimize', clickOutside);
254254
};
255255
256256
// The content is emitted only when the editor loses focus (blur) or closes.

0 commit comments

Comments
 (0)