mirror of
https://github.com/cudr/slate-collaborative.git
synced 2026-03-02 03:40:18 +00:00
Preserve external history option (#22)
* feat: handle node text in remove_text op * feat: add preserveExternalHistory option
This commit is contained in:
@@ -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"
|
||||
},
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user