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 theme manager script #16386

Merged
merged 11 commits into from
Jul 3, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,4 @@ else
$(document).trigger('contentpreview:render');
});
});
$.trumbowyg.svgPath = '@Url.Content("~/OrchardCore.Resources/Styles/ui/icons.svg")';
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,4 @@ else
$(document).trigger('contentpreview:render');
});
});
$.trumbowyg.svgPath = '@Url.Content("~/OrchardCore.Resources/Styles/ui/icons.svg")';
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<style asp-name="trumbowyg" version="2"></style>
<script asp-src="~/OrchardCore.Html/Scripts/trumbowyg.media.tag.min.js" debug-src="~/OrchardCore.Html/Scripts/trumbowyg.media.tag.js" depends-on="trumbowyg" at="Foot"></script>
<script asp-name="trumbowyg-shortcodes" at="Foot"></script>
<script asp-name="trumbowyg-theme" at="Foot"></script>

@await DisplayAsync(await New.ShortcodeModal())

Expand Down
7 changes: 7 additions & 0 deletions src/OrchardCore.Modules/OrchardCore.Resources/Assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,13 @@
],
"output": "wwwroot/Styles/trumbowyg/trumbowyg.css"
},
{
"copy": true,
"inputs": [
"node_modules/trumbowyg/dist/ui/icons.svg"
],
"output": "wwwroot/Scripts/trumbowyg/ui/icons.svg"
},
{
"generateRTL": true,
"inputs": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
document.addEventListener('DOMContentLoaded', function () {
function setTheme() {
var isDark = getPreferredTheme() == darkThemeName;
const setTheme = () => {
var isDark = false;

if (typeof getPreferredTheme === 'function') {
isDark = getPreferredTheme() == (darkThemeName || 'dark');
} else {
isDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
}

document
.querySelectorAll('.trumbowyg')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ ResourceManifest BuildManifest()
manifest
.DefineScript("trumbowyg-theme")
.SetUrl("~/OrchardCore.Resources/Scripts/trumbowyg/trumbowyg.theme.min.js", "~/OrchardCore.Resources/Scripts/trumbowyg/trumbowyg.theme.js")
.SetDependencies("trumbowyg", "theme-manager")
.SetDependencies("trumbowyg", "theme-head")
.SetVersion("1.0.0");

manifest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@
*/

document.addEventListener('DOMContentLoaded', function () {
function setTheme() {
var isDark = getPreferredTheme() == darkThemeName;
var setTheme = function setTheme() {
var isDark = false;
if (typeof getPreferredTheme === 'function') {
isDark = getPreferredTheme() == (darkThemeName || 'dark');
} else {
isDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
}
document.querySelectorAll('.trumbowyg').forEach(function (element) {
return element.parentElement.classList.toggle('trumbowyg-dark', isDark);
});
}
};
var mutationObserver = new MutationObserver(setTheme);
mutationObserver.observe(document.documentElement, {
attributes: true,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

This file was deleted.

16 changes: 15 additions & 1 deletion src/OrchardCore.Modules/OrchardCore.Themes/Assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,21 @@
},
{
"inputs": [
"Assets/js/theme-manager.js"
"Assets/js/constants.js",
"Assets/js/theme-loader.js"
],
"watch": [
"Assets/js/constants.js",
"Assets/js/theme-loader.js"
],
"output": "wwwroot/Scripts/theme-head.js"
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved
},
{
"inputs": [
"Assets/js/theme-toggler.js"
],
"watch": [
"Assets/js/theme-toggler.js"
],
"output": "wwwroot/Scripts/theme-manager.js"
Copy link
Contributor

Choose a reason for hiding this comment

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

In this case where you separate the theme-head from the toggler, then I think theme-toggler.js will be more clear, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

After cleanup the dependency, I thought theme-manager is better fit. Because I added dependency for the theme manager to the theme-head. So including the theme-manager will include the theme-head automatically. The reason I included them separately to ensure that the theme-head is loaded early on. So theme-manager would be fine.

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const darkThemeName = 'dark';
const lightThemeName = 'light';
const tenantName = document.currentScript.dataset.tenantName;
const themeStoreKeySuffix = 'theme';

const getTenantName = () => tenantName || document.documentElement.getAttribute('data-tenant') || 'default';
const getTenantName = () => document.documentElement.getAttribute('data-tenant') || 'default';
const getStoreKeySuffix = () => themeStoreKeySuffix || 'theme';
const getStoreKey = () => `${getTenantName()}-${getStoreKeySuffix()}`;
const getStoredTheme = () => localStorage.getItem(getStoreKey());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// We add some classes to the body tag to restore the sidebar to the state is was before reload.
// That state was saved to localstorage by userPreferencesPersistor.js
// We need to apply the classes BEFORE the page is rendered.
// That is why we use a MutationObserver instead of document.Ready().
const themeObserver = new MutationObserver(function (mutations) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
using Microsoft.Extensions.Options;
using OrchardCore.Environment.Shell;
using OrchardCore.ResourceManagement;

namespace OrchardCore.Themes;

public sealed class ResourceManagementOptionsConfiguration : IConfigureOptions<ResourceManagementOptions>
{
private readonly ShellSettings _shellSettings;
private static readonly ResourceManifest _manifest;

public ResourceManagementOptionsConfiguration(ShellSettings shellSettings)
static ResourceManagementOptionsConfiguration()
{
_shellSettings = shellSettings;
}
_manifest = new ResourceManifest();

public void Configure(ResourceManagementOptions options)
{
var manifest = new ResourceManifest();
_manifest
.DefineScript("theme-head")
.SetUrl("~/OrchardCore.Themes/Scripts/theme-head.min.js", "~/OrchardCore.Themes/Scripts/theme-head.js")
.SetVersion("1.0.0");

manifest
_manifest
.DefineScript("theme-manager")
.SetAttribute("data-tenant-name", _shellSettings.Name)
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved
.SetUrl("~/OrchardCore.Themes/Scripts/theme-manager.min.js", "~/OrchardCore.Themes/Scripts/theme-manager.js")
.SetDependencies("theme-head")
.SetVersion("1.0.0");
}

options.ResourceManifests.Add(manifest);
public void Configure(ResourceManagementOptions options)
{
options.ResourceManifests.Add(_manifest);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
** NOTE: This file is generated by Gulp and should not be edited directly!
** Any changes made directly to this file will be overwritten next time its asset group is processed by Gulp.
*/

var darkThemeName = 'dark';
var lightThemeName = 'light';
var themeStoreKeySuffix = 'theme';
var getTenantName = function getTenantName() {
return document.documentElement.getAttribute('data-tenant') || 'default';
};
var getStoreKeySuffix = function getStoreKeySuffix() {
return themeStoreKeySuffix || 'theme';
};
var getStoreKey = function getStoreKey() {
return "".concat(getTenantName(), "-").concat(getStoreKeySuffix());
};
var getStoredTheme = function getStoredTheme() {
return localStorage.getItem(getStoreKey());
};
var setStoredTheme = function setStoredTheme(theme) {
return localStorage.setItem(getStoreKey(), theme);
};
var isDarkMedia = function isDarkMedia() {
return window.matchMedia('(prefers-color-scheme: dark)').matches;
};
var getPreferredTheme = function getPreferredTheme() {
var storedTheme = getStoredTheme();
if (storedTheme) {
return storedTheme;
}
return isDarkMedia() ? darkThemeName : lightThemeName;
};
var setTheme = function setTheme(theme) {
if (theme === 'auto') {
document.documentElement.setAttribute('data-bs-theme', isDarkMedia() ? darkThemeName : lightThemeName);
} else {
document.documentElement.setAttribute('data-bs-theme', theme);
}
};
// We need to apply the classes BEFORE the page is rendered.
// That is why we use a MutationObserver instead of document.Ready().
var themeObserver = new MutationObserver(function (mutations) {
for (var i = 0; i < mutations.length; i++) {
for (var j = 0; j < mutations[i].addedNodes.length; j++) {
if (mutations[i].addedNodes[j].tagName == 'BODY') {
setTheme(getPreferredTheme());

// we're done:
themeObserver.disconnect();
}
;
}
}
});
themeObserver.observe(document.documentElement, {
childList: true,
subtree: true
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,48 @@
** Any changes made directly to this file will be overwritten next time its asset group is processed by Gulp.
*/

var darkThemeName = 'dark';
var lightThemeName = 'light';
var tenantName = document.currentScript.dataset.tenantName;
var themeStoreKeySuffix = 'theme';
var getTenantName = function getTenantName() {
return tenantName || document.documentElement.getAttribute('data-tenant') || 'default';
};
var getStoreKeySuffix = function getStoreKeySuffix() {
return themeStoreKeySuffix || 'theme';
};
var getStoreKey = function getStoreKey() {
return "".concat(getTenantName(), "-").concat(getStoreKeySuffix());
};
var getStoredTheme = function getStoredTheme() {
return localStorage.getItem(getStoreKey());
};
var setStoredTheme = function setStoredTheme(theme) {
return localStorage.setItem(getStoreKey(), theme);
};
var isDarkMedia = function isDarkMedia() {
return window.matchMedia('(prefers-color-scheme: dark)').matches;
};
var getPreferredTheme = function getPreferredTheme() {
var storedTheme = getStoredTheme();
if (storedTheme) {
return storedTheme;
}
return isDarkMedia() ? darkThemeName : lightThemeName;
};
var setTheme = function setTheme(theme) {
if (theme === 'auto') {
document.documentElement.setAttribute('data-bs-theme', isDarkMedia() ? darkThemeName : lightThemeName);
} else {
document.documentElement.setAttribute('data-bs-theme', theme);
}
};
(function () {
'use strict';

var showActiveTheme = function showActiveTheme(theme) {
var focus = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
var themeSwitcher = document.querySelector('#bd-theme');
if (!themeSwitcher) {
return;
}
var themeSwitcherText = document.querySelector('#bd-theme-text');
var activeThemeIcon = document.querySelector('.theme-icon-active');
var btnToActive = document.querySelector("[data-bs-theme-value=\"".concat(theme, "\"]"));
var svgOfActiveBtn = btnToActive.querySelector('.theme-icon');
btnToActive.classList.add('active');
btnToActive.setAttribute('aria-pressed', 'true');
activeThemeIcon.innerHTML = svgOfActiveBtn.innerHTML;
var themeSwitcherLabel = "".concat(themeSwitcherText.textContent, " (").concat(btnToActive.dataset.bsThemeValue, ")");
themeSwitcher.setAttribute('aria-label', themeSwitcherLabel);
var btnsToInactive = document.querySelectorAll("[data-bs-theme-value]:not([data-bs-theme-value=\"".concat(theme, "\"])"));
for (var i = 0; i < btnsToInactive.length; i++) {
btnsToInactive[i].classList.remove('active');
btnsToInactive[i].setAttribute('aria-pressed', 'false');
}
if (focus) {
themeSwitcher.focus();
}
};
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', function () {
var storedTheme = getStoredTheme();
if (storedTheme !== lightThemeName && storedTheme !== darkThemeName) {
setTheme(getPreferredTheme());
}
});
window.addEventListener('DOMContentLoaded', function () {
showActiveTheme(getPreferredTheme());
document.querySelectorAll('[data-bs-theme-value]').forEach(function (toggle) {
toggle.addEventListener('click', function () {
var theme = toggle.getAttribute('data-bs-theme-value');
setStoredTheme(theme);
setTheme(theme);
showActiveTheme(theme, true);
});
});
});
})();

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 1 addition & 10 deletions src/OrchardCore.Themes/TheAdmin/Assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,10 @@
{
"inputs": [
"Assets/js/header/global/*.js",
"Assets/js/header/theme/*.js"
],
"watch": [
"Assets/js/header/global/*.js",
"Assets/js/header/theme/*.js"
],
"output": "wwwroot/js/TheAdmin-header.js"
},
{
"inputs": [
"Assets/js/header/main/*.js"
],
"watch": [
"Assets/js/header/global/*.js",
"Assets/js/header/main/*.js"
],
"output": "wwwroot/js/TheAdmin-main.js"
Expand Down
9 changes: 8 additions & 1 deletion src/OrchardCore.Themes/TheAdmin/Manifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,12 @@
Website = ManifestConstants.OrchardCoreWebsite,
Version = ManifestConstants.OrchardCoreVersion,
Description = "The default Admin theme.",
Tags = [ManifestConstants.AdminTag]
Dependencies =
[
"OrchardCore.Themes",
],
Tags =
[
ManifestConstants.AdminTag,
]
)]
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,14 @@ static ResourceManagementOptionsConfiguration()

_manifest
.DefineScript("admin")
.SetDependencies("bootstrap", "admin-head", "jQuery")
.SetDependencies("bootstrap", "admin-main", "theme-manager", "jQuery")
.SetUrl("~/TheAdmin/js/TheAdmin.min.js", "~/TheAdmin/js/TheAdmin.js")
.SetVersion("1.0.0");

_manifest
.DefineScript("admin-head")
.SetUrl("~/TheAdmin/js/TheAdmin-header.min.js", "~/TheAdmin/js/TheAdmin-header.js")
.SetDependencies("js-cookie", "theme-manager")
.SetVersion("1.0.0");

_manifest
.DefineScript("admin-main")
.SetUrl("~/TheAdmin/js/TheAdmin-main.min.js", "~/TheAdmin/js/TheAdmin-main.js")
.SetDependencies("admin-head")
.SetDependencies("theme-head", "js-cookie")
.SetVersion("1.0.0");

_manifest
Expand Down
2 changes: 1 addition & 1 deletion src/OrchardCore.Themes/TheAdmin/Views/Layout-Login.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<link type="image/x-icon" rel="shortcut icon" href="~/TheAdmin/favicon.ico" />

<!-- This script can't wait till the footer -->
<script asp-name="admin-head" version="1" at="Head"></script>
<script asp-name="admin-main" version="1" at="Head"></script>

<!-- Bootstrap CSS -->
@if (Orchard.IsRightToLeft())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<link type="image/x-icon" rel="shortcut icon" href="~/TheAdmin/favicon.ico" />

<!-- This script can't wait till the footer -->
<script asp-name="admin-head" version="1" at="Head"></script>
<script asp-name="admin-main" version="1" at="Head"></script>

<!-- Bootstrap CSS -->
@if (Orchard.IsRightToLeft())
Expand Down
Loading