overleaf/services/web/frontend/js/shared/hooks/use-scope-event-emitter.js
Alf Eaton efcc15d5b5 Add CodeMirror 6 cursor/scroll position handling (#5653)
GitOrigin-RevId: bf756a415dbf1e4b5bbd9f580700bdc799c6b1d6
2021-11-16 09:02:41 +00:00

22 lines
513 B
JavaScript

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]
)
}