Skip to content

Commit

Permalink
fix selecting mention element in android, can't evoke the keyboard
Browse files Browse the repository at this point in the history
  • Loading branch information
YaoKaiLun committed Feb 18, 2023
1 parent f2607c2 commit 88b91e8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
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

0 comments on commit 88b91e8

Please sign in to comment.