-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
packages/lexical-playground/__tests__/e2e/Events.spec.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
import { | ||
assertHTML, | ||
evaluate, | ||
focusEditor, | ||
html, | ||
initialize, | ||
LEGACY_EVENTS, | ||
test, | ||
} from '../utils/index.mjs'; | ||
|
||
test.describe('Events', () => { | ||
test.beforeEach(({isCollab, page}) => | ||
initialize({isAutocomplete: true, isCollab, page}), | ||
); | ||
test('Autocapitalization (MacOS specific)', async ({page, isPlainText}) => { | ||
if (LEGACY_EVENTS) { | ||
return; | ||
} | ||
await focusEditor(page); | ||
await page.keyboard.type('i'); | ||
await evaluate(page, () => { | ||
const editable = document.querySelector('[contenteditable="true"]'); | ||
const span = editable.querySelector('span'); | ||
const textNode = span.firstChild; | ||
function singleRangeFn( | ||
startContainer, | ||
startOffset, | ||
endContainer, | ||
endOffset, | ||
) { | ||
return () => [ | ||
new StaticRange({ | ||
endContainer, | ||
endOffset, | ||
startContainer, | ||
startOffset, | ||
}), | ||
]; | ||
} | ||
const character = 'S'; // S for space because the space itself gets trimmed in the assertHTML | ||
const replacementCharacter = 'I'; | ||
const dataTransfer = new DataTransfer(); | ||
dataTransfer.setData('text/plain', replacementCharacter); | ||
dataTransfer.setData('text/html', replacementCharacter); | ||
const characterBeforeInputEvent = new InputEvent('beforeinput', { | ||
bubbles: true, | ||
cancelable: true, | ||
data: character, | ||
inputType: 'insertText', | ||
}); | ||
characterBeforeInputEvent.getTargetRanges = singleRangeFn( | ||
textNode, | ||
1, | ||
textNode, | ||
1, | ||
); | ||
const replacementBeforeInputEvent = new InputEvent('beforeinput', { | ||
bubbles: true, | ||
cancelable: true, | ||
clipboardData: dataTransfer, | ||
data: replacementCharacter, | ||
dataTransfer, | ||
inputType: 'insertReplacementText', | ||
}); | ||
replacementBeforeInputEvent.getTargetRanges = singleRangeFn( | ||
textNode, | ||
0, | ||
textNode, | ||
1, | ||
); | ||
const characterInputEvent = new InputEvent('input', { | ||
bubbles: true, | ||
cancelable: true, | ||
data: character, | ||
inputType: 'insertText', | ||
}); | ||
editable.dispatchEvent(characterBeforeInputEvent); | ||
textNode.textContent += character; | ||
editable.dispatchEvent(replacementBeforeInputEvent); | ||
editable.dispatchEvent(characterInputEvent); | ||
}); | ||
|
||
await assertHTML( | ||
page, | ||
html` | ||
<p | ||
class="PlaygroundEditorTheme__paragraph PlaygroundEditorTheme__ltr" | ||
dir="ltr"> | ||
<span data-lexical-text="true">IS</span> | ||
</p> | ||
`, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters