This commit is contained in:
Rizky 2023-11-07 02:29:52 +07:00
parent 348266be66
commit 29465f8729
7 changed files with 43 additions and 17 deletions

View File

@ -9,7 +9,7 @@ export const activity: SAction["activity"] = async function (
const me = { ws: this.ws }; const me = { ws: this.ws };
if (act.type === "join") a.site.room(act.id).join(me); if (act.type === "join") a.site.room(act.id).join(me);
if (act.type === "code") { if (act.type === "code") {
a.site.room(act.id).set(me, (ws, data) => { a.site.set(act.id, this.ws, (data) => {
if (act.action === "open") { if (act.action === "open") {
data.site_js = act.name; data.site_js = act.name;
} else { } else {

View File

@ -47,6 +47,9 @@ export const RoomList = class<T extends Record<string, string>> {
} }
room(id: string) { room(id: string) {
if (!id) {
throw new Error("room id is empty");
}
let room = this.rooms.get(id); let room = this.rooms.get(id);
if (!room) { if (!room) {
this.rooms.set(id, new Room<T>(this.name, id)); this.rooms.set(id, new Room<T>(this.name, id));
@ -128,11 +131,11 @@ export class Room<T extends Record<string, any>> {
triggeredBy?: ServerWebSocket<WSData> | null triggeredBy?: ServerWebSocket<WSData> | null
) => { ) => {
const clients: any = []; const clients: any = [];
this.clients.forEach((data, ws) => { this.clients.forEach((data, ws) => {
const client_id = wconns.get(ws); const client_id = wconns.get(ws);
if (client_id) {
console.log(event_name, client_id, data);
if (client_id) {
const client = conns.get(client_id); const client = conns.get(client_id);
if (client) if (client)
clients.push({ clients.push({

View File

@ -2,7 +2,6 @@ import { NodeModel } from "@minoru/react-dnd-treeview";
import { ReactElement } from "react"; import { ReactElement } from "react";
import { deepClone } from "web-utils"; import { deepClone } from "web-utils";
import { SAction } from "../../../../../srv/ws/sync/actions"; import { SAction } from "../../../../../srv/ws/sync/actions";
import { ActivityList } from "../../../../../srv/ws/sync/type";
import { clientStartSync } from "../../../utils/sync/ws-client"; import { clientStartSync } from "../../../utils/sync/ws-client";
import { IItem, MItem } from "../../../utils/types/item"; import { IItem, MItem } from "../../../utils/types/item";
import { DComp, DPage, IRoot } from "../../../utils/types/root"; import { DComp, DPage, IRoot } from "../../../utils/types/root";
@ -73,10 +72,6 @@ export const EDGlobal = {
| "ready", | "ready",
sync: null as unknown as Awaited<ReturnType<typeof clientStartSync>>, sync: null as unknown as Awaited<ReturnType<typeof clientStartSync>>,
site: deepClone(EmptySite), site: deepClone(EmptySite),
activity: {
page: {} as Record<string, ActivityList>,
comp: {} as Record<string, ActivityList>,
},
script: { siteTypes: {} as Record<string, string> }, script: { siteTypes: {} as Record<string, string> },
page: { page: {
cur: EmptyPage, cur: EmptyPage,
@ -116,9 +111,10 @@ export const EDGlobal = {
}, },
popup: { popup: {
code: { code: {
init: false,
open: false, open: false,
id: "", id: "",
file: "", name: "",
}, },
site: null as null | ((site_id: string) => void | Promise<void>), site: null as null | ((site_id: string) => void | Promise<void>),
site_form: null as null | { site_form: null as null | {

View File

@ -25,6 +25,7 @@ export const edInitSync = (p: PG) => {
if (params.site_id !== p.site.id) { if (params.site_id !== p.site.id) {
p.site = deepClone(EmptySite); p.site = deepClone(EmptySite);
p.site.id = "--loading--"; p.site.id = "--loading--";
p.ui.popup.code.init = false;
p.sync.site.load(params.site_id).then((site) => { p.sync.site.load(params.site_id).then((site) => {
if (site) { if (site) {
p.site = site; p.site = site;
@ -62,14 +63,13 @@ export const edInitSync = (p: PG) => {
page_id: params.page_id, page_id: params.page_id,
events: { events: {
activity(arg) { activity(arg) {
console.log(arg);
}, },
opened() { opened() {
if (w.offline) { if (w.offline) {
console.log("reconnected!"); console.log("reconnected!");
w.offline = false; w.offline = false;
p.ui.syncing = true; p.ui.syncing = true;
p.sync.activity("site", { type: "join", id: params.id }); p.sync.activity("site", { type: "join", id: params.site_id });
p.render(); p.render();
} else { } else {
w.offline = false; w.offline = false;

View File

@ -7,12 +7,15 @@ export const EdPopCode = () => {
const p = useGlobal(EDGlobal, "EDITOR"); const p = useGlobal(EDGlobal, "EDITOR");
useEffect(() => { useEffect(() => {
if (p.ui.popup.code.init) {
p.sync.activity("site", { p.sync.activity("site", {
action: p.ui.popup.code.open ? "open" : "close", action: p.ui.popup.code.open ? "open" : "close",
id: p.site.id, id: p.site.id,
type: "code", type: "code",
name: "main", name: "main",
}); });
}
p.ui.popup.code.init = true;
}, [p.ui.popup.code.open]); }, [p.ui.popup.code.open]);
return ( return (

View File

@ -1,10 +1,20 @@
import { useGlobal } from "web-utils"; import { useGlobal } from "web-utils";
import { EDGlobal } from "../../../logic/ed-global"; import { EDGlobal } from "../../../logic/ed-global";
import { EdMonaco } from "../monaco/monaco"; import { EdMonaco } from "../monaco/monaco";
import { useEffect } from "react";
export const EdCode = () => { export const EdCode = () => {
const p = useGlobal(EDGlobal, "EDITOR"); const p = useGlobal(EDGlobal, "EDITOR");
useEffect(() => {
p.sync.activity("site", {
type: "code",
id: p.site.id,
action: p.ui.popup.code.open ? "open" : "close",
name: p.ui.popup.code.name,
});
}, [p.ui.popup.code.open]);
if (!p.ui.popup.code.open) return null; if (!p.ui.popup.code.open) return null;
return ( return (

14
app/web/y.d.ts vendored Normal file
View File

@ -0,0 +1,14 @@
import type * as Y from "yjs";
import { syncronize } from "y-pojo";
export import Doc = Y.Doc;
export import UndoManager = Y.UndoManager;
export import applyUpdate = Y.applyUpdate;
export import encodeStateVector = Y.encodeStateVector;
export import encodeStateAsUpdate = Y.encodeStateAsUpdate;
export import Text = Y.Text;
export import Map = Y.Map;
export import Array = Y.Array;
export import syncronize = syncronize;
export as namespace Y;