Skip to content

Commit 9330a8b

Browse files
committed
test: 💍 setup text demo
1 parent e610af1 commit 9330a8b

File tree

4 files changed

+97
-1
lines changed

4 files changed

+97
-1
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"demo:e2e:json-crdt-server:http1": "ts-node src/__demos__/json-crdt-server/main-http1.ts",
5757
"demo:e2e:json-crdt-server:uws": "ts-node src/__demos__/json-crdt-server/main-uws.ts",
5858
"demo:ui:json": "webpack serve --config ./src/__demos__/ui-json/webpack.config.js",
59+
"demo:ui:text": "webpack serve --config ./src/__demos__/ui-text/webpack.config.js",
5960
"start:json-crdt-server:http1": "NODE_ENV=production PORT=80 JSON_CRDT_STORE=level pm2 start lib/__demos__/json-crdt-server/main-http1.js",
6061
"start": "NODE_ENV=production PORT=80 JSON_CRDT_STORE=level pm2 start lib/__demos__/json-crdt-server/main-http1.js --exp-backoff-restart-delay=100",
6162
"coverage": "yarn test --collectCoverage",

src/__demos__/ui-json/main.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {Model, Patch} from 'json-joy/lib/json-crdt';
99
const repo = new JsonCrdtRepo({
1010
wsUrl: 'wss://demo-iasd8921ondk0.jsonjoy.com/rpc',
1111
});
12-
const id = 'block-sync-ui-demo-id';
12+
const id = 'block-sync-ui-demo-json';
1313
const session = repo.make(id);
1414

1515
const model = session.model;

src/__demos__/ui-text/main.tsx

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import * as React from 'react';
2+
import * as ReactDOM from 'react-dom/client';
3+
import {JsonCrdtRepo} from '../../json-crdt-repo/JsonCrdtRepo';
4+
import {bind} from 'collaborative-input';
5+
import {Model, Patch} from 'json-joy/lib/json-crdt';
6+
7+
/* tslint:disable no-console */
8+
9+
const main = async () => {
10+
const repo = new JsonCrdtRepo({
11+
wsUrl: 'wss://demo-iasd8921ondk0.jsonjoy.com/rpc',
12+
});
13+
const id = 'block-sync-ui-demo-text';
14+
const session = await repo.sessions.load({id: [id], remote: {}});
15+
const model = session.model;
16+
const view = model.view();
17+
if (typeof view !== 'string') model.api.root('');
18+
19+
20+
const Demo: React.FC = () => {
21+
const [remote, setRemote] = React.useState<Model | null>(null);
22+
const ref = React.useRef<HTMLTextAreaElement | null>(null);
23+
React.useLayoutEffect(() => {
24+
if (!ref.current) return;
25+
const unbind = bind(() => model.api.str([]), ref.current);
26+
return () => unbind();
27+
}, []);
28+
29+
return (
30+
<div style={{padding: 32}}>
31+
<textarea ref={ref} style={{width: 600, height: 300}}></textarea>
32+
<hr />
33+
<button
34+
onClick={async () => {
35+
const {block} = await repo.remote.read(id);
36+
const model = Model.fromBinary(block.snapshot.blob);
37+
for (const batch of block.tip)
38+
for (const patch of batch.patches) model.applyPatch(Patch.fromBinary(patch.blob));
39+
setRemote(model);
40+
}}
41+
>
42+
Load remote state
43+
</button>
44+
<br />
45+
{!!remote && (
46+
<code style={{fontSize: 8}}>
47+
<pre>{remote.toString()}</pre>
48+
</code>
49+
)}
50+
</div>
51+
);
52+
};
53+
54+
const div = document.createElement('div');
55+
document.body.appendChild(div);
56+
const root = ReactDOM.createRoot(div);
57+
root.render(<Demo />);
58+
};
59+
60+
main().catch(() => {});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const path = require('path');
2+
const HtmlWebpackPlugin = require('html-webpack-plugin');
3+
4+
module.exports = {
5+
mode: 'development',
6+
devtool: 'inline-source-map',
7+
entry: {
8+
bundle: __dirname + '/main',
9+
},
10+
plugins: [
11+
new HtmlWebpackPlugin({
12+
title: 'Development',
13+
}),
14+
],
15+
module: {
16+
rules: [
17+
{
18+
test: /\.tsx?$/,
19+
exclude: /node_modules/,
20+
loader: 'ts-loader',
21+
},
22+
],
23+
},
24+
resolve: {
25+
extensions: ['.tsx', '.ts', '.js'],
26+
},
27+
output: {
28+
filename: '[name].js',
29+
path: path.resolve(__dirname, '../../dist'),
30+
},
31+
devServer: {
32+
port: 9949,
33+
hot: false,
34+
},
35+
};

0 commit comments

Comments
 (0)