-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
asmyshlyaev177
committed
Nov 27, 2024
1 parent
ace908e
commit e43db31
Showing
9 changed files
with
115 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 0 additions & 2 deletions
2
packages/example-nextjs14/src/app/(demo)/react-router/CodeBlocksRR.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,15 @@ | ||
'use client' | ||
import React from 'react'; | ||
'use server' | ||
|
||
import { highlight } from '../highlighter'; | ||
import { Langs } from '../types'; | ||
|
||
export const Code = ({ content, lang, ...rest }: { content: string, lang?: Langs } & React.ComponentPropsWithoutRef<'div'>) => { | ||
export const Code = async ({ content, lang, id, ...rest }: { content: string, lang?: Langs, id?: string } & React.ComponentPropsWithoutRef<'div'>) => { | ||
const text = await highlight(content, { lang }) | ||
|
||
const [text, setText] = React.useState(''); | ||
|
||
React.useEffect(() => { | ||
highlight(content, { lang }).then(setText) | ||
}, [content, lang]) | ||
|
||
return text ? | ||
<div dangerouslySetInnerHTML={{ __html: text }} {...rest}></div> | ||
: <div {...rest}>{content.split('\n').map((el, ind) => (<div key={ind}>{el || '_'}</div>))}</div> | ||
return <div id={id}>{text ? | ||
<div dangerouslySetInnerHTML={{ __html: text }} {...rest}></div> | ||
: <div {...rest}>{content.split('\n') | ||
.map((el, ind) => (<div key={ind}>{el || '_'}</div>))} | ||
</div>} | ||
</div> | ||
} |
74 changes: 74 additions & 0 deletions
74
packages/example-nextjs14/src/app/components/FakeTypes.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
'use client'; | ||
|
||
import React from 'react'; | ||
|
||
import { useFloating, autoUpdate, useClientPoint, offset } from '@floating-ui/react'; | ||
|
||
import { type Matcher, type Tooltip, type Langs } from '../types'; | ||
import { highlight } from '../highlighter'; | ||
|
||
export const FakeTypes = ({ matchers, id }: { matchers?: Matcher[], id: string }) => { | ||
const [tooltip, setTooltip] = React.useState<{ x: number, y: number, nodes: Tooltip[] }>({ nodes: [], x: 0, y: 0 }); | ||
|
||
const { refs, floatingStyles, context } = useFloating({ | ||
whileElementsMounted: autoUpdate, | ||
placement: 'top', | ||
middleware: [ | ||
offset({ mainAxis: 20, crossAxis: 50 }) | ||
] | ||
}); | ||
useClientPoint(context, | ||
{ x: tooltip.x, y: tooltip.y } | ||
) | ||
|
||
React.useEffect(() => { | ||
const codeBlock = document.querySelector(`[id="${id}"]`) | ||
|
||
if (codeBlock && !(codeBlock as HTMLDivElement).onmouseover) { | ||
const matchTooltips = (ev: MouseEvent) => { | ||
// @ts-expect-error fots | ||
const text = (ev?.target?.textContent || '').trim(); | ||
// @ts-expect-error fots | ||
const next = (ev?.target?.nextSibling?.textContent || '').trim() | ||
|
||
// if (text?.length < 12) { | ||
// console.log(`${text}${next}`, { ev, context }) | ||
// } | ||
|
||
const match = matchers?.find(el => el[0] === `${text}${next}`) | ||
|
||
if (match) { | ||
if (match[1] !== tooltip.nodes) { | ||
setTooltip({ nodes: match[1], x: ev.clientX, y: ev.clientY }) | ||
} | ||
} else if (tooltip.nodes.length) { | ||
setTooltip(curr => ({ ...curr, nodes: [] })) | ||
} | ||
} | ||
|
||
(codeBlock as HTMLDivElement).onmousemove = matchTooltips; | ||
(codeBlock as HTMLDivElement).onmouseleave = () => setTooltip(curr => ({...curr, nodes: []})); | ||
|
||
} | ||
}, [id, tooltip.nodes, matchers]) | ||
|
||
return tooltip.nodes.length ? <div | ||
style={floatingStyles} | ||
ref={refs.setFloating} | ||
className="text-[12px] p-4 absolute bg-slate-800 rounded-md max-w-[600px] transition-none border border-slate-500" | ||
> | ||
{tooltip.nodes.map((node, ind) => ( | ||
<CodeClient content={node.text} lang={node.lang} key={ind} /> | ||
))} | ||
</div> : null | ||
} | ||
|
||
const CodeClient = ({ content, lang }: { content: string, lang?: Langs, id?: string }) => { | ||
const [text, setText] = React.useState(''); | ||
|
||
React.useEffect(() => { | ||
highlight(content, { lang }).then(setText) | ||
}, [content, lang]) | ||
|
||
return <div dangerouslySetInnerHTML={{ __html: text || "." }} /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,3 +65,4 @@ function getHighlighter() { | |
}) | ||
} | ||
|
||
const _warmUp = getHighlighter() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export function stringToHash(text: string) { | ||
let hash = 0; | ||
|
||
if (text.length == 0) return '0'; | ||
|
||
for (let i = 0; i < text.length; i++) { | ||
const char = text.charCodeAt(i); | ||
hash = ((hash << 5) - hash) + char; | ||
hash = hash & hash; | ||
} | ||
|
||
return String(hash); | ||
} |