mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-04-21 13:51:59 +02:00
Feat: Enable selection when editor in readonly=true (#4620)
* make editor.selection avaliable when Editor is contenteditable="false" * remove dom selection limition of read-only * Improve grammar of comments regarding selection when read-only is true * Add Changeset Co-authored-by: Xheldon <c1006044256@gmail.com>
This commit is contained in:
parent
77d9f60ab5
commit
0b59ad5414
5
.changeset/short-squids-remember.md
Normal file
5
.changeset/short-squids-remember.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
'slate-react': minor
|
||||
---
|
||||
|
||||
Support selection in readOnly=true editors.
|
@ -234,11 +234,7 @@ export const AndroidEditable = (props: EditableProps): JSX.Element => {
|
||||
const onDOMSelectionChange = useCallback(
|
||||
throttle(() => {
|
||||
try {
|
||||
if (
|
||||
!readOnly &&
|
||||
!state.isUpdatingSelection &&
|
||||
!inputManager.isReconciling.current
|
||||
) {
|
||||
if (!state.isUpdatingSelection && !inputManager.isReconciling.current) {
|
||||
const root = ReactEditor.findDocumentOrShadowRoot(editor)
|
||||
const { activeElement } = root
|
||||
const el = ReactEditor.toDOMNode(editor, editor)
|
||||
|
@ -478,7 +478,6 @@ export const Editable = (props: EditableProps) => {
|
||||
const onDOMSelectionChange = useCallback(
|
||||
throttle(() => {
|
||||
if (
|
||||
!readOnly &&
|
||||
!state.isComposing &&
|
||||
!state.isUpdatingSelection &&
|
||||
!state.isDraggingInternally
|
||||
@ -585,7 +584,12 @@ export const Editable = (props: EditableProps) => {
|
||||
}
|
||||
data-slate-editor
|
||||
data-slate-node="value"
|
||||
contentEditable={readOnly ? undefined : true}
|
||||
// explicitly set this
|
||||
contentEditable={!readOnly}
|
||||
// in some cases, a decoration needs access to the range / selection to decorate a text node,
|
||||
// then you will select the whole text node when you select part the of text
|
||||
// this magic zIndex="-1" will fix it
|
||||
zindex={-1}
|
||||
suppressContentEditableWarning
|
||||
ref={ref}
|
||||
style={{
|
||||
|
@ -216,9 +216,12 @@ export const ReactEditor = {
|
||||
|
||||
return (
|
||||
targetEl.closest(`[data-slate-editor]`) === editorEl &&
|
||||
(!editable ||
|
||||
targetEl.isContentEditable ||
|
||||
!!targetEl.getAttribute('data-slate-zero-width'))
|
||||
(!editable || targetEl.isContentEditable
|
||||
? true
|
||||
: (typeof targetEl.isContentEditable === 'boolean' && // isContentEditable exists only on HTMLElement, and on other nodes it will be undefined
|
||||
// this is the core logic that lets you know you got the right editor.selection instead of null when editor is contenteditable="false"(readOnly)
|
||||
targetEl.closest('[contenteditable="false"]') === editorEl) ||
|
||||
!!targetEl.getAttribute('data-slate-zero-width'))
|
||||
)
|
||||
},
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user