Files
yjs/examples/html-editor/index.mjs

42 lines
1.4 KiB
JavaScript
Raw Normal View History

import YWebsocketsConnector from '../../src/Connectors/WebsocketsConnector/WebsocketsConnector.mjs'
import Y from '../../src/Y.mjs'
import DomBinding from '../../src/Bindings/DomBinding/DomBinding.mjs'
import UndoManager from '../../src/Util/UndoManager.mjs'
2018-05-23 16:15:11 +02:00
import YXmlFragment from '../../src/Types/YXml/YXmlFragment.mjs'
2018-06-03 21:58:51 +02:00
import YIndexdDBPersistence from '../../src/Persistences/IndexedDBPersistence.mjs'
const connector = new YWebsocketsConnector()
2018-06-03 21:58:51 +02:00
const persistence = new YIndexdDBPersistence()
2018-05-23 16:15:11 +02:00
const y = new Y('html-editor', null, null, { gc: true })
2018-06-03 21:58:51 +02:00
persistence.connectY('html-editor', y).then(() => {
// connect after persisted content was applied to y
// If we don't wait for persistence, the other peer will send all data, waisting
// network bandwidth..
connector.connectY('html-editor', y)
})
2018-06-02 13:33:04 +02:00
window.connector = connector
2018-06-03 21:58:51 +02:00
window.persistence = persistence
window.onload = function () {
window.domBinding = new DomBinding(window.yXmlType, document.body, { scrollingElement: document.scrollingElement })
}
window.y = y
2018-05-23 16:15:11 +02:00
window.yXmlType = y.define('xml', YXmlFragment)
window.undoManager = new UndoManager(window.yXmlType, {
captureTimeout: 500
})
document.onkeydown = function interceptUndoRedo (e) {
if (e.keyCode === 90 && (e.metaKey || e.ctrlKey)) {
if (!e.shiftKey) {
window.undoManager.undo()
} else {
window.undoManager.redo()
}
e.preventDefault()
}
}