Skip to content

Commit 237b380

Browse files
committed
docs: move RTD PR diff toolbar below navbar
1 parent 4b0ee53 commit 237b380

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

docs/.vitepress/theme/index.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type ViewTransitionDocument = Document & {
1212

1313
type PhobosWindow = Window & {
1414
__PHOBOS_SEARCH_HOTKEY_INSTALLED__?: boolean
15+
__PHOBOS_RTD_FILETREEDIFF_POSITION_PATCH_INSTALLED__?: boolean
1516
}
1617

1718
function isSearchKeyboardShortcut(event: KeyboardEvent) {
@@ -42,6 +43,60 @@ function installPhysicalSearchHotKey() {
4243
})
4344
}
4445

46+
function shouldPatchReadTheDocsFileTreeDiffPosition() {
47+
return window.location.hostname.endsWith('.readthedocs.build')
48+
}
49+
50+
function patchReadTheDocsFileTreeDiffPosition(host: Element) {
51+
const shadowRoot = host.shadowRoot
52+
53+
if (!shadowRoot || shadowRoot.querySelector('[data-phobos-rtd-filetreediff-position]')) {
54+
return
55+
}
56+
57+
const style = document.createElement('style')
58+
style.dataset.phobosRtdFiletreediffPosition = 'true'
59+
style.textContent = `
60+
:host > div {
61+
top: 72px !important;
62+
right: 16px !important;
63+
border-radius: 6px !important;
64+
}
65+
66+
@media (max-width: 768px) {
67+
:host > div {
68+
top: 64px !important;
69+
}
70+
}
71+
`
72+
73+
shadowRoot.append(style)
74+
}
75+
76+
function installReadTheDocsFileTreeDiffPositionPatch() {
77+
const phobosWindow = window as PhobosWindow
78+
79+
if (
80+
phobosWindow.__PHOBOS_RTD_FILETREEDIFF_POSITION_PATCH_INSTALLED__ ||
81+
!shouldPatchReadTheDocsFileTreeDiffPosition()
82+
) {
83+
return
84+
}
85+
86+
phobosWindow.__PHOBOS_RTD_FILETREEDIFF_POSITION_PATCH_INSTALLED__ = true
87+
88+
const patchAll = () => {
89+
document.querySelectorAll('readthedocs-filetreediff').forEach(patchReadTheDocsFileTreeDiffPosition)
90+
}
91+
92+
patchAll()
93+
94+
new MutationObserver(patchAll).observe(document.documentElement, {
95+
childList: true,
96+
subtree: true,
97+
})
98+
}
99+
45100
function shouldUseViewTransition() {
46101
return (
47102
typeof window !== 'undefined' &&
@@ -116,6 +171,7 @@ export default {
116171

117172
if (typeof window !== 'undefined') {
118173
installPhysicalSearchHotKey()
174+
installReadTheDocsFileTreeDiffPositionPatch()
119175
}
120176
},
121177
}

0 commit comments

Comments
 (0)