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

feat: add prop enableSelectionOp and onDOMSelectionChangeThrottleTime #5772

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

electroluxcode
Copy link
Contributor

@electroluxcode electroluxcode commented Nov 29, 2024

Description
the issue of the anchor, it was discovered that the function for setting the anchor was filtered out during throttle. Currently, the default value for this is 100 milliseconds. If it is changed to 0 milliseconds, this problem can be solved.

As for the issue of selection, it can be considered to add a new type in the operation name enableSelectionOp. This type can be used to determine whether the selection of the collaborator needs to be applied to the co-collaborator.

This pull request mainly adds the passing of two parameters to solve this issue if developer want to resolve the problem like me. and It will not interfere with the original logic.

Finally, the achieved effect is as follows. It can be seen that both the selection and the anchor are as expected.

Issue
fix #5771

Example

after.mp4
import React, { useMemo, useCallback } from 'react'
import {
  Slate,
  Editable,
  withReact,
} from 'slate-react'
import {
  createEditor,
  Descendant,
} from 'slate'
import { withHistory } from 'slate-history'

const initialValue: Descendant[] = [
  {
    type: 'paragraph',
    children: [
      {
        text: 'With Slater',
      },
    ],
  },
  {
    type: 'paragraph',
    children: [{ text: '' }],
  },
]

const CheckListsExample = () => {
  const renderElement = useCallback(props => <Element {...props} />, [])
  const editor = useMemo(
    () => withHistory(withReact(createEditor())),
    []
  )
  window.editor = editor
  let offset = 0
  const onClick = ()=>{
    setInterval(() => {
      window.editor.apply({
        type: 'insert_text',
        path: [1, 0],
        enableSelectionOp: false,
        offset,
        text: '3',
      })
      offset++
    }, 20);
  }
  return (
   <>
   <button onClick={onClick}>click me trigger</button>
    <Slate editor={editor} initialValue={initialValue}>
      <Editable
        onDOMSelectionChangeThrottleTime={0}
        scrollSelectionIntoView={()=>{
          console.log('scrollSelectionIntoView')
        }}
        renderElement={renderElement}
        placeholder="Get to work…"
        spellCheck
        autoFocus
      />
    </Slate>
   </>
  )
}
const Element = props => {
  const { attributes, children, element } = props

  switch (element.type) {
    default:
      return <p {...attributes}>{children}</p>
  }
}

export default CheckListsExample

Copy link

changeset-bot bot commented Nov 29, 2024

🦋 Changeset detected

Latest commit: d611fc0

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
slate-react Patch
slate Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Collaborator

@dylans dylans left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice improvement and thanks for the explanation.

I don't see an easy way to write an automated test for this, so I'll let that pass for now.

Could you please add a changeset and then I'll land it shortly?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Exception between Select and Anchor Operations in Collaboration
2 participants