Skip to content

Commit

Permalink
Chore/update-readme-and-examples (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
naporin0624 committed Apr 9, 2024
1 parent c7f6954 commit 4176b18
Show file tree
Hide file tree
Showing 18 changed files with 2,379 additions and 2,070 deletions.
5 changes: 5 additions & 0 deletions .changeset/heavy-clouds-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"y-durableobjects": patch
---

enhanced readme
44 changes: 43 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

The `y-durableobjects` library is designed to facilitate real-time collaboration in Cloudflare Workers environment using Yjs and Durable Objects. It provides a straightforward way to integrate Yjs for decentralized, scalable real-time editing features.

## Requirements

- Hono version 4.2 or higher is required.

## Installation

To use `y-durableobjects`, you need to install the package along with `hono`, as it is a peer dependency.
Expand Down Expand Up @@ -109,7 +113,7 @@ import { Hono } from "hono";
import { cors } from "hono/cors";
import { YDurableObjects } from "y-durableobjects";

const app = new Hono<Env>();
const app = new Hono();
app.use("*", cors());
app.get("/editor/:id", async (c) => {
const id = c.env.Y_DURABLE_OBJECTS.idFromName(c.req.param("id"));
Expand All @@ -128,3 +132,41 @@ app.get("/editor/:id", async (c) => {
export default app;
export { YDurableObjects };
```

### Hono RPC support

- Utilizes Hono's WebSocket Helper, making the `$ws` method available in `hono/client` for WebSocket communications.
- For more information on server and client setup, see the [Hono WebSocket Helper documentation](https://hono.dev/helpers/websocket#server-and-client).

#### Server Implementation

```typescript
import { Hono } from "hono";
import { cors } from "hono/cors";
import { YDurableObjects, yRoute } from "y-durableobjects";

const app = new Hono();
app.use("*", cors());

const route = app.route(
"/editor",
yRoute((env) => env.Y_DURABLE_OBJECTS),
);

export default route;
export type AppType = typeof route;
export { YDurableObjects };
```

#### Client Implementation

```typescript
import { hc } from "hono/client";
import type { AppType } from "./server"; // Adjust the import path as needed

const API_URL = "http://localhost:8787";

export const client = hc<AppType>(API_URL);
const ws = client.editor[":id"].$ws({ param: { id: "example" } });
// ^?const ws: WebSocket
```
9 changes: 6 additions & 3 deletions example/apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@
"build": "tsc && vite build",
"preview": "vite preview",
"typecheck": "tsc --noEmit",
"lint": "biome lint src",
"fmt": "biome format src --write"
"lint": "biome check src",
"fmt": "biome check --apply src"
},
"dependencies": {
"@acab/reset.css": "^0.8.0",
"@lexical/code": "^0.13.1",
"@lexical/html": "^0.13.1",
"@lexical/link": "^0.13.1",
"@lexical/list": "^0.13.1",
"@lexical/markdown": "^0.13.1",
"@lexical/react": "^0.13.1",
"@lexical/rich-text": "^0.13.1",
"@lexical/table": "^0.13.1",
"hono": "^4.2.3",
"lexical": "^0.13.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand All @@ -33,6 +35,7 @@
"@types/react-dom": "^18.2.17",
"@vitejs/plugin-react-swc": "^3.5.0",
"typescript": "^5.2.2",
"vite": "^5.0.8"
"vite": "^5.0.8",
"workers": "workspace:*"
}
}
8 changes: 8 additions & 0 deletions example/apps/web/src/adapters/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { hc } from "hono/client";
import type { AppType } from "workers";

const API_URL = import.meta.env.PROD ? import.meta.env.VITE_API_URL : "http://localhost:8787";

export const client = hc<AppType>(API_URL);

// const ws = client.editor[":id"].$ws({ param: { id: "1" } })
8 changes: 4 additions & 4 deletions example/apps/web/src/editor/config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ListItemNode, ListNode } from "@lexical/list";
import { CodeHighlightNode, CodeNode } from "@lexical/code";
import { AutoLinkNode, LinkNode } from "@lexical/link";
import { TableCellNode, TableNode, TableRowNode } from "@lexical/table";
import { InitialConfigType } from "@lexical/react/LexicalComposer";
import { ListItemNode, ListNode } from "@lexical/list";
import type { InitialConfigType } from "@lexical/react/LexicalComposer";
import { HeadingNode, QuoteNode } from "@lexical/rich-text";
import { CodeNode, CodeHighlightNode } from "@lexical/code";
import { TableCellNode, TableNode, TableRowNode } from "@lexical/table";

export const initialConfig: InitialConfigType = {
editorState: null,
Expand Down
24 changes: 12 additions & 12 deletions example/apps/web/src/editor/index.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { $generateHtmlFromNodes } from "@lexical/html";
import { TRANSFORMERS } from "@lexical/markdown";
import { CollaborationPlugin } from "@lexical/react/LexicalCollaborationPlugin";
import { LexicalComposer } from "@lexical/react/LexicalComposer";
import { ContentEditable } from "@lexical/react/LexicalContentEditable";
import { RichTextPlugin } from "@lexical/react/LexicalRichTextPlugin";
import { CollaborationPlugin } from "@lexical/react/LexicalCollaborationPlugin";
import { HistoryPlugin } from "@lexical/react/LexicalHistoryPlugin";
import { ListPlugin } from "@lexical/react/LexicalListPlugin";
import { LinkPlugin } from "@lexical/react/LexicalLinkPlugin";
import { TablePlugin } from "@lexical/react/LexicalTablePlugin";
import { TabIndentationPlugin } from "@lexical/react/LexicalTabIndentationPlugin";
import { EditorRefPlugin } from "@lexical/react/LexicalEditorRefPlugin";
import LexicalErrorBoundary from "@lexical/react/LexicalErrorBoundary";
import { TRANSFORMERS } from "@lexical/markdown";
import { HistoryPlugin } from "@lexical/react/LexicalHistoryPlugin";
import { LinkPlugin } from "@lexical/react/LexicalLinkPlugin";
import { ListPlugin } from "@lexical/react/LexicalListPlugin";
import { MarkdownShortcutPlugin } from "@lexical/react/LexicalMarkdownShortcutPlugin";
import { providerFactory } from "./provider";
import { RichTextPlugin } from "@lexical/react/LexicalRichTextPlugin";
import { TabIndentationPlugin } from "@lexical/react/LexicalTabIndentationPlugin";
import { TablePlugin } from "@lexical/react/LexicalTablePlugin";
import type { LexicalEditor } from "lexical";
import { type FC, useCallback, useRef } from "react";
import { initialConfig } from "./config";
import { FC, useCallback, useRef } from "react";
import { LexicalEditor } from "lexical";
import { $generateHtmlFromNodes } from "@lexical/html";
import { providerFactory } from "./provider";

type Props = {
id: string;
Expand Down
12 changes: 6 additions & 6 deletions example/apps/web/src/editor/provider.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { CollaborationPlugin } from "@lexical/react/LexicalCollaborationPlugin";
import { ComponentProps } from "react";
import { WebsocketProvider } from "y-websocket";
import type { CollaborationPlugin } from "@lexical/react/LexicalCollaborationPlugin";
import type { Provider } from "@lexical/yjs";
import type { ComponentProps } from "react";
import { WebsocketProvider } from "y-websocket";
import { Doc } from "yjs";

const WS_URL = import.meta.env.PROD ? import.meta.env.VITE_WSS_URL : "ws://localhost:8787";
import { client } from "../adapters/client";

type Props = ComponentProps<typeof CollaborationPlugin>;
type ProviderFactory = Props["providerFactory"];
Expand All @@ -13,7 +12,8 @@ export const providerFactory: ProviderFactory = (id, map) => {
const doc = new Doc();
map.set(id, doc);

const provider = new WebsocketProvider(new URL("/editor", WS_URL).href, id, doc);
const url = client.editor[":id"].$url();
const provider = new WebsocketProvider(url.toString().replace("http", "ws").replace("/:id", ""), id, doc);

// 公式通りやると型エラーになる調査する
return provider as unknown as Provider;
Expand Down
2 changes: 1 addition & 1 deletion example/apps/web/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "@acab/reset.css";
import React from "react";
import ReactDOM from "react-dom/client";
import Editor from "./editor";
import "@acab/reset.css";
import "./styles.css";

// biome-ignore lint/style/noNonNullAssertion: <explanation>
Expand Down
1 change: 0 additions & 1 deletion example/apps/web/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src"],
"references": [{ "path": "./tsconfig.node.json" }]
Expand Down
3 changes: 2 additions & 1 deletion example/apps/workers/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ dist
package-lock.json
yarn.lock
pnpm-lock.yaml
public
public
tsconfig.tsbuildinfo
9 changes: 7 additions & 2 deletions example/apps/workers/package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
{
"name": "workers",
"type": "module",
"main": "src/index.ts",
"types": "dist/index.d.ts",
"scripts": {
"dev": "wrangler dev",
"deploy": "wrangler deploy --minify src/index.ts",
"typecheck": "tsc --noEmit",
"lint": "biome lint src",
"fmt": "biome format src --write"
"fmt": "biome format src --write",
"build": "tsc --emitDeclarationOnly"
},
"devDependencies": {
"@biomejs/biome": "^1.4.1",
"@cloudflare/workers-types": "^4.20231218.0",
"typescript": "^5.2.2",
"wrangler": "^3.26.0"
},
"dependencies": {
"y-durableobjects": "file:../../.."
"hono": "^4.2.3",
"y-durableobjects": "*"
}
}
1 change: 1 addition & 0 deletions example/apps/workers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ const route = app.route(
);

export default route;
export type AppType = typeof route;
export { YDurableObjects };
6 changes: 5 additions & 1 deletion example/apps/workers/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
"lib": ["esnext"],
"types": ["@cloudflare/workers-types"],
"jsx": "react-jsx",
"jsxImportSource": "hono/jsx"
"jsxImportSource": "hono/jsx",
"outDir": "dist",
"rootDir": "src",
"skipLibCheck": true,
"declaration": true
},
"include": ["src"]
}
Loading

0 comments on commit 4176b18

Please sign in to comment.