ContentEditable.vue 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. nid: [{"value":this.data.nid}],
  47. [this.data.field.field_name]: [{[this.data.field.value]: content}]
  48. };
  49. // we need additional values for image alt for example
  50. // console.log('additional_values', this.data.field.additional_values);
  51. if (this.data.field.additional_values) {
  52. for (const key in this.data.field.additional_values) {
  53. if (Object.hasOwnProperty.call(this.data.field.additional_values, key)) {
  54. params[this.data.field.field_name][0][key] = this.data.field.additional_values[key]
  55. }
  56. }
  57. }
  58. const configs = {
  59. headers: {'X-CSRF-Token': this.csrf_token}
  60. };
  61. REST.patch(`/${this.data.entitytype}/${this.data.nid}?_format=json`, params, configs)
  62. .then(({ data }) => {
  63. console.log('user REST post node data', data)
  64. // TODO if success update the data in pinia
  65. // this.reloadConcernements();
  66. this.$emit('updated');
  67. })
  68. .catch(error => {
  69. console.warn(`Issue with patch node ${this.data.bundle}`, error)
  70. Promise.reject(error)
  71. })
  72. }
  73. },
  74. }
  75. </script>
  76. <template>
  77. <component
  78. v-if="!html"
  79. :is="tag"
  80. :contenteditable="contenteditable"
  81. @focusout="onContentEditableFocusOut"
  82. >{{ value }}</component>
  83. <component
  84. v-else
  85. :is="tag"
  86. v-html="value"
  87. :contenteditable="contenteditable"
  88. @focusout="onContentEditableFocusOut"
  89. />
  90. </template>