Skip to content

Commit 22113ff

Browse files
committed
feat: improved mode loading
Now loads the appropriate mode JS, when you set the mode. Updates the README with info about setting CodeMirror options, and about the mode option in particular. Updates stories to match.
1 parent 21b5ee1 commit 22113ff

File tree

3 files changed

+57
-15
lines changed

3 files changed

+57
-15
lines changed

README.md

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
![Preview](https://github.com/gwenaelp/vfg-field-sourcecode/blob/master/docs/preview.png)
1717

18-
```
18+
```vue
1919
<template>
2020
<vue-form-generator :schema="schema" :model="model"></vue-form-generator>
2121
</template>
@@ -30,7 +30,12 @@ export default {
3030
fields: [{
3131
type: "sourcecode",
3232
label: "source code",
33-
model: "source"
33+
model: "source",
34+
theme: 'eclipse',
35+
mode: {
36+
name: "javascript",
37+
json: true
38+
}
3439
}]
3540
}
3641
};
@@ -48,7 +53,7 @@ vfg-field-sourcecode can be used as a module in both CommonJS and ES modular env
4853
When in non-modular environment, vfg-field-sourcecode will register all the components to vue by itself.</p>
4954

5055
### ES6
51-
```js
56+
```vue
5257
//
5358
// You can register a component manually
5459
//
@@ -72,7 +77,7 @@ Vue.use(ModuleLibrary);
7277
```
7378

7479
### CommonJS
75-
```js
80+
```vue
7681
//
7782
// You can register a component manually
7883
//
@@ -107,18 +112,28 @@ Vue.use(ModuleLibrary);
107112

108113
### After that, you can use it with Vue Form Generator:
109114

110-
```json
115+
```js
111116
schema: {
112117
fields: [
113118
{
114119
type: "sourcecode",
115120
label: "source code",
116-
model: "source"
121+
model: "source",
122+
theme: 'monokai',
123+
mode: "ruby"
117124
}
118125
]
119126
}
120127
```
121128

129+
## CodeMirror Options
130+
131+
Any supported CodeMirror option can be passed in the `schema`. Available options are shown [here](http://codemirror.net/doc/manual.html#config).
132+
133+
### Special note about `mode` option
134+
135+
When setting the `mode` option in the schema, `vfg-field-sourcecode` will automatically load the JS for the specified mode. This currently only works if you set the mode by name, *not* by mime type. Mode can either by set as as string, eg: `mode: python`, or as an object, which is required if that mode supports options you want to set - eg: `mode: { name: 'javascript', json: true }`. See [here](http://codemirror.net/doc/manual.html#option_mode) for more details.
136+
122137
## Changelog
123138

124139
See the GitHub [release history](https://github.com/gwenaelp/vfg-field-sourcecode/releases).

src/components/field-sourcecode.vue

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,38 @@ export default {
2323
// http://codemirror.net/doc/manual.html#config
2424
2525
if (this.schema.hasOwnProperty("codeMirrorOptions")) {
26+
const cmOptions = this.schema.codeMirrorOptions;
27+
2628
// Load theme css, if specified in codeMirrorOptions
27-
if (this.schema.codeMirrorOptions.hasOwnProperty("theme")) {
28-
require("codemirror/theme/" +
29-
this.schema.codeMirrorOptions.theme +
30-
".css");
29+
if (cmOptions.hasOwnProperty("theme")) {
30+
try {
31+
require("codemirror/theme/" + cmOptions.theme + ".css");
32+
} catch (e) {
33+
console.log({ e });
34+
}
35+
}
36+
37+
if (cmOptions.hasOwnProperty("mode")) {
38+
let modeName = "";
39+
40+
if (
41+
typeof cmOptions.mode === "object" &&
42+
cmOptions.mode.hasOwnProperty("name")
43+
) {
44+
modeName = cmOptions.mode.name;
45+
} else if (typeof cmOptions.mode === "string") {
46+
modeName = cmOptions.mode;
47+
}
48+
49+
// Load the mode js, if specified in codeMirrorOptions
50+
try {
51+
require("codemirror/mode/" + modeName + "/" + modeName + ".js");
52+
} catch (e) {
53+
console.log({ e });
54+
}
3155
}
3256
33-
return this.schema.codeMirrorOptions;
57+
return cmOptions;
3458
} else {
3559
return {};
3660
}
@@ -51,7 +75,7 @@ export default {
5175
height: 120px;
5276
border: 1px solid #ccc;
5377
border-radius: 4px;
54-
box-shadow: inset 0 1px 1px rgba(0,0,0,.075);
78+
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
5579
font-size: 80%;
5680
}
5781
</style>

stories/index.stories.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ storiesOf("field-sourcecode", module).add("Story as a template", () => {
7272
label: "source code",
7373
model: "source",
7474
codeMirrorOptions: {
75-
mode: "text/x-vue"
75+
mode: "vue"
7676
}
7777
}
7878
]
@@ -98,7 +98,10 @@ storiesOf("field-sourcecode", module).add("Change mode to JSON", () => {
9898
label: "source code",
9999
model: "source",
100100
codeMirrorOptions: {
101-
mode: "application/json"
101+
mode: {
102+
name: "javascript",
103+
json: true
104+
}
102105
}
103106
}
104107
]
@@ -124,7 +127,7 @@ storiesOf("field-sourcecode", module).add("Use Monokai theme", () => {
124127
label: "source code",
125128
model: "source",
126129
codeMirrorOptions: {
127-
mode: "text/x-vue",
130+
mode: "vue",
128131
theme: "monokai"
129132
}
130133
}

0 commit comments

Comments
 (0)