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

fix selecting mention element in android, can't evoke the keyboard #5298

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -352,12 +352,25 @@ export function createAndroidInputManager({
insertPositionHint = false
}

let inVoidNode = false
let [nativeTargetRange] = (event as any).getTargetRanges()
if (nativeTargetRange) {
targetRange = ReactEditor.toSlateRange(editor, nativeTargetRange, {
exactMatch: false,
suppressThrow: true,
})

if (
nativeTargetRange.collapsed &&
nativeTargetRange.startContainer.nodeType === globalThis.Node.TEXT_NODE
) {
const closestElement = nativeTargetRange.startContainer.parentElement.closest(
'[data-slate-node=element]'
)
inVoidNode = closestElement?.attributes.hasOwnProperty(
'data-slate-void'
)
}
}

// COMPAT: SelectionChange event is fired after the action is performed, so we
Expand Down Expand Up @@ -515,12 +528,14 @@ export function createAndroidInputManager({
}

case 'insertLineBreak': {
if (inVoidNode) return
return scheduleAction(() => Editor.insertSoftBreak(editor), {
at: targetRange,
})
}

case 'insertParagraph': {
if (inVoidNode) return
return scheduleAction(() => Editor.insertBreak(editor), {
at: targetRange,
})
Expand All @@ -533,6 +548,7 @@ export function createAndroidInputManager({
case 'insertFromYank':
case 'insertReplacementText':
case 'insertText': {
if (inVoidNode) return
if (data?.constructor.name === 'DataTransfer') {
return scheduleAction(() => ReactEditor.insertData(editor, data), {
at: targetRange,
Expand Down
13 changes: 11 additions & 2 deletions site/examples/mentions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ import {
withReact,
useSelected,
useFocused,
useReadOnly,
} from 'slate-react'

import { Portal } from '../components'
import { MentionElement } from './custom-types'

export const IS_ANDROID =
typeof navigator !== 'undefined' && /Android/.test(navigator.userAgent)

const MentionExample = () => {
const ref = useRef<HTMLDivElement | null>()
const [target, setTarget] = useState<Range | undefined>()
Expand Down Expand Up @@ -204,6 +208,7 @@ const Element = props => {
const Mention = ({ attributes, children, element }) => {
const selected = useSelected()
const focused = useFocused()
const readOnly = useReadOnly()
const style: React.CSSProperties = {
padding: '3px 3px 2px',
margin: '0 1px',
Expand All @@ -221,14 +226,18 @@ const Mention = ({ attributes, children, element }) => {
if (element.children[0].italic) {
style.fontStyle = 'italic'
}

return (
<span
{...attributes}
contentEditable={false}
contentEditable={!readOnly && IS_ANDROID}
data-cy={`mention-${element.character.replace(' ', '-')}`}
style={style}
>
{children}@{element.character}
{children}
<span contentEditable="false" style={{ userSelect: 'none' }}>
@{element.character}
</span>
</span>
)
}
Expand Down