Skip to content

Commit

Permalink
feat: output code block should be readonly (#267)
Browse files Browse the repository at this point in the history
* feat: output code block should be readonly

* chore: fmt
  • Loading branch information
houyunlu authored Nov 14, 2023
1 parent 4430a89 commit 629fd35
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
1 change: 1 addition & 0 deletions web/playground/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ const handleToggleIsEntry = (item: any) => {
v-for="item in outputs"
:code="item.code"
:title="item.title"
:readonly="true"
@code="item.code = $event"
@title="item.title = $event.target.innerText"
/>
Expand Down
22 changes: 10 additions & 12 deletions web/playground/src/components/CodeBlock.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { ShallowRef, defineComponent, ref, shallowRef } from 'vue'
import { ShallowRef, shallowRef } from 'vue'
import { Codemirror } from 'vue-codemirror'
import { javascript } from '@codemirror/lang-javascript'
import { oneDark } from '@codemirror/theme-one-dark'
Expand All @@ -12,14 +12,19 @@ type Payload = {
container: HTMLElement
}
defineProps({
let props = defineProps({
code: String,
readonly: {
type: Boolean,
required: false,
},
})
const emit = defineEmits(['code'])
const extensions = [javascript(), oneDark]
if (props.readonly) {
extensions.push(EditorView.editable.of(false))
}
// Codemirror EditorView instance ref
const view = <ShallowRef<EditorView>>shallowRef()
const handleReady = (payload: Payload) => {
Expand All @@ -43,11 +48,6 @@ const handleReady = (payload: Payload) => {
// lines
// }
// }
const handleCodeChange = (e: string) => {
emit('code', e)
}
const log = console.log
</script>
<template>
<codemirror
Expand All @@ -59,8 +59,6 @@ const log = console.log
:tab-size="2"
:extensions="extensions"
@ready="handleReady"
@change="handleCodeChange"
@focus="log('focus', $event)"
@blur="console.log('blur', $event)"
@change="$emit('code', $event)"
/>
</template>
10 changes: 9 additions & 1 deletion web/playground/src/components/ModuleBlock.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ const props = defineProps({
type: Boolean,
required: false,
},
readonly: {
type: Boolean,
required: false,
},
})
const input = ref(null)
Expand Down Expand Up @@ -66,7 +70,11 @@ onMounted(() => {
</button>
</div>

<CodeBlock :code="code" @code="$emit('code', $event)" />
<CodeBlock
:code="code"
@code="$emit('code', $event)"
:readonly="readonly"
/>
</div>
</template>

Expand Down

0 comments on commit 629fd35

Please sign in to comment.