-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example codes to main and products page (#32)
- Loading branch information
Showing
7 changed files
with
363 additions
and
155 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const COMMON_CODE = ` | ||
// You can use a key-value pair to share data with peers. | ||
// The key is a string, and the value should be serializable - objects, arrays, and primitives. | ||
doc.update((root) => { | ||
root.num = 1; // {"num":1} | ||
root.obj = {'str':'a'}; // {"num":1,"obj":{"str":"a"}} | ||
root.arr = ['1', '2']; // {"num":1,"obj":{"str":"a"},"arr":[1,2]} | ||
}); | ||
`; | ||
|
||
const TEXT_CODE = ` | ||
// Text provides supports for collaborative plain text editing. | ||
// It also has selection information for sharing the cursor position. | ||
doc.update((root) => { | ||
root.text = new yorkie.Text(); // {"text":""} | ||
root.text.edit(0, 0, 'hello'); // {"text":"hello"} | ||
root.text.edit(0, 1, 'H'); // {"text":"Hello"} | ||
root.text.select(0, 1); // {"text":"^H^ello"} | ||
}); | ||
`; | ||
|
||
const RICHTEXT_CODE = ` | ||
// RichText is similar to Text except that we can add attributes to contents. | ||
doc.update((root) => { | ||
root.text = new yorkie.RichText(); // {"text":""} | ||
root.text.edit(0, 0, 'hello'); // {"text":"hello"} | ||
root.text.edit(0, 1, 'H'); // {"text":"Hello"} | ||
root.text.setStyle(0, 1, {bold: true}); // {"text":"<b>H</b>ello"} | ||
}); | ||
`; | ||
|
||
const COUNTER_CODE = ` | ||
// Counter supports numeric types that change with addition and subtraction. | ||
doc.update((root) => { | ||
root.counter = new yorkie.Counter(1); // {"counter":1} | ||
root.counter.increase(2); // {"counter":3} | ||
root.counter.increase(3.5); // {"counter":6.5} | ||
root.counter.increase(-3.5); // {"counter":3} | ||
}); | ||
`; | ||
|
||
export const DOCUMENT_CODE = { | ||
common: COMMON_CODE, | ||
text: TEXT_CODE, | ||
richText: RICHTEXT_CODE, | ||
counter: COUNTER_CODE, | ||
}; |
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,102 @@ | ||
const PROFILE_JS = ` | ||
import yorkie from 'yorkie-js-sdk'; | ||
async function main() { | ||
const client = new yorkie.Client('${process.env.NEXT_PUBLIC_API_ADDR}', { | ||
apiKey: 'MY_API_KEY', | ||
// set the client's name and color to presence. | ||
presence: { | ||
name: getRandomName(), | ||
color: getRandomColor(), | ||
}, | ||
}); | ||
await client.activate(); | ||
const doc = new yorkie.Document('example-profile'); | ||
await client.attach(doc); | ||
client.subscribe((event) => { | ||
if (event.type === 'peers-changed') { | ||
// get presence of all clients connected to the Document. | ||
// {<clientID>: {name: string, color: string}} | ||
const peers = event.value[doc.getKey()]; | ||
// show peer list | ||
updatePeerList(peers); | ||
} | ||
}); | ||
} | ||
main(); | ||
`; | ||
|
||
const CURSOR_JS = ` | ||
import yorkie from 'yorkie-js-sdk'; | ||
async function main() { | ||
const client = new yorkie.Client('${process.env.NEXT_PUBLIC_API_ADDR}', { | ||
apiKey: 'MY_API_KEY', | ||
}); | ||
await client.activate(); | ||
const doc = new yorkie.Document('my-first-document'); | ||
await client.attach(doc); | ||
client.subscribe((event) => { | ||
if (event.type === 'peers-changed') { | ||
// get presence of all clients connected to the Document. | ||
// {<clientID>: {cursor: {x: number, y: number}}} | ||
const peers = event.value[doc.getKey()]; | ||
// show peer cursors | ||
updatePeerCursors(peers); | ||
} | ||
}); | ||
document.body.addEventListener('mousemove', (e) => { | ||
// set the cursor position to presence. | ||
// Presence will be shared with other clients. | ||
client.updatePresence('cursor', { | ||
x: e.clientX, | ||
y: e.clientY, | ||
}) | ||
}); | ||
} | ||
main(); | ||
`; | ||
|
||
const SELECTION_JS = ` | ||
// js | ||
`; | ||
|
||
const EDITING_JS = ` | ||
// js | ||
`; | ||
|
||
const EDITING_ANDROID = ` | ||
// android | ||
`; | ||
|
||
const EDITING_IOS = ` | ||
// ios | ||
`; | ||
|
||
export const FEATURES_CODE = { | ||
profile: { | ||
tabOrder: ['js'], | ||
js: { title: 'JS SDK', code: PROFILE_JS, language: 'javascript' }, | ||
}, | ||
cursor: { | ||
tabOrder: ['js'], | ||
js: { title: 'JS SDK', code: CURSOR_JS, language: 'javascript' }, | ||
}, | ||
selection: { | ||
tabOrder: ['js'], | ||
js: { title: 'JS SDK', code: SELECTION_JS, language: 'javascript' }, | ||
}, | ||
editing: { | ||
tabOrder: ['js', 'android', 'ios'], | ||
js: { title: 'JS SDK', code: EDITING_JS, language: 'javascript' }, | ||
android: { title: 'Android SDK', code: EDITING_ANDROID, language: 'kotlin' }, | ||
ios: { title: 'iOS SDK', code: EDITING_IOS, language: 'swift' }, | ||
}, | ||
}; |
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
Oops, something went wrong.