Skip to content

Commit

Permalink
Fix Wysiwyg/Trumbowyg editor colors when dark theme enabled (OrchardC…
Browse files Browse the repository at this point in the history
…MS#16356)

---------

Co-authored-by: mdameer <mohammad_dameer@hotmail.com>
Co-authored-by: Hisham Bin Ateya <hishamco_2007@yahoo.com>
Co-authored-by: Mike Alhayek <mike@crestapps.com>
  • Loading branch information
4 people authored Jun 27, 2024
1 parent 51eb7b8 commit 9133c52
Show file tree
Hide file tree
Showing 35 changed files with 250 additions and 141 deletions.
4 changes: 2 additions & 2 deletions src/OrchardCore.Modules/OrchardCore.ContentFields/Assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@
},
{
"inputs": [
"Assets/trumbowyg.media.tag.js"
"Assets/js/trumbowyg.media.tag.js"
],
"output": "wwwroot/Scripts/trumbowyg.media.tag.js"
},
{
"inputs": [
"Assets/trumbowyg.media.url.js"
"Assets/js/trumbowyg.media.url.js"
],
"output": "wwwroot/Scripts/trumbowyg.media.url.js"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Microsoft.Extensions.Options;
using OrchardCore.ResourceManagement;

namespace OrchardCore.ContentFields;

public sealed class ResourceManagementOptionsConfiguration : IConfigureOptions<ResourceManagementOptions>
{
public void Configure(ResourceManagementOptions options)
{
var manifest = new ResourceManifest();

manifest
.DefineScript("trumbowyg-media-url")
.SetUrl("~/OrchardCore.ContentFields/Scripts/trumbowyg.media.url.min.js", "~/OrchardCore.ContentFields/Scripts/trumbowyg.media.url.js")
.SetDependencies("trumbowyg")
.SetVersion("1.0.0");

manifest
.DefineScript("trumbowyg-media-tag")
.SetUrl("~/OrchardCore.ContentFields/Scripts/trumbowyg.media.tag.min.js", "~/OrchardCore.ContentFields/Scripts/trumbowyg.media.tag.js")
.SetDependencies("trumbowyg")
.SetVersion("1.0.0");

options.ResourceManifests.Add(manifest);
}
}
4 changes: 4 additions & 0 deletions src/OrchardCore.Modules/OrchardCore.ContentFields/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using Fluid;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using OrchardCore.ContentFields.Drivers;
using OrchardCore.ContentFields.Fields;
using OrchardCore.ContentFields.Handlers;
Expand All @@ -16,6 +17,7 @@
using OrchardCore.Data.Migration;
using OrchardCore.Indexing;
using OrchardCore.Modules;
using OrchardCore.ResourceManagement;
using OrchardCore.Users;

namespace OrchardCore.ContentFields
Expand All @@ -24,6 +26,8 @@ public sealed class Startup : StartupBase
{
public override void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IConfigureOptions<ResourceManagementOptions>, ResourceManagementOptionsConfiguration>();

services.Configure<TemplateOptions>(o =>
{
o.MemberAccessStrategy.Register<BooleanField>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
<script asp-name="trumbowyg-plugins" version="2" at="Foot"></script>
<style asp-name="trumbowyg-plugins" version="2"></style>
<script asp-name="trumbowyg-shortcodes" at="Foot"></script>
<script asp-name="trumbowyg-theme" at="Foot"></script>

@if (trumbowygSettings.InsertMediaWithUrl)
{
<script asp-src="~/OrchardCore.ContentFields/Scripts/trumbowyg.media.url.min.js" debug-src="~/OrchardCore.ContentFields/Scripts/trumbowyg.media.url.js" depends-on="trumbowyg" at="Foot"></script>
<script asp-name="trumbowyg-media-url" at="Foot"></script>
}
else
{
<script asp-src="~/OrchardCore.ContentFields/Scripts/trumbowyg.media.tag.min.js" debug-src="~/OrchardCore.ContentFields/Scripts/trumbowyg.media.tag.js" depends-on="trumbowyg" at="Foot"></script>
<script asp-name="trumbowyg-media-tag" at="Foot"></script>
}

<div class="@Orchard.GetFieldWrapperClasses(Model.PartFieldDefinition)" id="@Html.IdFor(x => x.Html)_FieldWrapper">
Expand Down Expand Up @@ -59,6 +61,7 @@ else
</text>
}
}
$(function () {
@if (!string.IsNullOrEmpty(trumbowygSettings.Options))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
}

<style asp-name="trumbowyg" version="2"></style>
<script asp-src="~/OrchardCore.ContentFields/Scripts/trumbowyg.media.tag.min.js" debug-src="~/OrchardCore.ContentFields/Scripts/trumbowyg.media.tag.js" depends-on="trumbowyg" at="Foot"></script>
<script asp-name="trumbowyg-media-tag" at="Foot"></script>
<script asp-name="trumbowyg-shortcodes" at="Foot"></script>

<script asp-name="trumbowyg-theme" at="Foot"></script>

<div class="@Orchard.GetFieldWrapperClasses(Model.PartFieldDefinition)" id="@Html.IdFor(x => x.Html)_FieldWrapper">
<label asp-for="Html" class="@Orchard.GetLabelClasses()">@Model.PartFieldDefinition.DisplayName()</label>
Expand Down
16 changes: 11 additions & 5 deletions src/OrchardCore.Modules/OrchardCore.Resources/Assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -182,26 +182,32 @@
"inputs": [
"node_modules/trumbowyg/dist/ui/sass/trumbowyg.scss"
],
"output": "wwwroot/Styles/trumbowyg.css"
"output": "wwwroot/Styles/trumbowyg/trumbowyg.css"
},
{
"generateRTL": true,
"inputs": [
"Assets/trumbowyg/plugins/*/ui/sass/*.scss"
],
"output": "wwwroot/Styles/trumbowyg-plugins.css"
"output": "wwwroot/Styles/trumbowyg/trumbowyg-plugins.css"
},
{
"inputs": [
"node_modules/trumbowyg/dist/trumbowyg.js"
],
"output": "wwwroot/Scripts/trumbowyg.js"
"output": "wwwroot/Scripts/trumbowyg/trumbowyg.js"
},
{
"inputs": [
"Assets/trumbowyg/js/trumbowyg.shortcodes.js"
],
"output": "wwwroot/Scripts/trumbowyg.shortcodes.js"
"output": "wwwroot/Scripts/trumbowyg/trumbowyg.shortcodes.js"
},
{
"inputs": [
"Assets/trumbowyg/js/trumbowyg.theme.js"
],
"output": "wwwroot/Scripts/trumbowyg/trumbowyg.theme.js"
},
{
"inputs": [
Expand Down Expand Up @@ -229,7 +235,7 @@
"Assets/trumbowyg/plugins/template/trumbowyg.template.js",
"Assets/trumbowyg/plugins/upload/trumbowyg.upload.js"
],
"output": "wwwroot/Scripts/trumbowyg-plugins.js"
"output": "wwwroot/Scripts/trumbowyg/trumbowyg-plugins.js"
},
{
"copy": true,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
document.addEventListener('DOMContentLoaded', function () {
function setTheme() {
var isDark = getPreferredTheme() == darkThemeName;

document
.querySelectorAll('.trumbowyg')
.forEach((element) =>
element.parentElement.classList.toggle('trumbowyg-dark', isDark)
);
}

const mutationObserver = new MutationObserver(setTheme);
mutationObserver.observe(document.documentElement, {
attributes: true,
childList: true,
subtree: true,
});

setTheme();
});

Original file line number Diff line number Diff line change
Expand Up @@ -412,35 +412,41 @@ ResourceManifest BuildManifest()

manifest
.DefineStyle("trumbowyg")
.SetUrl("~/OrchardCore.Resources/Styles/trumbowyg.min.css", "~/OrchardCore.Resources/Styles/trumbowyg.css")
.SetUrl("~/OrchardCore.Resources/Styles/trumbowyg/trumbowyg.min.css", "~/OrchardCore.Resources/Styles/trumbowyg/trumbowyg.css")
.SetCdn("https://cdn.jsdelivr.net/npm/trumbowyg@2.28.0/dist/ui/trumbowyg.min.css", "https://cdn.jsdelivr.net/npm/trumbowyg@2.28.0/dist/ui/trumbowyg.css")
.SetCdnIntegrity("sha384-XfI6P0jtm0X3QDEQxS1DotzhIXkJeuSV1wtBOntPaRxzpzTkMWkpZpkuzj8qVHzl", "sha384-GUyfWhYsIKKkAMejJuy50VTSfyfkrrJX2csg7fxJJt7vi+gXH8qxqH29C5GURaum")
.SetVersion("2.28.0");

manifest
.DefineScript("trumbowyg")
.SetDependencies("jquery-resizable")
.SetUrl("~/OrchardCore.Resources/Scripts/trumbowyg.min.js", "~/OrchardCore.Resources/Scripts/trumbowyg.js")
.SetUrl("~/OrchardCore.Resources/Scripts/trumbowyg/trumbowyg.min.js", "~/OrchardCore.Resources/Scripts/trumbowyg/trumbowyg.js")
.SetCdn("https://cdn.jsdelivr.net/npm/trumbowyg@2.28.0/dist/trumbowyg.min.js", "https://cdn.jsdelivr.net/npm/trumbowyg@2.28.0/dist/trumbowyg.js")
.SetCdnIntegrity("sha384-O4OAGMDq5hnqt4/WQz+fW6yVgZ02jmw+Yf1j02zIgglnCYXf/7TmET8tFbrTN6u5", "sha384-dNxlebCuuiNWPhPBEd69nEAtkEWa7Z9IWkrL+OSmJ456dlu6TAASXgL72Bn4GGju")
.SetVersion("2.28.0");

manifest
.DefineScript("trumbowyg-shortcodes")
.SetDependencies("trumbowyg")
.SetUrl("~/OrchardCore.Resources/Scripts/trumbowyg.shortcodes.min.js", "~/OrchardCore.Resources/Scripts/trumbowyg.shortcodes.js")
.SetUrl("~/OrchardCore.Resources/Scripts/trumbowyg/trumbowyg.shortcodes.min.js", "~/OrchardCore.Resources/Scripts/trumbowyg/trumbowyg.shortcodes.js")
.SetVersion("1.0.0");

manifest
.DefineScript("trumbowyg-theme")
.SetUrl("~/OrchardCore.Resources/Scripts/trumbowyg/trumbowyg.theme.min.js", "~/OrchardCore.Resources/Scripts/trumbowyg/trumbowyg.theme.js")
.SetDependencies("trumbowyg", "theme-manager")
.SetVersion("1.0.0");

manifest
.DefineStyle("trumbowyg-plugins")
.SetDependencies("trumbowyg")
.SetUrl("~/OrchardCore.Resources/Styles/trumbowyg-plugins.min.css", "~/OrchardCore.Resources/Styles/trumbowyg-plugins.css")
.SetUrl("~/OrchardCore.Resources/Styles/trumbowyg/trumbowyg-plugins.min.css", "~/OrchardCore.Resources/Styles/trumbowyg/trumbowyg-plugins.css")
.SetVersion("2.28.0");

manifest
.DefineScript("trumbowyg-plugins")
.SetDependencies("trumbowyg")
.SetUrl("~/OrchardCore.Resources/Scripts/trumbowyg-plugins.min.js", "~/OrchardCore.Resources/Scripts/trumbowyg-plugins.js")
.SetUrl("~/OrchardCore.Resources/Scripts/trumbowyg/trumbowyg-plugins.min.js", "~/OrchardCore.Resources/Scripts/trumbowyg/trumbowyg-plugins.js")
.SetVersion("2.28.0");

manifest
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
** 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.
*/

document.addEventListener('DOMContentLoaded', function () {
function setTheme() {
var isDark = getPreferredTheme() == darkThemeName;
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,
childList: true,
subtree: true
});
setTheme();
});

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

6 changes: 6 additions & 0 deletions src/OrchardCore.Modules/OrchardCore.Themes/Assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@
"Assets/scss/admin.scss"
],
"output": "wwwroot/Styles/themes.admin.css"
},
{
"inputs": [
"Assets/js/theme-manager.js"
],
"output": "wwwroot/Scripts/theme-manager.js"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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 getStoreKeySuffix = () => themeStoreKeySuffix || 'theme';
const getStoreKey = () => `${getTenantName()}-${getStoreKeySuffix()}`;
const getStoredTheme = () => localStorage.getItem(getStoreKey());
const setStoredTheme = (theme) => localStorage.setItem(getStoreKey(), theme);
const isDarkMedia = () => window.matchMedia('(prefers-color-scheme: dark)').matches;

const getPreferredTheme = () => {
const storedTheme = getStoredTheme();
if (storedTheme) {
return storedTheme;
}

return isDarkMedia() ? darkThemeName : lightThemeName;
};

const setTheme = (theme) => {
if (theme === 'auto') {
document.documentElement.setAttribute('data-bs-theme', isDarkMedia() ? darkThemeName : lightThemeName);
} else {
document.documentElement.setAttribute('data-bs-theme', theme);
}
};

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Microsoft.Extensions.Options;
using OrchardCore.Environment.Shell;
using OrchardCore.ResourceManagement;

namespace OrchardCore.Themes;

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

public ResourceManagementOptionsConfiguration(ShellSettings shellSettings)
{
_shellSettings = shellSettings;
}

public void Configure(ResourceManagementOptions options)
{
var manifest = new ResourceManifest();

manifest
.DefineScript("theme-manager")
.SetAttribute("data-tenant-name", _shellSettings.Name)
.SetUrl("~/OrchardCore.Themes/Scripts/theme-manager.min.js", "~/OrchardCore.Themes/Scripts/theme-manager.js")
.SetVersion("1.0.0");

options.ResourceManifests.Add(manifest);
}
}
3 changes: 3 additions & 0 deletions src/OrchardCore.Modules/OrchardCore.Themes/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using OrchardCore.Admin.Models;
using OrchardCore.Deployment;
using OrchardCore.DisplayManagement.Handlers;
using OrchardCore.DisplayManagement.Theming;
using OrchardCore.Modules;
using OrchardCore.Navigation;
using OrchardCore.Recipes;
using OrchardCore.ResourceManagement;
using OrchardCore.Security.Permissions;
using OrchardCore.Themes.Deployment;
using OrchardCore.Themes.Drivers;
Expand All @@ -22,6 +24,7 @@ public sealed class Startup : StartupBase
{
public override void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IConfigureOptions<ResourceManagementOptions>, ResourceManagementOptionsConfiguration>();
services.AddRecipeExecutionStep<ThemesStep>();
services.AddScoped<IPermissionProvider, Permissions>();
services.AddScoped<IThemeSelector, SiteThemeSelector>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
** 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 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);
}
};

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

Loading

0 comments on commit 9133c52

Please sign in to comment.