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

[WIP][lexical][lexical-selection] Manually handle selection around TextNode boundaries #7317

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
17 changes: 14 additions & 3 deletions packages/lexical-selection/src/range-selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ import {
$isChildCaret,
$isDecoratorNode,
$isElementNode,
$isExtendableTextPointCaret,
$isLeafNode,
$isRangeSelection,
$isRootNode,
$isRootOrShadowRoot,
$isTextNode,
$isTextPointCaret,
$setSelection,
INTERNAL_$isBlock,
} from 'lexical';
Expand Down Expand Up @@ -430,8 +430,19 @@ export function $shouldOverrideDefaultCharacterSelection(
selection.focus,
isBackward ? 'previous' : 'next',
);
if ($isExtendableTextPointCaret(focusCaret)) {
return false;

if ($isTextPointCaret(focusCaret)) {
// https://github.com/facebook/lexical/issues/7301
// Always handle the event manually at the boundaries of a TextNode
// because the DOM selection may not be normalized to the same Text
// child as the point. It could be on another node entirely that
// happens to be zero distance away.
// Conversely, *never* handle the event if it is internal to the
// TextNode.
return (
focusCaret.offset === 0 ||
focusCaret.offset === focusCaret.origin.getTextContentSize()
);
}
for (const nextCaret of $extendCaretToRange(focusCaret)) {
if ($isChildCaret(nextCaret)) {
Expand Down
77 changes: 54 additions & 23 deletions packages/lexical/src/LexicalSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1554,31 +1554,62 @@ export class RangeSelection implements BaseSelection {
) {
removeDOMBlockCursorElement(blockCursorElement, editor, rootElement);
}
if (this.dirty) {
let nextAnchorDOM: HTMLElement | Text | null = getElementByKeyOrThrow(
editor,
this.anchor.key,
);
let nextFocusDOM: HTMLElement | Text | null = getElementByKeyOrThrow(
editor,
this.focus.key,
);
if (this.anchor.type === 'text') {
nextAnchorDOM = getDOMTextNode(nextAnchorDOM);
}
if (this.focus.type === 'text') {
nextFocusDOM = getDOMTextNode(nextFocusDOM);
}
if (nextAnchorDOM && nextFocusDOM) {
setDOMSelectionBaseAndExtent(
domSelection,
nextAnchorDOM,
this.anchor.offset,
nextFocusDOM,
this.focus.offset,
);
let nextAnchorOffset = this.anchor.offset;
let nextAnchorDOM: HTMLElement | Text | null = getElementByKeyOrThrow(
editor,
this.anchor.key,
);
let nextFocusOffset = this.focus.offset;
let nextFocusDOM: HTMLElement | Text | null = getElementByKeyOrThrow(
editor,
this.focus.key,
);
if (this.anchor.type === 'text') {
nextAnchorDOM = getDOMTextNode(nextAnchorDOM);
}
if (this.focus.type === 'text') {
const originalFocusDOM = nextFocusDOM;
const domText = getDOMTextNode(nextFocusDOM);
nextFocusDOM = domText;
if (granularity === 'character') {
// Workaround for https://github.com/facebook/lexical/issues/7301
// We want to make sure we are moving the selection into the next
// text node if we are at a boundary. This is a zero distance movement
// in lexical coordinates.
if (isBackward && nextFocusOffset === 0) {
if ($isTextNode(focusNode.getPreviousSibling())) {
const siblingDOM = getDOMTextNode(originalFocusDOM.previousSibling);
if (siblingDOM) {
nextFocusDOM = siblingDOM;
nextFocusOffset = siblingDOM.length;
}
}
} else if (
!isBackward &&
nextFocusOffset === focusNode.getTextContentSize() &&
$isTextNode(focusNode.getNextSibling())
) {
const siblingDOM = getDOMTextNode(originalFocusDOM.nextSibling);
if (siblingDOM) {
nextFocusDOM = siblingDOM;
nextFocusOffset = 0;
}
}
if (collapse) {
nextAnchorOffset = nextFocusOffset;
nextAnchorDOM = nextFocusDOM;
}
}
}
if (nextAnchorDOM && nextFocusDOM) {
setDOMSelectionBaseAndExtent(
domSelection,
nextAnchorDOM,
nextAnchorOffset,
nextFocusDOM,
nextFocusOffset,
);
}
// We use the DOM selection.modify API here to "tell" us what the selection
// will be. We then use it to update the Lexical selection accordingly. This
// is much more reliable than waiting for a beforeinput and using the ranges
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# React + TypeScript + Vite

This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.

Currently, two official plugins are available:

- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh

## Expanding the ESLint configuration

If you are developing a production application, we recommend updating the configuration to enable type-aware lint rules:

```js
export default tseslint.config({
extends: [
// Remove ...tseslint.configs.recommended and replace with this
...tseslint.configs.recommendedTypeChecked,
// Alternatively, use this for stricter rules
...tseslint.configs.strictTypeChecked,
// Optionally, add this for stylistic rules
...tseslint.configs.stylisticTypeChecked,
],
languageOptions: {
// other options...
parserOptions: {
project: ['./tsconfig.node.json', './tsconfig.app.json'],
tsconfigRootDir: import.meta.dirname,
},
},
})
```

You can also install [eslint-plugin-react-x](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-x) and [eslint-plugin-react-dom](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-dom) for React-specific lint rules:

```js
// eslint.config.js
import reactX from 'eslint-plugin-react-x'
import reactDom from 'eslint-plugin-react-dom'

export default tseslint.config({
plugins: {
// Add the react-x and react-dom plugins
'react-x': reactX,
'react-dom': reactDom,
},
rules: {
// other rules...
// Enable its recommended typescript rules
...reactX.configs['recommended-typescript'].rules,
...reactDom.configs.recommended.rules,
},
})
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import js from '@eslint/js'
import globals from 'globals'
import reactHooks from 'eslint-plugin-react-hooks'
import reactRefresh from 'eslint-plugin-react-refresh'
import tseslint from 'typescript-eslint'

export default tseslint.config(
{ ignores: ['dist'] },
{
extends: [js.configs.recommended, ...tseslint.configs.recommended],
files: ['**/*.{ts,tsx}'],
languageOptions: {
ecmaVersion: 2020,
globals: globals.browser,
},
plugins: {
'react-hooks': reactHooks,
'react-refresh': reactRefresh,
},
rules: {
...reactHooks.configs.recommended.rules,
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
},
},
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Lexical adjacent span deletion test case</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
Loading
Loading