ContentEditable.vue 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <script>
  2. import REST from '@api/rest-axios'
  3. // import JSONAPI from '@api/json-axios'
  4. // import qs from 'querystring-es3'
  5. // import { print } from 'graphql/language/printer'
  6. // import gql from 'graphql-tag'
  7. // import GQL from '@api/graphql-axios'
  8. import { mapActions, mapState } from 'pinia'
  9. import { UserStore } from '@stores/user'
  10. export default {
  11. props: {
  12. tag: String,
  13. value: String,
  14. contenteditable : {
  15. type : [Boolean, String],
  16. default : true,
  17. },
  18. html : {
  19. type : [Boolean, String],
  20. default : false,
  21. },
  22. data: Object
  23. },
  24. // emits: ['returned'],
  25. data(){
  26. return {
  27. }
  28. },
  29. computed: {
  30. ...mapState(UserStore,['csrf_token']),
  31. },
  32. created () {
  33. console.log('ContentEditable created');
  34. },
  35. mounted () {
  36. },
  37. methods: {
  38. onContentEditableFocusOut(e){
  39. console.log('onContentEditableFocusOut', e);
  40. // console.log('onContentEditableFocusOut data', this.data);
  41. let new_field_content = this.html ? e.target.innerHTML : e.target.innerText;
  42. // console.log('onContentEditableFocusOut', new_field_content);
  43. this.save(new_field_content)
  44. },
  45. save(content){
  46. console.log('save csrf_token', this.csrf_token);
  47. const params = {
  48. type: this.data.bundle,
  49. nid: [{"value":this.data.nid}],
  50. [this.data.field]: {value: content}
  51. };
  52. const configs = {
  53. headers: {
  54. 'X-CSRF-Token': this.csrf_token,
  55. // Authorization: `Bearer ${this.csrf_token}`,
  56. // 'customheader': "bobobo"
  57. }
  58. };
  59. console.log('REST', REST);
  60. REST.patch(`/node/${this.data.nid}?_format=json`, params, configs)
  61. .then(({ data }) => {
  62. console.log('user REST post node data', data)
  63. // TODO if success update the data in pinia
  64. })
  65. .catch(error => {
  66. console.warn('Issue with post node', error)
  67. Promise.reject(error)
  68. })
  69. }
  70. },
  71. }
  72. </script>
  73. <template>
  74. <component
  75. v-if="!html"
  76. :is="tag"
  77. :contenteditable="contenteditable"
  78. @focusout="onContentEditableFocusOut"
  79. >{{ value }}</component>
  80. <component
  81. v-else
  82. :is="tag"
  83. v-html="value"
  84. :contenteditable="contenteditable"
  85. @focusout="onContentEditableFocusOut"
  86. />
  87. </template>