Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use hidden shiki element so that we can interpolate at the right time #38

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 33 additions & 12 deletions playgrounds/app/src/components/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ export default function Editor(props: EditorProps) {
height: number
}>()
const [code, setCode] = createSignal(props.startCode)
const [hiddenCode, setHiddenCode] = createSignal(props.startCode)
const [isResizing, setIsResizing] = createSignal(false)
const [isLooping, setIsLooping] = createSignal(true)
const [isGenerating, setIsGenerating] = createSignal(false)
const [isGenerated, setIsGenerated] = createSignal(false)
const [gifDataUrl, setGifDataUrl] = createSignal('')
const [isShowingGifDialog, setIsShowingGifDialog] = createSignal(false)

Expand Down Expand Up @@ -134,8 +134,6 @@ export default function Editor(props: EditorProps) {
document.body.addEventListener('mousemove', e => {
if (isResizing()) {
const deltaX = e.movementX
console.log('mousemove')
// console.log(e.)
props.setSnippetWidth(props.snippetWidth + deltaX)
}
})
Expand Down Expand Up @@ -496,12 +494,14 @@ export default function Editor(props: EditorProps) {
<Button
onClick={async () => {
setIsGenerating(true)
setHiddenCode(props.endCode)
setTimeout(async () => {
const dataUrl = await generateGifDataUrl()()
setGifDataUrl(dataUrl)
setIsGenerating(false)
setIsShowingGifDialog(true)
}, 0)
setHiddenCode(props.startCode)
}, 1000)
}}
>
{isGenerating() ? 'Generating...' : 'Generate'}
Expand Down Expand Up @@ -551,16 +551,37 @@ export default function Editor(props: EditorProps) {
duration: 800,
stagger: 0,
lineNumbers: false,
onAnimationStart: async (elements, maxContainerDimensions) => {
if (elements.length === 0) {
return
}

setMagicMoveElements(elements)
setMaxContainerDimensions(maxContainerDimensions)
},
}}
/>
{/* The hidden shiki that we use to generate the magic move elements */}
<div
aria-hidden="true"
class=" absolute top-[-20000px] left-[-20000px]"
style={{
width: `${props.snippetWidth}px`,
}}
>
<ShikiMagicMove
lang={props.language}
theme={props.theme}
class="p-4 shadow-xl rounded select-none overflow-hidden !text-pretty"
highlighter={highlighter()}
code={hiddenCode()}
options={{
duration: 800,
stagger: 0,
lineNumbers: false,
onAnimationStart: async (elements, maxContainerDimensions) => {
if (elements.length === 0) {
return
}

setMagicMoveElements(elements)
setMaxContainerDimensions(maxContainerDimensions)
},
}}
/>
</div>
</div>
<div
class={clsx(
Expand Down
Loading