[don't merge this] A very rough sketch of Automerge support in the Nova playground. #15
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "pvh/myte:automerge"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@ -10,0 +14,4 @@
isValidAutomergeUrl,
initializeWasm,
updateText,
} = await import("https://esm.sh/@automerge/vanillajs/slim?standalone");
Here we import the Automerge-specific dependencies: the Repo which holds documents, browser-friendly networking and storage adapters (many others exist but these are pretty good starting points), and a couple of helper functions.
@ -10,0 +18,4 @@
await initializeWasm(
fetch("https://esm.sh/@automerge/automerge@2.2.9/dist/automerge.wasm")
);
The core of Automerge is written in Rust and made available via webassembly. Most bundlers will use the non
slim
version of the library and handle loading the wasm object automatically. In a vanilla environment we need this annoying line until the browser vendors decide how to support depending on WASM files will work. (Any year now, I hope.)@ -10,0 +23,4 @@
const repo = new Repo({
storage: new IndexedDBStorageAdapter(),
network: [new WebSocketClientAdapter("wss://sync3.automerge.org")],
});
This is the main bit of setup: we create a repository that will hold documents and configure it to write locally and synchronize with a sync server. You could also add other network adapters like BroadcastChannel to handle local in-browser synchronization, or you could sync to other sync servers.
@ -10,0 +42,4 @@
console.log("DocHandle", handle);
const docUrl = (document.location.hash = handle.url);
This code (from line 30) either loads a document from the hash in the URL or else creates a document and puts it there. It's a little bit lazy in its implementation because it doesn't watch the hash: if you change the hash you need to manually reload.
@ -37,2 +70,2 @@
codeElem.value = shareProgram;
}
codeElem.value = handle.doc().code;
handle.on("change", ({ doc }) => (codeElem.value = doc.code));
And this is the meat of the patch! First, if the document changes, set the text area's value to a particular field in the document.
@ -42,1 +73,3 @@
}
codeElem.addEventListener("input", function (event) {
handle.change((doc) => updateText(doc, ["code"], event.target.value));
});
And second, when the text area changes, update the text in the CRDT. Note the use of updateText -- this means that your edits will be diffed against the current value you have for the CRDT and applied as a patch rather than replacing the whole program on each keystroke.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.