commit d6e15dc3d2e154615e5f7fa2485d094abfef4977
parent 4190bfcb38a13b24846375f7277d6cb49853230b
Author: tongong <tongong@gmx.net>
Date: Fri, 1 Apr 2022 18:01:58 +0200
first working version
Diffstat:
3 files changed, 37 insertions(+), 11 deletions(-)
diff --git a/src/components/edit-view.js b/src/components/edit-view.js
@@ -1,20 +1,19 @@
const m = require("mithril");
-
-const Y = require("yjs");
-const { WebrtcProvider } = require("y-webrtc");
const { TextAreaBinding } = require("y-textarea");
-const ydoc = new Y.Doc();
-const textelem = ydoc.getText("shared-buffer");
-new WebrtcProvider("jaskdfasdfasdf", ydoc, { });
-
+const store = require("../modules/store.js");
module.exports = () => {
return {
view: () => m(".page",
m(".main.absolute-center", m("textarea.maxsize", {
oncreate: (v) => {
- new TextAreaBinding(textelem, v.dom);
+ let binding = null;
+ store.subscribe(s => s.ydoc, ydoc => {
+ if (binding) binding.destroy();
+ binding = new TextAreaBinding(
+ ydoc.getText("shared-buffer"), v.dom);
+ }, { fireImmediately: true });
v.dom.focus();
},
})),
diff --git a/src/main.js b/src/main.js
@@ -1,7 +1,11 @@
const m = require("mithril");
+const y = require("yjs");
+const { WebrtcProvider } = require("y-webrtc");
+
const store = require("./modules/store.js");
const editView = require("./components/edit-view.js");
const qrView = require("./components/qr-view.js");
+const navigation = require("./modules/navigation.js");
store.subscribe(s => s, () => m.redraw());
@@ -25,7 +29,28 @@ const main = () => {
document.body.onclick = store().qrClose;
-store.subscribe(s => s.id, id => m.route.set(id));
+if (!window.crypto.subtle) {
+ console.log("no crypto :(");
+}
+
+store.subscribe(s => s.id, (id) => {
+ store().ydocSet(new y.Doc());
+ let sp = navigation.splitId(id);
+ let provider = new WebrtcProvider(sp[0], store().ydoc, {password: sp[1]});
+ provider.awareness.on("change", () => {
+ store.setState({users: provider.awareness.getStates().size });
+ });
+ /* useless edit. only important to propagate the info that a user joined */
+ provider.awareness.setLocalStateField("user", { "update": 1 });
+});
+
+store.subscribe(s => s.users, (unew, uold) => {
+ if (unew == 2 && uold == 1) {
+ store().qrClose();
+ }
+});
+
+store.subscribe(s => s.id, id => m.route.set("/" + id));
m.route(document.body, "/-", {
"/:id": { onmatch: (args) => {
store().idSet(args.id);
diff --git a/src/modules/store.js b/src/modules/store.js
@@ -20,8 +20,10 @@ module.exports = bindGetFn(createStore(subscribeWithSelector(
id: "",
idSet: (n) => {
if (!navigation.isIdValid(n)) n = navigation.generateId();
- return set(() => ({id: n}))
+ set(() => ({id: n}))
},
- ydoc: undefined,
+ ydoc: null,
+ ydocSet: (n) => set(() => ({ ydoc: n })),
+ users: 0,
})
)));