Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions packages/craftcms-legacy/cpcompat/src/legacy-html-control.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ import {
// Legacy inputs (e.g. element select inputs) announce changes with
// jQuery's `.trigger('change')`, which native listeners never see.
window.jQuery?.(this).on('change', this.handleLegacyChange);
// Legacy scripts (e.g. Behave.js) can edit a value on keydown without an
// `input` event. Craft 5's element editor picked those up on keyup.
this.addEventListener('keyup', this.handleLegacyChange);
this.ready = this.mount();
}

Expand All @@ -206,6 +209,7 @@ import {
this.removeEventListener('input', this.handleInput);
this.removeEventListener('change', this.handleInput);
window.jQuery?.(this).off('change', this.handleLegacyChange);
this.removeEventListener('keyup', this.handleLegacyChange);
this._form = null;
this._runId++;
this.dispose();
Expand Down Expand Up @@ -584,12 +588,15 @@ import {
function isTypingEvent(event) {
const target = event.target;

if (!['input', 'keyup'].includes(event.type)) {
return false;
}

if (target instanceof HTMLTextAreaElement) {
return event.type === 'input';
return true;
}

return (
event.type === 'input' &&
target instanceof HTMLInputElement &&
['text', 'email', 'url', 'tel', 'password', 'search'].includes(
target.type
Expand Down
30 changes: 30 additions & 0 deletions yii2-adapter/resources/js/legacy-html-control.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,36 @@ describe('Legacy HTML UI Control', () => {
expect(mutation.value).toEqual({fields: {keywords: ['5', '6']}});
});

it('publishes edits legacy scripts make on keydown without an input event', async () => {
const {container, mutation} = await mount(
{
html: '<textarea name="fields[notes]">Hello</textarea>',
headHtml: '',
bodyHtml: '',
},
{
path: ['fields', 'notes'],
values: {fields: {notes: {'fields[notes]': 'Hello'}}},
}
);
await container.querySelector<HTMLElement & {ready: Promise<void>}>(
'craft-legacy-html-control'
)!.ready;
const textarea = container.querySelector('textarea')!;

// A key that changes nothing, such as an arrow key
textarea.dispatchEvent(new KeyboardEvent('keyup', {bubbles: true}));
await nextTick();
expect(mutation.value).toEqual({});

// What Behave.js does on Backspace: it sets the value itself
textarea.value = 'Hell';
textarea.dispatchEvent(new KeyboardEvent('keyup', {bubbles: true}));
await nextTick();

expect(mutation.value).toEqual({fields: {notes: 'Hell'}});
});

it('restores list inputs from an already-expanded value', async () => {
// After an autosave, the island remounts with the value it last published
// (expanded), not the server's flat input names.
Expand Down
Loading