-
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
23e4dab
6b8e5aa
85cbf8f
cf28328
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,159 @@ | ||||||||||||||
| /** | ||||||||||||||
| * 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} 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; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| export function registerTouchIndentation( | ||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
| editor: LexicalEditor, | ||||||||||||||
| swipeThreshold: number = DEFAULT_SWIPE_THRESHOLD, | ||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can use a signal and doesn't need to support defaults for legacy code
Suggested change
|
||||||||||||||
| ): () => 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()); | ||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
| } | ||||||||||||||
| }; | ||||||||||||||
|
|
||||||||||||||
| 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 && | ||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
| 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 && | ||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
| Math.abs(deltaY) < DEFAULT_VERTICAL_GUARD | ||||||||||||||
| ) { | ||||||||||||||
| event.preventDefault(); | ||||||||||||||
| if (deltaX > 0) { | ||||||||||||||
| editor.dispatchCommand(INDENT_CONTENT_COMMAND, undefined); | ||||||||||||||
| } else { | ||||||||||||||
| editor.dispatchCommand(OUTDENT_CONTENT_COMMAND, undefined); | ||||||||||||||
| } | ||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| 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.value); | ||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
| } | ||||||||||||||
| }); | ||||||||||||||
| }, | ||||||||||||||
| }); | ||||||||||||||
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.
also doesn't need to change