Skip to content

Commit

Permalink
Merge pull request #750 from Innercode/3.0
Browse files Browse the repository at this point in the history
Updating VueCKEditor
  • Loading branch information
nWidart committed Jan 14, 2020
2 parents 0013f69 + 297c73e commit 64beb77
Showing 1 changed file with 164 additions and 92 deletions.
256 changes: 164 additions & 92 deletions Modules/Core/Assets/js/components/CkEditor.vue
Original file line number Original file line Diff line number Diff line change
@@ -1,99 +1,171 @@
<template> <template>
<div class="ckeditor"> <div class="ckeditor">
<textarea <textarea
:name="name" :name="name"
:id="id" :id="id"
:value="value" :value="value"
:types="types" :types="types"
:config="config"> :config="config"
</textarea> :disabled="readOnlyMode"
</div> >
</textarea>
</div>
</template> </template>


<script> <script>
// Source: https://github.com/dangvanthanh/vue-ckeditor2 let inc = new Date().getTime();
let inc = new Date().getTime();
export default { export default {
name: 'vue-ckeditor', name: 'VueCkeditor',
props: { props: {
name: { name: {
type: String, type: String,
default: () => `editor-${++inc}` default: () => `editor-${++inc}`
}, },
value: { value: {
type: String type: String
}, },
id: { id: {
type: String, type: String,
default: () => `editor-${inc}` default: () => `editor-${inc}`
}, },
types: { types: {
type: String, type: String,
default: () => `classic` default: () => `classic`
}, },
config: { config: {
type: Object, type: Object,
default: () => {} default: () => {}
} },
}, instanceReadyCallback: {
data () { type: Function
return { destroyed: false } },
}, readOnlyMode: {
computed: { type: Boolean,
instance () { default: () => false
return CKEDITOR.instances[this.id] }
} },
}, data() {
watch: { return {
value (val) { instanceValue: ''
if (this.instance) { };
let html = this.instance.getData(); },
if (val !== html) { computed: {
this.instance.setData(val); instance() {
} return CKEDITOR.instances[this.id];
} }
} },
}, watch: {
mounted () { value(val) {
if (typeof CKEDITOR === 'undefined') { try {
console.log('CKEDITOR is missing (http://ckeditor.com/)') if (this.instance) {
} else { this.update(val);
if (this.types === 'inline') {
CKEDITOR.inline(this.id, this.config)
} else {
CKEDITOR.replace(this.id, this.config)
}
this.instance.on('change', () => {
let html = this.instance.getData()
if (html !== this.value) {
this.$emit('input', html)
this.$emit('update:value', html)
}
})
this.instance.on('blur', () => {
this.$emit('blur', this.instance)
})
this.instance.on('focus', () => {
this.$emit('focus', this.instance)
})
}
},
beforeDestroy () {
if (!this.destroyed) {
this.instance.focusManager.blur(true)
this.instance.removeAllListeners()
try {
this.instance.destroy()
} catch (e) { }
this.destroyed = true
}
} }
} catch (e) {}
},
readOnlyMode(val) {
this.instance.setReadOnly(val);
} }
</script> },
<style> mounted() {
.ckeditor::after { this.create();
content: ""; },
display: table; methods: {
clear: both; create() {
if (typeof CKEDITOR === 'undefined') {
console.log('CKEDITOR is missing (http://ckeditor.com/)');
} else {
if (this.types === 'inline') {
CKEDITOR.inline(this.id, this.config);
} else {
CKEDITOR.replace(this.id, this.config);
}
this.instance.setData(this.value);
this.instance.on('instanceReady', () => {
this.instance.setData(this.value);
});
// Ckeditor change event
this.instance.on('change', this.onChange);
// Ckeditor mode html or source
this.instance.on('mode', this.onMode);
// Ckeditor blur event
this.instance.on('blur', evt => {
this.$emit('blur', evt);
});
// Ckeditor focus event
this.instance.on('focus', evt => {
this.$emit('focus', evt);
});
// Ckeditor contentDom event
this.instance.on('contentDom', evt => {
this.$emit('contentDom', evt);
});
// Ckeditor dialog definition event
CKEDITOR.on('dialogDefinition', evt => {
this.$emit('dialogDefinition', evt);
});
// Ckeditor file upload request event
this.instance.on('fileUploadRequest', evt => {
this.$emit('fileUploadRequest', evt);
});
// Ckditor file upload response event
this.instance.on('fileUploadResponse', evt => {
setTimeout(() => {
this.onChange();
}, 0);
this.$emit('fileUploadResponse', evt);
});
// Listen for instanceReady event
if (typeof this.instanceReadyCallback !== 'undefined') {
this.instance.on('instanceReady', this.instanceReadyCallback);
}
// Registering the beforeDestroyed hook right after creating the instance
this.$once('hook:beforeDestroy', () => {
this.destroy();
});
}
},
update(val) {
if (this.instanceValue !== val) {
this.instance.setData(val, { internal: false });
this.instanceValue = val;
}
},
destroy() {
try {
let editor = window['CKEDITOR'];
if (editor.instances && editor.instances[this.id]) {
editor.instances[this.id].destroy();
}
} catch (e) {}
},
onMode() {
if (this.instance.mode === 'source') {
let editable = this.instance.editable();
editable.attachListener(editable, 'input', () => {
this.onChange();
});
}
},
onChange() {
let html = this.instance.getData();
if (html !== this.value) {
this.$emit('input', html);
this.instanceValue = html;
}
} }
</style> }
};
</script>

0 comments on commit 64beb77

Please sign in to comment.