feat: don't let read-only users send their cursors or selections

This was done as it may be used to distract or annoy other users either intentionally or unintentionally.

Signed-off-by: Philip Molares <philip.molares@udo.edu>
Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Philip Molares 2023-03-30 21:30:32 +02:00 committed by Tilman Vatteroth
parent 7636480d8a
commit 2fc89a7de5
6 changed files with 121 additions and 40 deletions

View file

@ -14,16 +14,18 @@ import type { Listener } from 'eventemitter2'
*/
export class SendCursorViewPlugin implements PluginValue {
private lastCursor: SelectionRange | undefined
private listener: Listener
private listener?: Listener
constructor(private view: EditorView, private messageTransporter: MessageTransporter) {
this.listener = messageTransporter.doAsSoonAsReady(() => {
this.sendCursor(this.lastCursor)
})
constructor(private view: EditorView, private messageTransporter: MessageTransporter, private mayEdit: boolean) {
if (mayEdit) {
this.listener = messageTransporter.doAsSoonAsReady(() => {
this.sendCursor(this.lastCursor)
})
}
}
destroy() {
this.listener.off()
this.listener?.off()
}
update(update: ViewUpdate) {
@ -37,7 +39,8 @@ export class SendCursorViewPlugin implements PluginValue {
if (
!this.messageTransporter.isReady() ||
currentCursor === undefined ||
(this.lastCursor?.to === currentCursor?.to && this.lastCursor?.from === currentCursor?.from)
(this.lastCursor?.to === currentCursor?.to && this.lastCursor?.from === currentCursor?.from) ||
!this.mayEdit
) {
return
}

View file

@ -3,6 +3,7 @@
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { useMayEdit } from '../../../../../hooks/common/use-may-edit'
import {
createCursorLayer,
createSelectionLayer,
@ -19,14 +20,16 @@ import { useMemo } from 'react'
* Bundles all extensions that are needed for the remote cursor display.
* @return The created codemirror extensions
*/
export const useCodeMirrorRemoteCursorsExtension = (messageTransporter: MessageTransporter): Extension =>
useMemo(
export const useCodeMirrorRemoteCursorsExtension = (messageTransporter: MessageTransporter): Extension => {
const mayEdit = useMayEdit()
return useMemo(
() => [
remoteCursorStateField.extension,
createCursorLayer(),
createSelectionLayer(),
ViewPlugin.define((view) => new ReceiveRemoteCursorViewPlugin(view, messageTransporter)),
ViewPlugin.define((view) => new SendCursorViewPlugin(view, messageTransporter))
ViewPlugin.define((view) => new SendCursorViewPlugin(view, messageTransporter, mayEdit))
],
[messageTransporter]
[mayEdit, messageTransporter]
)
}