mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-07 20:31:06 -05:00
Add CodeMirror 6 cursor/scroll position handling (#5653)
GitOrigin-RevId: bf756a415dbf1e4b5bbd9f580700bdc799c6b1d6
This commit is contained in:
parent
205d853cea
commit
efcc15d5b5
3 changed files with 51 additions and 0 deletions
15
services/web/frontend/js/shared/hooks/use-event-listener.js
Normal file
15
services/web/frontend/js/shared/hooks/use-event-listener.js
Normal file
|
@ -0,0 +1,15 @@
|
|||
import { useEffect } from 'react'
|
||||
|
||||
/**
|
||||
* @param {string} eventName
|
||||
* @param {function} [listener]
|
||||
*/
|
||||
export default function useEventListener(eventName, listener) {
|
||||
useEffect(() => {
|
||||
window.addEventListener(eventName, listener)
|
||||
|
||||
return () => {
|
||||
window.removeEventListener(eventName, listener)
|
||||
}
|
||||
}, [eventName, listener])
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
import { useCallback } from 'react'
|
||||
import { useIdeContext } from '../context/ide-context'
|
||||
|
||||
/**
|
||||
* @param {string} eventName
|
||||
* @param {boolean} [broadcast]
|
||||
* @returns function
|
||||
*/
|
||||
export default function useScopeEventEmitter(eventName, broadcast = true) {
|
||||
const { $scope } = useIdeContext()
|
||||
|
||||
return useCallback(
|
||||
detail => {
|
||||
if (broadcast) {
|
||||
$scope.$broadcast(eventName, detail)
|
||||
} else {
|
||||
$scope.$emit(eventName, detail)
|
||||
}
|
||||
},
|
||||
[$scope, eventName, broadcast]
|
||||
)
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
import { useEffect } from 'react'
|
||||
import { useIdeContext } from '../context/ide-context'
|
||||
|
||||
/**
|
||||
* @param {string} eventName
|
||||
* @param {function} [listener]
|
||||
*/
|
||||
export default function useScopeEventListener(eventName, listener) {
|
||||
const { $scope } = useIdeContext()
|
||||
|
||||
useEffect(() => {
|
||||
return $scope.$on(eventName, listener)
|
||||
}, [$scope, eventName, listener])
|
||||
}
|
Loading…
Reference in a new issue