-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path23-page-format-dropdown.js
More file actions
206 lines (183 loc) · 6.77 KB
/
23-page-format-dropdown.js
File metadata and controls
206 lines (183 loc) · 6.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
;(() => {
'use strict'
const pushCtaLink = (clickText, clickUrl) => {
const h1 = document.querySelector('h1')
window.dataLayer = window.dataLayer || []
window.dataLayer.push({
event: 'content_click',
clickText,
itemTitle: h1 ? h1.textContent.trim() : document.title,
clickUrl,
elementType: 'link',
contentType: 'Page Options Dropdown',
})
}
const dropdowns = document.querySelectorAll('.page-options-dropdown')
if (!dropdowns.length) return
const href = window.location.href
const mdUrl = href.endsWith('/') ? href + 'index.md' : href.replace(/(?:\.html)?(?=#|$)/, '.md')
const prompt = 'Read from ' + href + ' so I can ask questions about it.'
const removeDropdowns = () => dropdowns.forEach((d) => d.remove())
// Skip markdown check on localhost (local testing)
const isLocalhost = window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1'
if (!isLocalhost) {
fetch(mdUrl, { method: 'HEAD' })
.then((res) => {
if (!res.ok) removeDropdowns()
})
.catch(removeDropdowns)
}
// Adjust sidebar top when banners are visible
const sidebar = document.querySelector('.toc-sidebar')
if (sidebar) {
const adjustSidebarTop = () => {
const topBanner = document.querySelector('.top-banner:not(.hide)')
sidebar.style.top = topBanner ? 'var(--banner-height)' : '0px'
}
adjustSidebarTop()
// Re-check when banners are dismissed
document.addEventListener('click', (e) => {
if (e.target.closest('.close-button')) setTimeout(adjustSidebarTop, 100)
})
}
dropdowns.forEach((dropdown) => {
const primaryBtn = dropdown.querySelector('.page-options-primary')
const toggle = dropdown.querySelector('.page-options-toggle')
const optionsPanel = dropdown.querySelector('.page-options-menu')
const statusEl = dropdown.querySelector('.page-options-status')
if (!toggle || !optionsPanel) return
const menuItems = optionsPanel.querySelectorAll('[role="menuitem"]')
if (!menuItems.length) return
const scrollParent = dropdown.closest('.scrollbar')
const openMenu = () => {
if (scrollParent) scrollParent.style.overflow = 'visible'
toggle.setAttribute('aria-expanded', 'true')
optionsPanel.removeAttribute('hidden')
optionsPanel.classList.add('is-open')
menuItems[0].focus()
}
const closeMenu = (restoreFocus) => {
if (scrollParent) scrollParent.style.overflow = ''
toggle.setAttribute('aria-expanded', 'false')
optionsPanel.classList.remove('is-open')
optionsPanel.setAttribute('hidden', '')
if (restoreFocus) toggle.focus()
}
// Set href for link items
const viewMd = optionsPanel.querySelector('[data-action="view-md"]')
const openChatgpt = optionsPanel.querySelector('[data-action="open-chatgpt"]')
const openClaude = optionsPanel.querySelector('[data-action="open-claude"]')
const openPerplexity = optionsPanel.querySelector('[data-action="open-perplexity"]')
if (viewMd) viewMd.href = mdUrl
if (openChatgpt) openChatgpt.href = 'https://chatgpt.com/?q=' + encodeURIComponent(prompt)
if (openClaude) openClaude.href = 'https://claude.ai/new?q=' + encodeURIComponent(prompt)
if (openPerplexity) openPerplexity.href = 'https://www.perplexity.ai/search?q=' + encodeURIComponent(prompt)
const copyMd = () => {
fetch(mdUrl)
.then((res) => res.text())
.then((text) => {
const h1Index = text.indexOf('\n# ')
const content = h1Index !== -1 ? text.substring(h1Index + 1) : text
return navigator.clipboard.writeText(content).then(() => {
if (statusEl) {
statusEl.textContent = 'Copied to clipboard'
setTimeout(() => {
statusEl.textContent = ''
}, 3000)
}
if (copyTooltip) {
copyTooltip.show()
setTimeout(() => {
copyTooltip.hide()
}, 2000)
}
})
})
.catch(() => {
window.open(mdUrl, '_blank')
})
}
// Tooltip for copy feedback
let copyTooltip
if (primaryBtn && typeof tippy === 'function') {
const isFooter = dropdown.classList.contains('page-options-footer')
const combo = dropdown.querySelector('.page-options-combo')
copyTooltip = tippy(combo || primaryBtn, {
arrow: tippy.roundArrow,
animation: 'shift-away',
content: 'Copied!',
delay: 200,
maxWidth: 240,
placement: isFooter ? 'top' : 'bottom',
trigger: 'manual',
theme: 'copy-link-popover',
zIndex: 'var(--z-nav-mobile)',
})
}
if (primaryBtn) {
primaryBtn.addEventListener('click', () => {
pushCtaLink(primaryBtn.textContent.trim(), mdUrl)
copyMd()
})
}
// Menu copy button also copies
const menuCopyBtn = optionsPanel.querySelector('[data-action="copy-md"]')
if (menuCopyBtn) {
menuCopyBtn.addEventListener('click', () => {
pushCtaLink(menuCopyBtn.textContent.trim(), mdUrl)
copyMd()
closeMenu(true)
})
}
toggle.addEventListener('click', () => {
const expanded = toggle.getAttribute('aria-expanded') === 'true'
if (expanded) {
closeMenu(false)
} else {
openMenu()
}
})
toggle.addEventListener('keydown', (e) => {
if (e.key === 'ArrowDown' || e.key === 'Down') {
e.preventDefault()
openMenu()
}
})
optionsPanel.addEventListener('keydown', (e) => {
const currentIndex = [].indexOf.call(menuItems, document.activeElement)
if (e.key === 'ArrowDown' || e.key === 'Down') {
e.preventDefault()
const next = (currentIndex + 1) % menuItems.length
menuItems[next].focus()
} else if (e.key === 'ArrowUp' || e.key === 'Up') {
e.preventDefault()
const prev = (currentIndex - 1 + menuItems.length) % menuItems.length
menuItems[prev].focus()
} else if (e.key === 'Home') {
e.preventDefault()
menuItems[0].focus()
} else if (e.key === 'End') {
e.preventDefault()
menuItems[menuItems.length - 1].focus()
} else if (e.key === 'Escape' || e.key === 'Esc') {
e.preventDefault()
closeMenu(true)
} else if (e.key === 'Tab') {
closeMenu(false)
}
})
// Close on click outside
document.addEventListener('click', (e) => {
if (!dropdown.contains(e.target)) {
closeMenu(false)
}
})
// Close after clicking a link item
optionsPanel.querySelectorAll('a[role="menuitem"]').forEach((link) => {
link.addEventListener('click', () => {
pushCtaLink(link.textContent.trim(), link.href)
closeMenu(false)
})
})
})
})()