generated from arvinxx/npm-template
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
jiangchu
committed
May 7, 2024
1 parent
1fcb4c1
commit 034d953
Showing
8 changed files
with
169 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import Color from 'color'; | ||
Check failure on line 1 in src/Awareness/Cursors/Cursor.tsx
|
||
import { memo } from 'react'; | ||
|
||
import { createStyles } from '@/theme'; | ||
// import { createStyles } from '@/theme'; | ||
import CursorSvg from './CursorSvg'; | ||
|
||
const useStyles = createStyles(({ css }) => ({ | ||
Check failure on line 7 in src/Awareness/Cursors/Cursor.tsx
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { ColorPicker, Form, Input, Modal } from 'antd'; | ||
import React from 'react'; | ||
|
||
interface Values { | ||
name: string; | ||
color: string; | ||
} | ||
|
||
interface SessionFormProps { | ||
open: boolean; | ||
onCreate: (values: Values) => void; | ||
onCancel: () => void; | ||
} | ||
|
||
export const SessionForm: React.FC<SessionFormProps> = ({ open, onCreate, onCancel }) => { | ||
const [form] = Form.useForm(); | ||
return ( | ||
<Modal | ||
open={open} | ||
title="Join Session" | ||
okText="Join" | ||
onCancel={onCancel} | ||
onOk={() => { | ||
form | ||
.validateFields() | ||
.then(({ color, name }) => { | ||
form.resetFields(); | ||
const colorHex = typeof color === 'string' ? color : color?.toHexString(); | ||
onCreate({ color: colorHex, name }); | ||
}) | ||
.catch((info) => { | ||
console.log('Validate Failed:', info); | ||
}); | ||
}} | ||
> | ||
<Form form={form} layout="vertical" name="form_in_modal" initialValues={{ color: '#1677FF' }}> | ||
<Form.Item | ||
name="name" | ||
label="Name" | ||
rules={[{ required: true, message: 'Please input your name' }]} | ||
> | ||
<Input /> | ||
</Form.Item> | ||
<Form.Item | ||
name="color" | ||
label="Color" | ||
rules={[{ required: true, message: 'Please select your color' }]} | ||
> | ||
<ColorPicker format="hex" /> | ||
</Form.Item> | ||
</Form> | ||
</Modal> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/** | ||
* title: 实时协同 | ||
* description: 打开多个窗口,点击 "Join" 加入协作 | ||
*/ | ||
|
||
import { Button, Divider, Input } from 'antd'; | ||
import { memo, useContext, useMemo, useState } from 'react'; | ||
import { WebrtcProvider } from 'y-webrtc'; | ||
// import { SessionForm } from './SessionForm'; | ||
import Awareness from '../Awareness'; | ||
import { SessionForm } from './SessionForm'; | ||
import { StoreContext, doc } from './store'; | ||
|
||
const App = memo(() => { | ||
// const store = useStore((state) => ({ | ||
// count: state.count, | ||
// increment: state.increment, | ||
// updateText: state.updateText, | ||
// text: state.text, | ||
// })); | ||
const { count, increment, updateText, text } = useContext(StoreContext)!; | ||
|
||
return ( | ||
<> | ||
<Divider /> | ||
<Input | ||
value={text} | ||
onChange={(e) => { | ||
updateText(e.target.value); | ||
}} | ||
/> | ||
<Divider /> | ||
<p>Count: {count}</p> | ||
<Button onClick={increment}>To the Moon!</Button> | ||
</> | ||
); | ||
}); | ||
|
||
const Container = () => { | ||
const [user, setUser] = useState(null); | ||
const [open, setOpen] = useState(false); | ||
const [count, setCount] = useState(0); | ||
const [text, setText] = useState(''); | ||
const p = useMemo(() => { | ||
console.log(doc); | ||
return new WebrtcProvider('test1-room', doc); | ||
}, []); | ||
|
||
const onCreate = (values) => { | ||
setUser(values); | ||
setOpen(false); | ||
}; | ||
|
||
return ( | ||
<StoreContext.Provider | ||
value={{ | ||
count, | ||
text, | ||
increment: () => setCount(count + 1), | ||
updateText: (text) => setText(text), | ||
}} | ||
> | ||
{user ? null : ( | ||
<Button | ||
type="primary" | ||
onClick={() => { | ||
setOpen(true); | ||
}} | ||
> | ||
Join | ||
</Button> | ||
)} | ||
<SessionForm | ||
open={open} | ||
onCreate={onCreate} | ||
onCancel={() => { | ||
setOpen(false); | ||
}} | ||
/> | ||
{user ? <Awareness provider={p} user={user} /> : null} | ||
<App /> | ||
</StoreContext.Provider> | ||
); | ||
}; | ||
|
||
export default memo(Container); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { createContext } from 'react'; | ||
import * as Y from 'yjs'; | ||
export { default as yjsMiddleware } from 'zustand-middleware-yjs'; | ||
|
||
interface Store { | ||
count: number; | ||
text: string; | ||
increment: () => void; | ||
updateText: (text: string) => void; | ||
} | ||
|
||
export const doc = new Y.Doc(); | ||
|
||
export const StoreContext = createContext<Store | null>(null); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters