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

fix: Handle partial page updates #54

Merged
merged 6 commits into from
Jan 18, 2025
Merged

Conversation

fsbraun
Copy link
Member

@fsbraun fsbraun commented Jan 18, 2025

Summary by Sourcery

Update the editor to handle partial page updates, addressing issues with inline editing and toolbar interactions. Improve handling of global settings and ensure editor components are correctly initialized and destroyed.

Bug Fixes:

  • Fix handling of partial page updates in the editor.

Enhancements:

  • Improve initialization and destruction of editors.
  • Enhance inline rich text editor initialization and wrapping.
  • Refine toolbar button creation and improve accessibility.

Copy link
Contributor

sourcery-ai bot commented Jan 18, 2025

Reviewer's Guide by Sourcery

This pull request addresses an issue where the editor would not properly re-initialize when parts of the page were updated. This was caused by the editor not being properly destroyed and re-initialized when the page was updated. The fix ensures that the editor is properly destroyed and re-initialized when the page is updated, and also ensures that the editor is not initialized twice when an inline editor is scrolled back into the viewport.

Sequence diagram for editor re-initialization on page update

sequenceDiagram
    participant Page
    participant CMSEditor
    participant Observer
    participant EditorPlugin

    Page->>CMSEditor: Page content updated
    CMSEditor->>CMSEditor: Check if editor exists
    alt Editor already initialized
        CMSEditor-->>Page: Skip initialization
    else Editor not initialized
        CMSEditor->>Observer: Add element to observer
        CMSEditor->>EditorPlugin: Create editor instance
        CMSEditor->>CMSEditor: Store editor settings
    end
Loading

Class diagram showing updated CMSEditor structure

classDiagram
    class CMSEditor {
        -_global_settings: Object
        -_editor_settings: Object
        -_generic_editors: Object
        -_admin_selector: String
        -_admin_add_row_selector: String
        -_inline_admin_selector: String
        +init(el)
        +initAll()
        +destroyAll()
        +destroyRTE()
        +destroyGenericEditor()
        -_createRTE(el)
        -_initInlineRichText(elements, url, cls)
        -getSettings(el)
        -_resetInlineEditors()
    }
    note for CMSEditor "Changed from array-based
to object-based storage
for better handling of
partial updates"
Loading

State diagram for editor initialization process

stateDiagram-v2
    [*] --> CheckEditor
    CheckEditor --> AlreadyInitialized: Editor ID exists
    CheckEditor --> Initialize: Editor ID not found
    Initialize --> CreateRTE: TextPlugin/HTMLField
    Initialize --> CreateGenericEditor: CharField
    CreateRTE --> AddEventListeners
    CreateGenericEditor --> AddEventListeners
    AddEventListeners --> [*]
    AlreadyInitialized --> [*]
Loading

File-Level Changes

Change Details Files
The editor is now properly destroyed and re-initialized when the page is updated.
  • The _editors array was removed.
  • The _generic_editors array was changed to an object.
  • The init method now checks if the editor has already been initialized before initializing it again.
  • The destroyAll method now iterates over the _editor_settings object and destroys all editors.
  • The destroyRTE method was added to destroy all RTE editors.
  • The destroyGenericEditor method was updated to destroy all generic editors.
  • The _resetInlineEditors method no longer calls destroyAll.
  • The _initInlineRichText method now adds the cms-plugin class to the wrapper element.
  • The _initInlineRichText method now checks if the wrapper element is already observed before adding it to the observer.
  • The _createRTE method was added to create a new RTE editor.
  • The getSettings method now returns an empty object if the settings element is not found.
  • The _highlightTextplugin method now uses a shorter timeout.
private/js/cms.editor.js
The tests were updated to reflect the changes in the editor.
  • The tests now check the length of the _editor_settings object instead of the _editors array.
  • The tests now initialize the editor after the DOMContentLoaded event.
  • The tests now clear the _editor_settings object before each test.
  • The tests now check if the global settings are read correctly.
tests/js/cms.editor.test.js
The toolbar button now uses a button tag instead of a div tag.
  • The toolbar button now uses a button tag instead of a div tag.
private/js/tiptap_plugins/cms.toolbar.js
The toolbar tests were updated to reflect the changes in the toolbar.
  • The tests now check the length of the _editor_settings object instead of the _editors array.
tests/js/cms.tiptap.toolbar.test.js
The version number was updated.
  • The version number was updated to 0.5.3.
djangocms_text/__init__.py
The toolbar CSS was updated.
  • The toolbar CSS was updated to include a min-width and z-index.
private/css/cms.toolbar.css
The tiptap plugin was updated.
  • The tiptap plugin was updated to prevent multiple initializations.
private/js/cms.tiptap.js

Possibly linked issues

  • #0: The PR fixes the issue described in the issue. The PR changes the editor initialization logic to handle partial page updates, which addresses the problem of the editor not initializing correctly in inline admins.

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.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a 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. You can also use
    this command to specify where the summary should be inserted.

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

Copy link

