| | import { preserveAnchorNodePosition } from 'scroll-anchoring' |
| |
|
| | import { useArticleContext } from '@/frame/components/context/ArticleContext' |
| | import { InArticlePicker } from './InArticlePicker' |
| |
|
| | |
| |
|
| | |
| | |
| |
|
| | |
| | |
| | |
| | function showToolSpecificContent(tool: string, supportedTools: Array<string>) { |
| | const markdowns = Array.from(document.querySelectorAll<HTMLElement>('.ghd-tool')) |
| | const supportedMarkdowns = markdowns.filter((xel) => |
| | supportedTools.some((toolName) => xel.classList.contains(toolName)), |
| | ) |
| | for (const el of supportedMarkdowns) { |
| | el.style.display = el.classList.contains(tool) ? '' : 'none' |
| |
|
| | |
| | |
| | |
| | |
| | if (el.tagName === 'A' && el.parentElement && el.parentElement.tagName === 'LI') { |
| | el.parentElement.style.display = el.classList.contains(tool) ? '' : 'none' |
| | } |
| | } |
| |
|
| | |
| | |
| | const toolEls = Array.from( |
| | document.querySelectorAll<HTMLElement>( |
| | supportedTools.map((toolOption) => `.tool-${toolOption}`).join(', '), |
| | ), |
| | ) |
| | for (const el of toolEls) { |
| | el.style.display = el.classList.contains(`tool-${tool}`) ? '' : 'none' |
| | } |
| | } |
| |
|
| | function getDefaultTool(defaultTool: string | undefined, detectedTools: Array<string>): string { |
| | |
| | if (defaultTool && detectedTools.includes(defaultTool)) return defaultTool |
| |
|
| | |
| | if (detectedTools.includes('webui')) return 'webui' |
| |
|
| | |
| | if (detectedTools.includes('cli')) return 'cli' |
| |
|
| | |
| | return detectedTools[0] |
| | } |
| |
|
| | const toolQueryKey = 'tool' |
| | export const ToolPicker = () => { |
| | |
| | const { defaultTool, detectedTools, allTools } = useArticleContext() |
| |
|
| | if (!detectedTools.length) return null |
| |
|
| | const options = detectedTools.map((value) => { |
| | return { value, label: allTools[value] } |
| | }) |
| |
|
| | return ( |
| | <InArticlePicker |
| | fallbackValue={getDefaultTool(defaultTool, detectedTools)} |
| | cookieKey="toolPreferred" |
| | queryStringKey={toolQueryKey} |
| | onValue={(value: string) => { |
| | preserveAnchorNodePosition(document, () => { |
| | showToolSpecificContent(value, Object.keys(allTools)) |
| | }) |
| | }} |
| | preferenceName="application" |
| | ariaLabel="Tool" |
| | options={options} |
| | /> |
| | ) |
| | } |
| |
|