tiptap/docs/collaboration/guides/naming-documents.md

74 lines
2.6 KiB
Markdown
Raw Normal View History

2024-03-13 15:28:32 +08:00
# Naming documents with unique identifiers
This guide outlines best practices for naming documents and organizing content within a single document, to help you define your own document structure.
For a comprehensive understanding of how to choose document names, you should review our [authorization guide](https://tiptap.dev/docs/editor/collaboration/authenticate#authorization-in-collaboration), as document naming plays a crucial role in access control as well.
## Structuring document names
2024-03-13 15:39:08 +08:00
Tiptap Collaboration uses document names to facilitate collaborative sessions, they serve as unique identifiers that link users to the same document. In theory it could be any string.
2024-03-13 15:28:32 +08:00
While the following example uses an entity's name combined with a unique ID, typical for CMS applications, you're free to adopt any naming convention that suits your application's requirements.
2024-03-13 15:39:08 +08:00
New documents are automatically generated as needed; you only need to provide a string identifier to the provider.
```typescript
2024-03-13 15:28:32 +08:00
const documentName = "article.123";
```
This naming format allows you to separate out the key details easily:
2024-03-13 15:39:08 +08:00
```typescript
2024-03-13 15:28:32 +08:00
const documentName = "article.123";
// Splitting the document name into separate parts
const [entityType, entityID] = documentName.split(".");
console.log(entityType); // Output: "article"
console.log(entityID); // Output: "123"
```
## Managing nested documents with fragments
2024-03-13 15:39:08 +08:00
Yjs's fragments are ideal for handling complex documents with distinct sections. This might be relevant in case you want to nest your documents, like for example a blog post with separate `title` and `content` parts.
2024-03-13 15:28:32 +08:00
With fragments, you can use one Y.Doc instance (e.g. one document) and use different editors for its distinct sections.
For example, in this blog post setup:
2024-03-13 15:39:08 +08:00
```typescript
2024-03-13 15:28:32 +08:00
const ydoc = new Y.Doc();
2024-03-13 15:39:08 +08:00
// Title editor
2024-03-13 15:28:32 +08:00
const titleEditor = new Editor({
extensions: [
Collaboration.configure({
document: this.ydoc,
field: "title",
}),
],
})
2024-03-13 15:39:08 +08:00
// Content editor
2024-03-13 15:28:32 +08:00
const bodyEditor = new Editor({
extensions: [
Collaboration.configure({
document: this.ydoc,
2024-03-13 15:39:08 +08:00
field: "content",
2024-03-13 15:28:32 +08:00
}),
],
})
```
2024-03-13 15:39:08 +08:00
For complex setups with nested fragments, you can directly use a raw Y.js fragment, bypassing the `document` and `field` settings.
```typescript
// a raw Y.js fragment
Collaboration.configure({
fragment: ydoc.getXmlFragment('custom'),
})
```
2024-03-13 15:28:32 +08:00
To fully grasp how document naming influences access control in Tiptap Collaboration, it's essential to consult our [authorization guide](https://tiptap.dev/docs/editor/collaboration/authenticate#authorization-in-collaboration).