Skip to content

Commit

Permalink
refactor: fix component types, use defineModel
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Jun 25, 2023
1 parent a445c93 commit 434d64a
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 46 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"@rollup/plugin-replace": "^5.0.2",
"@types/codemirror": "^5.60.2",
"@types/node": "^18.15.3",
"@vitejs/plugin-vue": "^4.1.0",
"@vitejs/plugin-vue": "^4.2.3",
"@volar/monaco": "1.7.8",
"@volar/typescript": "1.7.8",
"@vue/language-service": "1.8.1",
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 4 additions & 12 deletions src/editor/CodeMirrorEditor.vue
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
<script setup lang="ts">
import CodeMirror, { type Props } from '../codemirror/CodeMirror.vue'
import { computed } from 'vue'
import type { PreviewMode } from './types'
import type { EditorEmits, EditorProps } from './types'
defineOptions({
editorType: 'codemirror',
})
const props = defineProps<{
value: string
filename: string
readonly?: boolean
mode?: PreviewMode
}>()
const emits = defineEmits<{
(e: 'change', code: string): void
}>()
const props = defineProps<EditorProps>()
const emit = defineEmits<EditorEmits>()
const onChange = (code: string) => {
emits('change', code)
emit('change', code)
}
const modes: Record<string, Props['mode']> = {
Expand Down
17 changes: 4 additions & 13 deletions src/editor/MessageToggle.vue
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
<script setup lang="ts">
withDefaults(
defineProps<{
modelValue?: boolean
}>(),
{
modelValue: false,
}
)
defineEmits<{
(e: 'update:modelValue', value: boolean): void
}>()
const visible = defineModel<boolean>()
</script>

<template>
<div class="wrapper" @click="$emit('update:modelValue', !modelValue)">
<div class="wrapper" @click="visible = !visible">
<span>Show Error</span>
<div class="toggle" :class="[{ active: modelValue }]">
<div class="indicator" />
</div>
</div>
</template>

<style scoped>
.wrapper {
position: absolute;
Expand Down
14 changes: 3 additions & 11 deletions src/editor/MonacoEditor.vue
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
<script setup lang="ts">
import Monaco from '../monaco/Monaco.vue'
import type { PreviewMode } from './types'
import type { EditorEmits, EditorProps } from './types'
defineProps<{
value: string
filename: string
readonly?: boolean
mode?: PreviewMode
}>()
const emit = defineEmits<{
(e: 'change', code: string): void
}>()
defineProps<EditorProps>()
const emit = defineEmits<EditorEmits>()
defineOptions({
editorType: 'monaco',
Expand Down
11 changes: 7 additions & 4 deletions src/editor/types.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { Component } from 'vue'
import { type FunctionalComponent } from 'vue'

export type PreviewMode = 'js' | 'css' | 'ssr'

interface EditorProps {
export interface EditorProps {
value: string
filename: string
readonly?: boolean
mode?: PreviewMode
}

interface EditorEmits {
export interface EditorEmits {
(e: 'change', code: string): void
}

export type EditorComponentType = Component<EditorProps, EditorEmits> & {
export type EditorComponentType = FunctionalComponent<
EditorProps,
{ change: (code: string) => void }
> & {
editorType: 'monaco' | 'codemirror'
}

Expand Down
8 changes: 7 additions & 1 deletion vite.preview.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import vue from '@vitejs/plugin-vue'
import replace from '@rollup/plugin-replace'

export default defineConfig({
plugins: [vue()],
plugins: [
vue({
script: {
defineModel: true,
},
}),
],
resolve: {
alias: {
path: 'path-browserify',
Expand Down

0 comments on commit 434d64a

Please sign in to comment.