-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(ai): add basic scrollycoding components from codehike * feat(ai): add basic scrollycoding components from codehike * feat(ai): add basic scrollycoding components from codehike * feat(ai): add basic scrollycoding components from codehike * feat(ai): add basic scrollycoding components from codehike * feat(ai): add basic scrollycoding components from codehike * Merge branch 'main' of https://github.com/skillrecordings/course-builder into vh/feat/ai/scrollycoding * feat(ai): add basic scrollycoding components from codehike * feat(ai): add basic scrollycoding components from codehike * feat(ai): add basic scrollycoding components from codehike
- Loading branch information
1 parent
cb911e5
commit 6a799d1
Showing
19 changed files
with
542 additions
and
77 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
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import React from 'react' | ||
import { AnnotationHandler, InlineAnnotation } from 'codehike/code' | ||
|
||
export const callout: AnnotationHandler = { | ||
name: 'callout', | ||
transform: (annotation: InlineAnnotation) => { | ||
const { name, query, lineNumber, fromColumn, toColumn, data } = annotation | ||
return { | ||
name, | ||
query, | ||
fromLineNumber: lineNumber, | ||
toLineNumber: lineNumber, | ||
data: { ...data, column: (fromColumn + toColumn) / 2 }, | ||
} | ||
}, | ||
Block: ({ annotation, children }) => { | ||
const { column } = annotation.data | ||
return ( | ||
<> | ||
{children} | ||
<div | ||
style={{ minWidth: `${column + 4}ch` }} | ||
className="relative -ml-[1ch] mt-1 w-fit whitespace-break-spaces rounded border border-current bg-zinc-800 px-2" | ||
> | ||
<div | ||
style={{ left: `${column}ch` }} | ||
className="absolute -top-[1px] h-2 w-2 -translate-y-1/2 rotate-45 border-l border-t border-current bg-zinc-800" | ||
/> | ||
{annotation.query} | ||
</div> | ||
</> | ||
) | ||
}, | ||
} |
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,16 @@ | ||
import React from 'react' | ||
import { highlight, Pre, RawCode } from 'codehike/code' | ||
|
||
import { callout, diff, focus, fold, link, mark } from './handlers' | ||
|
||
export async function Code({ codeblock }: { codeblock: RawCode }) { | ||
const highlighted = await highlight(codeblock, 'github-dark') | ||
return ( | ||
<Pre | ||
code={highlighted} | ||
className="bg-background text-xs sm:text-sm" | ||
style={{ ...highlighted.style, padding: '1rem', borderRadius: '0.5rem' }} | ||
handlers={[callout, fold, mark, diff, focus, link]} | ||
/> | ||
) | ||
} |
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,19 @@ | ||
import React from 'react' | ||
import { AnnotationHandler, BlockAnnotation, InnerLine } from 'codehike/code' | ||
|
||
export const diff: AnnotationHandler = { | ||
name: 'diff', | ||
onlyIfAnnotated: true, | ||
transform: (annotation: BlockAnnotation) => { | ||
const color = annotation.query == '-' ? '#f85149' : '#3fb950' | ||
return [annotation, { ...annotation, name: 'mark', query: color }] | ||
}, | ||
Line: ({ annotation, ...props }) => ( | ||
<> | ||
<div className="box-content min-w-[1ch] select-none pl-2 opacity-70"> | ||
{annotation?.query} | ||
</div> | ||
<InnerLine merge={props} /> | ||
</> | ||
), | ||
} |
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,41 @@ | ||
'use client' | ||
|
||
import React, { useLayoutEffect, useRef } from 'react' | ||
import { AnnotationHandler, getPreRef, InnerPre } from 'codehike/code' | ||
|
||
export const PreWithFocus: AnnotationHandler['PreWithRef'] = (props) => { | ||
const ref = getPreRef(props) | ||
useScrollToFocus(ref) | ||
return <InnerPre merge={props} /> | ||
} | ||
|
||
function useScrollToFocus(ref: React.RefObject<HTMLPreElement>) { | ||
const firstRender = useRef(true) | ||
useLayoutEffect(() => { | ||
if (ref.current) { | ||
// find all descendants whith data-focus="true" | ||
const focusedElements = ref.current.querySelectorAll( | ||
'[data-focus=true]', | ||
) as NodeListOf<HTMLElement> | ||
|
||
// find top and bottom of the focused elements | ||
const containerRect = ref.current.getBoundingClientRect() | ||
let top = Infinity | ||
let bottom = -Infinity | ||
focusedElements.forEach((el) => { | ||
const rect = el.getBoundingClientRect() | ||
top = Math.min(top, rect.top - containerRect.top) | ||
bottom = Math.max(bottom, rect.bottom - containerRect.top) | ||
}) | ||
|
||
// scroll to the focused elements if any part of them is not visible | ||
if (bottom > containerRect.height || top < 0) { | ||
ref.current.scrollTo({ | ||
top: ref.current.scrollTop + top - 10, | ||
behavior: firstRender.current ? 'instant' : 'smooth', | ||
}) | ||
} | ||
firstRender.current = false | ||
} | ||
}) | ||
} |
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,19 @@ | ||
import React from 'react' | ||
import { AnnotationHandler, InnerLine } from 'codehike/code' | ||
|
||
import { PreWithFocus } from './focus.client' | ||
|
||
export const focus: AnnotationHandler = { | ||
name: 'focus', | ||
onlyIfAnnotated: true, | ||
PreWithRef: PreWithFocus, | ||
Line: (props) => ( | ||
<InnerLine | ||
merge={props} | ||
className="px-2 opacity-50 data-[focus]:opacity-100" | ||
/> | ||
), | ||
AnnotatedLine: ({ annotation, ...props }) => ( | ||
<InnerLine merge={props} data-focus={true} className="bg-zinc-700/30" /> | ||
), | ||
} |
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,21 @@ | ||
'use client' | ||
|
||
import React, { useState } from 'react' | ||
import { AnnotationHandler } from 'codehike/code' | ||
|
||
const InlineFold: AnnotationHandler['Inline'] = ({ children }) => { | ||
const [folded, setFolded] = useState(true) | ||
if (!folded) { | ||
return children | ||
} | ||
return ( | ||
<button onClick={() => setFolded(false)} aria-label="Expand"> | ||
... | ||
</button> | ||
) | ||
} | ||
|
||
export const fold: AnnotationHandler = { | ||
name: 'fold', | ||
Inline: InlineFold, | ||
} |
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,8 @@ | ||
import { callout } from './callout' | ||
import { diff } from './diff' | ||
import { focus } from './focus' | ||
import { fold } from './fold' | ||
import { link } from './link' | ||
import { mark } from './mark' | ||
|
||
export { callout, diff, fold, mark, focus, link } |
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,10 @@ | ||
import React from 'react' | ||
import { AnnotationHandler } from 'codehike/code' | ||
|
||
export const link: AnnotationHandler = { | ||
name: 'link', | ||
Inline: ({ annotation, children }) => { | ||
const { query } = annotation | ||
return <a href={query}>{children}</a> | ||
}, | ||
} |
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,35 @@ | ||
import React from 'react' | ||
import { AnnotationHandler, InnerLine } from 'codehike/code' | ||
|
||
export const mark: AnnotationHandler = { | ||
name: 'mark', | ||
Line: ({ annotation, ...props }) => { | ||
const color = annotation?.query || 'rgb(14 165 233)' | ||
return ( | ||
<div | ||
className="flex" | ||
style={{ | ||
borderLeft: 'solid 2px transparent', | ||
borderLeftColor: annotation && color, | ||
backgroundColor: annotation && `rgb(from ${color} r g b / 0.1)`, | ||
}} | ||
> | ||
<InnerLine merge={props} className="flex-1 px-2" /> | ||
</div> | ||
) | ||
}, | ||
Inline: ({ annotation, children }) => { | ||
const color = annotation?.query || 'rgb(14 165 233)' | ||
return ( | ||
<span | ||
className="-mx-0.5 rounded px-0.5 py-0" | ||
style={{ | ||
outline: `solid 1px rgb(from ${color} r g b / 0.5)`, | ||
background: `rgb(from ${color} r g b / 0.13)`, | ||
}} | ||
> | ||
{children} | ||
</span> | ||
) | ||
}, | ||
} |
Oops, something went wrong.