File size: 5,546 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
import React, {
useRef,
useCallback,
MutableRefObject,
useState,
useEffect,
} from 'react'
import { JSX } from 'react'
import { Element, LeafPosition, Text } from 'slate'
import { ResizeObserver as ResizeObserverPolyfill } from '@juggle/resize-observer'
import String from './string'
import { PLACEHOLDER_SYMBOL, EDITOR_TO_PLACEHOLDER_ELEMENT } from 'slate-dom'
import { RenderLeafProps, RenderPlaceholderProps } from './editable'
import { useSlateStatic } from '../hooks/use-slate-static'
import { IS_WEBKIT, IS_ANDROID } from 'slate-dom'
// Delay the placeholder on Android to prevent the keyboard from closing.
// (https://github.com/ianstormtaylor/slate/pull/5368)
const PLACEHOLDER_DELAY = IS_ANDROID ? 300 : 0
function disconnectPlaceholderResizeObserver(
placeholderResizeObserver: MutableRefObject<ResizeObserver | null>,
releaseObserver: boolean
) {
if (placeholderResizeObserver.current) {
placeholderResizeObserver.current.disconnect()
if (releaseObserver) {
placeholderResizeObserver.current = null
}
}
}
type TimerId = ReturnType<typeof setTimeout> | null
function clearTimeoutRef(timeoutRef: MutableRefObject<TimerId>) {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current)
timeoutRef.current = null
}
}
const defaultRenderLeaf = (props: RenderLeafProps) => <DefaultLeaf {...props} />
/**
* Individual leaves in a text node with unique formatting.
*/
const Leaf = (props: {
isLast: boolean
leaf: Text
parent: Element
renderPlaceholder: (props: RenderPlaceholderProps) => JSX.Element
renderLeaf?: (props: RenderLeafProps) => JSX.Element
text: Text
leafPosition?: LeafPosition
}) => {
const {
leaf,
isLast,
text,
parent,
renderPlaceholder,
renderLeaf = defaultRenderLeaf,
leafPosition,
} = props
const editor = useSlateStatic()
const placeholderResizeObserver = useRef<ResizeObserver | null>(null)
const placeholderRef = useRef<HTMLElement | null>(null)
const [showPlaceholder, setShowPlaceholder] = useState(false)
const showPlaceholderTimeoutRef = useRef<TimerId>(null)
const callbackPlaceholderRef = useCallback(
(placeholderEl: HTMLElement | null) => {
disconnectPlaceholderResizeObserver(
placeholderResizeObserver,
placeholderEl == null
)
if (placeholderEl == null) {
EDITOR_TO_PLACEHOLDER_ELEMENT.delete(editor)
leaf.onPlaceholderResize?.(null)
} else {
EDITOR_TO_PLACEHOLDER_ELEMENT.set(editor, placeholderEl)
if (!placeholderResizeObserver.current) {
// Create a new observer and observe the placeholder element.
const ResizeObserver = window.ResizeObserver || ResizeObserverPolyfill
placeholderResizeObserver.current = new ResizeObserver(() => {
leaf.onPlaceholderResize?.(placeholderEl)
})
}
placeholderResizeObserver.current.observe(placeholderEl)
placeholderRef.current = placeholderEl
}
},
[placeholderRef, leaf, editor]
)
let children = (
<String isLast={isLast} leaf={leaf} parent={parent} text={text} />
)
const leafIsPlaceholder = Boolean(leaf[PLACEHOLDER_SYMBOL])
useEffect(() => {
if (leafIsPlaceholder) {
if (!showPlaceholderTimeoutRef.current) {
// Delay the placeholder, so it will not render in a selection
showPlaceholderTimeoutRef.current = setTimeout(() => {
setShowPlaceholder(true)
showPlaceholderTimeoutRef.current = null
}, PLACEHOLDER_DELAY)
}
} else {
clearTimeoutRef(showPlaceholderTimeoutRef)
setShowPlaceholder(false)
}
return () => clearTimeoutRef(showPlaceholderTimeoutRef)
}, [leafIsPlaceholder, setShowPlaceholder])
if (leafIsPlaceholder && showPlaceholder) {
const placeholderProps: RenderPlaceholderProps = {
children: leaf.placeholder,
attributes: {
'data-slate-placeholder': true,
style: {
position: 'absolute',
top: 0,
pointerEvents: 'none',
width: '100%',
maxWidth: '100%',
display: 'block',
opacity: '0.333',
userSelect: 'none',
textDecoration: 'none',
// Fixes https://github.com/udecode/plate/issues/2315
WebkitUserModify: IS_WEBKIT ? 'inherit' : undefined,
},
contentEditable: false,
ref: callbackPlaceholderRef,
},
}
children = (
<React.Fragment>
{children}
{renderPlaceholder(placeholderProps)}
</React.Fragment>
)
}
// COMPAT: Having the `data-` attributes on these leaf elements ensures that
// in certain misbehaving browsers they aren't weirdly cloned/destroyed by
// contenteditable behaviors. (2019/05/08)
const attributes: {
'data-slate-leaf': true
} = {
'data-slate-leaf': true,
}
return renderLeaf({
attributes,
children,
leaf,
text,
leafPosition,
})
}
const MemoizedLeaf = React.memo(Leaf, (prev, next) => {
return (
next.parent === prev.parent &&
next.isLast === prev.isLast &&
next.renderLeaf === prev.renderLeaf &&
next.renderPlaceholder === prev.renderPlaceholder &&
next.text === prev.text &&
Text.equals(next.leaf, prev.leaf) &&
next.leaf[PLACEHOLDER_SYMBOL] === prev.leaf[PLACEHOLDER_SYMBOL]
)
})
export const DefaultLeaf = (props: RenderLeafProps) => {
const { attributes, children } = props
return <span {...attributes}>{children}</span>
}
export default MemoizedLeaf
|