Skip to content

Commit

Permalink
feat: Added awarenessFilter option for filtering `awareness.getStat…
Browse files Browse the repository at this point in the history
…es()`
  • Loading branch information
RubaXa committed Sep 6, 2023
1 parent 6349748 commit 6f78e75
Showing 1 changed file with 18 additions and 1 deletion.
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

0 comments on commit 6f78e75

Please sign in to comment.