Skip to content

feat/content text wysiwyg - #32

Merged
metaforx merged 4 commits into
mainfrom
feat/content-text-wysiwyg
Jun 1, 2026
Merged

feat/content text wysiwyg#32
metaforx merged 4 commits into
mainfrom
feat/content-text-wysiwyg

Conversation

@metaforx

@metaforx metaforx commented Jun 1, 2026

Copy link
Copy Markdown
Owner
  • Add unfold WYSIWYG support to text FOBI content
  • Remove cursor pointer (not needed anymore).

Summary by Sourcery

Add Unfold-styled WYSIWYG support for the Fobi content_text plugin and adjust related admin UI and docs.

New Features:

  • Render Fobi content_text fields using an Unfold inline-toolbar Trix WYSIWYG widget with configurable toolbar buttons and template.

Enhancements:

  • Broaden the default bleach allowlist for content_text fields to preserve Trix-generated markup unless project-specific overrides are defined.
  • Hook the content_text WYSIWYG integration into the global patch application flow so it is applied alongside other Fobi patches.
  • Tidy form edit UI by removing the unnecessary drag-handle cursor styling.

Documentation:

  • Document the new content_text WYSIWYG editor behavior and configuration options in the README, including toolbar customization and bleach overrides.

Chores:

  • Make minor wording tweaks in widget-related docstrings for clarity.

@sourcery-ai

sourcery-ai Bot commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Adds an Unfold-styled Trix WYSIWYG editor for the Fobi content_text plugin by introducing an inline-toolbar WYSIWYG widget, patching Fobi’s content_text form wiring and bleach allowlist, wiring the patch into the global patch pipeline, and updating docs and templates (including removing the drag cursor).

Sequence diagram for applying WYSIWYG widget on content_text form initialisation

sequenceDiagram
    actor User
    participant BasePlugin
    participant ContentTextForm
    participant InlineToolbarWysiwygWidget

    User->>BasePlugin: get_initialised_edit_form_or_404(slug)
    BasePlugin->>ContentTextForm: __init__(*args, **kwargs)  
    activate ContentTextForm
    ContentTextForm->>ContentTextForm: patched_init(*args, **kwargs)
    ContentTextForm->>ContentTextForm: force_wysiwyg(self)
    ContentTextForm->>ContentTextForm: fields.get(text)
    ContentTextForm->>InlineToolbarWysiwygWidget: InlineToolbarWysiwygWidget()
    deactivate ContentTextForm
    BasePlugin-->>User: ContentTextForm(instance) with InlineToolbarWysiwygWidget
Loading

File-Level Changes

Change Details Files
Introduce InlineToolbarWysiwygWidget to render Trix toolbar inline and support configurable toolbar templates/buttons.
  • Add DEFAULT_CONTENT_TEXT_TOOLBAR_TEMPLATE constant used as the default toolbar partial.
  • Implement InlineToolbarWysiwygWidget subclass of WysiwygWidget with a custom template_name.
  • Populate toolbar_template, toolbar_buttons, and toolbar_buttons_id in the widget context from Django settings.
src/unfold_fobi/forms/widgets.py
Add a content_text WYSIWYG patch that swaps the text widget, patches form creation paths, and widens bleach allowlists.
  • Define apply() that safely imports Fobi’s content_text plugin and no-ops if unavailable.
  • Patch ContentTextForm.init to force the InlineToolbarWysiwygWidget on the text field and mark sentinel attributes to coordinate with apply_widgets.
  • Wrap BasePlugin.get_initialised_edit_form_or_404 and get_initialised_create_form_or_404 to post-process returned forms with the WYSIWYG widget.
  • Extend content_text_forms.ALLOWED_TAGS and ALLOWED_ATTRIBUTES to Trix-compatible defaults unless the project overrides FOBI_PLUGIN_CONTENT_TEXT_ALLOWED_* settings.
src/unfold_fobi/patches/content_text_wysiwyg.py
Wire the new content_text WYSIWYG patch into the global patch application sequence.
  • Call apply_content_text_wysiwyg() from apply_all() after apply_widgets to ensure wrapper ordering.
  • Export apply_content_text_wysiwyg in the module’s public all list.
src/unfold_fobi/patches/__init__.py
Provide a custom template for inline-toolbar WYSIWYG rendering with optional toolbar button filtering.
  • Create wysiwyg_inline_toolbar.html to include the toolbar partial and serialize toolbar button configuration via json_script.
  • Instantiate a hidden input for the underlying field value and a Trix toolbar container targeted by ID.
  • Add inline JavaScript to clone the toolbar template into the instance toolbar, filter buttons based on allowed names (with aliases), remove empty groups and dialogs when link support is disabled, and render a Trix editor bound to the hidden input.
src/unfold_fobi/templates/unfold_fobi/forms/wysiwyg_inline_toolbar.html
Update documentation to describe the new Content Text WYSIWYG behavior and configuration and adjust section numbering.
  • Insert a new README section explaining the Unfold-styled Trix editor for the content_text plugin, including example settings for toolbar buttons, custom toolbar templates, and bleach allowlist overrides.
  • Renumber subsequent README sections (DRF notes and Fobi AppConfig overrides) to account for the new section.
