ContentEditable.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <script>
  2. import REST from '@api/rest-axios'
  3. import { mapActions, mapState } from 'pinia'
  4. import { ConcernementsStore } from '@stores/concernements'
  5. import { UserStore } from '@stores/user'
  6. export default {
  7. props: {
  8. tag: String,
  9. value: String,
  10. contenteditable : {
  11. type : [Boolean, String],
  12. default : true,
  13. },
  14. html : {
  15. type : [Boolean, String],
  16. default : false,
  17. },
  18. data: Object
  19. },
  20. emits: ['updated'],
  21. data(){
  22. return {
  23. }
  24. },
  25. computed: {
  26. ...mapState(UserStore,['csrf_token']),
  27. },
  28. created () {
  29. console.log('ContentEditable created');
  30. },
  31. mounted () {
  32. },
  33. methods: {
  34. ...mapActions(ConcernementsStore, ['reloadConcernements']),
  35. onContentEditableFocusOut(e){
  36. console.log('onContentEditableFocusOut', e);
  37. // console.log('onContentEditableFocusOut data', this.data);
  38. let new_field_content = this.html ? e.target.innerHTML : e.target.innerText;
  39. // console.log('onContentEditableFocusOut', new_field_content);
  40. this.save(new_field_content)
  41. },
  42. save(content){
  43. // console.log('save csrf_token', this.csrf_token);
  44. const params = {
  45. type: this.data.bundle,
  46. [this.data.field.field_name]: [{[this.data.field.value]: content}]
  47. };
  48. if (this.data.entitytype === 'node') {
  49. params.nid = [{"value":this.data.id}];
  50. } else {
  51. params.id = [{"value":this.data.id}];
  52. }
  53. // we need additional values for image alt for example
  54. // console.log('additional_values', this.data.field.additional_values);
  55. if (this.data.field.additional_values) {
  56. for (const key in this.data.field.additional_values) {
  57. if (Object.hasOwnProperty.call(this.data.field.additional_values, key)) {
  58. params[this.data.field.field_name][0][key] = this.data.field.additional_values[key]
  59. }
  60. }
  61. }
  62. const configs = {
  63. headers: {'X-CSRF-Token': this.csrf_token}
  64. };
  65. let url_base = `/${this.data.entitytype === 'node' ? '' : 'entity/'}${this.data.entitytype}`;
  66. REST.patch(`${url_base}/${this.data.id}?_format=json`, params, configs)
  67. .then(({ data }) => {
  68. console.log('user REST post node data', data)
  69. // TODO if success update the data in pinia
  70. // this.reloadConcernements();
  71. this.$emit('updated');
  72. })
  73. .catch(error => {
  74. console.warn(`Issue with patch node ${this.data.bundle}`, error)
  75. Promise.reject(error)
  76. })
  77. }
  78. },
  79. }
  80. </script>
  81. <template>
  82. <component
  83. v-if="!html"
  84. :is="tag"
  85. :contenteditable="contenteditable"
  86. @focusout="onContentEditableFocusOut"
  87. >{{ value }}</component>
  88. <component
  89. v-else
  90. :is="tag"
  91. v-html="value"
  92. :contenteditable="contenteditable"
  93. @focusout="onContentEditableFocusOut"
  94. />
  95. </template>