mirror of
https://github.com/EnixCoda/Gitako.git
synced 2026-03-11 08:54:44 +00:00
feat: onPJAXComplete
This commit is contained in:
parent
e1cd6c95b8
commit
e09935230d
2 changed files with 23 additions and 9 deletions
|
|
@ -51,19 +51,24 @@ const RawGitako: React.FC<Props & ConnectorState> = function RawGitako(props) {
|
|||
return () => window.removeEventListener('keydown', onKeyDown)
|
||||
}, [props.disabled, onKeyDown])
|
||||
|
||||
const onPJAXEnd = React.useCallback(() => {
|
||||
const updateMeta = React.useCallback(() => {
|
||||
if (props.disabled) return
|
||||
DOMHelper.unmountTopProgressBar()
|
||||
props.setMetaData({ ...props.metaData, ...URLHelper.parse() })
|
||||
// TODO: update state to re-trigger DOM effects
|
||||
// if (configContext.val.copyFileButton) DOMHelper.attachCopyFileBtn()
|
||||
// if (configContext.val.copySnippetButton) DOMHelper.attachCopySnippet()
|
||||
}, [props.metaData, configContext.val])
|
||||
}, [props.disabled, props.metaData, configContext.val])
|
||||
useOnPJAXComplete(updateMeta)
|
||||
|
||||
React.useEffect(() => {
|
||||
const attachCopyFileButton = React.useCallback(() => {
|
||||
if (props.disabled) return
|
||||
window.addEventListener('pjax:complete', onPJAXEnd)
|
||||
return () => window.removeEventListener('pjax:complete', onPJAXEnd)
|
||||
}, [props.disabled, onPJAXEnd])
|
||||
if (configContext.val.copyFileButton) return DOMHelper.attachCopyFileBtn()
|
||||
}, [props.disabled, configContext.val.copyFileButton])
|
||||
useOnPJAXComplete(attachCopyFileButton)
|
||||
|
||||
const attachCopySnippetButton = React.useCallback(() => {
|
||||
if (props.disabled) return
|
||||
if (configContext.val.copySnippetButton) return DOMHelper.attachCopySnippet()
|
||||
}, [props.disabled, configContext.val.copySnippetButton])
|
||||
useOnPJAXComplete(attachCopySnippetButton)
|
||||
|
||||
React.useEffect(() => {
|
||||
if (configContext.val.intelligentToggle === null) {
|
||||
|
|
@ -138,6 +143,13 @@ RawGitako.defaultProps = {
|
|||
|
||||
export const SideBar = connect(SideBarCore)(RawGitako)
|
||||
|
||||
function useOnPJAXComplete(onPJAXComplete: () => void, deps: React.DependencyList = []) {
|
||||
React.useEffect(() => {
|
||||
window.addEventListener('pjax:complete', onPJAXComplete)
|
||||
return () => window.removeEventListener('pjax:complete', onPJAXComplete)
|
||||
}, [onPJAXComplete, ...deps])
|
||||
}
|
||||
|
||||
function renderAccessDeniedError() {
|
||||
return (
|
||||
<div className={'description'}>
|
||||
|
|
|
|||
|
|
@ -217,6 +217,7 @@ export function attachCopyFileBtn() {
|
|||
buttons.push(button)
|
||||
}
|
||||
})
|
||||
// TODO: query from DOM again when detach
|
||||
return () =>
|
||||
buttons.forEach(button => {
|
||||
button.parentElement?.removeChild(button)
|
||||
|
|
@ -275,6 +276,7 @@ export function attachCopySnippet() {
|
|||
}
|
||||
}
|
||||
})
|
||||
// TODO: query from DOM again when detach
|
||||
return () =>
|
||||
buttons.forEach(button => {
|
||||
button.parentElement?.removeChild(button)
|
||||
|
|
|
|||
Loading…
Reference in a new issue