Skip to content

Commit e9d396d

Browse files
authored
feat: export preview and editor (#29)
1 parent ab26905 commit e9d396d

File tree

6 files changed

+121
-20
lines changed

6 files changed

+121
-20
lines changed

demo/App.vue

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,17 @@
5454
href="https://fonts.googleapis.com/css?family=Roboto+Mono"
5555
rel="stylesheet"
5656
/>
57+
<h2>Separate use Editor and Preview with change event</h2>
58+
<div class="separate">
59+
<div class="preview-separate">
60+
<VueLivePreview :code="separateCode" />
61+
</div>
62+
<VueLiveEditor :code="separateCode" @change="updateCode" />
63+
</div>
5764
</main>
5865
</template>
5966
<script>
60-
import VueLive from "../src/VueLive";
67+
import { VueLive, VueLiveEditor, VueLivePreview } from '../src';
6168
import CustomLayout from "./CustomLayout";
6269
import DatePicker from "vuejs-datepicker";
6370
import codeSfc from "!!raw-loader!./assets/Button.vue";
@@ -72,7 +79,7 @@ import "vue-prism-editor/dist/VuePrismEditor.css";
7279
7380
export default {
7481
name: "VueLiveDemo",
75-
components: { VueLive },
82+
components: { VueLive, VueLiveEditor, VueLivePreview },
7683
data() {
7784
return {
7885
registeredComponents: { DatePicker },
@@ -82,8 +89,14 @@ export default {
8289
codeChicago,
8390
CustomLayout,
8491
chicagoRequires: { "./chicagoNeighbourhoods": all },
85-
realjsx
92+
realjsx,
93+
separateCode: codeSfc
8694
};
95+
},
96+
methods: {
97+
updateCode(code) {
98+
this.separateCode = code;
99+
}
87100
}
88101
};
89102
</script>
@@ -97,4 +110,18 @@ body {
97110
body .prism-editor__line-numbers {
98111
box-sizing: border-box;
99112
}
113+
114+
.separate {
115+
display: flex;
116+
flex-direction: column;
117+
width: 950px;
118+
margin: 30px auto;
119+
}
120+
121+
.preview-separate {
122+
padding: 30px;
123+
background-color: #fff;
124+
text-align: center;
125+
border-radius: 0 10px 10px 0;
126+
}
100127
</style>

src/Editor.vue

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<template>
2+
<PrismEditor
3+
v-model="stableCode"
4+
:language="prismLang"
5+
@change="updatePreview"
6+
v-bind="editorProps"
7+
/>
8+
</template>
9+
10+
<script>
11+
//load prism somewhere
12+
import "prismjs";
13+
import "prismjs/components/prism-jsx.min";
14+
15+
import PrismEditor from "vue-prism-editor";
16+
import debounce from "debounce";
17+
18+
const UPDATE_DELAY = 300;
19+
20+
export default {
21+
name: 'VueLiveEditor',
22+
components: { PrismEditor },
23+
props: {
24+
code: {
25+
type: String,
26+
required: true
27+
},
28+
delay: {
29+
type: Number,
30+
default: UPDATE_DELAY
31+
},
32+
editorProps: {
33+
type: Object,
34+
default: () => ({})
35+
},
36+
prismLang: {
37+
type: String,
38+
default: 'html'
39+
}
40+
},
41+
data() {
42+
return {
43+
updatePreview: () => {},
44+
/**
45+
* this data only gets changed when changing language.
46+
* it allows for copy and pasting without having the code
47+
* editor repainted every keystroke
48+
*/
49+
stableCode: this.code
50+
}
51+
},
52+
watch: {
53+
code(value) {
54+
this.updatePreview(value);
55+
}
56+
},
57+
created() {
58+
this.updatePreview = debounce(value => {
59+
this.stableCode = value;
60+
61+
this.$emit('change', value);
62+
}, this.delay)
63+
}
64+
}
65+
</script>

src/Preview.vue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ export default {
6161
created() {
6262
this.renderComponent(this.code.trim());
6363
},
64+
watch: {
65+
code(value) {
66+
this.renderComponent(value.trim());
67+
}
68+
},
6469
methods: {
6570
/**
6671
* Generates the Scope Id attribute value. It will be added to each

src/VueLive.vue

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
:components="components"
1010
>
1111
<template v-slot:editor>
12-
<PrismEditor
13-
v-model="stableCode"
12+
<Editor
13+
:code="stableCode"
14+
:delay="delay"
15+
:prism-lang="prismLang"
16+
:editor-props="editorProps"
1417
@change="updatePreview"
15-
:language="prismLang"
16-
v-bind="editorProps"
1718
/>
1819
</template>
1920
<template v-slot:preview>
@@ -29,15 +30,10 @@
2930
</component>
3031
</template>
3132
<script>
32-
//load prism somewhere
33-
import "prismjs";
34-
import "prismjs/components/prism-jsx.min";
35-
36-
import PrismEditor from "vue-prism-editor";
3733
import hash from "hash-sum";
38-
import debounce from "debounce";
3934
4035
import Preview from "./Preview.vue";
36+
import Editor from './Editor.vue';
4137
import VueLiveDefaultLayout from "./VueLiveDefaultLayout.vue";
4238
4339
const LANG_TO_PRISM = {
@@ -49,7 +45,7 @@ const UPDATE_DELAY = 300;
4945
5046
export default {
5147
name: "VueLivePreview",
52-
components: { PrismEditor, Preview },
48+
components: { Preview, Editor },
5349
props: {
5450
/**
5551
* code rendered in the preview and the editor
@@ -122,7 +118,6 @@ export default {
122118
lang: "vue",
123119
prismLang: "html",
124120
VueLiveDefaultLayout,
125-
updatePreview: () => {},
126121
/**
127122
* this data only gets changed when changing language.
128123
* it allows for copy and pasting without having the code
@@ -131,11 +126,6 @@ export default {
131126
stableCode: this.code
132127
};
133128
},
134-
created() {
135-
this.updatePreview = debounce(value => {
136-
this.model = value;
137-
}, this.delay);
138-
},
139129
computed: {
140130
codeKey() {
141131
return hash(this.model);
@@ -148,6 +138,12 @@ export default {
148138
}
149139
},
150140
methods: {
141+
updatePreview(code) {
142+
this.stableCode = code;
143+
this.model = code;
144+
145+
this.$emit('change', code);
146+
},
151147
switchLanguage(newLang) {
152148
this.lang = newLang;
153149
const newPrismLang = LANG_TO_PRISM[newLang];

src/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import VueLive from './VueLive';
2+
import VueLiveEditor from './Editor';
3+
import VueLivePreview from './Preview';
4+
5+
export { VueLive, VueLiveEditor, VueLivePreview };

src/main.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import VueLive from "./VueLive.vue";
22
import VueLivePreview from "./Preview.vue";
3+
import VueLiveEditor from './Editor.vue';
34

45
// Export components individually
56
export { VueLive };
67
export { VueLivePreview };
8+
export { VueLiveEditor };
79

810
// What should happen if the user installs the library as a plugin
911
function install(Vue) {
1012
Vue.component("VueLive", VueLive);
1113
Vue.component("VueLivePreview", VueLivePreview);
14+
Vue.component("VueLiveEditor", VueLiveEditor);
1215
}
1316

1417
// Export the library as a plugin

0 commit comments

Comments
 (0)