Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow doc creation with initial content #330

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions js-pkg/sdk/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ export class DocumentManager {
const clientToken = await this.getClientToken(docId, authDocRequest)
return new DocConnection(clientToken)
}

/**
* Creates a new document with initial content.
*
* @param update A Yjs update byte string representing the initial content.
* @returns A {@link DocCreationResult} object containing the ID of the created document.
*/
public async createDocWithContent(update: Uint8Array): Promise<DocCreationResult> {
const result = await this.createDoc()
await this.updateDoc(result.docId, update)
return result
}
paulgb marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down Expand Up @@ -187,3 +199,19 @@ export async function createDoc(
const manager = new DocumentManager(connectionString)
return await manager.createDoc(docId)
}

/**
* A convenience wrapper around {@link DocumentManager.createDocWithContent} for creating a new document
* with initial content.
*
* @param connectionString A connection string (starting with `ys://` or `yss://`) referring to a y-sweet server.
* @param update A Yjs update byte string representing the initial content.
* @returns A {@link DocCreationResult} object containing the ID of the created document.
*/
export async function createDocWithContent(
connectionString: string,
update: Uint8Array,
): Promise<DocCreationResult> {
const manager = new DocumentManager(connectionString)
return await manager.createDocWithContent(update)
}
Comment on lines +202 to +217
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm actually tempted to deprecate these functions that sit outside of the DocumentManager, so I'm ok with just making this a function on the document manager.