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 };
if (act.type === "join") a.site.room(act.id).join(me);
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") {
data.site_js = act.name;
} else {

View File

@ -47,6 +47,9 @@ export const RoomList = class<T extends Record<string, string>> {
}
room(id: string) {
if (!id) {
throw new Error("room id is empty");
}
let room = this.rooms.get(id);
if (!room) {
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
) => {
const clients: any = [];
this.clients.forEach((data, 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);
if (client)
clients.push({

View File

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

View File

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

View File

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

View File

@ -1,10 +1,20 @@
import { useGlobal } from "web-utils";
import { EDGlobal } from "../../../logic/ed-global";
import { EdMonaco } from "../monaco/monaco";
import { useEffect } from "react";
export const EdCode = () => {
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;
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;