move code editor to separate component
This commit is contained in:
76
src/components/Code.vue
Normal file
76
src/components/Code.vue
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
<script>
|
||||||
|
import { defineComponent, shallowRef } from 'vue';
|
||||||
|
|
||||||
|
import { Codemirror } from 'vue-codemirror'
|
||||||
|
import { javascript } from '@codemirror/lang-javascript'
|
||||||
|
import { oneDark } from '@codemirror/theme-one-dark'
|
||||||
|
|
||||||
|
export default defineComponent({
|
||||||
|
name: 'Code',
|
||||||
|
props: [
|
||||||
|
'code',
|
||||||
|
],
|
||||||
|
components: {
|
||||||
|
Codemirror,
|
||||||
|
},
|
||||||
|
data() {
|
||||||
|
const extensions = [javascript(), oneDark]
|
||||||
|
|
||||||
|
// Codemirror EditorView instance ref
|
||||||
|
const view = shallowRef();
|
||||||
|
const handleReady = payload => {
|
||||||
|
view.value = payload.view;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Status is available at all times via Codemirror EditorView
|
||||||
|
const getCodemirrorStates = () => {
|
||||||
|
const state = view.value.state;
|
||||||
|
const ranges = state.selection.ranges;
|
||||||
|
const selected = ranges.reduce((r, range) => r + range.to - range.from, 0);
|
||||||
|
const cursor = ranges[0].anchor;
|
||||||
|
const length = state.doc.length;
|
||||||
|
const lines = state.doc.lines;
|
||||||
|
// more state info ...
|
||||||
|
// return ...
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
extensions,
|
||||||
|
handleReady,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.code {
|
||||||
|
height: 100%;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
<style>
|
||||||
|
.p-splitter {
|
||||||
|
border: none !important;
|
||||||
|
}
|
||||||
|
.p-splitter-gutter {
|
||||||
|
outline-width: 0;
|
||||||
|
}
|
||||||
|
.p-splitter-gutter-handle:focus {
|
||||||
|
box-shadow: none !important;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<div class="code">
|
||||||
|
<codemirror
|
||||||
|
v-model="code.value"
|
||||||
|
placeholder="Code goes here..."
|
||||||
|
:style="{ height: '100%' }"
|
||||||
|
:autofocus="true"
|
||||||
|
:indent-with-tab="true"
|
||||||
|
:tab-size="2"
|
||||||
|
:extensions="extensions"
|
||||||
|
@ready="handleReady"
|
||||||
|
@change="$emit('change', $event)"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
<script>
|
<script>
|
||||||
|
|
||||||
import Result from '../components/Result.vue';
|
import Result from '../components/Result.vue';
|
||||||
|
import Code from '../components/Code.vue';
|
||||||
|
|
||||||
import Worker from '../file.worker.js?worker';
|
import Worker from '../file.worker.js?worker';
|
||||||
// const worker = new Worker();
|
|
||||||
|
|
||||||
// const lodash = await fetch('https://registry.npmjs.org/lodash');
|
// const lodash = await fetch('https://registry.npmjs.org/lodash');
|
||||||
|
|
||||||
@@ -11,15 +11,11 @@ import Splitter from 'primevue/splitter';
|
|||||||
import SplitterPanel from 'primevue/splitterpanel';
|
import SplitterPanel from 'primevue/splitterpanel';
|
||||||
|
|
||||||
import { defineComponent, shallowRef } from 'vue';
|
import { defineComponent, shallowRef } from 'vue';
|
||||||
import { Codemirror } from 'vue-codemirror'
|
|
||||||
import { javascript } from '@codemirror/lang-javascript'
|
|
||||||
import { oneDark } from '@codemirror/theme-one-dark'
|
|
||||||
|
|
||||||
export default defineComponent({
|
export default defineComponent({
|
||||||
components: {
|
components: {
|
||||||
Codemirror,
|
|
||||||
Splitter, SplitterPanel,
|
Splitter, SplitterPanel,
|
||||||
Result,
|
Result, Code,
|
||||||
},
|
},
|
||||||
data() {
|
data() {
|
||||||
const code = `
|
const code = `
|
||||||
@@ -34,37 +30,14 @@ for (let i = 0; i < 42; ++i) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
`;
|
`;
|
||||||
const extensions = [javascript(), oneDark]
|
|
||||||
|
|
||||||
// Codemirror EditorView instance ref
|
|
||||||
const view = shallowRef()
|
|
||||||
const handleReady = (payload) => {
|
|
||||||
view.value = payload.view
|
|
||||||
}
|
|
||||||
|
|
||||||
// Status is available at all times via Codemirror EditorView
|
|
||||||
const getCodemirrorStates = () => {
|
|
||||||
const state = view.value.state
|
|
||||||
const ranges = state.selection.ranges
|
|
||||||
const selected = ranges.reduce((r, range) => r + range.to - range.from, 0)
|
|
||||||
const cursor = ranges[0].anchor
|
|
||||||
const length = state.doc.length
|
|
||||||
const lines = state.doc.lines
|
|
||||||
// more state info ...
|
|
||||||
// return ...
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
code,
|
code: { value: code },
|
||||||
extensions,
|
|
||||||
handleReady,
|
|
||||||
log: _ => _, // console.log,
|
|
||||||
worker: null,
|
worker: null,
|
||||||
result: '',
|
result: '',
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
mounted() {
|
mounted() {
|
||||||
this.run(this.code);
|
this.run(this.code.value);
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
run(code) {
|
run(code) {
|
||||||
@@ -109,49 +82,13 @@ main {
|
|||||||
overflow-y: auto;
|
overflow-y: auto;
|
||||||
outline-width: 0;
|
outline-width: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
.result {
|
|
||||||
overflow-x: auto;
|
|
||||||
overflow-y: auto;
|
|
||||||
margin: 0;
|
|
||||||
padding: 8px;
|
|
||||||
height: 100%;
|
|
||||||
display: block;
|
|
||||||
font-family: monospace;
|
|
||||||
white-space: pre;
|
|
||||||
line-height: normal;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
|
|
||||||
<style>
|
|
||||||
.p-splitter {
|
|
||||||
border: none !important;
|
|
||||||
}
|
|
||||||
.p-splitter-gutter {
|
|
||||||
outline-width: 0;
|
|
||||||
}
|
|
||||||
.p-splitter-gutter-handle:focus {
|
|
||||||
box-shadow: none !important;
|
|
||||||
}
|
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<main>
|
<main>
|
||||||
<Splitter style="height: 100%" :step="50" :gutterSize="8" layout="horizontal">
|
<Splitter style="height: 100%" :step="50" :gutterSize="8" layout="horizontal">
|
||||||
<SplitterPanel class="left-pane">
|
<SplitterPanel class="left-pane">
|
||||||
<codemirror
|
<Code :code="code" @change="run($event)"/>
|
||||||
v-model="code"
|
|
||||||
placeholder="Code goes here..."
|
|
||||||
:style="{ height: '100%' }"
|
|
||||||
:autofocus="true"
|
|
||||||
:indent-with-tab="true"
|
|
||||||
:tab-size="2"
|
|
||||||
:extensions="extensions"
|
|
||||||
@ready="handleReady"
|
|
||||||
@change="run($event)"
|
|
||||||
@focus="log('focus', $event)"
|
|
||||||
@blur="log('blur', $event)"
|
|
||||||
/>
|
|
||||||
</SplitterPanel>
|
</SplitterPanel>
|
||||||
<SplitterPanel class="right-pane">
|
<SplitterPanel class="right-pane">
|
||||||
<Result :data="result"/>
|
<Result :data="result"/>
|
||||||
|
|||||||
Reference in New Issue
Block a user