import type { Language } from "prism-react-renderer"; import { Highlight } from "prism-react-renderer"; import queryString from "query-string"; import * as React from "react"; import { useHotkeys } from "react-hotkeys-hook"; import { stories, storySource, StorySourceHeader, } from "virtual:generated-list"; import { ActionType, AddonProps, GlobalState } from "../../../shared/types"; import config from "../get-config"; import { Source } from "../icons"; import { Modal } from "../ui"; export const getQuery = (locationSearch: string) => { const urlVal = queryString.parse(locationSearch).source; if (urlVal === "true") return true; if (urlVal === "false") return false; return config.addons.source.defaultState; }; export const CodeHighlight = ({ children, theme, language = "tsx", locStart, locEnd, className, }: { children: string; theme: string; language?: Language; locStart?: number; locEnd?: number; className?: string; }) => { const withLoc = typeof locStart !== "undefined" && typeof locEnd !== "undefined"; const match = /language-(\w+)/.exec(className || ""); if (match) { language = match[1] as Language; return ( {({ className, style, tokens, getTokenProps }) => (
{tokens.map((line, i) => (
{line.map((token, key) => ( ))}
))}
)}
); } if (withLoc) { return ( {({ className, style, tokens, getLineProps, getTokenProps }) => (
            {tokens.map((line, i) => (
              
= locStart && i + 1 <= locEnd ? { backgroundColor: "var(--ladle-color-accent)", color: "#FFF", } : undefined } > {i + 1}
{line.map((token, key) => ( ))}
))}
)}
); } return {children}; }; const CodeFrame = ({ globalState }: { globalState: GlobalState }) => { if (!stories[globalState.story]) { return <>There is no story loaded.; } const { entry, locStart, locEnd } = stories[globalState.story]; React.useEffect(() => { window.location.hash = ``; window.location.hash = `ladle_loc_${locStart}`; }, [locStart]); return ( <> {decodeURIComponent(storySource[globalState.story])} ); }; export const Button = ({ globalState, dispatch }: AddonProps) => { const text = "Show the story source code."; useHotkeys( config.hotkeys.source, () => { dispatch({ type: ActionType.UpdateSource, value: !globalState.source, }); }, { enabled: globalState.hotkeys && config.addons.source.enabled, }, ); return (
  • ); };