Added document ID in URL
This commit is contained in:
parent
c61efa7b52
commit
2ea5f45eb9
@ -1,6 +1,7 @@
|
|||||||
// docManager.js (Automerge 2.x style)
|
// docManager.js (Automerge 2.x style)
|
||||||
|
|
||||||
import { Repo } from "@automerge/automerge-repo"
|
import { Repo, isValidAutomergeUrl } from "@automerge/automerge-repo"
|
||||||
|
import * as Automerge from "@automerge/automerge"
|
||||||
// For IndexedDB storage
|
// For IndexedDB storage
|
||||||
import { IndexedDBStorageAdapter } from '@automerge/automerge-repo-storage-indexeddb'
|
import { IndexedDBStorageAdapter } from '@automerge/automerge-repo-storage-indexeddb'
|
||||||
import { BrowserWebSocketClientAdapter } from "@automerge/automerge-repo-network-websocket"
|
import { BrowserWebSocketClientAdapter } from "@automerge/automerge-repo-network-websocket"
|
||||||
@ -9,29 +10,28 @@ let handle
|
|||||||
|
|
||||||
export function initRepo() {
|
export function initRepo() {
|
||||||
const repo = new Repo({
|
const repo = new Repo({
|
||||||
// Choose a network adapter or omit if you’re doing pure local
|
network: [
|
||||||
network: [new BrowserWebSocketClientAdapter("wss://automerge.rheinheim.fraction.ch")],
|
new BrowserWebSocketClientAdapter("wss://sync.automerge.org")
|
||||||
|
],
|
||||||
|
storage: new IndexedDBStorageAdapter()
|
||||||
|
})
|
||||||
|
|
||||||
// Use IndexedDB instead of localStorage
|
// Check for existing docId in URL hash
|
||||||
storage: new IndexedDBStorageAdapter({
|
const rootDocUrl = document.location.hash.substring(1)
|
||||||
// optionally specify a database name (defaults to "automerge")
|
|
||||||
databaseName: "myAutomergeDB"
|
if (rootDocUrl && isValidAutomergeUrl(rootDocUrl)) {
|
||||||
|
handle = repo.find(rootDocUrl)
|
||||||
|
} else {
|
||||||
|
// Create new document if none exists
|
||||||
|
handle = repo.create({
|
||||||
|
shapes: []
|
||||||
})
|
})
|
||||||
})
|
// Set the URL hash to the new document's URL
|
||||||
|
document.location.hash = handle.url
|
||||||
|
}
|
||||||
|
|
||||||
// Now create or open a document
|
|
||||||
handle = repo.create()
|
|
||||||
|
|
||||||
// Listen for 'change' events
|
|
||||||
handle.on("change", ({doc}) => {
|
handle.on("change", ({doc}) => {
|
||||||
console.log("Document changed!")
|
console.log("Document changed!", doc)
|
||||||
// The up-to-date document is accessible via handle.doc
|
|
||||||
console.log("Current doc state:", doc)
|
|
||||||
})
|
|
||||||
|
|
||||||
// Initialize the doc with some data
|
|
||||||
handle.change(doc => {
|
|
||||||
doc.shapes = []
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return handle
|
return handle
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user