|
| 1 | +# 入门 |
| 2 | + |
| 3 | +**Getting Started** |
| 4 | + |
| 5 | +=== "中文" |
| 6 | + |
| 7 | + 使用您喜欢的包管理器安装 Yjs 和提供者: |
| 8 | + |
| 9 | + ```sh |
| 10 | + npm i yjs y-websocket |
| 11 | + ``` |
| 12 | + |
| 13 | + 启动 y-websocket 服务器: |
| 14 | + |
| 15 | + ```sh |
| 16 | + PORT=1234 node ./node_modules/y-websocket/bin/server.cjs |
| 17 | + ``` |
| 18 | + |
| 19 | +=== "英文" |
| 20 | + |
| 21 | + Install Yjs and a provider with your favorite package manager: |
| 22 | + |
| 23 | + ```sh |
| 24 | + npm i yjs y-websocket |
| 25 | + ``` |
| 26 | + |
| 27 | + Start the y-websocket server: |
| 28 | + |
| 29 | + ```sh |
| 30 | + PORT=1234 node ./node_modules/y-websocket/bin/server.cjs |
| 31 | + ``` |
| 32 | + |
| 33 | +## 示例:观察类型 |
| 34 | + |
| 35 | +**Example: Observe types** |
| 36 | + |
| 37 | + |
| 38 | +=== "中文" |
| 39 | + |
| 40 | + ```js |
| 41 | + import * as Y from 'yjs'; |
| 42 | + |
| 43 | + const doc = new Y.Doc(); |
| 44 | + const yarray = doc.getArray('my-array') |
| 45 | + yarray.observe(event => { |
| 46 | + console.log('yarray 被修改了') |
| 47 | + }) |
| 48 | + // 每当本地或远程客户端修改 yarray 时,观察者都会被调用 |
| 49 | + yarray.insert(0, ['val']) // => "yarray 被修改了" |
| 50 | + ``` |
| 51 | + |
| 52 | +=== "英文" |
| 53 | + |
| 54 | + ```js |
| 55 | + import * as Y from 'yjs'; |
| 56 | + |
| 57 | + const doc = new Y.Doc(); |
| 58 | + const yarray = doc.getArray('my-array') |
| 59 | + yarray.observe(event => { |
| 60 | + console.log('yarray was modified') |
| 61 | + }) |
| 62 | + // every time a local or remote client modifies yarray, the observer is called |
| 63 | + yarray.insert(0, ['val']) // => "yarray was modified" |
| 64 | + ``` |
| 65 | + |
| 66 | +## 示例:嵌套类型 |
| 67 | + |
| 68 | +**Example: Nest types** |
| 69 | + |
| 70 | +=== "中文" |
| 71 | + |
| 72 | + 请记住,共享类型只是普通的数据类型。唯一的限制是共享类型在共享文档中必须只存在一次。 |
| 73 | + |
| 74 | + ```js |
| 75 | + const ymap = doc.getMap('map') |
| 76 | + const foodArray = new Y.Array() |
| 77 | + foodArray.insert(0, ['apple', 'banana']) |
| 78 | + ymap.set('food', foodArray) |
| 79 | + ymap.get('food') === foodArray // => true |
| 80 | + ymap.set('fruit', foodArray) // => 错误!foodArray 已经被定义 |
| 81 | + ``` |
| 82 | + |
| 83 | + 现在你了解了如何在共享文档上定义类型。接下来你可以跳转到 [演示仓库](https://github.com/yjs/yjs-demos) 或继续阅读 API 文档。 |
| 84 | + |
| 85 | +=== "英文" |
| 86 | + |
| 87 | + Remember, shared types are just plain old data types. The only limitation is |
| 88 | + that a shared type must exist only once in the shared document. |
| 89 | + |
| 90 | + ```js |
| 91 | + const ymap = doc.getMap('map') |
| 92 | + const foodArray = new Y.Array() |
| 93 | + foodArray.insert(0, ['apple', 'banana']) |
| 94 | + ymap.set('food', foodArray) |
| 95 | + ymap.get('food') === foodArray // => true |
| 96 | + ymap.set('fruit', foodArray) // => Error! foodArray is already defined |
| 97 | + ``` |
| 98 | + |
| 99 | + Now you understand how types are defined on a shared document. Next you can jump |
| 100 | + to the [demo repository](https://github.com/yjs/yjs-demos) or continue reading |
| 101 | + the API docs. |
| 102 | + |
| 103 | +## 示例:使用和组合提供者 |
| 104 | + |
| 105 | +**Example: Using and combining providers** |
| 106 | + |
| 107 | +=== "中文" |
| 108 | + |
| 109 | + 任何 Yjs 提供者都可以相互组合。因此你可以通过不同的网络技术同步数据。 |
| 110 | + |
| 111 | + 在大多数情况下,你希望将网络提供者(如 y-websocket 或 y-webrtc)与持久化提供者(浏览器中的 y-indexeddb)结合使用。持久化允许你更快地加载文档,并在离线时持久化创建的数据。 |
| 112 | + |
| 113 | + 为了演示,我们将两个不同的网络提供者与一个持久化提供者结合使用。 |
| 114 | + |
| 115 | + ```js |
| 116 | + import * as Y from 'yjs' |
| 117 | + import { WebrtcProvider } from 'y-webrtc' |
| 118 | + import { WebsocketProvider } from 'y-websocket' |
| 119 | + import { IndexeddbPersistence } from 'y-indexeddb' |
| 120 | + |
| 121 | + const ydoc = new Y.Doc() |
| 122 | + |
| 123 | + // 这允许你立即获取(缓存的)文档数据 |
| 124 | + const indexeddbProvider = new IndexeddbPersistence('count-demo', ydoc) |
| 125 | + indexeddbProvider.whenSynced.then(() => { |
| 126 | + console.log('从索引数据库加载的数据') |
| 127 | + }) |
| 128 | + |
| 129 | + // 使用 y-webrtc 提供者同步客户端。 |
| 130 | + const webrtcProvider = new WebrtcProvider('count-demo', ydoc) |
| 131 | + |
| 132 | + // 使用 y-websocket 提供者同步客户端 |
| 133 | + const websocketProvider = new WebsocketProvider( |
| 134 | + 'wss://demos.yjs.dev', 'count-demo', ydoc |
| 135 | + ) |
| 136 | + |
| 137 | + // 生成和计算总和的数字数组 |
| 138 | + const yarray = ydoc.getArray('count') |
| 139 | + |
| 140 | + // 观察总和的变化 |
| 141 | + yarray.observe(event => { |
| 142 | + // 当数据变化时打印更新 |
| 143 | + console.log('新总和: ' + yarray.toArray().reduce((a,b) => a + b)) |
| 144 | + }) |
| 145 | + |
| 146 | + // 将 1 加到总和中 |
| 147 | + yarray.push([1]) // => "新总和: 1" |
| 148 | + ``` |
| 149 | + |
| 150 | +=== "英文" |
| 151 | + |
| 152 | + Any of the Yjs providers can be combined with each other. So you can sync data |
| 153 | + over different network technologies. |
| 154 | + |
| 155 | + In most cases you want to use a network provider (like y-websocket or y-webrtc) |
| 156 | + in combination with a persistence provider (y-indexeddb in the browser). |
| 157 | + Persistence allows you to load the document faster and to persist data that is |
| 158 | + created while offline. |
| 159 | + |
| 160 | + For the sake of this demo we combine two different network providers with a |
| 161 | + persistence provider. |
| 162 | + |
| 163 | + ```js |
| 164 | + import * as Y from 'yjs' |
| 165 | + import { WebrtcProvider } from 'y-webrtc' |
| 166 | + import { WebsocketProvider } from 'y-websocket' |
| 167 | + import { IndexeddbPersistence } from 'y-indexeddb' |
| 168 | + |
| 169 | + const ydoc = new Y.Doc() |
| 170 | + |
| 171 | + // this allows you to instantly get the (cached) documents data |
| 172 | + const indexeddbProvider = new IndexeddbPersistence('count-demo', ydoc) |
| 173 | + indexeddbProvider.whenSynced.then(() => { |
| 174 | + console.log('loaded data from indexed db') |
| 175 | + }) |
| 176 | + |
| 177 | + // Sync clients with the y-webrtc provider. |
| 178 | + const webrtcProvider = new WebrtcProvider('count-demo', ydoc) |
| 179 | + |
| 180 | + // Sync clients with the y-websocket provider |
| 181 | + const websocketProvider = new WebsocketProvider( |
| 182 | + 'wss://demos.yjs.dev', 'count-demo', ydoc |
| 183 | + ) |
| 184 | + |
| 185 | + // array of numbers which produce a sum |
| 186 | + const yarray = ydoc.getArray('count') |
| 187 | + |
| 188 | + // observe changes of the sum |
| 189 | + yarray.observe(event => { |
| 190 | + // print updates when the data changes |
| 191 | + console.log('new sum: ' + yarray.toArray().reduce((a,b) => a + b)) |
| 192 | + }) |
| 193 | + |
| 194 | + // add 1 to the sum |
| 195 | + yarray.push([1]) // => "new sum: 1" |
| 196 | + ``` |
0 commit comments