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

Improve heuristics around node selection and keyboard navigation #2884

Merged
merged 11 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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 @@ -71,6 +71,15 @@ test.describe('HorizontalRule', () => {

await page.keyboard.press('ArrowRight');

await assertSelection(page, {
anchorOffset: 0,
anchorPath: [0],
focusOffset: 0,
focusPath: [0],
});

await page.keyboard.press('ArrowRight');

await assertSelection(page, {
anchorOffset: 0,
anchorPath: [2],
Expand All @@ -87,10 +96,14 @@ test.describe('HorizontalRule', () => {
focusPath: [0],
});

await page.keyboard.press('ArrowLeft');

await page.keyboard.type('Some text');

await page.keyboard.press('ArrowRight');

await page.keyboard.press('ArrowRight');

await assertSelection(page, {
anchorOffset: 0,
anchorPath: [2],
Expand Down Expand Up @@ -126,6 +139,8 @@ test.describe('HorizontalRule', () => {

await page.keyboard.press('ArrowLeft');

await page.keyboard.press('ArrowLeft');

if (browserName === 'webkit') {
await assertSelection(page, {
anchorOffset: 9,
Expand Down
9 changes: 6 additions & 3 deletions packages/lexical-playground/__tests__/e2e/Images.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,15 @@ test.describe('Images', () => {

await focusEditor(page);
await page.keyboard.press('ArrowLeft');
await page.keyboard.press('ArrowLeft');
await assertSelection(page, {
anchorOffset: 0,
anchorPath: [0],
focusOffset: 0,
focusPath: [0],
});

await page.keyboard.press('ArrowRight');
await page.keyboard.press('ArrowRight');
await assertSelection(page, {
anchorOffset: 1,
Expand All @@ -82,6 +84,7 @@ test.describe('Images', () => {
focusPath: [0],
});

await page.keyboard.press('ArrowRight');
await page.keyboard.press('ArrowRight');
await page.keyboard.press('Backspace');

Expand Down Expand Up @@ -117,7 +120,7 @@ test.describe('Images', () => {
alt="Yellow flower in tilt shift lens"
draggable="false"
style="height: inherit; max-width: 500px; width: inherit;"
class="focused" />
class="focused draggable" />
</div>
<div>
<button class="image-caption-button">Add Caption</button>
Expand Down Expand Up @@ -213,7 +216,7 @@ test.describe('Images', () => {
await insertSampleImage(page);

await focusEditor(page);
await moveLeft(page, 2);
await moveLeft(page, 4);

await assertHTML(
page,
Expand Down Expand Up @@ -301,7 +304,7 @@ test.describe('Images', () => {
await insertSampleImage(page);

await focusEditor(page);
await moveLeft(page, 2);
await moveLeft(page, 4);

await assertHTML(
page,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,7 @@ test.describe('TextFormatting', () => {
<div draggable="false">
<img
alt="Yellow flower in tilt shift lens"
class="focused"
draggable="false"
src="${SAMPLE_IMAGE_URL}"
style="height: inherit; max-width: 500px; width: inherit" />
Expand Down
4 changes: 2 additions & 2 deletions packages/lexical-playground/__tests__/e2e/Toolbar.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ test.describe('Toolbar', () => {
<div draggable="true">
<img
alt="Yellow flower in tilt shift lens"
class="focused"
class="focused draggable"
draggable="false"
src="${SAMPLE_IMAGE_URL}"
style="height: inherit; max-width: 500px; width: inherit" />
Expand Down Expand Up @@ -260,7 +260,7 @@ test.describe('Toolbar', () => {
<div draggable="true">
<img
alt="Yellow flower in tilt shift lens"
class="focused"
class="focused draggable"
draggable="false"
src="${SAMPLE_IMAGE_URL}"
style="height: inherit; max-width: 500px; width: inherit" />
Expand Down
27 changes: 25 additions & 2 deletions packages/lexical-playground/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -910,16 +910,19 @@ i.prettier-error {

.editor-shell .editor-image img {
max-width: 100%;
cursor: default;
}

.editor-shell .editor-image img.focused {
outline: 2px solid rgb(60, 132, 244);
user-select: none;
cursor: move;
}

.editor-shell .editor-image img.focused.draggable {
cursor: grab;
}

.editor-shell .editor-image img.focused:active {
.editor-shell .editor-image img.focused.draggable:active {
cursor: grabbing;
}

Expand Down Expand Up @@ -1533,3 +1536,23 @@ button.item.dropdown-item-active {
button.item.dropdown-item-active i {
opacity: 1;
}

hr {
padding: 2px 2px;
border: none;
margin: 1em 0;
cursor: pointer;
}

hr:after {
content: "";
display: block;
height: 2px;
background-color: #ccc;
line-height: 2px;
}

hr.selected {
outline: 2px solid rgb(60, 132, 244);
user-select: none;
}
18 changes: 17 additions & 1 deletion packages/lexical-playground/src/nodes/EquationNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
import {mergeRegister} from '@lexical/utils';
import {
$getNodeByKey,
$getSelection,
$isNodeSelection,
COMMAND_PRIORITY_HIGH,
DecoratorNode,
KEY_ESCAPE_COMMAND,
Expand Down Expand Up @@ -96,8 +98,22 @@ function EquationComponent({
COMMAND_PRIORITY_HIGH,
),
);
} else {
return editor.registerUpdateListener(({editorState}) => {
const isSelected = editorState.read(() => {
const selection = $getSelection();
return (
$isNodeSelection(selection) &&
selection.has(nodeKey) &&
selection.getNodes().length === 1
);
});
if (isSelected) {
setShowEquationEditor(true);
}
});
}
}, [editor, onHide, showEquationEditor]);
}, [editor, nodeKey, onHide, showEquationEditor]);

return (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ function ExcalidrawComponent({
);
const imageContainerRef = useRef<HTMLImageElement | null>(null);
const buttonRef = useRef<HTMLButtonElement | null>(null);
const captionButtonRef = useRef<HTMLButtonElement | null>(null);
const [isSelected, setSelected, clearSelection] =
useLexicalNodeSelection(nodeKey);
const [isResizing, setIsResizing] = useState<boolean>(false);
Expand Down Expand Up @@ -184,6 +185,7 @@ function ExcalidrawComponent({
/>
{(isSelected || isResizing) && (
<ImageResizer
buttonRef={captionButtonRef}
showCaption={true}
setShowCaption={() => null}
imageRef={imageContainerRef}
Expand Down
Loading