-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #750 from Innercode/3.0
Updating VueCKEditor
- Loading branch information
Showing
1 changed file
with
164 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |