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

How to disable image pasting? #407

Closed
hurricane766 opened this issue Sep 26, 2016 · 9 comments
Closed

How to disable image pasting? #407

hurricane766 opened this issue Sep 26, 2016 · 9 comments
Labels

Comments

@hurricane766
Copy link

I just want to have a text editor without the ability to embed images. Is it possible to disable image pasting?

@pekpon
Copy link

pekpon commented Sep 27, 2016

Add the "remove format pasted" option:

$('.text-editor').trumbowyg({removeformatPasted: true});

@hurricane766
Copy link
Author

That will prevent images from being pasted in, but it also prevents bold, italics, etc.

I'm still looking for a solution to this problem... is there someway to blacklist or whitelist tags?

@Alex-D
Copy link
Owner

Alex-D commented Sep 27, 2016

You need to create a simple plugin.

You can give a "pasteHandler" in options: https://github.com/Alex-D/Trumbowyg/blob/develop/src/trumbowyg.js#L377

Example of use: https://github.com/Alex-D/Trumbowyg/blob/develop/plugins/pasteimage/trumbowyg.pasteimage.js

There is no option for white/blacklisted elements because it should be managed in backend or in a plugin.

@hurricane766
Copy link
Author

Ok, that sounds reasonable, thank you.

@oza75
Copy link

oza75 commented Feb 15, 2020

@hurricane766 have you find a way to disable image pasting ?

@hurricane766
Copy link
Author

@oza75 This was a while ago, but I believe we did as Alex-D suggested. In the end we moved away from Trumbowyg to a different editor, so I can't say for sure if this still works.

@VrbovskyM
Copy link

@oza75 This was a while ago, but I believe we did as Alex-D suggested. In the end we moved away from Trumbowyg to a different editor, so I can't say for sure if this still works.

What other editor ? I so far couldnt find better WYSIWYG editor for free

@hurricane766
Copy link
Author

What other editor ? I so far couldnt find better WYSIWYG editor for free

I believe we used ckeditor (4) at the time. It looks like v5 might still offer the core features for free.

@Alex-D
Copy link
Owner

Alex-D commented Feb 11, 2023

#1371 (comment)

(function ($) {
    'use strict';

    $.extend(true, $.trumbowyg, {
        plugins: {
            preventImagePaste: {
                init: function (trumbowyg) {
                    trumbowyg.pasteHandlers.push(function (pasteEvent) {
                        try {
                            var items = (pasteEvent.originalEvent || pasteEvent).clipboardData.items,
                                mustPreventDefault = false,
                                reader;

                            for (var i = items.length - 1; i >= 0; i -= 1) {
                                if (items[i].type.match(/^image\//)) {
                                    mustPreventDefault = true;
                                }
                            }

                            if (mustPreventDefault) {
                                pasteEvent.stopPropagation();
                                pasteEvent.preventDefault();
                            }
                        } catch (c) {
                        }
                    });
                }
            }
        }
    });
})(jQuery);

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

No branches or pull requests

5 participants