Skip to content Skip to sidebar Skip to footer

Catch Ctrl+enter When User Typing Text In Ext.form.field.htmleditor

I'm trying to make ajax request when user press ctrl+enter while is entering text in Ext.form.field.HtmlEditor (xtype:'htmleditor'), but I don't know how to do it. I'got button nex

Solution 1:

You cannot listen for events in default htmleditor. So you need use updated version of it.

This code can help you (it is for extjs 3, so maybe you need change it for 4 version):

Cyber.ui.HtmlEditor = Ext.extend(Ext.form.HtmlEditor, {
        frame : true,
        initComponent : function() {
            Cyber.ui.HtmlEditor.superclass.initComponent.call(this);
            this.addEvents('submit');
        },
        initEditor : function() {
           Cyber.ui.HtmlEditor.superclass.initEditor.call(this);
            if (Ext.isGecko) {
                Ext.EventManager.on(this.doc, 'keypress', this.fireSubmit,
                        this);
            }
            if (Ext.isIE || Ext.isWebKit || Ext.isOpera) {
                Ext.EventManager.on(this.doc, 'keydown', this.fireSubmit,
                        this);
            }
        },
        fireSubmit : function(e) {
            if (e.ctrlKey && Ext.EventObject.ENTER == e.getKey()) {
                // Do what you need here
            }
        }
});

Ext.reg('customeditor', Cyber.ui.HtmlEditor);

And in your form:

this.htmleditor = this.addComment.add({
    region:'center',
    xtype:'customeditor',
    margin:'0 0 0 0',
    enableSourceEdit:false,
    height:200
});

I played a lot with Extjs 4 and found the way (you need just include this code before you'll use htmleditor):

Ext.form.HtmlEditor.override({
    frame : true,
    initComponent: function() {
        this.callOverridden();
        this.addEvents('submit');
    },

    initEditor : function() {
        this.callOverridden();

        var me = this;
        var doc = me.getDoc();

        if (Ext.isGecko) {
            Ext.EventManager.on(doc, 'keypress', me.fireSubmit, me);
        }

        if (Ext.isIE || Ext.isWebKit || Ext.isOpera) {
            Ext.EventManager.on(doc, 'keydown', me.fireSubmit, me);
        }
    },

    fireSubmit : function(e) {
        if (e.ctrlKey && Ext.EventObject.ENTER == e.getKey()) {
            // Do what you need herealert('yes!');
        }
    }
});

Solution 2:

Is this what you're after (was already on stack :P)? Ctrl+Enter jQuery in TEXTAREA:

$('#textareaId').keydown(function (e) {
e = e || event; // for compatibility with IE (i belive)if (e.ctrlKey && e.keyCode == 13) {
    // Ctrl-Enter pressed
  }
});

Solution 3:

Worked for ExtJs 6 (Example disables Enter key):

Ext.create('Ext.form.HtmlEditor', {
    width: 580,
    height: 250,
    renderTo: Ext.getBody(),
    listeners:{
       initialize: function(editor){
           const doc = editor.getDoc();
           const docEl = Ext.get(doc);
           docEl.on({
              keypress: (e)=>{
                if (e.event.code === 'Enter'){
                  e.preventDefault();
                }
              },
              delegated:false,
              scope: editor
           });
       }
    }
});

Post a Comment for "Catch Ctrl+enter When User Typing Text In Ext.form.field.htmleditor"