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

Table selection keyboard controls #4689

Merged
merged 11 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from 10 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
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ test.describe('HTML Tables CopyAndPaste', () => {
page,
html`
<p class="PlaygroundEditorTheme__paragraph"><br /></p>
<table class="PlaygroundEditorTheme__table disable-selection">
<table
class="PlaygroundEditorTheme__table PlaygroundEditorTheme__tableSelection">
<tr>
<th
class="PlaygroundEditorTheme__tableCell PlaygroundEditorTheme__tableCellHeader"
Expand Down
15 changes: 10 additions & 5 deletions packages/lexical-playground/__tests__/e2e/Indentation.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ test.describe('Identation', () => {
<span data-lexical-text="true">code</span>
</code>
<p class="PlaygroundEditorTheme__paragraph"><br /></p>
<table class="PlaygroundEditorTheme__table disable-selection">
<table
class="PlaygroundEditorTheme__table PlaygroundEditorTheme__tableSelection">
<tr>
<th
class="PlaygroundEditorTheme__tableCell PlaygroundEditorTheme__tableCellHeader"
Expand Down Expand Up @@ -183,7 +184,8 @@ test.describe('Identation', () => {
style="padding-inline-start: calc(40px)">
<br />
</p>
<table class="PlaygroundEditorTheme__table disable-selection">
<table
class="PlaygroundEditorTheme__table PlaygroundEditorTheme__tableSelection">
<tr>
<th
class="PlaygroundEditorTheme__tableCell PlaygroundEditorTheme__tableCellHeader"
Expand Down Expand Up @@ -274,7 +276,8 @@ test.describe('Identation', () => {
style="padding-inline-start: calc(80px)">
<br />
</p>
<table class="PlaygroundEditorTheme__table disable-selection">
<table
class="PlaygroundEditorTheme__table PlaygroundEditorTheme__tableSelection">
<tr>
<th
class="PlaygroundEditorTheme__tableCell PlaygroundEditorTheme__tableCellHeader"
Expand Down Expand Up @@ -359,7 +362,8 @@ test.describe('Identation', () => {
style="padding-inline-start: calc(40px)">
<br />
</p>
<table class="PlaygroundEditorTheme__table disable-selection">
<table
class="PlaygroundEditorTheme__table PlaygroundEditorTheme__tableSelection">
<tr>
<th
class="PlaygroundEditorTheme__tableCell PlaygroundEditorTheme__tableCellHeader"
Expand Down Expand Up @@ -434,7 +438,8 @@ test.describe('Identation', () => {
<span data-lexical-text="true">code</span>
</code>
<p class="PlaygroundEditorTheme__paragraph" style=""><br /></p>
<table class="PlaygroundEditorTheme__table disable-selection">
<table
class="PlaygroundEditorTheme__table PlaygroundEditorTheme__tableSelection">
<tr>
<th
class="PlaygroundEditorTheme__tableCell PlaygroundEditorTheme__tableCellHeader"
Expand Down
13 changes: 12 additions & 1 deletion packages/lexical-playground/__tests__/e2e/Tables.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ test.describe('Tables', () => {
page,
isPlainText,
isCollab,
browserName,
}) => {
await initialize({isCollab, page});
test.skip(isPlainText);
Expand All @@ -308,6 +309,10 @@ test.describe('Tables', () => {

await page.keyboard.down('Shift');
await page.keyboard.press('ArrowRight');
// Firefox range selection spans across cells after two arrow key press
if (browserName === 'firefox') {
await page.keyboard.press('ArrowRight');
}
await page.keyboard.press('ArrowDown');
await page.keyboard.up('Shift');

Expand Down Expand Up @@ -801,6 +806,7 @@ test.describe('Tables', () => {
page,
isPlainText,
isCollab,
browserName,
}) => {
await initialize({isCollab, page});
test.skip(isPlainText);
Expand All @@ -814,6 +820,10 @@ test.describe('Tables', () => {

await page.keyboard.down('Shift');
await page.keyboard.press('ArrowRight');
// Firefox range selection spans across cells after two arrow key press
if (browserName === 'firefox') {
await page.keyboard.press('ArrowRight');
}
await page.keyboard.press('ArrowDown');
await page.keyboard.up('Shift');

Expand Down Expand Up @@ -1301,7 +1311,8 @@ test.describe('Tables', () => {
page,
html`
<p class="PlaygroundEditorTheme__paragraph"><br /></p>
<table class="PlaygroundEditorTheme__table disable-selection">
<table
class="PlaygroundEditorTheme__table PlaygroundEditorTheme__tableSelection">
<tr>
<th
class="PlaygroundEditorTheme__tableCell PlaygroundEditorTheme__tableCellHeader"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import {
assertGridSelectionCoordinates,
assertIsGridSelection,
click,
focusEditor,
initialize,
Expand Down Expand Up @@ -47,8 +46,6 @@ test.describe('Regression test #4697', () => {
false,
);

await assertIsGridSelection(page);

await assertGridSelectionCoordinates(page, {
anchor: {x: 2, y: 1},
focus: {x: 2, y: 2},
Expand Down
26 changes: 8 additions & 18 deletions packages/lexical-playground/__tests__/utils/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ async function exposeLexicalEditor(page) {
await leftFrame.waitForSelector('.tree-view-output pre');
await leftFrame.evaluate(() => {
window.lexicalEditor = document.querySelector(
'.tree-view-output pre',
'[data-lexical-editor="true"]',
).__lexicalEditor;
});
}
Expand Down Expand Up @@ -202,22 +202,16 @@ async function retryAsync(page, fn, attempts) {
}
}

export async function assertIsGridSelection(page, coordinates) {
const gridKey = await page.evaluate(() => {
const editor = window.lexicalEditor;
const editorState = editor.getEditorState();
const selection = editorState._selection;
return selection.gridKey;
});

expect(gridKey).not.toBeUndefined();
}

export async function assertGridSelectionCoordinates(page, coordinates) {
const {_anchor, _focus} = await page.evaluate(() => {
const pageOrFrame = IS_COLLAB ? await page.frame('left') : page;

const {_anchor, _focus} = await pageOrFrame.evaluate(() => {
const editor = window.lexicalEditor;
const editorState = editor.getEditorState();
const selection = editorState._selection;
if (!selection.gridKey) {
throw new Error('Expected grid selection');
}
const anchorElement = editor.getElementByKey(selection.anchor.key);
const focusElement = editor.getElementByKey(selection.focus.key);
return {
Expand Down Expand Up @@ -826,11 +820,7 @@ export async function selectCellsFromTableCords(
);

// Focus on inside the iFrame or the boundingBox() below returns null.
await firstRowFirstColumnCell.click(
// This is a test runner quirk. Chrome seems to need two clicks to focus on the
// content editable cell before dragging, but Firefox treats it as a double click event.
E2E_BROWSER === 'chromium' ? {clickCount: 2} : {},
);
await firstRowFirstColumnCell.click();

await dragMouse(
page,
Expand Down
17 changes: 0 additions & 17 deletions packages/lexical-playground/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -1246,23 +1246,6 @@ i.prettier-error {
background-image: url(images/icons/plug-fill.svg);
}

table.disable-selection {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}

table.disable-selection span::selection {
background-color: transparent;
}

table.disable-selection br::selection {
background-color: transparent;
}

.table-cell-action-button-container {
position: absolute;
top: 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@
width: calc(100% - 25px);
margin: 30px 0;
}
.PlaygroundEditorTheme__tableSelection *::selection {
background-color: transparent;
}
.PlaygroundEditorTheme__tableSelected {
outline: 2px solid rgb(60, 132, 244);
}
Expand All @@ -135,7 +138,6 @@
text-align: start;
padding: 6px 8px;
position: relative;
cursor: default;
outline: none;
}
.PlaygroundEditorTheme__tableCellSortedIndicator {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ const theme: EditorThemeClasses = {
tableCellSortedIndicator: 'PlaygroundEditorTheme__tableCellSortedIndicator',
tableResizeRuler: 'PlaygroundEditorTheme__tableCellResizeRuler',
tableSelected: 'PlaygroundEditorTheme__tableSelected',
tableSelection: 'PlaygroundEditorTheme__tableSelection',
text: {
bold: 'PlaygroundEditorTheme__textBold',
code: 'PlaygroundEditorTheme__textCode',
Expand Down
5 changes: 4 additions & 1 deletion packages/lexical-react/src/LexicalTablePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ function $insertFirst(parent: ElementNode, node: LexicalNode): void {
export function TablePlugin({
hasCellMerge = true,
hasCellBackgroundColor = true,
hasTabHandler = true,
}: {
hasCellMerge?: boolean;
hasCellBackgroundColor?: boolean;
hasTabHandler?: boolean;
}): JSX.Element | null {
const [editor] = useLexicalComposerContext();

Expand Down Expand Up @@ -103,6 +105,7 @@ export function TablePlugin({
tableNode,
tableElement,
editor,
hasTabHandler,
);
tableSelections.set(nodeKey, tableSelection);
}
Expand Down Expand Up @@ -150,7 +153,7 @@ export function TablePlugin({
tableSelection.removeListeners();
}
};
}, [editor]);
}, [editor, hasTabHandler]);

// Unmerge cells when the feature isn't enabled
useEffect(() => {
Expand Down
12 changes: 9 additions & 3 deletions packages/lexical-table/src/LexicalTableSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import type {
TextFormatType,
} from 'lexical';

import {
addClassNamesToElement,
removeClassNamesFromElement,
} from '@lexical/utils';
import {
$createParagraphNode,
$createRangeSelection,
Expand All @@ -37,8 +41,6 @@ import {
getTableGrid,
} from './LexicalTableSelectionHelpers';

export const BACKGROUND_COLOR = 'background-color';
export const BACKGROUND_IMAGE = 'background-image';
export type Cell = {
elem: HTMLElement;
highlighted: boolean;
Expand Down Expand Up @@ -197,6 +199,10 @@ export class TableSelection {
throw new Error('Expected to find TableElement in DOM');
}

removeClassNamesFromElement(
tableElement,
editor._config.theme.tableSelection,
);
tableElement.classList.remove('disable-selection');
this.hasHijackedSelectionStyles = false;
});
Expand All @@ -211,7 +217,7 @@ export class TableSelection {
throw new Error('Expected to find TableElement in DOM');
}

tableElement.classList.add('disable-selection');
addClassNamesToElement(tableElement, editor._config.theme.tableSelection);
this.hasHijackedSelectionStyles = true;
});
}
Expand Down
Loading