Skip to content

Commit

Permalink
Add basic support for conditional dark mode themes in our editors (#4…
Browse files Browse the repository at this point in the history
  • Loading branch information
wilsonge authored Nov 18, 2023
1 parent 9a2e18c commit 84585cc
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 6 deletions.
2 changes: 2 additions & 0 deletions administrator/language/en-GB/plg_editors_tinymce.ini
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ PLG_TINY_FIELD_RESIZE_HORIZONTAL_LABEL="Horizontal Resizing"
PLG_TINY_FIELD_RESIZING_LABEL="Resizing"
PLG_TINY_FIELD_SETACCESS_LABEL="Assign this Set to"
PLG_TINY_FIELD_SKIN_ADMIN_LABEL="Administrator Skin"
PLG_TINY_FIELD_SKIN_ADMIN_DARK_LABEL="Administrator Skin (Dark Mode)"
PLG_TINY_FIELD_SKIN_INFO_DESC="Copy your new skins to: /media/vendor/tinymce/skins/ui."
PLG_TINY_FIELD_SKIN_INFO_LABEL="For customised skins go to: <a href=\"https://skin.tiny.cloud/t5/\" target=\"_blank\" rel=\"noopener noreferrer\">Skin Creator</a>"
PLG_TINY_FIELD_SKIN_LABEL="Site Skin"
PLG_TINY_FIELD_SKIN_DARK_LABEL="Site Skin (Dark Mode)"
PLG_TINY_FIELD_SOURCECODE_LABEL="Source Code Highlighting"
PLG_TINY_FIELD_TEXTPATTERN_DESC="Use Markdown syntax to compose content with links, lists, and other styles." ; Do not translate the word Markdown
PLG_TINY_FIELD_TEXTPATTERN_LABEL="Markdown"
Expand Down
2 changes: 1 addition & 1 deletion administrator/templates/atum/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
<jdoc:include type="scripts" />
</head>

<body class="admin <?php echo $option . ' view-' . $view . ' layout-' . $layout . ($task ? ' task-' . $task : '') . ($monochrome || $a11y_mono ? ' monochrome' : '') . ($a11y_contrast ? ' a11y_contrast' : '') . ($a11y_highlight ? ' a11y_highlight' : ''); ?>">
<body data-color-scheme-os class="admin <?php echo $option . ' view-' . $view . ' layout-' . $layout . ($task ? ' task-' . $task : '') . ($monochrome || $a11y_mono ? ' monochrome' : '') . ($a11y_contrast ? ' a11y_contrast' : '') . ($a11y_highlight ? ' a11y_highlight' : ''); ?>">
<noscript>
<div class="alert alert-danger" role="alert">
<?php echo Text::_('JGLOBAL_WARNJAVASCRIPT'); ?>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

joomla-editor-codemirror {
display: block;
background-color: #fff;
}
.cm-editor {
margin-bottom: 16px;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
} from '@codemirror/commands';
import { highlightSelectionMatches, searchKeymap } from '@codemirror/search';
import { closeBrackets } from '@codemirror/autocomplete';
import { oneDark } from '@codemirror/theme-one-dark';

const minimalSetup = (() => [
highlightSpecialChars(),
Expand Down Expand Up @@ -60,7 +61,7 @@ const optionsToExtensions = async (options) => {
extensions.push(modeMod[options.mode](modeOptions));
}).catch((error) => {
// eslint-disable-next-line no-console
console.error(`Cannot creat an extension for "${options.mode}" syntax mode.`, error);
console.error(`Cannot create an extension for "${options.mode}" syntax mode.`, error);
}));
}

