Search code examples
extjsselectionpanel

Hot to get selected text in panel in extjs


How to get selected text in panel in extjs? The panel html property contains some formated html text, but I would like to get only plain text, and not a formated html text. So, if user selects some text, how to get the selection?

Ext.application({
    name: 'Fiddle',

    launch: function () {
        Ext.create('Ext.Container', {
            renderTo: Ext.getBody(),
            items: [{
                xtype: 'panel',
                title: 'my Panel',
                html: "Some text. Let's say for the sake of this question: <i>Lorem ipsum dolor sit amet,...</i>" ,
                width: 350
                },
                {
                xtype: 'button',
                text: 'my Button',
                listeners:{
                    click: function(){
                        var selectedText=''; //THIS IS WHERE THE QUESTION IS
                        alert(selectedText);
                    }
                }
            }]
        });
    }
});

Solution

  • You can use window.getSelection().toString() and document.selection.createRange().text

    Ext.application({
        name: 'Fiddle',
    
        launch: function () {
            Ext.create('Ext.panel.Panel', {
                renderTo: Ext.getBody(),
                layout: {
                    type: 'hbox',
                    align: 'stretch'
                },
                defaults: {
                    height: 200,
                    flex: 1
                },
                items: [{
                    xtype: 'panel',
                    itemId: 'panelOne',
                    title: 'Panel One',
                    html: "Some <b>text</b>. Let's say for the sake of this question: <i>Lorem ipsum dolor sit amet,...</i>"
                }, {
                    xtype: 'panel',
                    itemId: 'panelTwo',
                    title: 'Panel Two',
                    html: "Some <b>text</b>. Let's say for the sake of this question: <i>Lorem ipsum dolor sit amet,...</i>"
                }],
                buttons: [{
                    xtype: 'button',
                    text: 'my Button',
                    listeners: {
                        click: function () {
                            var selection = window.getSelection();
                            const panelItemIds = ['panelOne', 'panelTwo'];
                            panelItemIds.forEach(panelItemId => {
                                var panelComponent = this.up('panel').getComponent(panelItemId);
                                var node = selection.baseNode;
                                if (panelComponent) {
                                    var panelComponentDom = panelComponent.getEl().dom;
                                    while (node !== null) {
                                        if (node == panelComponentDom) {
                                            console.log(`Found selection "${selection.toString()}" in panel "${panelComponent.getTitle()}"`)
                                        }
                                        node = node.parentNode;
                                    }
                                }
                            });
                        }
                    }
                }]
            });
        }
    });