codecov bot commented Jan 18, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 81.22%. Comparing base (e90fb53) to head (5c81033).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main      #54   +/-   ##
=======================================
  Coverage   81.22%   81.22%           
=======================================
  Files          17       17           
  Lines         932      932           
  Branches      104      104           
=======================================
  Hits          757      757           
  Misses        132      132           
  Partials       43       43           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@fsbraun fsbraun marked this pull request as ready for review January 18, 2025 20:44
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @fsbraun - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

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 on lines 182 to 205
if (wrapper) {
// Catch CMS single click event to highlight the plugin
// Catch CMS double click event if present, since double click is needed by Editor
this.observer.observe(wrapper);
if (this.CMS) {
// Remove django CMS core's double click event handler which opens an edit dialog
this.CMS.$(wrapper).off('dblclick.cms.plugin')
.on('dblclick.cms-editor', function (event) {
event.stopPropagation();
});
wrapper.addEventListener('focusin.cms-editor', () => {
this._highlightTextplugin(id);
}, true);
// Prevent tooltip on hover
this.CMS.$(wrapper).off('pointerover.cms.plugin pointerout.cms.plugin')
.on('pointerover.cms-editor', function (event) {
window.CMS.API.Tooltip.displayToggle(false, event.target, '', id);
event.stopPropagation();
});
if (!Array.from(this.observer.root?.children || []).includes(wrapper)) {
// Only add to the observer if not already observed (e.g., if the page only was update partially)
this.observer.observe(wrapper);
if (this.CMS) {
// Remove django CMS core's double click event handler which opens an edit dialog
this.CMS.$(wrapper).off('dblclick.cms.plugin')
.on('dblclick.cms-editor', function (event) {
event.stopPropagation();
});
wrapper.addEventListener('focusin', () => {
this._highlightTextplugin(id);
}, true);
// Prevent tooltip on hover
this.CMS.$(wrapper).off('pointerover.cms.plugin pointerout.cms.plugin')
.on('pointerover.cms-editor', function (event) {
window.CMS.API.Tooltip.displayToggle(false, event.target, '', id);
event.stopPropagation();
});
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion (code-quality): Merge nested if conditions (merge-nested-ifs)

Suggested change
if (wrapper) {
// Catch CMS single click event to highlight the plugin
// Catch CMS double click event if present, since double click is needed by Editor
this.observer.observe(wrapper);
if (this.CMS) {
// Remove django CMS core's double click event handler which opens an edit dialog
this.CMS.$(wrapper).off('dblclick.cms.plugin')
.on('dblclick.cms-editor', function (event) {
event.stopPropagation();
});
wrapper.addEventListener('focusin.cms-editor', () => {
this._highlightTextplugin(id);
}, true);
// Prevent tooltip on hover
this.CMS.$(wrapper).off('pointerover.cms.plugin pointerout.cms.plugin')
.on('pointerover.cms-editor', function (event) {
window.CMS.API.Tooltip.displayToggle(false, event.target, '', id);
event.stopPropagation();
});
if (!Array.from(this.observer.root?.children || []).includes(wrapper)) {
// Only add to the observer if not already observed (e.g., if the page only was update partially)
this.observer.observe(wrapper);
if (this.CMS) {
// Remove django CMS core's double click event handler which opens an edit dialog
this.CMS.$(wrapper).off('dblclick.cms.plugin')
.on('dblclick.cms-editor', function (event) {
event.stopPropagation();
});
wrapper.addEventListener('focusin', () => {
this._highlightTextplugin(id);
}, true);
// Prevent tooltip on hover
this.CMS.$(wrapper).off('pointerover.cms.plugin pointerout.cms.plugin')
.on('pointerover.cms-editor', function (event) {
window.CMS.API.Tooltip.displayToggle(false, event.target, '', id);
event.stopPropagation();
});
}
}
}
if (wrapper && !Array.from(this.observer.root?.children || []).includes(wrapper)) {
this.observer.observe(wrapper);
if (this.CMS) {
// Remove django CMS core's double click event handler which opens an edit dialog
this.CMS.$(wrapper).off('dblclick.cms.plugin')
.on('dblclick.cms-editor', function (event) {
event.stopPropagation();
});
wrapper.addEventListener('focusin', () => {
this._highlightTextplugin(id);
}, true);
// Prevent tooltip on hover
this.CMS.$(wrapper).off('pointerover.cms.plugin pointerout.cms.plugin')
.on('pointerover.cms-editor', function (event) {
window.CMS.API.Tooltip.displayToggle(false, event.target, '', id);
event.stopPropagation();
});
}
}


ExplanationReading deeply nested conditional code is confusing, since you have to keep track of which
conditions relate to which levels. We therefore strive to reduce nesting where
possible, and the situation where two if conditions can be combined using
and is an easy win.

private/js/cms.editor.js Show resolved Hide resolved
@fsbraun fsbraun merged commit 37f8957 into main Jan 18, 2025
31 checks passed
@fsbraun fsbraun deleted the fix/handle-partial-updates branch January 18, 2025 20:46
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