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

feat: Added awarenessStateFilter option for filtering awareness.getState() #140

Merged
merged 1 commit into from
Sep 8, 2023
Merged
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
19 changes: 18 additions & 1 deletion src/plugins/cursor-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ import { yCursorPluginKey, ySyncPluginKey } from './keys.js'

import * as math from 'lib0/math'

/**
* Default awareness state filter
*
* @param {number} currentClientId current client id
* @param {number} userClientId user client id
* @param {any} user user data
* @return {boolean}
*/
export const defaultAwarenessStateFilter = (currentClientId, userClientId, user) => currentClientId !== userClientId;

/**
* Default generator for a cursor element
*
Expand Down Expand Up @@ -50,13 +60,15 @@ const rxValidColor = /^#[0-9a-fA-F]{6}$/
/**
* @param {any} state
* @param {Awareness} awareness
* @param {function(number, number, any):boolean} awarenessFilter
* @param {function({ name: string, color: string }):Element} createCursor
* @param {function({ name: string, color: string }):import('prosemirror-view').DecorationAttrs} createSelection
* @return {any} DecorationSet
*/
export const createDecorations = (
state,
awareness,
awarenessFilter,
createCursor,
createSelection
) => {
Expand All @@ -71,9 +83,10 @@ export const createDecorations = (
return DecorationSet.create(state.doc, [])
}
awareness.getStates().forEach((aw, clientId) => {
if (clientId === y.clientID) {
if (!awarenessFilter(y.clientID, clientId, aw)) {
return
}

if (aw.cursor != null) {
const user = aw.user || {}
if (user.color == null) {
Expand Down Expand Up @@ -128,6 +141,7 @@ export const createDecorations = (
* @public
* @param {Awareness} awareness
* @param {object} opts
* @param {function(any, any, any):boolean} [opts.awarenessStateFilter]
* @param {function(any):HTMLElement} [opts.cursorBuilder]
* @param {function(any):import('prosemirror-view').DecorationAttrs} [opts.selectionBuilder]
* @param {function(any):any} [opts.getSelection]
Expand All @@ -137,6 +151,7 @@ export const createDecorations = (
export const yCursorPlugin = (
awareness,
{
awarenessStateFilter = defaultAwarenessStateFilter,
cursorBuilder = defaultCursorBuilder,
selectionBuilder = defaultSelectionBuilder,
getSelection = (state) => state.selection
Expand All @@ -150,6 +165,7 @@ export const yCursorPlugin = (
return createDecorations(
state,
awareness,
awarenessStateFilter,
cursorBuilder,
selectionBuilder
)
Expand All @@ -164,6 +180,7 @@ export const yCursorPlugin = (
return createDecorations(
newState,
awareness,
awarenessStateFilter,
cursorBuilder,
selectionBuilder
)
Expand Down