Skip to content

Allow arbitrary awareness fields in Collaboration plugin - #4507

Merged
zurfyx merged 9 commits into
meta:mainfrom
attnov:arbitrary_data_to_awareness
May 19, 2023
Merged

zurfyx merged 9 commits into
meta:mainfrom
attnov:arbitrary_data_to_awareness

Conversation

@attnov

@attnov attnov commented May 17, 2023 •

Copy link
Copy Markdown
Contributor

Summary

This change allows arbitrary data (for example 3rd party UUID) to be added to awareness. Right now user name and color is hard coded in Collaboration plugin. The new parameter awarenessData is optional.

Usage example

We can specify our arbitrary data like this:

const awarenessData = {
  uuid: 42,
  foo: 'bar',
);

With the new parameter, we can send down our arbitrary data:

<CollaborationPlugin
  id="id"
  providerFactory={providerFactory}
  shouldBootstrap={shouldBootstrap}
  awarenessData={awarenessData}
/>

@vercel

vercel Bot commented May 17, 2023 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
lexical ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 19, 2023 3:02pm
lexical-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 19, 2023 3:02pm

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label May 17, 2023
@attnov attnov changed the title Allow arbitrary awareness data in Collaboration plugin Allow arbitrary awareness fields in Collaboration plugin May 17, 2023

@ivailop7 ivailop7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you, Attila. @acywatson and @zurfyx . Could you give it a look as well.

@fantactuka

fantactuka commented May 18, 2023 •

Copy link
Copy Markdown
Contributor

I don't think this works since map is not json serializeable, should be plain object like remaining of awareness data. Personal opinion: naming looks a bit odd, as usage will be awareness.getLocalState().awarenessFields, maybe data or extraData would do better, no strong opinion tho.

Also can add a unit test (see Collaboration.test), as collab is quite sensitive for changes, so it's better to get it covered

@fantactuka fantactuka left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

~

@attnov

attnov commented May 18, 2023

Copy link
Copy Markdown
Contributor Author

I don't think this works since map is not json serializeable, should be plain object like remaining of awareness data. Personal opinion: naming looks a bit odd, as usage will be awareness.getLocalState().awarenessFields, maybe data or extraData would do better, no strong opinion tho.

Also can add a unit test (see Collaboration.test), as collab is quite sensitive for changes, so it's better to get it covered

@fantactuka Thanks for the review! I have changed the type from Map to object and added unit test.

Comment on lines +305 to +307
expect(client1.awareness.getLocalState().awarenessFields).toEqual(
awarenessFields1,
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you use awareness.getStates() instead, to ensure that client1 has proper awareness from client2 and vise versa?

@attnov attnov May 19, 2023 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It looks like here the mock for getStates() only inserts the current client's awareness state. That's why I opted for the getLocalState(). However, I have manually tested and between two collaborating users the awareness is sync correctly.

name: string,
color: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
awarenessFields?: object,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I wonder if we can use { ... } here and remove eslint comment above

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Actually, we don't need the eslint comments anymore - any is not used anymore. Removed all of them.

@fantactuka

Copy link
Copy Markdown
Contributor

Thanks for updates!

@acywatson @zurfyx thoughts on naming?

@acywatson

Copy link
Copy Markdown
Contributor

Thanks for updates!

@acywatson @zurfyx thoughts on naming?

I like having awareness in the name, but fields is probably wrong. "awarenessData" or "extraAwaresnessData" seems fine.

doc block on the interface would help people know what it's for.

@attnov

attnov commented May 19, 2023

Copy link
Copy Markdown
Contributor Author

Thanks for updates!
@acywatson @zurfyx thoughts on naming?

I like having awareness in the name, but fields is probably wrong. "awarenessData" or "extraAwaresnessData" seems fine.

doc block on the interface would help people know what it's for.

Renamed the parameter to awarenessData and added documentation on the inferface.

awareness: {
getLocalState(): void;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
getLocalState(): any;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we make this better? Besides the object prop what other types we don't know?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have added types for getLocalState(), getStates(), setLocalState(), etc.
Thanks for checking!

Comment thread packages/lexical-yjs/src/index.ts Outdated
): void {
provider.awareness.setLocalState({
anchorPos: null,
awarenessData: awarenessData,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
awarenessData: awarenessData,
awarenessData,

Similar one below

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added the change.

This branch was successfully deployed

2 active deployments
Preview – lexical-playground — 63f9bf21 Deployed May 19, 2023 by vercel[bot]
Preview – lexical — 63f9bf21 Deployed May 19, 2023 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants