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

feat: react renderer #3

Merged
merged 16 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

// Auto fix
"editor.codeActionsOnSave": {
"source.fixAll": "explicit",
"source.fixAll.eslint": "explicit",
"source.organizeImports": "never"
},

Expand Down
1 change: 1 addition & 0 deletions build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export default defineBuildConfig({
entries: [
'src/index',
'src/vue',
'src/react',
'src/core',
'src/types',
'src/renderer',
Expand Down
1 change: 1 addition & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default antfu(
ignores: [
// eslint ignore globs here
],
react: true,
},
{
rules: {
Expand Down
17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
"./vue": {
"import": "./dist/vue.mjs"
},
"./react": {
"import": "./dist/react.mjs"
},
"./style.css": "./dist/style.css",
"./dist/*": "./dist/*"
},
Expand All @@ -45,6 +48,9 @@
"./vue": [
"./dist/vue.d.ts"
],
"./react": [
"./dist/react.d.ts"
],
"./core": [
"./dist/core.d.ts"
],
Expand All @@ -71,10 +77,14 @@
"play:build": "nr -C playground build"
},
"peerDependencies": {
"react": "^18.2.0",
hyoban marked this conversation as resolved.
Show resolved Hide resolved
"shiki": "^1.1.6",
"vue": "^3.4.0"
},
"peerDependenciesMeta": {
"react": {
"optional": true
},
"shiki": {
"optional": true
},
Expand All @@ -92,13 +102,20 @@
"@antfu/utils": "^0.7.7",
"@types/diff-match-patch": "^1.0.36",
"@types/node": "^20.11.20",
"@types/react": "^18.2.56",
"@types/react-dom": "^18.2.19",
"@unocss/reset": "^0.58.5",
"@vueuse/core": "^10.9.0",
"bumpp": "^9.3.0",
"eslint": "^8.57.0",
"eslint-plugin-react": "^7.34.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.5",
"esno": "^4.0.0",
"lint-staged": "^15.2.2",
"pnpm": "^8.15.4",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"rimraf": "^5.0.5",
"shiki": "^1.1.7",
"simple-git-hooks": "^2.9.0",
Expand Down
10 changes: 8 additions & 2 deletions playground/src/Playground.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import { bundledLanguagesInfo } from 'shiki/langs'
import { ref, shallowRef, watch, watchEffect } from 'vue'
import { toRefs, useLocalStorage } from '@vueuse/core'
import { vueAfter, vueBefore } from './fixture'
import type { RendererFactoryOptions, RendererFactoryResult, RendererType, RendererUpdatePayload } from './renderer/types'
import { createRendererVue } from './renderer/vue'
import { createRendererReact } from './renderer/react'
import type { RendererFactoryOptions, RendererFactoryResult, RendererType, RendererUpdatePayload } from './renderer/types'

const defaultOptions = {
theme: 'vitesse-dark',
Expand Down Expand Up @@ -89,7 +90,9 @@ function rendererUpdate() {
}

if (!renderer) {
renderer = createRendererVue(rendererOptions)
renderer = rendererType.value === 'vue'
? createRendererVue(rendererOptions)
: createRendererReact(rendererOptions)
renderer.mount(rendererContainer.value, payload)
}
else {
Expand Down Expand Up @@ -283,6 +286,9 @@ watch(
<option value="vue">
Vue Renderer
</option>
<option value="react">
React Renderer
</option>
</select>
</div>
<div ref="rendererContainer" class="of-auto" />
Expand Down
56 changes: 56 additions & 0 deletions playground/src/renderer/react.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* eslint-disable no-console */
import React from 'react'
import type { Root } from 'react-dom/client'
import ReactDOM from 'react-dom/client'
import { shallowReactive, watch } from 'vue'
import { ShikiMagicMove } from '../../../src/react'
import type { RendererFactory, RendererFactoryResult } from './types'

export const createRendererReact: RendererFactory = (options): RendererFactoryResult => {
let app: Root | undefined

const props = shallowReactive<any>({
onStart: options.onStart,
onEnd: options.onEnd,
})

function App() {
// TODO: make React not render twice
const [count, setCounter] = React.useState(0)

React.useEffect(() => {
watch(props, () => {
// Force React to re-render
setCounter(c => c + 1)
})
}, [])

console.log('React rendering', count)

// eslint-disable-next-line react/prop-types
return <ShikiMagicMove {...props} className={props.class} />
}

return {
mount: (element, payload) => {
Object.assign(props, payload)
app = ReactDOM.createRoot(element)
app.render(
<React.StrictMode>
<App />
</React.StrictMode>,
)

console.log('React renderer mounted')
},

update: (payload) => {
Object.assign(props, payload)
},

dispose: () => {
app?.unmount()
app = undefined
},
}
}
2 changes: 1 addition & 1 deletion playground/src/renderer/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { HighlighterCore } from 'shiki/core'
import type { MagicMoveRenderOptions } from '../../../src/core'

export type RendererType = 'vue'
export type RendererType = 'vue' | 'react'

export interface RendererUpdatePayload {
highlighter: HighlighterCore
Expand Down
Loading
Loading