otto-torino / django-baton

A cool, modern and responsive django admin application based on bootstrap 5 that brings AI to the Django admin - https://baton.sqrt64.it/
MIT License
859 stars 92 forks source link

Signal on theme switch #281

Closed tyctor closed 5 months ago

tyctor commented 5 months ago

Hi

i wonder if we could have signal triggered on theme (dark/light) switching i am using TinyMCE as WYSIWYG for django TextField, and i am able to re-init editor in javascript so such signal will be ideal solution for my use case.

or if you have some hint about WYSIWYG which can do this in coexistence with baton, i would like to try it

looking forward to your reply

abidibo commented 5 months ago

Hi @tyctor you can simply use the Mutation Observer interface (https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver/observe)

Example:

            var observer = new MutationObserver(function(mutations) {
                mutations.forEach(function(mutation) {
                    if (mutation.type === "attributes") {
                        const theme = $('html').attr('data-bs-theme'); // light | dark
                        // do something useful
                    }
                });
            });

            observer.observe($('html')[0], {
              attributes: true //configure it to listen to attribute changes
            });
tyctor commented 5 months ago

thanks very much for your example it works like a charm when i replace $ with django.jQuery

it not work with $, because i have problem with jQuery reference in $, i have django admin jquery included in page, and when i put console.log on the end of that file, window.$ is there, but when page loads, and i try to print $ in chrome developer console, i get undefined

dont you have any suggestion why $ is lost?

looking forward to your answer

abidibo commented 5 months ago

Hi @tyctor is the expected behavior to have jQuery namespaced inside django object

tyctor commented 5 months ago

ok, but in every external script is used $, IMHO i cannot update external dependencies to use django.JQuery i am not sure why or when it is stopped working, i am upgrading from django 3.2 with django-suit to django 4.2 with django-baton so mainly what changed was replacing django-suit with django-baton i found this https://github.com/otto-torino/django-baton/issues/256 cannot be lost of $ reference related to django-baton ?

tyctor commented 5 months ago

ok, it seems that it is going with django 4.2, previous versions has $