diff --git a/src/docManager.js b/src/docManager.js index 970ca55..f3b3e9b 100644 --- a/src/docManager.js +++ b/src/docManager.js @@ -1,6 +1,7 @@ // 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 import { IndexedDBStorageAdapter } from '@automerge/automerge-repo-storage-indexeddb' import { BrowserWebSocketClientAdapter } from "@automerge/automerge-repo-network-websocket" @@ -9,29 +10,28 @@ let handle export function initRepo() { const repo = new Repo({ - // Choose a network adapter or omit if you’re doing pure local - network: [new BrowserWebSocketClientAdapter("wss://automerge.rheinheim.fraction.ch")], + network: [ + new BrowserWebSocketClientAdapter("wss://sync.automerge.org") + ], + storage: new IndexedDBStorageAdapter() + }) - // Use IndexedDB instead of localStorage - storage: new IndexedDBStorageAdapter({ - // optionally specify a database name (defaults to "automerge") - databaseName: "myAutomergeDB" + // Check for existing docId in URL hash + const rootDocUrl = document.location.hash.substring(1) + + 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}) => { - console.log("Document changed!") - // 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 = [] + console.log("Document changed!", doc) }) return handle