Preserve external history option (#22)

* feat: handle node text in remove_text op

* feat: add preserveExternalHistory option
This commit is contained in:
George
2020-07-02 00:56:48 +03:00
committed by GitHub
parent b49cf33464
commit 55b1b2ca5a
10 changed files with 72 additions and 36 deletions

View File

@@ -29,6 +29,7 @@
"@slate-collaborative/bridge": "^0.6.7",
"automerge": "0.14.0",
"slate": "0.58.3",
"slate-history": "0.58.3",
"socket.io-client": "^2.3.0",
"typescript": "^3.8.3"
},

View File

@@ -1,6 +1,7 @@
import Automerge from 'automerge'
import { Editor, Operation } from 'slate'
import { HistoryEditor } from 'slate-history'
import {
toJS,
@@ -111,7 +112,8 @@ export const AutomergeEditor = {
applyOperation: (
e: AutomergeEditor,
docId: string,
data: Automerge.Message
data: Automerge.Message,
preserveExternalHistory?: boolean
) => {
try {
const current: any = e.docSet.getDoc(docId)
@@ -126,12 +128,16 @@ export const AutomergeEditor = {
e.isRemote = true
Editor.withoutNormalizing(e, () => {
slateOps.forEach((o: Operation) => {
e.apply(o)
})
})
if (HistoryEditor.isHistoryEditor(e) && !preserveExternalHistory) {
HistoryEditor.withoutSaving(e, () => {
slateOps.forEach((o: Operation) => e.apply(o))
})
} else {
slateOps.forEach((o: Operation) => e.apply(o))
}
e.onCursor && e.onCursor(updated.cursors)
e.onCursor && e.onCursor(updated.cursors)
})
Promise.resolve().then(_ => (e.isRemote = false))
}

View File

@@ -9,6 +9,7 @@ import { CursorData, CollabAction } from '@slate-collaborative/bridge'
export interface AutomergeOptions {
docId: string
cursorData?: CursorData
preserveExternalHistory?: boolean
}
/**
@@ -23,7 +24,7 @@ const withAutomerge = <T extends Editor>(
const { onChange } = e
const { docId, cursorData } = options || {}
const { docId, cursorData, preserveExternalHistory } = options || {}
e.docSet = new Automerge.DocSet()
@@ -73,11 +74,9 @@ const withAutomerge = <T extends Editor>(
if (!e.isRemote) {
AutomergeEditor.applySlateOps(e, docId, operations, cursorData)
onChange()
}
onChange()
// console.log('e', e.children)
}
/**
@@ -97,7 +96,7 @@ const withAutomerge = <T extends Editor>(
e.receiveOperation = data => {
if (docId !== data.docId) return
AutomergeEditor.applyOperation(e, docId, data)
AutomergeEditor.applyOperation(e, docId, data, preserveExternalHistory)
}
return e