mirror of
https://github.com/langgenius/dify.git
synced 2026-09-08 11:04:27 +08:00
use const for cursor move config
This commit is contained in:
parent
2035186cd2
commit
58cd785da6
@ -19,12 +19,8 @@ export function useCollaboration(appId: string, reactFlowStore?: any) {
|
|||||||
|
|
||||||
let connectionId: string | null = null
|
let connectionId: string | null = null
|
||||||
|
|
||||||
if (!cursorServiceRef.current) {
|
if (!cursorServiceRef.current)
|
||||||
cursorServiceRef.current = new CursorService({
|
cursorServiceRef.current = new CursorService()
|
||||||
minMoveDistance: 10,
|
|
||||||
throttleMs: 300,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const initCollaboration = async () => {
|
const initCollaboration = async () => {
|
||||||
connectionId = await collaborationManager.connect(appId, reactFlowStore)
|
connectionId = await collaborationManager.connect(appId, reactFlowStore)
|
||||||
|
|||||||
@ -2,10 +2,8 @@ import type { RefObject } from 'react'
|
|||||||
import type { CursorPosition } from '../types/collaboration'
|
import type { CursorPosition } from '../types/collaboration'
|
||||||
import type { ReactFlowInstance } from 'reactflow'
|
import type { ReactFlowInstance } from 'reactflow'
|
||||||
|
|
||||||
export type CursorServiceConfig = {
|
const CURSOR_MIN_MOVE_DISTANCE = 10
|
||||||
minMoveDistance?: number
|
const CURSOR_THROTTLE_MS = 500
|
||||||
throttleMs?: number
|
|
||||||
}
|
|
||||||
|
|
||||||
export class CursorService {
|
export class CursorService {
|
||||||
private containerRef: RefObject<HTMLElement> | null = null
|
private containerRef: RefObject<HTMLElement> | null = null
|
||||||
@ -15,14 +13,6 @@ export class CursorService {
|
|||||||
private onEmitPosition: ((position: CursorPosition) => void) | null = null
|
private onEmitPosition: ((position: CursorPosition) => void) | null = null
|
||||||
private lastEmitTime = 0
|
private lastEmitTime = 0
|
||||||
private lastPosition: { x: number; y: number } | null = null
|
private lastPosition: { x: number; y: number } | null = null
|
||||||
private config: Required<CursorServiceConfig>
|
|
||||||
|
|
||||||
constructor(config: CursorServiceConfig = {}) {
|
|
||||||
this.config = {
|
|
||||||
minMoveDistance: config.minMoveDistance ?? 5,
|
|
||||||
throttleMs: config.throttleMs ?? 300,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
startTracking(
|
startTracking(
|
||||||
containerRef: RefObject<HTMLElement>,
|
containerRef: RefObject<HTMLElement>,
|
||||||
@ -78,10 +68,11 @@ export class CursorService {
|
|||||||
|
|
||||||
// Always emit cursor position (remove boundary check since world coordinates can be negative)
|
// Always emit cursor position (remove boundary check since world coordinates can be negative)
|
||||||
const now = Date.now()
|
const now = Date.now()
|
||||||
const timeThrottled = now - this.lastEmitTime > this.config.throttleMs
|
const timeThrottled = now - this.lastEmitTime > CURSOR_THROTTLE_MS
|
||||||
|
const minDistance = CURSOR_MIN_MOVE_DISTANCE / (this.reactFlowInstance?.getZoom() || 1)
|
||||||
const distanceThrottled = !this.lastPosition
|
const distanceThrottled = !this.lastPosition
|
||||||
|| (Math.abs(x - this.lastPosition.x) > this.config.minMoveDistance / (this.reactFlowInstance?.getZoom() || 1))
|
|| (Math.abs(x - this.lastPosition.x) > minDistance)
|
||||||
|| (Math.abs(y - this.lastPosition.y) > this.config.minMoveDistance / (this.reactFlowInstance?.getZoom() || 1))
|
|| (Math.abs(y - this.lastPosition.y) > minDistance)
|
||||||
|
|
||||||
if (timeThrottled && distanceThrottled) {
|
if (timeThrottled && distanceThrottled) {
|
||||||
this.lastPosition = { x, y }
|
this.lastPosition = { x, y }
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user