vue-redactor.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. Redactor Vue Component
  3. Version 1.2
  4. Updated: June 9, 2021
  5. http://imperavi.com/redactor/
  6. Copyright 2021, Imperavi Ltd.
  7. License: MIT
  8. */
  9. Vue.component('Redactor', {
  10. template: '<textarea ref="redactor" :name="name" :placeholder="placeholder" :value="value" />',
  11. redactor: false,
  12. props: {
  13. value: {
  14. default: '',
  15. type: String
  16. },
  17. placeholder: {
  18. type: String,
  19. default: null
  20. },
  21. name: {
  22. type: String,
  23. default: null
  24. },
  25. config: {
  26. default: {},
  27. type: Object
  28. }
  29. },
  30. watch: {
  31. value(newValue, oldValue){
  32. if (this.redactor.editor.isFocus() || this.redactor.editor.isSourceMode()) {
  33. return;
  34. }
  35. this.redactor.source.setCode(newValue);
  36. }
  37. },
  38. mounted() {
  39. this.init()
  40. },
  41. beforeDestroy() {
  42. this.destroy()
  43. },
  44. methods: {
  45. init () {
  46. var me = this;
  47. var callbacks = {
  48. changed: function(html) {
  49. me.handleInput(html);
  50. return html
  51. }
  52. };
  53. // extend config
  54. if (typeof this.config.callbacks === 'undefined') {
  55. Vue.set(this.config, 'callbacks', callbacks);
  56. }
  57. else {
  58. this.config.callbacks.changed = callbacks.changed;
  59. }
  60. // call Redactor
  61. var app = Redactor(this.$refs.redactor, this.config);
  62. // set instance
  63. this.redactor = app;
  64. this.$parent.redactor = app;
  65. },
  66. destroy () {
  67. // Call destroy on redactor to cleanup event handlers
  68. Redactor(this.$refs.redactor, 'destroy');
  69. // unset instance for garbage collection
  70. this.redactor = null;
  71. this.$parent.redactor = null;
  72. },
  73. handleInput (val) {
  74. this.$emit('input', val);
  75. }
  76. }
  77. });