-
Notifications
You must be signed in to change notification settings - Fork 0
feat/content text wysiwyg #32
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
afd9fdd
feat: add WYSIWYG support for content_text plugin with customizable t…
metaforx 612fac5
fix: remove unnecessary cursor on form-rows
metaforx 8e346f7
fix: correct "underlined" to "underline" in README and templates, and…
metaforx 8ff3225
fix: remove redundant attribute included from the WYSIWYG toolbar tem…
metaforx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| """WYSIWYG support for the Fobi ``content_text`` plugin. | ||
|
|
||
| Swaps the default single-line widget for "InlineToolbarWysiwygWidget" and | ||
| widens the bleach allowlist so Trix's output survives sanitization. Must run | ||
| after "apply_widgets" so our wrappers go on top of its ones. | ||
| """ | ||
|
|
||
| from django.conf import settings | ||
|
|
||
| _TRIX_ALLOWED_TAGS = [ | ||
| "a", "abbr", "acronym", "b", "blockquote", "br", "code", "div", | ||
| "em", "h1", "h2", "h3", "i", "li", "ol", "p", "pre", "s", "strike", | ||
| "strong", "u", "ul", | ||
| ] | ||
| _TRIX_ALLOWED_ATTRIBUTES = { | ||
| "a": ["href", "title", "target", "rel"], | ||
| "abbr": ["title"], | ||
| "acronym": ["title"], | ||
| } | ||
|
|
||
|
|
||
| def apply(): | ||
| """Install the content_text WYSIWYG swap — idempotent. | ||
|
|
||
| Silently no-ops if the "content_text" plugin isn't installed. | ||
| """ | ||
| try: | ||
| from fobi import base as fobi_base | ||
| from fobi.contrib.plugins.form_elements.content.content_text import ( | ||
| forms as content_text_forms, | ||
| ) | ||
| except ImportError: | ||
| return | ||
|
|
||
| from unfold_fobi.forms.widgets import InlineToolbarWysiwygWidget | ||
|
|
||
| content_text_form_class = content_text_forms.ContentTextForm | ||
|
|
||
| if getattr(content_text_form_class, "_wysiwyg_widget_applied", False): | ||
| return | ||
|
|
||
| def force_wysiwyg(form): | ||
| if not isinstance(form, content_text_form_class): | ||
| return form | ||
| text_field = form.fields.get("text") | ||
| if text_field is not None and not isinstance( | ||
| text_field.widget, InlineToolbarWysiwygWidget | ||
| ): | ||
| text_field.widget = InlineToolbarWysiwygWidget() | ||
| return form | ||
|
|
||
| # --- Patch ContentTextForm.__init__ for direct instantiation paths --- | ||
| original_init = content_text_form_class.__init__ | ||
|
|
||
| def patched_init(self, *args, **kwargs): | ||
| original_init(self, *args, **kwargs) | ||
| force_wysiwyg(self) | ||
|
|
||
| content_text_form_class.__init__ = patched_init | ||
|
sourcery-ai[bot] marked this conversation as resolved.
|
||
| content_text_form_class._wysiwyg_widget_applied = True | ||
| # Sentinel that short-circuits apply_widgets' lazy __init__ re-wrap. | ||
| content_text_form_class._unfold_widgets_applied = True | ||
|
|
||
| # --- Re-wrap BasePlugin.get_initialised_{edit,create}_form_or_404 --- | ||
| def wrap_method(cls, name): | ||
| orig = getattr(cls, name, None) | ||
| if orig is None: | ||
| return | ||
|
|
||
| def wrapped(self, *args, **kwargs): | ||
| form = orig(self, *args, **kwargs) | ||
| return force_wysiwyg(form) if form is not None else form | ||
|
|
||
| setattr(cls, name, wrapped) | ||
|
|
||
| wrap_method(fobi_base.BasePlugin, "get_initialised_edit_form_or_404") | ||
| wrap_method(fobi_base.BasePlugin, "get_initialised_create_form_or_404") | ||
|
|
||
| # --- Widen bleach allowlist unless the project already overrode it --- | ||
| if not hasattr(settings, "FOBI_PLUGIN_CONTENT_TEXT_ALLOWED_TAGS"): | ||
| content_text_forms.ALLOWED_TAGS = _TRIX_ALLOWED_TAGS | ||
| if not hasattr(settings, "FOBI_PLUGIN_CONTENT_TEXT_ALLOWED_ATTRIBUTES"): | ||
| content_text_forms.ALLOWED_ATTRIBUTES = _TRIX_ALLOWED_ATTRIBUTES | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/unfold_fobi/templates/unfold_fobi/forms/wysiwyg_inline_toolbar.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| {% comment %} | ||
| Renders <trix-toolbar> inline beside the editor to bypass Trix.config | ||
| load-order races. Filters buttons by UNFOLD_FOBI_CONTENT_TEXT_TOOLBAR_BUTTONS | ||
| when set. | ||
| {% endcomment %} | ||
|
|
||
| {% include toolbar_template %} | ||
| {{ toolbar_buttons|json_script:toolbar_buttons_id }} | ||
|
|
||
| <input type="hidden" name="{{ widget.name }}" id="wysiwyg-{{ widget.name }}"{% if widget.value != None %} value="{{ widget.value }}"{% endif %} {% include "django/forms/widgets/attrs.html" %}> | ||
|
sourcery-ai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| <div class="max-w-4xl relative"> | ||
| <trix-toolbar id="trix-toolbar-{{ widget.name }}"></trix-toolbar> | ||
| <script> | ||
| (() => { | ||
| const source = document.querySelector('template[id="trix-toolbar"]'); | ||
| const target = document.getElementById("trix-toolbar-{{ widget.name }}"); | ||
| if (!source || !target || target.children.length > 0) return; | ||
| target.innerHTML = source.innerHTML; | ||
| source.remove(); | ||
|
|
||
| const data = document.getElementById("{{ toolbar_buttons_id }}"); | ||
| const allowed = data ? JSON.parse(data.textContent) : null; | ||
| if (!allowed) return; | ||
|
|
||
| const aliases = { | ||
| href: "link", | ||
| increaseNestingLevel: "indent", | ||
| decreaseNestingLevel: "outdent", | ||
| }; | ||
| const allowedSet = new Set(allowed); | ||
|
|
||
| target.querySelectorAll("button[data-trix-attribute], button[data-trix-action]").forEach((btn) => { | ||
| const raw = btn.dataset.trixAttribute || btn.dataset.trixAction; | ||
| if (!allowedSet.has(aliases[raw] || raw)) btn.remove(); | ||
| }); | ||
| target.querySelectorAll("[data-trix-button-group]").forEach((group) => { | ||
| if (!group.querySelector("button")) group.remove(); | ||
| }); | ||
| if (!allowedSet.has("link")) { | ||
| target.querySelectorAll("[data-trix-dialogs]").forEach((el) => el.remove()); | ||
| } | ||
| })(); | ||
| </script> | ||
|
|
||
| <trix-editor input="wysiwyg-{{ widget.name }}" toolbar="trix-toolbar-{{ widget.name }}" {% include "django/forms/widgets/attrs.html" %}></trix-editor> | ||
| </div> | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.