Skip to content

Commit

Permalink
feat: introduce ShikiMagicMoveCompiled component
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Feb 23, 2024
1 parent 49be745 commit bf4cab5
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
3 changes: 1 addition & 2 deletions src/vue/components/ShikiMagicMove.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { HighlighterCore } from 'shiki/core'
import type { PropType } from 'vue'
import { computed, defineComponent, h } from 'vue'
import { codeToKeyedTokens, createMagicMoveMachine, syncTokenKeys, toKeyedTokens } from '../../core'
import { codeToKeyedTokens, createMagicMoveMachine } from '../../core'
import type { AnimationOptions } from '../types'
import { TokensRenderer } from './TokensRenderer'

Expand Down Expand Up @@ -36,7 +36,6 @@ export const ShikiMagicMove = /* #__PURE__ */ defineComponent({
theme: props.theme,
}),
)

const tokens = computed(() => machine.commit(props.code))

return () => h(TokensRenderer, {
Expand Down
35 changes: 35 additions & 0 deletions src/vue/components/ShikiMagicMoveCompiled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { PropType } from 'vue'
import { computed, defineComponent, h } from 'vue'
import type { KeyedTokensInfo } from '../../core'
import type { AnimationOptions } from '..'
import { TokensRenderer } from './TokensRenderer'

/**
* Component to render a compiled magic move step,
* Where the tokens can be generated on build time.
*/
export const ShikiMagicMoveCompiled = /* #__PURE__ */ defineComponent({
name: 'ShikiMagicMoveCompiled',
props: {
steps: {
type: Array as PropType<KeyedTokensInfo[]>,
required: true,
},
step: {
type: Number,
default: 0,
},
animation: {
type: Object as PropType<AnimationOptions>,
default: () => ({}),
},
},
setup(props) {
const current = computed(() => props.steps[props.step])

return () => h(TokensRenderer, {
tokens: current.value,
animation: props.animation,
})
},
})
6 changes: 5 additions & 1 deletion src/vue/components/TokensRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import type { PropType } from 'vue'
import { TransitionGroup, computed, defineComponent, h, nextTick, onMounted, reactive, ref, renderList } from 'vue'
import { TransitionGroup, computed, defineComponent, h, nextTick, onMounted, ref, renderList } from 'vue'
import type { KeyedTokensInfo } from '../../core'
import type { AnimationOptions } from '../types'

/**
* Component to render a compiled tokens and animate them.
* This is a low-level component.
*/
export const TokensRenderer = /* #__PURE__ */ defineComponent({
name: 'TokensRenderer',
props: {
Expand Down
2 changes: 1 addition & 1 deletion src/vue/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import type { App } from 'vue'
import { ShikiMagicMove } from './components/ShikiMagicMove'

export * from './types'
export * from '../core'

export { ShikiMagicMove }
export { ShikiMagicMoveCompiled } from './components/ShikiMagicMoveCompiled'
export { TokensRenderer } from './components/TokensRenderer'

export function install(app: App<any>) {
Expand Down

0 comments on commit bf4cab5

Please sign in to comment.