Skip to content

Commit 1fb006a

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 1fb006a

5 files changed

Lines changed: 265 additions & 5 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
import { render, screen, waitFor, within } from '@testing-library/vue';
2+
import userEvent from '@testing-library/user-event';
3+
import VueRouter from 'vue-router';
4+
import EditableChipList from '../index.vue';
5+
import { qtiEditorStrings } from '../../../qtiEditorStrings';
6+
import { stubProseMirrorLayout } from 'shared/utils/testing';
7+
import { getTipTapEditorStrings } from 'shared/views/TipTapEditor/TipTapEditor/TipTapEditorStrings';
8+
9+
// jsdom defines `ontouchstart`, which would put the editor in its touch layout.
10+
jest.mock('shared/utils/browserInfo', () => ({ isTouchDevice: false }));
11+
12+
const {
13+
distractorsLabel$,
14+
addDistractorBtn$,
15+
editDistractorLabel$,
16+
deleteDistractorBtn$,
17+
saveChipBtn$,
18+
} = qtiEditorStrings;
19+
const { TipTapEditorLabel$ } = getTipTapEditorStrings();
20+
21+
const CHIPS = [
22+
{ id: 'choice_a', content: '<p>Antonio</p>' },
23+
{ id: 'choice_b', content: '<p>Prospero</p>' },
24+
];
25+
26+
const LABELS = {
27+
addLabel: addDistractorBtn$(),
28+
listLabel: distractorsLabel$(),
29+
chipLabel: number => editDistractorLabel$({ number }),
30+
deleteLabel: number => deleteDistractorBtn$({ number }),
31+
};
32+
33+
const Host = {
34+
components: { EditableChipList },
35+
template: `
36+
<EditableChipList
37+
:chips="chips"
38+
:addMode="addMode"
39+
v-bind="labels"
40+
@add-chip="html => chips.push({ id: 'choice_new', content: html })"
41+
@update-chip="(index, html) => chips.splice(index, 1, { ...chips[index], content: html })"
42+
@remove-chip="index => chips.splice(index, 1)"
43+
/>
44+
`,
45+
props: {
46+
addMode: { type: String, default: 'button' },
47+
initialChips: { type: Array, default: () => CHIPS },
48+
},
49+
data() {
50+
return { chips: [...this.initialChips], labels: LABELS };
51+
},
52+
};
53+
54+
const list = () => screen.getByRole('list', { name: distractorsLabel$() });
55+
const chipTexts = () =>
56+
within(list())
57+
.queryAllByRole('listitem')
58+
.map(item => item.textContent.trim());
59+
const button = name => screen.getByRole('button', { name });
60+
const editButton = number => button(editDistractorLabel$({ number }));
61+
const openEditor = () => screen.queryByRole('textbox', { name: TipTapEditorLabel$() });
62+
63+
async function writeIn(user, opener) {
64+
await user.click(opener);
65+
await waitFor(() => expect(openEditor()).toContainElement(document.activeElement));
66+
await user.keyboard('!');
67+
}
68+
69+
describe('EditableChipList save', () => {
70+
stubProseMirrorLayout();
71+
72+
let consoleError;
73+
74+
beforeEach(() => {
75+
consoleError = jest.spyOn(console, 'error');
76+
});
77+
78+
afterEach(() => {
79+
try {
80+
expect(consoleError).not.toHaveBeenCalled();
81+
} finally {
82+
consoleError.mockRestore();
83+
}
84+
});
85+
86+
const renderHost = (addMode, initialChips) => {
87+
const user = userEvent.setup();
88+
render(Host, { props: { addMode, initialChips }, routes: new VueRouter() });
89+
return user;
90+
};
91+
92+
describe.each(['button', 'region'])('in %s mode', addMode => {
93+
it('closes an open chip on a click on its save button, keeping the edit', async () => {
94+
const user = renderHost(addMode);
95+
await writeIn(user, editButton(2));
96+
97+
await user.click(button(saveChipBtn$()));
98+
99+
await waitFor(() => expect(editButton(2)).toHaveFocus());
100+
expect(openEditor()).toBeNull();
101+
expect(chipTexts()).toEqual(['Antonio', 'Prospero!']);
102+
});
103+
104+
it('closes an open chip on Enter on its save button, keeping the edit', async () => {
105+
const user = renderHost(addMode);
106+
await writeIn(user, editButton(2));
107+
108+
button(saveChipBtn$()).focus();
109+
await user.keyboard('{Enter}');
110+
111+
await waitFor(() => expect(editButton(2)).toHaveFocus());
112+
expect(openEditor()).toBeNull();
113+
expect(chipTexts()).toEqual(['Antonio', 'Prospero!']);
114+
});
115+
116+
it('closes and removes an open chip on a click on its delete button', async () => {
117+
const user = renderHost(addMode);
118+
await writeIn(user, editButton(2));
119+
120+
await user.click(button(deleteDistractorBtn$({ number: 2 })));
121+
122+
expect(openEditor()).toBeNull();
123+
expect(chipTexts()).toEqual(['Antonio']);
124+
});
125+
126+
describe.each([
127+
['the gap beside the closed chips', () => document.querySelector('ul.chip-list')],
128+
["the open chip's message line", () => document.querySelector('li.is-editing .is-reserved')],
129+
])('on a click on %s', (_, target) => {
130+
it('closes an open chip, keeping the edit, without opening another', async () => {
131+
const user = renderHost(addMode);
132+
await writeIn(user, editButton(2));
133+
134+
await user.click(target());
135+
136+
await waitFor(() => expect(openEditor()).toBeNull());
137+
expect(chipTexts()).toEqual(['Antonio', 'Prospero!']);
138+
});
139+
});
140+
141+
describe('with a blank chip open', () => {
142+
const BLANK_SECOND = [
143+
{ id: 'choice_a', content: '<p>Antonio</p>' },
144+
{ id: 'choice_b', content: '' },
145+
{ id: 'choice_c', content: '<p>Caliban</p>' },
146+
{ id: 'choice_d', content: '<p>Miranda</p>' },
147+
];
148+
149+
const openBlank = async addMode => {
150+
const user = renderHost(addMode, BLANK_SECOND);
151+
await user.click(editButton(2));
152+
await waitFor(() => expect(openEditor()).toContainElement(document.activeElement));
153+
return user;
154+
};
155+
156+
it('removes only it on a click on its delete button', async () => {
157+
const user = await openBlank(addMode);
158+
159+
await user.click(button(deleteDistractorBtn$({ number: 2 })));
160+
161+
expect(openEditor()).toBeNull();
162+
expect(chipTexts()).toEqual(['Antonio', 'Caliban', 'Miranda']);
163+
});
164+
165+
it('removes a later chip whose delete button is clicked', async () => {
166+
const user = await openBlank(addMode);
167+
168+
await user.click(button(deleteDistractorBtn$({ number: 3 })));
169+
170+
expect(openEditor()).toBeNull();
171+
expect(chipTexts()).toEqual(['Antonio', 'Miranda']);
172+
});
173+
174+
it('opens the chip that is clicked', async () => {
175+
const user = await openBlank(addMode);
176+
177+
await user.click(editButton(3));
178+
179+
await waitFor(() => expect(openEditor()).toHaveTextContent('Caliban'));
180+
expect(chipTexts()).toHaveLength(3);
181+
});
182+
});
183+
184+
it('throws a written draft away on a click on its discard button', async () => {
185+
const user = renderHost(addMode);
186+
await writeIn(user, button(addDistractorBtn$()));
187+
188+
await user.click(button(deleteDistractorBtn$({ number: 3 })));
189+
190+
expect(openEditor()).toBeNull();
191+
expect(chipTexts()).toEqual(['Antonio', 'Prospero']);
192+
});
193+
});
194+
195+
it('adds a region-mode draft on save without opening another', async () => {
196+
const user = renderHost('region');
197+
await writeIn(user, button(addDistractorBtn$()));
198+
199+
await user.click(button(saveChipBtn$()));
200+
201+
await waitFor(() => expect(button(addDistractorBtn$())).toHaveFocus());
202+
expect(openEditor()).toBeNull();
203+
expect(chipTexts()).toEqual(['Antonio', 'Prospero', '!']);
204+
});
205+
});

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