Expand Down Expand Up @@ -104,6 +105,11 @@ const optionsToExtensions = async (options) => {
readOnly.$j_name = 'readOnly';
extensions.push(readOnly.of(EditorState.readOnly.of(!!options.readOnly)));

// TODO: Use compartments to update on change of dark mode like: https://discuss.codemirror.net/t/dynamic-light-mode-dark-mode-how/4709
if ('colorSchemeOs' in document.body.dataset && window.matchMedia('(prefers-color-scheme: dark)').matches) {
extensions.push(oneDark);
}

// Check for custom extensions,
// in format [['module1 name or URL', ['init method2']], ['module2 name or URL', ['init method2']], () => <return extension>]
if (options.customExtensions && options.customExtensions.length) {
Expand Down
12 changes: 12 additions & 0 deletions build/media_source/plg_editors_tinymce/js/tinymce.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,18 @@ Joomla.JoomlaTinyMCE = {
options.target = element;
}

const skinLight = options.skin_light;
const skinDark = options.skin_dark;

if ('colorSchemeOs' in document.body.dataset) {
options.skin = (window.matchMedia('(prefers-color-scheme: dark)').matches ? skinDark : skinLight);
} else {
options.skin = skinLight;
}

delete options.skin_light;
delete options.skin_dark;

// Ensure tinymce is initialised in readonly mode if the textarea has readonly applied
let readOnlyMode = false;

Expand Down
1 change: 1 addition & 0 deletions layouts/plugins/editors/tinymce/field/tinymcebuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@

$wa->getRegistry()->addExtensionRegistryFile('plg_editors_tinymce');
$wa->registerAndUseStyle('tinymce.skin', 'media/vendor/tinymce/skins/ui/oxide/skin.min.css')
->registerAndUseStyle('tinymce.skin.dark', 'media/vendor/tinymce/skins/ui/oxide-dark/skin.min.css')
->registerAndUseStyle('plg_editors_tinymce.builder', 'plg_editors_tinymce/tinymce-builder.css', [], [], ['tinymce.skin', 'dragula'])
->registerScript('plg_editors_tinymce.builder', 'plg_editors_tinymce/tinymce-builder.js', [], ['type' => 'module'], ['dragula', 'plg_editors_tinymce'])
->useScript('plg_editors_tinymce.builder')
Expand Down
12 changes: 12 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@codemirror/lint": "^6.4.1",
"@codemirror/search": "^6.5.2",
"@codemirror/state": "^6.2.1",
"@codemirror/theme-one-dark": "^6.1.2",
"@codemirror/view": "^6.17.1",
"@fortawesome/fontawesome-free": "^6.4.2",
"@joomla/joomla-a11y-checker": "^1.0.0",
Expand Down
20 changes: 20 additions & 0 deletions plugins/editors/tinymce/forms/setoptions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@
validate="options"
/>

<field
name="skin_dark"
type="folderlist"
label="PLG_TINY_FIELD_SKIN_DARK_LABEL"
directory="media/vendor/tinymce/skins/ui"
recursive="false"
hide_none="true"
validate="options"
/>

<field
name="skin_admin"
type="folderlist"
Expand All @@ -39,6 +49,16 @@
validate="options"
/>

<field
name="skin_admin_dark"
type="folderlist"
label="PLG_TINY_FIELD_SKIN_ADMIN_DARK_LABEL"
directory="media/vendor/tinymce/skins/ui"
recursive="false"
hide_none="true"
validate="options"
/>

<field
name="toolbar_mode"
type="list"
Expand Down
9 changes: 6 additions & 3 deletions plugins/editors/tinymce/src/PluginTraits/DisplayTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,12 @@ public function display(string $name, string $content = '', array $attributes =
$levelParams->loadObject($extraOptions);

// Set the selected skin
$skin = $levelParams->get($app->isClient('administrator') ? 'skin_admin' : 'skin', 'oxide');
$skin = $levelParams->get($app->isClient('administrator') ? 'skin_admin' : 'skin', 'oxide');
$skinDark = $levelParams->get($app->isClient('administrator') ? 'skin_admin_dark' : 'skin_dark', 'oxide-dark');

// Check that selected skin exists.
$skin = Folder::exists(JPATH_ROOT . '/media/vendor/tinymce/skins/ui/' . $skin) ? $skin : 'oxide';
$skin = Folder::exists(JPATH_ROOT . '/media/vendor/tinymce/skins/ui/' . $skin) ? $skin : 'oxide';
$skinDark = Folder::exists(JPATH_ROOT . '/media/vendor/tinymce/skins/ui/' . $skinDark) ? $skinDark : 'oxide-dark';

if (!$levelParams->get('lang_mode', 1)) {
// Admin selected language
Expand Down Expand Up @@ -390,7 +392,8 @@ public function display(string $name, string $content = '', array $attributes =
'directionality' => $language->isRtl() ? 'rtl' : 'ltr',
'language' => $langPrefix,
'autosave_restore_when_empty' => false,
'skin' => $skin,
'skin_light' => $skin,
'skin_dark' => $skinDark,
'theme' => $theme,
'schema' => 'html5',

Expand Down

0 comments on commit 84585cc

Please sign in to comment.