123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- <script>
- import REST from '@api/rest-axios'
- import { mapActions, mapState } from 'pinia'
- import { ConcernementsStore } from '@stores/concernements'
- import { UserStore } from '@stores/user'
- import MediumEditor from 'medium-editor-x'
- import 'medium-editor-x/dist/css/medium-editor.css'
- import 'medium-editor-x/dist/css/themes/beagle.css'
- export default {
- props: {
- tag: String,
- value: String,
- contenteditable : {
- type : [Boolean, String],
- default : true,
- },
- html : {
- type : [Boolean, String],
- default : false,
- },
- data: Object,
- placeholder : {
- type : String,
- default : 'Ajouter du texte'
- }
- },
- emits: ['updated'],
- data(){
- return {
- editor: null,
- spellcheck: false
- }
- },
- computed: {
- ...mapState(UserStore,['csrf_token']),
- },
- created () {
- console.log('ContentEditable created');
- },
- mounted () {
- if (this.contenteditable && this.html) {
- this.editor = new MediumEditor(this.$refs.html_editable, {
- toolbar: {
- allowMultiParagraphSelection: false,
- buttons: ['anchor'],
- },
- placeholder: {
- /* This example includes the default options for placeholder,
- if nothing is passed this is what it used */
- text: this.placeholder,
- hideOnClick: true
- }
- });
- }
- },
- beforeUnmount() {
- },
- methods: {
- ...mapActions(ConcernementsStore, ['reloadConcernements']),
- onContentEditableFocusOut(e){
- console.log('onContentEditableFocusOut', e);
- // console.log('onContentEditableFocusOut data', this.data);
- let new_field_content = this.html ? e.target.innerHTML : e.target.innerText;
- new_field_content = new_field_content.replace(/<br>$/g, '');
- // console.log('onContentEditableFocusOut', new_field_content);
- this.$emit('focusout');
- if (this.data) {
- this.save(new_field_content)
- }
- },
- save(content){
- // console.log('save csrf_token', this.csrf_token);
- const params = {
- type: this.data.bundle,
- [this.data.field.field_name]: [{[this.data.field.value]: content}]
- };
- if (this.data.entitytype === 'node') {
- params.nid = [{"value":this.data.id}];
- } else {
- params.id = [{"value":this.data.id}];
- }
- // we need additional values for image alt for example
- // console.log('additional_values', this.data.field.additional_values);
- if (this.data.field.additional_values) {
- for (const key in this.data.field.additional_values) {
- if (Object.hasOwnProperty.call(this.data.field.additional_values, key)) {
- params[this.data.field.field_name][0][key] = this.data.field.additional_values[key]
- }
- }
- }
-
- const configs = {
- headers: {'X-CSRF-Token': this.csrf_token}
- };
- // url is not the same between nodes and others entities
- let url_base = `/${this.data.entitytype === 'node' ? '' : 'entity/'}${this.data.entitytype}`;
- // call the api
- REST.patch(`${url_base}/${this.data.id}?_format=json`, params, configs)
- .then(({ data }) => {
- console.log('user REST post node data', data)
- this.$emit('updated');
- })
- .catch(error => {
- console.warn(`Issue with patch ${this.data.entitytype} ${this.data.bundle}`, error)
- Promise.reject(error)
- })
- }
- },
- components: {
- }
- }
- </script>
- <template>
- <component
- v-if="!html"
- :is="tag"
- :contenteditable="contenteditable"
- :spellcheck="spellcheck"
- @focusout="onContentEditableFocusOut"
- >{{ value }}</component>
- <component
- v-else
- :is="tag"
- class="html-editable"
- ref="html_editable"
- v-html="value"
- :contenteditable="contenteditable"
- :spellcheck="spellcheck"
- @focusout="onContentEditableFocusOut"
- />
- </template>
|