Lines changed: 11 additions & 0 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"
@@ -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">
@@ -334,6 +337,13 @@
334337
}
335338
}
336339
340+
function onListClick(event) {
341+
if (!open.value || isDraftOpen.value) return;
342+
if (chipRegions.value.some(region => region.$el.contains(event.target))) return;
343+
event.stopPropagation();
344+
close();
345+
}
346+
337347
async function onDiscard(event) {
338348
event.stopPropagation();
339349
open.value = null;
@@ -367,6 +377,7 @@
367377
onDraftUpdate,
368378
onRemove,
369379
onSave,
380+
onListClick,
370381
onDiscard,
371382
saveChipBtn$,
372383
};

‎contentcuration/contentcuration/frontend/shared/views/TipTapEditor/TipTapEditor/composables/useClickOutside.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { onBeforeUnmount, watch } from 'vue';
88
const hasOpenPopup = container =>
99
Boolean(container.querySelector('[aria-haspopup][aria-expanded="true"]'));
1010

11+
const editorBoundary = container => container.closest('[data-editor-boundary]') ?? container;
12+
1113
/**
1214
* Closes the editor on a click outside it, and syncs its content when it loses focus.
1315
*
@@ -53,7 +55,8 @@ export function useClickOutside({ container, isFocused, isEditing, syncContent,
5355
// still count, and so this runs before the click's own handlers.
5456
const handleClick = event => {
5557
const root = container.value;
56-
const isOutside = isEditing() && root && !root.contains(event.target) && !hasOpenPopup(root);
58+
const isOutside =
59+
isEditing() && root && !editorBoundary(root).contains(event.target) && !hasOpenPopup(root);
5760
if (isOutside) {
5861
// `close` syncs the content itself.
5962
hasHeldSync = false;

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

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,16 @@ function renderHarness({ editing = true } = {}) {
3636
dialogOpen: { type: Boolean, default: false },
3737
},
3838
template: `<div>
39-
<div ref="container">
40-
<button>Inside</button>
41-
<button aria-haspopup="menu" :aria-expanded="String(menuOpen)">Menu</button>
42-
<p><button aria-haspopup="dialog" :aria-expanded="String(dialogOpen)">Chip</button></p>
39+
<div data-editor-boundary>
40+
<div ref="container">
41+
<button>Inside</button>
42+
<button aria-haspopup="menu" :aria-expanded="String(menuOpen)">Menu</button>
43+
<p><button aria-haspopup="dialog" :aria-expanded="String(dialogOpen)">Chip</button></p>
44+
</div>
45+
<button>Save</button>
4346
</div>
4447
<button>Outside</button>
48+
<div data-editor-boundary><button>Other editor's save</button></div>
4549
<div @click.stop><button>Stopped</button></div>
4650
</div>`,
4751
}),
@@ -89,6 +93,24 @@ describe('useClickOutside', () => {
8993
expect(close).not.toHaveBeenCalled();
9094
});
9195

96+
it('stays open on a click on a control within its boundary', async () => {
97+
const user = userEvent.setup();
98+
const { close } = renderHarness();
99+
100+
await user.click(button('Save'));
101+
102+
expect(close).not.toHaveBeenCalled();
103+
});
104+
105+
it("closes on a click within another editor's boundary", async () => {
106+
const user = userEvent.setup();
107+
const { close } = renderHarness();
108+
109+
await user.click(button("Other editor's save"));
110+
111+
expect(close).toHaveBeenCalledTimes(1);
112+
});
113+
92114
it('stays open on a click outside while one of its menus is open', async () => {
93115
const user = userEvent.setup();
94116
const { close, updateProps } = renderHarness();
@@ -155,6 +177,21 @@ describe('useClickOutside', () => {
155177
expect(close).not.toHaveBeenCalled();
156178
});
157179

180+
it('syncs a held blur before a click on a control within its boundary', async () => {
181+
const user = userEvent.setup();
182+
const { syncContent, blur } = renderHarness();
183+
let syncsAtClick;
184+
button('Save').addEventListener('click', () => {
185+
syncsAtClick = syncContent.mock.calls.length;
186+
});
187+
188+
await user.pointer({ keys: '[MouseLeft>]', target: button('Save') });
189+
await blur();
190+
await user.pointer({ keys: '[/MouseLeft]', target: button('Save') });
191+
192+
expect(syncsAtClick).toBe(1);
193+
});
194+
158195
it('drops the held sync when the click closes, as closing syncs', async () => {
159196
const user = userEvent.setup();
160197
const { syncContent, close, blur } = renderHarness();

‎docs/rich_text_editor.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ The context is re-evaluated on every transaction; no listener is needed. The edi
103103
- The editor knows a popup is open from its trigger: any element inside the editor with `aria-haspopup` and `aria-expanded="true"`. KDS's `KDropdownMenu` sets both on its trigger.
104104
- A node view whose button opens a panel rendered beside the editor sets both on that button for as long as the panel is open, and the editor stays open while the panel is used. The inline choice chip does this for its options panel.
105105

106+
### Controls beside the editor
107+
- Put the editor and the controls beside it that act on it, such as Save, in a common ancestor marked `data-editor-boundary`: a click anywhere inside the editor's closest such ancestor counts as inside.
108+
- A control inside the boundary that closes the editor, such as Save or Discard, closes it in its own handler.
109+
106110
### Node views reading consumer state
107111
- ProseMirror mounts node views outside the Vue render tree: no slots, no `$emit` listener.
108112
- tiptap mounts them with `parent: editor.contentComponent`, so `inject` reaches whatever an ancestor of `TipTapEditor` provides.

0 commit comments

Comments
 (0)