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

Prevent y-websocket initial value from entering slate-history undos #205

Closed
SamDuvall opened this issue Apr 27, 2021 · 3 comments
Closed

Comments

@SamDuvall
Copy link

I'm running into an issue where loading the initial value from y-websocket is adding an operation into undos from slate-history that I don't want. Having that initial value operation in editor.history allows the user to click undo and wipe out the document, which is not desirable.

I'm creating my editor like slate-yjs-example

  const yDoc = new Y.Doc()
  const sharedType = yDoc.getArray<SyncElement>('content')
  const editor = withYjs(withHistory(withReact(createEditor())), sharedType)

I added a hack to pop off the initial value operation from the history for now:

  const onLoad = () => {
    editor.history.undos.pop()
    sharedType.unobserveDeep(onLoad)
  }
  sharedType.observeDeep(onLoad)

I'm not sure if a fix belongs in slate-yjs or somewhere else, but does anyone have a better approach to using slate-yjs in combinations with y-websocket & slate-history for the initial value?

@BitPhinix
Copy link
Owner

BitPhinix commented Apr 28, 2021

Hi @SamDuvall,

Another way you could solve this is by wrapping editor.apply with a handler that checks if YjsEditor.isRemote(editor) is true and then wrapping the next call with HistoryEditor.withoutSaving

I haven't actually tested it, but it should work 😄

@SamDuvall
Copy link
Author

@BitPhinix, thank you. That was an excellent suggestion and worked beautifully.

I can't think of a good reason why one would want remote changes to creep into their local history. I wish there was an easy way to add this to slate-jys, but it feels wrong to require slate-history in slate-yjs even though I bet a large majority of people using slate-yjs use slate-history as well. Below is the code I added after creating the editor for anyone else who is interested:

  const { apply } = editor
  editor.apply = (...args) => {
    if (YjsEditor.isRemote(editor)) {
      HistoryEditor.withoutSaving(editor, () => {
        apply.apply(editor, args)
      })
    } else {
      apply.apply(editor, args)
    }
  }

@BitPhinix
Copy link
Owner

Awesome, glad I could help!

I haven't yet added this to slate-yjs because I'm planning to add another history handling plugin based on Y.UndoManager to have proper Yjs history support. I'm really busy at the moment but will get back to it in the coming months

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

No branches or pull requests

2 participants