8000 feat: Configurable dark mode by fsbraun · Pull Request #7329 · django-cms/django-cms · GitHub
[go: up one dir, main page]

Skip to content

feat: Configurable dark mode #7329

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 30 commits into from
Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
592a2b6
Fix: toolbar bug 3.10.rc1
fsbraun Feb 9, 2022
7778c29
Merge branch 'django-cms:develop' into develop
fsbraun Feb 11, 2022
96f89cd
Feat: Dark mode support, including input from @marksweb, bugfix for t…
fsbraun Feb 18, 2022
f92fe75
Upstream change to be able to merge
fsbraun Feb 18, 2022
ffee65a
Merge branch 'develop' of github.com:fsbraun/django-cms into develop
fsbraun Feb 18, 2022
ecb9fab
Feat: Dark mode support, including input from @marksweb, bugfix for t…
fsbraun Feb 18, 2022
76876a0
Merge branch 'develop' of github.com:fsbraun/django-cms into develop
fsbraun Feb 18, 2022
a0995e3
Revert "Fix: toolbar bug 3.10.rc1"
fsbraun Feb 18, 2022
4ec8a5b
Fix: Recommit toolbar fix (??)
fsbraun Feb 18, 2022
e1158f8
Fix: After lint failure: Remove spaces added by PyCharm
fsbraun Feb 18, 2022
1e725f8
Merge branch 'develop' into develop
vinitkumar Mar 5, 2022
4d95141
Fix: Wizzard button color
fsbraun Mar 5, 2022
1fdf757
Merge branch 'develop' of github.com:fsbraun/django-cms into develop
fsbraun Mar 5, 2022
4b0c0be
Merge branch 'develop' into develop
fsbraun Mar 12, 2022
872699a
Merge branch 'django-cms:develop' into develop
fsbraun Mar 22, 2022
281c355
Fix: Correct toolbar according to cms_path
fsbraun Mar 23, 2022
587cdb0
Merge branch 'django-cms:develop' into develop
fsbraun Mar 28, 2022
b67b53e
TASK: use isort to sort imports
Mar 29, 2022
9b8e9cb
Add: Color scheme configurable
fsbraun May 28, 2022
3c124c6
Add: Toolbar toggle (always on)
fsbraun May 28, 2022
656e0c6
Add: CMS_COLOR_SCHEME_TOGGLE setting
fsbraun May 29, 2022
092d6fd
Try fixing test
fsbraun May 29, 2022
f7c4d8f
Add: Basic documentation of new settings
fsbraun May 29, 2022
fb822a3
Merge branch 'develop' into featuire/dark-mode
May 30, 2022
2489871
Fix: unused imports, isort issue
fsbraun May 30, 2022
1857a63
Update CHANGELOG.rst
fsbraun May 30, 2022
b79c569
Fix: color scheme switch as css variables
fsbraun May 30, 2022
5952d77
Fix: Cascade settings into iframes of iframes (e.g., cms plugins ins…
fsbraun May 31, 2022
9093afb
Fix: no toggle in collapsed toolbar
fsbraun May 31, 2022
a2ff041
Merge branch 'develop' into featuire/dark-mode
Jun 6, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Changelog
unreleased
==========

* Added dark mode support to css
* Added dark mode support to css, dark mode settings and toggle button
* Fix publishing of static placeholders outside of CMS pages
* Allow to override the template rendered after a plugin has been saved.
* Revert change to the toolbar sites menu to use ``http`` protocol.
Expand Down
6 changes: 5 additions & 1 deletion cms/admin/settingsadmin.py
EDBE
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
HttpResponse, HttpResponseBadRequest, HttpResponseRedirect,
)
from django.http.request import QueryDict
from django.urls import re_path
from django.urls import Resolver404, re_path, resolve
from django.utils.translation import override

from cms.admin.forms import RequestToolbarForm
Expand Down Expand Up @@ -97,6 +97,10 @@ def get_toolbar(self, request):
request = copy.copy(request)
request.GET = data
request.current_page = current_page
try:
request.resolver_match = resolve(origin_url.path)
except Resolver404:
pass
request.toolbar = CMSToolbar(request, request_path=origin_url.path, _async=True)
request.toolbar.set_object(attached_obj or current_page)
return HttpResponse(request.toolbar.render())
Expand Down
16 changes: 16 additions & 0 deletions cms/cms_toolbars.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,22 @@ def add_wizard_button(self):
disabled=disabled,
on_close=REFRESH_PAGE)

@toolbar_pool.register
class AppearanceToolbar(CMSToolbar):
"""
Adds appearance switches, esp. for dark and light mode
"""
color_scheme_toggle = get_cms_setting('COLOR_SCHEME_TOGGLE')

def populate(self):
if self.color_scheme_toggle:
dark_mode_toggle = TemplateItem(
template="cms/toolbar/items/dark_mode_toggle.html",
side=self.toolbar.RIGHT,
)
self.toolbar.add_item(dark_mode_toggle)