README.md
Remove the now-unnecessary drag cursor styling from the form edit snippet template and make minor docstring touch-ups.
  • Drop inline style "cursor: move;" from the form row div in the form_edit_snippet template, removing the pointer/drag cursor.
  • Adjust some docstrings in widgets.py to quote Django types and slightly reword a helper docstring for clarity.
src/unfold_fobi/templates/override_simple_theme/snippets/form_edit_snippet.html
src/unfold_fobi/forms/widgets.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 3 issues, and left some high level feedback:

  • The inline toolbar template uses a fixed id="trix-toolbar" and document.querySelector('template[id="trix-toolbar"]'), so with multiple WYSIWYG fields on the same page the first script will remove the template and later instances won't find it; consider making the template ID unique per widget (e.g. include widget.name or the field ID) and querying relative to the widget container instead of the global document.
  • In InlineToolbarWysiwygWidget.get_context, toolbar_buttons_id is derived directly from name and then used as a DOM ID; to avoid invalid or clashing IDs when field names contain characters like dots or brackets, it would be safer to derive this from attrs['id'] or run name through a sanitizing helper (similar to Django’s auto_id handling).
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The inline toolbar template uses a fixed `id="trix-toolbar"` and `document.querySelector('template[id="trix-toolbar"]')`, so with multiple WYSIWYG fields on the same page the first script will remove the template and later instances won't find it; consider making the template ID unique per widget (e.g. include `widget.name` or the field ID) and querying relative to the widget container instead of the global document.
- In `InlineToolbarWysiwygWidget.get_context`, `toolbar_buttons_id` is derived directly from `name` and then used as a DOM ID; to avoid invalid or clashing IDs when field names contain characters like dots or brackets, it would be safer to derive this from `attrs['id']` or run `name` through a sanitizing helper (similar to Django’s `auto_id` handling).

## Individual Comments

### Comment 1
<location path="src/unfold_fobi/templates/unfold_fobi/forms/wysiwyg_inline_toolbar.html" line_range="10" />
<code_context>
+{% 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" %}>
+
+<div class="max-w-4xl relative">
</code_context>
<issue_to_address>
**issue (bug_risk):** Avoid duplicating the `id` attribute on the hidden input when `attrs` already provides one.

Including `django/forms/widgets/attrs.html` here can emit a second `id` when `attrs` already defines one, resulting in invalid HTML and brittle JS selectors. Either remove the hardcoded `id` and rely on `attrs`, or strip `id` from `attrs` for this element and only reuse it on `<trix-editor>`.
</issue_to_address>

### Comment 2
<location path="src/unfold_fobi/patches/content_text_wysiwyg.py" line_range="53-59" />
<code_context>
+        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
+    content_text_form_class._wysiwyg_widget_applied = True
+    # Sentinel that short-circuits apply_widgets' lazy __init__ re-wrap.
</code_context>
<issue_to_address>
**suggestion:** Preserve the original `__init__` metadata when monkey-patching `ContentTextForm`.

The replacement `__init__` drops metadata such as `__name__`, `__qualname__`, and `__doc__`, which can hinder debugging and introspection. Wrap `patched_init` with `functools.wraps(original_init)` to retain these attributes without changing behavior.

Suggested implementation:

```python
    original_init = content_text_form_class.__init__

    @functools.wraps(original_init)
    def patched_init(self, *args, **kwargs):
        original_init(self, *args, **kwargs)
        force_wysiwyg(self)

```

Ensure `functools` is imported at the top of this module, e.g.:
- `import functools`
If the file does not yet import `functools`, add that import alongside the other standard-library imports.
</issue_to_address>

### Comment 3
<location path="README.md" line_range="264-267" />
<code_context>
+
+```python
+# Restrict which buttons appear (subset of the names below).
+# Available: p, underlined, bold, italic, strike, link,
+#            heading1, heading2, heading3, heading4,
+#            quote, code, bullet, number, indent, outdent, undo, redo.
</code_context>
<issue_to_address>
**suggestion (typo):** Consider confirming whether `underlined` should be `underline` for the toolbar button name.

`underlined` is inconsistent with the other labels (`bold`, `italic`, `strike`, etc.). If the actual config value is `underline`, please update this entry to match and avoid confusion.

```suggestion
# Restrict which buttons appear (subset of the names below).
# Available: p, underline, bold, italic, strike, link,
#            heading1, heading2, heading3, heading4,
#            quote, code, bullet, number, indent, outdent, undo, redo.
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread src/unfold_fobi/templates/unfold_fobi/forms/wysiwyg_inline_toolbar.html Outdated
Comment thread src/unfold_fobi/patches/content_text_wysiwyg.py
Comment thread README.md
@metaforx
metaforx merged commit e3b5911 into main Jun 1, 2026
4 checks passed
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

Successfully merging this pull request may close these issues.

1 participant