Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

display-tool-specific-content.ts 3.5 KB

You have to be logged in to leave a comment. Sign In
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
  1. import Cookies from 'js-cookie'
  2. import { preserveAnchorNodePosition } from 'scroll-anchoring'
  3. import { sendEvent, EventType } from './events'
  4. const supportedTools = ['cli', 'desktop', 'webui', 'curl']
  5. export default function displayToolSpecificContent() {
  6. const toolElements = Array.from(document.querySelectorAll('.extended-markdown')).filter((el) =>
  7. supportedTools.some((tool) => el.classList.contains(tool))
  8. ) as Array<HTMLElement>
  9. if (!toolElements.length) return
  10. const detectedTools = toolElements.flatMap((el) =>
  11. Array.from(el.classList).filter((className) => supportedTools.includes(className))
  12. ) as Array<string>
  13. const tool = getDefaultTool(detectedTools)
  14. showToolSpecificContent(tool, toolElements)
  15. hideSwitcherLinks(detectedTools)
  16. highlightTabForTool(tool)
  17. // configure links for switching tool content
  18. switcherLinks().forEach((link) => {
  19. link.addEventListener('click', (event) => {
  20. event.preventDefault()
  21. const target = event.target as HTMLElement
  22. highlightTabForTool(target.dataset.tool || '')
  23. preserveAnchorNodePosition(document, () => {
  24. showToolSpecificContent(target.dataset.tool || '', toolElements)
  25. })
  26. // Save this preference as a cookie.
  27. Cookies.set('toolPreferred', target.dataset.tool || '', { sameSite: 'strict', secure: true })
  28. // Send event data
  29. sendEvent({
  30. type: EventType.preference,
  31. preference_name: 'application',
  32. preference_value: target.dataset.tool,
  33. })
  34. })
  35. })
  36. }
  37. function highlightTabForTool(tool: string) {
  38. // (de)activate switcher link appearances
  39. switcherLinks().forEach((link) => {
  40. link.dataset.tool === tool ? link.classList.add('selected') : link.classList.remove('selected')
  41. })
  42. }
  43. function showToolSpecificContent(tool: string, toolElements: Array<HTMLElement>) {
  44. // show the content only for the highlighted tool
  45. toolElements
  46. .filter((el) => supportedTools.some((tool) => el.classList.contains(tool)))
  47. .forEach((el) => {
  48. el.style.display = el.classList.contains(tool) ? '' : 'none'
  49. })
  50. }
  51. // hide links for any tool-specific sections that are not present
  52. function hideSwitcherLinks(detectedTools: Array<string>) {
  53. const links = Array.from(document.querySelectorAll('a.tool-switcher')) as Array<HTMLAnchorElement>
  54. links.forEach((link) => {
  55. if (detectedTools.includes(link.dataset.tool || '')) return
  56. link.style.display = 'none'
  57. })
  58. }
  59. function getDefaultTool(detectedTools: Array<string>): string {
  60. // If the user selected a tool preference and the tool is present on this page
  61. const cookieValue = Cookies.get('toolPreferred')
  62. if (cookieValue && detectedTools.includes(cookieValue)) return cookieValue
  63. // If there is a default tool and the tool is present on this page
  64. const defaultToolEl = document.querySelector('[data-default-tool]') as HTMLElement
  65. const defaultToolValue = defaultToolEl ? defaultToolEl.dataset.defaultTool : ''
  66. if (defaultToolValue && detectedTools.includes(defaultToolValue)) {
  67. return defaultToolValue
  68. }
  69. // Default to webui if present (this is generally the case where we show UI/CLI/Desktop info)
  70. if (detectedTools.includes('webui')) return 'webui'
  71. // Default to cli if present (this is generally the case where we show curl/CLI info)
  72. if (detectedTools.includes('cli')) return 'cli'
  73. // Otherwise, just choose the first detected tool
  74. return detectedTools[0]
  75. }
  76. function switcherLinks() {
  77. return Array.from(document.querySelectorAll('a.tool-switcher')) as Array<HTMLAnchorElement>
  78. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...