Skip to content

Commit

Permalink
Add code form element
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrekalo committed Oct 18, 2018
1 parent ec3653b commit 9cd5219
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 1 deletion.
5 changes: 5 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"axios": "^0.18.0",
"blueimp-load-image": "^2.19.0",
"ckeditor": "^4.10.0",
"codemirror": "^5.40.2",
"dragula": "^3.7.2",
"dropzone": "^5.4.0",
"escape-html": "^1.0.3",
Expand Down
3 changes: 2 additions & 1 deletion src/js/appServices.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export default {
FileAttachmentFormElement: () => import('./formElements/fileAttachment'),
MediaPreviewFormElement: () => import('./formElements/mediaPreview'),
MediaFormElement: () => import('./formElements/media'),
NestedSelectFormElement: () => import('./formElements/nestedSelect')
NestedSelectFormElement: () => import('./formElements/nestedSelect'),
CodeFormElement: () => import('./formElements/code')

};
98 changes: 98 additions & 0 deletions src/js/formElements/code.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<template>
<element-wrapper v-bind="elementWrapperProps">
<div
ref="content"
v-once
v-bind="inputAttributes"
></div>
</element-wrapper>
</template>

<script>
import base from './base';
import ElementWrapper from 'cmf/js/formElements/elementWrapper';
import {assign} from 'cmf/js/library/toolkit';
import CodeMirror from 'codemirror';
import 'codemirror/mode/htmlmixed/htmlmixed.js';
import 'codemirror/lib/codemirror.css';
export default {
elementType: 'code',
components: {ElementWrapper},
mixins: [base],
props: {
value: {type: String, default: ''},
editorConfig: {type: Object}
},
data() {
return {
editorValue: this.value
};
},
watch: {
value(newValue) {
if (newValue !== this.editorValue && this.editor) {
this.editorValue = newValue;
this.editor.setValue(newValue);
}
}
},
mounted() {
const options = assign({
lineNumbers: true
}, this.editorConfig, {
readOnly: this.readOnly,
value: this.value
});
const editor = this.editor = CodeMirror(this.$refs.content, options);
editor.on('change', () => {
const newValue = editor.getValue();
this.editorValue = newValue;
this.$emit('input', newValue);
});
}
};
</script>

<style lang="scss">
.codeInputType1 {
.CodeMirror {
font-size: 1.6em; line-height: 1.5;
border: 1px solid $colorGrayLight2; border-radius: em(4,16);
height: auto;
}
.CodeMirror-scroll {
min-height: 8em;
}
.cm-s-default .cm-atom {
color: $colorMain1;
}
}
</style>
7 changes: 7 additions & 0 deletions src/js/formElements/elementDefaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,13 @@ export default {
label: {class: 'labelType2'},
wrapper: {class: 'inputBlockType1'}
}
},
code: {
attributes: {
label: {class: 'labelType2'},
wrapper: {class: 'inputBlockType1'},
inputWrapper: {class: 'codeInputType1'}
}
}
},
sideRegion: {
Expand Down

0 comments on commit 9cd5219

Please sign in to comment.