Add CodeMirror 6 cursor/scroll position handling (#5653)

GitOrigin-RevId: bf756a415dbf1e4b5bbd9f580700bdc799c6b1d6
This commit is contained in:
Alf Eaton 2021-11-15 12:25:19 +00:00 committed by Copybot
parent 205d853cea
commit efcc15d5b5
3 changed files with 51 additions and 0 deletions

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

View file

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

View file

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