added YIndexedDB

This commit is contained in:
Kevin Jahns
2018-06-03 21:58:51 +02:00
parent a1fb1a6258
commit 9df20fac8a
10 changed files with 299 additions and 201 deletions

View File

@@ -4,11 +4,20 @@ import Y from '../../src/Y.mjs'
import DomBinding from '../../src/Bindings/DomBinding/DomBinding.mjs'
import UndoManager from '../../src/Util/UndoManager.mjs'
import YXmlFragment from '../../src/Types/YXml/YXmlFragment.mjs'
import YIndexdDBPersistence from '../../src/Persistences/IndexedDBPersistence.mjs'
const connector = new YWebsocketsConnector()
const persistence = new YIndexdDBPersistence()
const y = new Y('html-editor', null, null, { gc: true })
connector.connectY('html-editor2', y)
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)
})
window.connector = connector
window.persistence = persistence
window.onload = function () {
window.domBinding = new DomBinding(window.yXmlType, document.body, { scrollingElement: document.scrollingElement })

View File

@@ -0,0 +1,8 @@
<!DOCTYPE html>
<html>
</head>
<script src="./index.mjs" type="module"></script>
</head>
<body contenteditable="true">
</body>
</html>

48
examples/notes/index.mjs Normal file
View File

@@ -0,0 +1,48 @@
import IndexedDBPersistence from '../../src/Persistences/IndexeddbPersistence.mjs'
import YWebsocketsConnector from '../../src/Connectors/WebsocketsConnector/WebsocketsConnector.mjs'
import Y from '../../src/Y.mjs'
import YXmlFragment from '../../src/Types/YXml/YXmlFragment.mjs'
const yCollection = new YCollection(new YWebsocketsConnector(), new IndexedDBPersistence())
const y = yCollection.getDocument('my-notes')
persistence.addConnector(persistence)
const y = new Y()
await persistence.persistY(y)
connector.connectY('html-editor', y)
persistence.connectY('html-editor', y)
window.connector = connector
window.onload = function () {
window.domBinding = new DomBinding(window.yXmlType, document.body, { scrollingElement: document.scrollingElement })
}
window.y = y
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()
}
}