@toolbar_pool.register
class BasicToolbar(CMSToolbar):
Expand Down
4 changes: 4 additions & 0 deletions cms/static/cms/fonts/src/moon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions cms/static/cms/fonts/src/sun.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 9 additions & 11 deletions cms/static/cms/js/modules/cms.structureboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -1022,26 +1022,24 @@ class StructureBoard {
// refresh toolbar
var currentMode = CMS.settings.mode;

this._loadToolbar()
.done(newToolbar => {
CMS.API.Toolbar._refreshMarkup($(newToolbar).find('.cms-toolbar'));
})
.fail(() => Helpers.reloadBrowser());

if (currentMode === 'structure') {
this._requestcontent = null;

if (this._loadedContent && action !== 'COPY') {
this.updateContent();
return; // Toolbar loaded
}
return;
}

// invalidate the content mode
if (action !== 'COPY') {
} else if (action !== 'COPY') {
this._requestcontent = null;
this.updateContent();
return; // Toolbar loaded

}
this._loadToolbar()
.done(newToolbar => {
CMS.API.Toolbar._refreshMarkup($(newToolbar).find('.cms-toolbar'));
})
.fail(() => Helpers.reloadBrowser());
}

_propagateInvalidatedState(action, data) {
Expand Down
67 changes: 67 additions & 0 deletions cms/static/cms/js/modules/cms.toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,12 @@ var Toolbar = new Class({
this._debug();
}

if (CMS.settings.color_scheme) {
this.set_color_scheme (CMS.settings.color_scheme);
} else if (CMS.config.color_scheme) {
this.set_color_scheme (CMS.config.color_scheme);
}

// check if there are messages and display them
if (CMS.config.messages) {
CMS.API.Messages.open({
Expand Down Expand Up @@ -616,6 +622,18 @@ var Toolbar = new Class({
onSuccess: el.data('on-success')
});
break;
case 'color-toggle':
switch (this.get_color_scheme()) {
case 'light':
this.set_color_scheme('dark');
break;
case 'dark':
this.set_color_scheme('light');
break;
default:
break;
}
break;
default:
Helpers._getWindow().location.href = el.attr('href');
}
Expand Down Expand Up @@ -756,6 +774,55 @@ var Toolbar = new Class({
CMS.API.Clipboard.ui.triggers = $('.cms-clipboard-trigger a');
CMS.API.Clipboard.ui.triggerRemove = $('.cms-clipboard-empty a');
CMS.API.Clipboard._toolbarEvents();
},

/**
* Get color scheme either from :root[data-color-scheme] or user system setting
*
* @method get_color_scheme
* @public
* @returns {String}
*/
get_color_scheme: function () {
let state = this.ui.body.attr('data-color-scheme');

if (!state && window.matchMedia) {
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
state = 'dark'; // dark mode
} else {
state = 'light';
}
}
return state;
},

/**
* Sets the color scheme for the current document and all iframes contained.
*
* @method set_color_scheme
* @public
* @param scheme {String}
* @retiurns {void}
*/

set_color_scheme: function (scheme) {
CMS.API.Helpers.setSettings({ color_scheme: scheme });
if (scheme === 'auto') {
this.ui.body.removeAttr('data-color-scheme');
this.ui.body.find('div.cms iframe').each(function(i, e) {
delete e.contentDocument.documentElement.dataset.colorScheme;
});
} else {
this.ui.body.attr('data-color-scheme', scheme);
this.ui.body.find('div.cms iframe').each(function setFrameColorScheme(i, e) {
if (e.contentDocument) {
e.contentDocument.documentElement.dataset.colorScheme = scheme;
// ckeditor (and potentially other apps) have iframes inside their admin forms
// also set color scheme there
$(e.contentDocument).find('iframe').each(setFrameColorScheme);
}
});
}
}
});

Expand Down
3 changes: 1 addition & 2 deletions cms/static/cms/sass/cms.base.scss
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
@import "components/dialog";
@import "components/content";
@import "components/loader";
@import "components/dark-mode-toggle";

// div.cms needs to beat .cms-reset a selectors
div.cms {
Expand All @@ -33,8 +34,6 @@ div.cms {
@import "components/pluginpicker";
@import "components/shortcuts";

color-scheme: light dark;

*:not(.cms-modal):focus {
outline: 2px dotted $gray-darker;
outline-offset: -3px;
Expand Down
25 changes: 25 additions & 0 deletions cms/static/cms/sass/components/_dark-mode-toggle.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@use "sass:math";

div.cms .cms-toolbar-item-dark-mode-toggle span.cms-icon.cms-toggle {
font-size: 170%;
line-height: 150%;
color: var(--dca-gray-darkest);
}

html[data-color-scheme="light"] .cms-toolbar-item-dark-mode-toggle .cms-toggle {
@extend .cms-icon-moon;
}

html[data-color-scheme="dark"] .cms-toolbar-item-dark-mode-toggle .cms-toggle {
@extend .cms-icon-sun;
}

html:not([data-color-scheme]) .cms-toolbar-item-dark-mode-toggle .cms-toggle {
@extend .cms-icon-moon;
}

@media (prefers-color-scheme: dark) {
html:not([data-color-scheme]) .cms-toolbar-item-dark-mode-toggle .cms-toggle {
// @extend .cms-icon-sun;
}
}
3 changes: 2 additions & 1 deletion cms/static/cms/sass/components/_dialog.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//##############################################################################
// DIALOG
@use "sass:math";

// used in: /cms/admin/page/tree/copy_premissions
.cms-dialog {
Expand All @@ -9,7 +10,7 @@
z-index: z(dialog);
box-sizing: border-box;
width: $dialog-width;
margin: -100px 0 0 (-$dialog-width/2);
margin: -100px 0 0 math.div(-$dialog-width, 2);
padding: $padding-large;
border: 1px solid $gray-lighter;
border-radius: $dialog-border-radius;
Expand Down
3 changes: 2 additions & 1 deletion cms/static/cms/sass/components/_hovertooltip.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//##############################################################################
// TOOLTIP
@use "sass:math";

.cms-hover-tooltip {
position: relative;
Expand Down Expand Up @@ -84,7 +85,7 @@
}

.cms-hover-tooltip:after {
margin-left: -$hover-tooltip-width / 2;
margin-left: math.div(-$hover-tooltip-width, 2);
}

.cms-hover-tooltip:hover:before,
Expand Down
38 changes: 26 additions & 12 deletions cms/static/cms/sass/components/_iconography.scss
Original file line number Diff line number Diff line change
Expand Up @@ -101,42 +101,48 @@
@if $filename == minus {
$char: "E019";
}
@if $filename == paste {
@if $filename == moon {
$char: "E01A";
}
@if $filename == pencil {
@if $filename == paste {
$char: "E01B";
}
@if $filename == pin {
@if $filename == pencil {
$char: "E01C";
}
@if $filename == plugins {
@if $filename == pin {
$char: "E01D";
}
@if $filename == plus-square-o {
@if $filename == plugins {
$char: "E01E";
}
@if $filename == plus {
@if $filename == plus-square-o {
$char: "E01F";
}
@if $filename == puzzle {
@if $filename == plus {
$char: "E020";
}
@if $filename == scissors {
@if $filename == puzzle {
$char: "E021";
}
@if $filename == search {
@if $filename == scissors {
$char: "E022";
}
@if $filename == sitemap {
@if $filename == search {
$char: "E023";
}
@if $filename == squares {
@if $filename == sitemap {
$char: "E024";
}
@if $filename == window {
@if $filename == squares {
$char: "E025";
}
@if $filename == sun {
$char: "E026";
}
@if $filename == window {
$char: "E027";
}

@return $char;
}
Expand Down Expand Up @@ -257,6 +263,10 @@
@include icon(minus);
}

.cms-icon-moon {
@include icon(moon);
}

.cms-icon-paste {
@include icon(paste);
}
Expand Down Expand Up @@ -301,6 +311,10 @@
@include icon(squares);
}

.cms-icon-sun {
@include icon(sun);
}

.cms-icon-window {
@include icon(window);
}
12 changes: 7 additions & 5 deletions cms/static/cms/sass/components/_modal.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//##############################################################################
// MODAL

@use "sass:math";

// Editing plugins in frontend
.cms-modal {
display: none;
Expand Down Expand Up @@ -151,7 +153,7 @@
display: block;
position: absolute;
top: 50%;
margin-top: -$modal-header-button-area-size / 2;
margin-top: math.div(-$modal-header-button-area-size, 2);
right: $padding-normal;
color: $gray-light;
text-align: center;
Expand All @@ -160,7 +162,7 @@
cursor: pointer;
&:before {
position: relative;
top: ($modal-header-button-area-size - $icon-size) / 2;
top: math.div($modal-header-button-area-size - $icon-size, 2);
}
&:hover {
color: $color-primary;
Expand Down Expand Up @@ -250,7 +252,7 @@
padding: 0 $modal-resize-size 0 $padding-normal;
}
.cms-modal-item-buttons {
$margin: ($toolbar-height - $toolbar-button-height) / 2;
$margin: math.div($toolbar-height - $toolbar-button-height, 2);
@extend .cms-toolbar-item-buttons;
float: right;
margin-left: $margin;
Expand All @@ -262,10 +264,10 @@
// alter footer when html markup is loaded
.cms-modal-markup {
.cms-modal-foot {
height: $modal-footer-height / 2;
height: math.div($modal-footer-height, 2);
}
.cms-modal-body {
bottom: $modal-footer-height / 2;
bottom: math.div($modal-footer-height, 2);
}
}

Expand Down
Loading
0