Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues related to selecting all text #11099

Merged
merged 5 commits into from
Mar 31, 2022
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
12 changes: 11 additions & 1 deletion packages/rich-text/src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* External dependencies
*/
import { Editor } from 'draft-js';
import { Editor, getDefaultKeyBinding, KeyBindingUtil } from 'draft-js';
import PropTypes from 'prop-types';
import {
useEffect,
Expand Down Expand Up @@ -82,6 +82,15 @@ function RichTextEditor({ content, onChange }, ref) {
return null;
}

const { hasCommandModifier } = KeyBindingUtil;

function bindKeys(e) {
if (e.code === 'KeyA' && hasCommandModifier(e)) {
return 'selectall';
}
return getDefaultKeyBinding(e);
}

// Handle basic key commands such as bold, italic and underscore.
const handleKeyCommand = getHandleKeyCommand();
return (
Expand All @@ -91,6 +100,7 @@ function RichTextEditor({ content, onChange }, ref) {
editorState={editorState}
handleKeyCommand={handleKeyCommand}
handlePastedText={handlePastedText}
keyBindingFn={bindKeys}
customStyleFn={customInlineDisplay}
spellCheck
stripPastedStyles
Expand Down
8 changes: 7 additions & 1 deletion packages/rich-text/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* External dependencies
*/
import { SelectionState } from 'draft-js';
import { EditorState, SelectionState } from 'draft-js';
import { filterEditorState } from 'draftjs-filters';

/**
Expand Down Expand Up @@ -63,6 +63,12 @@ function getStateFromCommmand(command, oldEditorState) {
case 'uppercase':
return uppercaseFormatter.setters.toggleUppercase(oldEditorState);

case 'selectall':
return EditorState.forceSelection(
oldEditorState,
getSelectionForAll(oldEditorState.getCurrentContent())
);

default:
return null;
}
Expand Down
26 changes: 26 additions & 0 deletions packages/story-editor/src/karma/element-library/text/edit.karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,32 @@ describe('TextEdit integration', () => {
'<span style="font-weight: 700">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</span>'
);
});

it('should select all text and delete it', async () => {
await fixture.events.mouse.clickOn(frame, 30, 5); // enter the edit mode by clicking

// Needed because of https://github.com/puppeteer/puppeteer/issues/1313
if (navigator.userAgentData.platform === 'macOS') {
document.execCommand('selectAll'); // not the same as mod+a, but does work on macOS
} else {
await fixture.events.keyboard.shortcut('mod+a'); // doesn't work on macOS, works on Ubuntu
}
merapi marked this conversation as resolved.
Show resolved Hide resolved
const text = '461';
await fixture.events.keyboard.type(text);

// Exit edit mode using the Esc key
await fixture.events.keyboard.press('Esc');

// The element is still selected and updated.
await waitFor(async () => {
const story = await fixture.renderHook(() => useStory());
if (!story.state.selectedElements.length) {
throw new Error('story not ready');
}

expect(story.state.selectedElements[0].content).toEqual(text);
});
});
});

describe('shortcuts', () => {
Expand Down