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

Is it possible to have different button bar depending on the form #262

Open
julienfumey opened this issue Oct 15, 2024 · 3 comments
Open

Comments

@julienfumey
Copy link

Hey,

I want to use martor on different forms of my websites. One of the form I want to use it on is a public form for which I would like to restrict the number of features (like removing the image upload button). But I want to keep those features for all the other forms.
Is there a way to change the configuration of the button bar on a specific form?

@agusmakmun
Copy link
Owner

agusmakmun commented Oct 23, 2024

hello @julienfumey thank you for using the martor package.
regarding your case, you can remove some of the buttons in the settings.py:

MARTOR_TOOLBAR_BUTTONS = [
    'bold', 'italic', 'horizontal', 'heading', 'pre-code',
    'blockquote', 'unordered-list', 'ordered-list',
    'link', 'image-link', 'image-upload', 'emoji',
    'direct-mention', 'toggle-maximize', 'help'
]

See this settings config for more

@julienfumey
Copy link
Author

Hello @agusmakmun,

Thanks for your answer.
Changing the config in the settings.py will change it for every form that use martor, right ? What I would like to do is having a toolbar config for one form (for example a blog post form that only admin can see), and an other one for an other form (like a comment form visible by any visitors). Is that possible?

@agusmakmun
Copy link
Owner

agusmakmun commented Oct 23, 2024

hmm interesting, for now not supported yet. But let's try to this solution:

# widgets.py
from martor.widgets import MartorWidget

class CustomMartorWidget(MartorWidget):
    def __init__(self, *args, **kwargs):
        settings.MARTOR_TOOLBAR_BUTTONS = ['bold', 'italic', 'horizontal', 'heading']
# forms.py
from martor.widgets import MartorWidget
from yourapp.widgets import CustomMartorWidget

class CustomVisitorForm(forms.CharField):
    def __init__(self, *args, **kwargs):
        # to setup the editor without label
        if not MARTOR_ENABLE_LABEL:
            kwargs["label"] = ""

        super().__init__(*args, **kwargs)

        if not issubclass(self.widget.__class__, MartorWidget):
            self.widget = CustomMartorWidget()

class VisitorForm(forms.ModelForm):
    comment = CustomVisitorForm()

If above still not working, for the quick solution you can use the JavaScript to hide some of the buttons,

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

No branches or pull requests

2 participants