StevenDevooght / tinyMCE-mention

Mention/Autocomplete plugin for tinyMCE WYSIWYG editor.
http://stevendevooght.github.io/tinyMCE-mention/
221 stars 96 forks source link

Remove   surrounding insert #32

Closed alexcroox closed 8 years ago

alexcroox commented 8 years ago

When I save the message TinyMCE is adding   either side of the mention text which is causing my forum mention system to ignore it.

tinymce.init({
        mode : "textareas",
        skin_url: '/css/tinymce/custom',
        skin: 'custom',
        plugins: 'code anchor image jbimages imagetools autoresize link paste fullscreen',
        external_plugins: {
            'mention': appPath + 'js/third_party/tinymce/mention/plugin.min.js',
            'jbimages': appPath + 'js/third_party/tinymce/jbimages/plugin.min.js'
        },
        toolbar: 'styleselect bold italic underline strikethrough blockquote | link image jbimages | bullist numlist | fullscreen paste undo redo',
        menubar: false,
        width: '100%',
        height: 350,
        statusbar: false,
        autoresize_min_height: 350,
        paste_data_images: true,
        relative_urls: false,
        force_p_newlines: false,
        force_br_newlines: true,
        inline_styles: true,
        convert_newlines_to_brs: true,
        remove_linebreaks: true,    
        document_base_url: window.location.protocol + appPath,
        paste_as_text: true,
        images_upload_url: window.location.protocol + appPath + 'image-upload.php',
        browser_spellcheck: true,
        content_css: appPath + 'css/tinymce/skin.css',
        mentions: {
            source: function(query, process, delimiter) {
                // Do your ajax call
                // When using multiple delimiters you can alter the query depending on the delimiter used
                if (delimiter === '@') {
                    ajax.request({
                        request: 'fetchForumMentions'
                    }, function(data) {
                        //call process to show the result
                        process(data)
                    });
                }
            },
            insert: function(item) {
                return '@' + item.name;
            }
        },
        setup: function(editor) {
            editor.on('change', function() {
                editor.save();
            });
        }
    });

Produces

<p>this is a test&nbsp;@Alex&nbsp;</p>

I'm not sure where the &nbsp; is coming from

StevenDevooght commented 8 years ago

TinyMCE automatically adds &nbsp; in some cases. I don't think this is an issue since we're dealing with html markup.

dano1066 commented 8 years ago

I had a similar issue. Since this is a tinymce issue, the best thing you can do is add some sort of placeholder in the front and back of the insert. Then with your server side code you can use this placeholder mark to be able to grab the data. So it might look like&nbsp;$%@Alex$%&nbsp;. You can then get replace the $% with whatever you like rather than having to do a blanked replace of all occurrences of &nbsp;

StevenDevooght commented 8 years ago

Indeed, we also added a wrapper element in our insert function.

insert: function(item) {
    return '<span class="mention">' + item.name + '</span>';
}

This way we can easily parse the mentions.