Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(editor): propagate normalization of remote changes
BREAKING CHANGE: removed isLocal, asLocal, applySlateOperations, applyYjsEvents from YjsEditor. asRemote applies to the callback function only, as opposed to spanning to the next tick as well, which prevents it from inadvertently marking operations such as normalizations as remote operations.
- Loading branch information
Showing
9 changed files
with
145 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { Node, Element } from 'slate'; | ||
import { toSlateDoc, toSyncElement, YjsEditor } from '../../src'; | ||
import { createTestEditor } from '../utils'; | ||
|
||
// onChange fires in the next tick, see slate createEditor | ||
async function waitForSlateOnChangeAround(callback: () => void) { | ||
await callback(); | ||
await Promise.resolve(); | ||
} | ||
|
||
describe('YjsEditor', () => { | ||
let yjsEditor: YjsEditor; | ||
|
||
const initialValue: Node[] = [ | ||
{ type: 'paragraph', children: [{ text: '' }] }, | ||
]; | ||
|
||
const newElement: Element = { | ||
type: 'paragraph', | ||
children: [{ text: 'new' }], | ||
}; | ||
|
||
beforeEach(async () => { | ||
yjsEditor = await createTestEditor(initialValue); | ||
}); | ||
|
||
it('should sync changes to the shared type with slate', async () => { | ||
await waitForSlateOnChangeAround(() => | ||
yjsEditor.sharedType.push([toSyncElement(newElement)]) | ||
); | ||
|
||
expect(yjsEditor.children).toEqual<Node[]>([...initialValue, newElement]); | ||
// make sure we didn't erroneously add elements to the shared type - can happen | ||
// if the observer we have for it applies "local" events | ||
expect(toSlateDoc(yjsEditor.sharedType)).toEqual<Node[]>( | ||
yjsEditor.children | ||
); | ||
}); | ||
|
||
it('should apply slate operations to yjs', async () => { | ||
await waitForSlateOnChangeAround(() => yjsEditor.insertNode(newElement)); | ||
|
||
expect(toSlateDoc(yjsEditor.sharedType)).toEqual<Node[]>([ | ||
...initialValue, | ||
newElement, | ||
]); | ||
}); | ||
|
||
it('should apply slate normalizations to yjs', async () => { | ||
const normalization: Element = { | ||
type: 'paragraph', | ||
children: [{ text: 'some normalization' }], | ||
}; | ||
|
||
const { normalizeNode } = yjsEditor; | ||
|
||
yjsEditor.normalizeNode = (entry) => { | ||
const isNewElement = entry[0].text === newElement.children[0].text; | ||
if (isNewElement) { | ||
yjsEditor.insertNode(normalization); | ||
} | ||
|
||
normalizeNode(entry); | ||
}; | ||
|
||
await waitForSlateOnChangeAround(() => | ||
yjsEditor.sharedType.push([toSyncElement(newElement)]) | ||
); | ||
|
||
expect(toSlateDoc(yjsEditor.sharedType)).toEqual<Node[]>([ | ||
...initialValue, | ||
newElement, | ||
normalization, | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters