Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OnChange event not firing when using ck-editor in source mode #36

Open
wittsparks opened this issue Aug 11, 2017 · 8 comments
Open

OnChange event not firing when using ck-editor in source mode #36

wittsparks opened this issue Aug 11, 2017 · 8 comments
Assignees

Comments

@wittsparks
Copy link

wittsparks commented Aug 11, 2017

Hello @dangvanthanh - Thanks for creating this!!

I added a console.log in ckeditor.vue here:

this.instance.on('change', () => {
        console.log('ckeditor onchange');
        let html = this.instance.getData()
        if (html !== this.value) {
          this.$emit('changed', this.instance)
        }
      })

I see my log message whenever I make a change in ck-editor's 'normal' mode, but I don't see when in 'source' mode.

Update: looks like ck-editor itself doesn't fire change events in source mode:

Sounds like there are some valid reasons for that behavior, but it leaves me unable to detect the changed content if a user makes a change in ck-editor source mode. Anyone know of a workaround for this?

Thanks for any help!!

@wittsparks
Copy link
Author

I'm wrapping my ckeditor component in a another vue component which is a modal dialog. I ended out getting around this issue by having the ckeditor component emit an event containing the ckeditor instance when it's mounted.

Then when the parent component responds to the mounted event emitted by the ckeditor vue component, it saves a reference to the underlying ckeditor instance passed with the event. When the modal is closed, I can call getData on the ckeditor instance and update the v-model.

This has the added benefit of not calling getData() on the ckeditor with every keystroke.

@dangvanthanh
Copy link
Owner

Hi @wittsparks

Can you post your code in here. I can see problem.

@wittsparks
Copy link
Author

Hi @dangvanthanh ,

Here's the component where I'm using the ckeditor:

<template>
  <div class="text-editor">
    <div :id="id" class="modal modal-fixed-footer">
      <ckeditor v-model="content" @mounted="onMounted($event)"></ckeditor>
      <div class="modal-footer">
        <button class="btn waves-effect waves-light" type="button" v-on:click="save()">Done
          <i class="material-icons right">send</i>
        </button>
        <button class="btn waves-effect waves-light modal-action modal-close" type="button">Cancel </button>
      </div>
    </div>
  </div>
</template>

<script>
    export default {
        props: ['id', 'content'],
        mounted() {
          $('.modal').modal({
            dismissible:false,
          });
        },

        data() {
          return {
            newContent: this.content,
            editorInstance: null
          }
        },

        methods: {
          onMounted ( editor ) {
            console.log("ckeditor mounted");
            this.editorInstance = editor;
          },
          save( ) {
            this.newContent = this.editorInstance.getData();
            this.$emit('changed', this.newContent);
            $('#' + this.id).modal('close');
          }
        },
    }
</script>

In ckeditor.vue, I added a console.log in this.instance.on('change') as shown in my original post. When I run the code, I see my log message when making changes in regular mode, but not in source mode.

From my research, it sounds like the underlying ckeditor instance itself does not emit on-changed events in source mode. It does emit an event when you switch back to regular mode.

As I said above, I solved the problem for my purposes by calling getData() on the ckeditor instance after the user closes the modal containing the editor, and that's working for me. So as far as I'm concerned this can be closed.

Thanks,
Witt

@dangvanthanh
Copy link
Owner

Hi @wittsparks. Thank you very much. I will close thread. You can open new issues when you working with vue-ckeditor.

@MugenFTW
Copy link

In case anyone comes across this I have a solution here:

this.instance.on('mode', () => { if (this.instance.mode === 'source') { var editable = this.instance.editable(); editable.attachListener(editable, 'input', () => { var value = editable.getData(); this.$emit('input', value); this.$emit('update:value', value); }); } });

By adding this extra event listener to the mounted method of the main ckeditor vue component it will update when inputs change when the source view is selected.

AJ

@dangvanthanh dangvanthanh reopened this Sep 25, 2020
@dangvanthanh
Copy link
Owner

Thank you very much @MugenFTW

I will be adding the extra event listeners to this case.

@dangvanthanh dangvanthanh self-assigned this Sep 25, 2020
@tbwork
Copy link

tbwork commented Jan 14, 2021

Why do not just add a change event... Above solutions are so tedious.

@MugenFTW
Copy link

@tbwork It has to be done this way because the change event isn't fired by ck-editor when the editor is changed to source mode.

https://dev.ckeditor.com/ticket/12031

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants