Alex-D / Trumbowyg

A lightweight and amazing WYSIWYG JavaScript editor under 10kB
https://alex-d.github.io/Trumbowyg
MIT License
3.97k stars 606 forks source link

Getting html from editor does not work | trumbowyg is not a function #1365

Closed TCAby closed 1 year ago

TCAby commented 1 year ago

Really I read closed issue #995 but my case doesn't be solved by given advices.

Description

System information (is it necessary):

Expected behavior

I want to get html from editor and POST the result with an AJAX request if onClick event occurs.

$ = jQuery.noConflict();
//trumbowyg
$.trumbowyg.svgPath = trumbowyg_svg;
$('#request_body').trumbowyg({
    lang: 'he',
    changeActiveDropdownIcon: true,
    tagsToRemove: ['script', 'embed', 'iframe', 'input'],
    urlProtocol: true,
    btnsDef: {
        mention: {
            title: 'Shortcodes',
            title: 'Select & Insert shortcode from the list that will be replaced',
            ico: 'insert-shortcode',
            hasIcon: true
        },
    },
    btns: [
        ['undo', 'redo'], // Only supported in Blink browsers
        ['formatting'],
        ['strong', 'em', 'del'],
        ['superscript', 'subscript'],
        ['link'],
        ['insertImage'],
        ['justifyLeft', 'justifyCenter', 'justifyRight', 'justifyFull'],
        ['unorderedList', 'orderedList'],
        ['horizontalRule'],
        ['mention'],
    ],
    plugins: {
        mention: {
            source: [
                {code: 'reviewer_name', name: 'Reviewer name'},
                {code: 'project_name', name: 'Name of the Project'},
                {code: 'originator_name', name: 'Name of the Originator'},
                {code: 'reviewform_url', name: 'URL of Review Form'},
            ],
            formatDropdownItem: function (item) {
                return item.name
            },
            formatResult: function (item) {
                return ' [[' + item.code + ']] '
            }
        }
    }
})

$('#request_body').trumbowyg('html', body_feedback_request)
//end of trumbowyg

then on onclick event I want to read the value in the editor - and I'm getting an error "Uncaught TypeError: $(...).trumbowyg is not a function":


$('#btnSubmitDesigner').on('click', function(e) {
    //getEditorValue('#request_body')
    let feedback_request_title = $('#feedback_request_title').val()
    let request_body = $('#request_body').trumbowyg('html')

})

My attempts to solve it:

Both of my ideas based on that initial part $('#request_body').trumbowyg('html', body_feedback_request) is Ok.

1) tried to call function (//getEditorValue('#request_body')) instead of direct call inside the event 2) tried to use inline function like

TCAby commented 1 year ago

Okay, I got out of it, I found a workaround through a direct property request: let request_body = $('#request_body')[0].innerHTML

But I'd still like to solve this problem properly, through library methods.

Alex-D commented 1 year ago

To be honest, I did not understand the bug ; and I the mentioned error is due to not having Trumbowyg loaded at all, so I cannot really help :/

TCAby commented 1 year ago

stop-stop, @Alex-D ! Trumbowyg loaded! Loaded, working, uses its top-panel (and I added some custom buttons there also)! But I can't get values from it. How can I show you details? By Zoom (sharing the screen) for example..