-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[lexical] Feature: Add touch swipe indentation for list items #8478
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
Draft
abhishekvishwakarma007
wants to merge
4
commits into
facebook:main
Choose a base branch
from
abhishekvishwakarma007:feat/touch-swipe-indentation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
23e4dab
[lexical] Feature: Add touch swipe indentation for list items (#8268)
6b8e5aa
[lexical] Fix: Address review feedback for touch swipe indentation
85cbf8f
[lexical] Address review feedback: extension-only, no legacy plugin
cf28328
[lexical] Address review feedback: add flow types, revert tsconfig.bu…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
158 changes: 158 additions & 0 deletions
158
packages/lexical-extension/src/TouchIndentationExtension.ts
This file contains hidden or 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,158 @@ | ||
| /** | ||
| * 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 type {LexicalEditor} from 'lexical'; | ||
|
|
||
| import {$findMatchingParent} from '@lexical/utils'; | ||
| import { | ||
| $getSelection, | ||
| $isElementNode, | ||
| $isRangeSelection, | ||
| defineExtension, | ||
| INDENT_CONTENT_COMMAND, | ||
| OUTDENT_CONTENT_COMMAND, | ||
| safeCast, | ||
| } from 'lexical'; | ||
|
|
||
| import {namedSignals} from './namedSignals'; | ||
| import {effect, type Signal} from './signals'; | ||
|
|
||
| const DEFAULT_SWIPE_THRESHOLD = 50; | ||
| const DEFAULT_VERTICAL_GUARD = 30; | ||
|
|
||
| function $isSelectionInListItem(): boolean { | ||
| const selection = $getSelection(); | ||
| if (!$isRangeSelection(selection)) { | ||
| return false; | ||
| } | ||
| const node = selection.anchor.getNode(); | ||
| const listItem = $findMatchingParent( | ||
| node, | ||
| parent => $isElementNode(parent) && parent.getType() === 'listitem', | ||
| ); | ||
| return listItem != null; | ||
| } | ||
|
|
||
| function registerTouchIndentation( | ||
| editor: LexicalEditor, | ||
| swipeThreshold: Signal<number>, | ||
| ): () => void { | ||
| return editor.registerRootListener(rootElement => { | ||
| if (rootElement !== null) { | ||
| let startX = 0; | ||
| let startY = 0; | ||
| let isSwiping = false; | ||
| let isInListItem = false; | ||
|
|
||
| const handleTouchStart = (event: TouchEvent) => { | ||
| if (event.touches.length > 1) { | ||
| return; | ||
| } | ||
| const touch = event.touches[0]; | ||
| if (touch != null && editor.isEditable()) { | ||
| startX = touch.clientX; | ||
| startY = touch.clientY; | ||
| isSwiping = false; | ||
| isInListItem = editor.read(() => $isSelectionInListItem()); | ||
| } | ||
| }; | ||
|
|
||
| const handleTouchMove = (event: TouchEvent) => { | ||
| if (!isInListItem || event.touches.length > 1) { | ||
| return; | ||
| } | ||
| const touch = event.touches[0]; | ||
| if (touch != null) { | ||
| const deltaX = touch.clientX - startX; | ||
| const deltaY = touch.clientY - startY; | ||
| if ( | ||
| Math.abs(deltaX) > swipeThreshold.peek() && | ||
| Math.abs(deltaY) < DEFAULT_VERTICAL_GUARD | ||
| ) { | ||
| isSwiping = true; | ||
| event.preventDefault(); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| const handleTouchEnd = (event: TouchEvent) => { | ||
| if (!isSwiping) { | ||
| isSwiping = false; | ||
| isInListItem = false; | ||
| return; | ||
| } | ||
| const touch = event.changedTouches[0]; | ||
| if (touch != null) { | ||
| const deltaX = touch.clientX - startX; | ||
| const deltaY = touch.clientY - startY; | ||
| if ( | ||
| Math.abs(deltaX) > swipeThreshold.peek() && | ||
| Math.abs(deltaY) < DEFAULT_VERTICAL_GUARD | ||
| ) { | ||
| event.preventDefault(); | ||
| editor.dispatchCommand( | ||
| deltaX > 0 ? INDENT_CONTENT_COMMAND : OUTDENT_CONTENT_COMMAND, | ||
| undefined, | ||
| ); | ||
| } | ||
| } | ||
| isSwiping = false; | ||
| isInListItem = false; | ||
| }; | ||
|
|
||
| rootElement.addEventListener('touchstart', handleTouchStart, { | ||
| capture: true, | ||
| passive: true, | ||
| }); | ||
| rootElement.addEventListener('touchmove', handleTouchMove, { | ||
| capture: true, | ||
| passive: false, | ||
| }); | ||
| rootElement.addEventListener('touchend', handleTouchEnd, { | ||
| capture: true, | ||
| passive: false, | ||
| }); | ||
|
|
||
| return () => { | ||
| rootElement.removeEventListener('touchstart', handleTouchStart, { | ||
| capture: true, | ||
| }); | ||
| rootElement.removeEventListener('touchmove', handleTouchMove, { | ||
| capture: true, | ||
| }); | ||
| rootElement.removeEventListener('touchend', handleTouchEnd, { | ||
| capture: true, | ||
| }); | ||
| }; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| export interface TouchIndentationConfig { | ||
| disabled: boolean; | ||
| swipeThreshold: number; | ||
| } | ||
|
|
||
| export const TouchIndentationExtension = defineExtension({ | ||
| build(editor, config, state) { | ||
| return namedSignals(config); | ||
| }, | ||
| config: safeCast<TouchIndentationConfig>({ | ||
| disabled: false, | ||
| swipeThreshold: DEFAULT_SWIPE_THRESHOLD, | ||
| }), | ||
| name: '@lexical/extension/TouchIndentation', | ||
| register(editor, config, state) { | ||
| const {disabled, swipeThreshold} = state.getOutput(); | ||
| return effect(() => { | ||
| if (!disabled.value) { | ||
| return registerTouchIndentation(editor, swipeThreshold); | ||
| } | ||
| }); | ||
| }, | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.