Skip to content

Commit

Permalink
chore: update logic
Browse files Browse the repository at this point in the history
  • Loading branch information
zouhangwithsweet committed Feb 25, 2024
1 parent ec4f0a1 commit f54847d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 30 deletions.
31 changes: 1 addition & 30 deletions entrypoints/injected/components/Code.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { useAtom, useAtomValue } from 'jotai'
import { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { Clipboard } from 'react-feather'
import { getHighlighterCore } from 'shiki/core'
import getWasm from 'shiki/wasm'
import { toTailwindcss } from 'transform-to-tailwindcss-core'
import { toUnocssClass } from 'transform-to-unocss-core'
import { useCopyToClipboard } from 'usehooks-ts'
Expand All @@ -18,8 +16,6 @@ export const CodeArea = memo((props: { minimized?: boolean }) => {
const [, setCurrentSelection] = useAtom(currentSelection)
const [unoResult, setUnoResult] = useState<{ title: string; code: string }[]>()

const [highlighCode, setHighlightCode] = useState(' ')

const handleSelectionChange = useCallback(async () => {
const node = figma.currentPage?.selection?.[0]
setCurrentSelection(node ?? null)
Expand Down Expand Up @@ -78,25 +74,6 @@ export const CodeArea = memo((props: { minimized?: boolean }) => {
code: cssCode,
},
])

const highlighter = await getHighlighterCore({
themes: [
// or a dynamic import if you want to do chunk splitting
import('shiki/themes/vitesse-light.mjs'),
],
langs: [import('shiki/langs/css.mjs')],
loadWasm: getWasm,
})

// optionally, load themes and languages after creation
await highlighter.loadTheme(import('shiki/themes/vitesse-light.mjs'))

const code = highlighter.codeToHtml(`html{${cssCode}}`, {
lang: 'css',
theme: 'vitesse-light',
})

setHighlightCode(code)
}, [engine, isRem, setCurrentSelection])

useEffect(() => {
Expand Down Expand Up @@ -181,7 +158,7 @@ export const CodeArea = memo((props: { minimized?: boolean }) => {
ref={inputRef}
rows={1}
autoComplete="off"
className="hidden px-4 h-auto py-4 lh-4.5 bg-#f5f5f5 cursor-text font-['Roboto_Mono'] resize-none scrollbar-hide"
className="px-4 h-auto py-4 lh-4.5 bg-#f5f5f5 cursor-text font-['Roboto_Mono'] resize-none scrollbar-hide"
value={u.code}
readOnly
onSelect={(e) => {
Expand All @@ -191,12 +168,6 @@ export const CodeArea = memo((props: { minimized?: boolean }) => {
handleCopy(selection)()
}}
></textarea>
<div
className="px-4 h-auto py-4 lh-4.5 bg-#f5f5f5 cursor-text font-['Roboto_Mono'] scrollbar-hide [&_pre]:bg-transparent! [&_pre]:overflow-auto"
dangerouslySetInnerHTML={{
__html: highlighCode.replace('html', '').replace('{', '').replace('}', ''),
}}
></div>
</>
)}
</div>
Expand Down
39 changes: 39 additions & 0 deletions entrypoints/injected/components/HighLighter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { useEffect, useState } from 'react'
import { getHighlighterCore } from 'shiki/core'
import getWasm from 'shiki/wasm'

export const HighLighter = (props: { cssCode: string }) => {
const [highlighCode, setHighlightCode] = useState(' ')

useEffect(() => {
;(async () => {
const highlighter = await getHighlighterCore({
themes: [
// or a dynamic import if you want to do chunk splitting
import('shiki/themes/vitesse-light.mjs'),
],
langs: [import('shiki/langs/css.mjs')],
loadWasm: getWasm,
})

// optionally, load themes and languages after creation
await highlighter.loadTheme(import('shiki/themes/vitesse-light.mjs'))

const code = highlighter.codeToHtml(`html{${props.cssCode}}`, {
lang: 'css',
theme: 'vitesse-light',
})

setHighlightCode(code)
})()
})

return (
<div
className="px-4 h-auto py-4 lh-4.5 bg-#f5f5f5 cursor-text font-['Roboto_Mono'] scrollbar-hide [&_pre]:bg-transparent! [&_pre]:overflow-auto"
dangerouslySetInnerHTML={{
__html: highlighCode.replace('html', '').replace('{', '').replace('}', ''),
}}
></div>
)
}

0 comments on commit f54847d

Please sign in to comment.