From dbe6494d93c5e4907a199576dbf4d3fa7321608e Mon Sep 17 00:00:00 2001 From: dzonidoo Date: Fri, 25 Aug 2023 15:33:23 +0200 Subject: [PATCH 1/4] link authoring theme with selected superdesk one --- .../services/AuthoringThemesService.ts | 12 ++++++++++++ .../directives/UserPreferencesDirective.ts | 17 ++++++----------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/scripts/apps/authoring/authoring/services/AuthoringThemesService.ts b/scripts/apps/authoring/authoring/services/AuthoringThemesService.ts index cd0f0a2b2e..3b46275e74 100644 --- a/scripts/apps/authoring/authoring/services/AuthoringThemesService.ts +++ b/scripts/apps/authoring/authoring/services/AuthoringThemesService.ts @@ -101,5 +101,17 @@ export function AuthoringThemesService(storage, preferencesService) { }); }; + service.syncWithApplicationTheme = (appTheme: string) => { + service.get('theme').then((res) => { + if (res.theme === 'default' || res.theme === 'dark') { + let activeTheme = appTheme === 'dark-ui' + ? {...res, theme: 'dark'} + : {...res, theme: 'default'}; + + service.save('theme', activeTheme); + } + }); + }; + return service; } diff --git a/scripts/apps/users/directives/UserPreferencesDirective.ts b/scripts/apps/users/directives/UserPreferencesDirective.ts index 49fba616da..41570e778a 100644 --- a/scripts/apps/users/directives/UserPreferencesDirective.ts +++ b/scripts/apps/users/directives/UserPreferencesDirective.ts @@ -14,11 +14,11 @@ import {applyDefault} from 'core/helpers/typescript-helpers'; * themselves. */ UserPreferencesDirective.$inject = ['session', 'preferencesService', 'notify', 'asset', - 'metadata', 'desks', 'modal', '$timeout', '$q', 'userList', 'lodash', 'search']; + 'metadata', 'desks', 'modal', '$timeout', '$q', 'userList', 'lodash', 'search', 'authThemes']; export function UserPreferencesDirective( session, preferencesService, notify, asset, metadata, desks, modal, - $timeout, $q, userList, _, search, + $timeout, $q, userList, _, search, authThemes, ) { // human readable labels for server values const LABELS = { @@ -46,15 +46,6 @@ export function UserPreferencesDirective( scope.activeNavigation = null; scope.activeTheme = localStorage.getItem('theme'); - scope.$watch('activeTheme', (val) => { - if (!val) { - return; - } - - localStorage.setItem('theme', val); - body.attr('data-theme', val); - }); - /* * Set this to true after adding all the preferences to the scope. If done before, then the * directives which depend on scope variables might fail to load properly. @@ -119,6 +110,10 @@ export function UserPreferencesDirective( if (_.get(preferences, 'desktop:notification.enabled')) { preferencesService.desktopNotification.requestPermission(); } + + authThemes.syncWithApplicationTheme(scope.activeTheme); + localStorage.setItem('theme', scope.activeTheme); + body.attr('data-theme', scope.activeTheme); notify.success(gettext('User preferences saved')); scope.cancel(); }, (reason) => { From bae024b53991bd2750667730c321c508f6e2a95b Mon Sep 17 00:00:00 2001 From: dzonidoo Date: Tue, 29 Aug 2023 13:25:44 +0200 Subject: [PATCH 2/4] fix CI tests --- scripts/apps/users/tests/sdUserPreferences.spec.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/scripts/apps/users/tests/sdUserPreferences.spec.ts b/scripts/apps/users/tests/sdUserPreferences.spec.ts index 36c4c3b633..f4a9367688 100644 --- a/scripts/apps/users/tests/sdUserPreferences.spec.ts +++ b/scripts/apps/users/tests/sdUserPreferences.spec.ts @@ -43,6 +43,10 @@ describe('sdUserPreferences directive', () => { category: 'article_defaults', place: '', }, + 'editor:theme': { + type: 'string', + theme: {}, + }, }; metadata.values = { @@ -122,7 +126,7 @@ describe('sdUserPreferences directive', () => { expect(modal.confirm).not.toHaveBeenCalled(); callArgs = preferencesService.update.calls.allArgs(); - expect(callArgs.length).toEqual(1); + expect(callArgs.length).toEqual(2); callArgs = callArgs[0][0] || {}; // first arg of the first call arg = callArgs['categories:preferred'] || {}; @@ -155,7 +159,7 @@ describe('sdUserPreferences directive', () => { // check if the API was called and with what data callArgs = preferencesService.update.calls.allArgs(); - expect(callArgs.length).toEqual(1); + expect(callArgs.length).toEqual(2); callArgs = callArgs[0][0] || {}; // first arg of the first call arg = callArgs['categories:preferred'] || {}; From 39e11f54dba26a93c39cd7bd8de9879e52558d35 Mon Sep 17 00:00:00 2001 From: dzonidoo Date: Wed, 30 Aug 2023 16:09:56 +0200 Subject: [PATCH 3/4] changde the logic --- .../services/AuthoringThemesService.ts | 21 +++++++++++-------- .../directives/UserPreferencesDirective.ts | 6 +++++- .../users/tests/sdUserPreferences.spec.ts | 7 ++++++- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/scripts/apps/authoring/authoring/services/AuthoringThemesService.ts b/scripts/apps/authoring/authoring/services/AuthoringThemesService.ts index 3b46275e74..d8d0efba31 100644 --- a/scripts/apps/authoring/authoring/services/AuthoringThemesService.ts +++ b/scripts/apps/authoring/authoring/services/AuthoringThemesService.ts @@ -101,16 +101,19 @@ export function AuthoringThemesService(storage, preferencesService) { }); }; - service.syncWithApplicationTheme = (appTheme: string) => { - service.get('theme').then((res) => { - if (res.theme === 'default' || res.theme === 'dark') { - let activeTheme = appTheme === 'dark-ui' - ? {...res, theme: 'dark'} - : {...res, theme: 'default'}; + // when the user change theme of application, the theme of editor will inherit the app's + service.syncWithApplicationTheme = (appTheme: string, themeObject: any) => { + let activeThemeObject = JSON.parse(themeObject); - service.save('theme', activeTheme); - } - }); + if (activeThemeObject.theme === 'default' || activeThemeObject.theme === 'dark') { + let activeTheme = appTheme === 'dark-ui' + ? {...activeThemeObject, theme: 'dark'} + : {...activeThemeObject, theme: 'default'}; + + return activeTheme; + } else { + return activeThemeObject; + } }; return service; diff --git a/scripts/apps/users/directives/UserPreferencesDirective.ts b/scripts/apps/users/directives/UserPreferencesDirective.ts index 41570e778a..8d5bdd7a29 100644 --- a/scripts/apps/users/directives/UserPreferencesDirective.ts +++ b/scripts/apps/users/directives/UserPreferencesDirective.ts @@ -111,7 +111,6 @@ export function UserPreferencesDirective( preferencesService.desktopNotification.requestPermission(); } - authThemes.syncWithApplicationTheme(scope.activeTheme); localStorage.setItem('theme', scope.activeTheme); body.attr('data-theme', scope.activeTheme); notify.success(gettext('User preferences saved')); @@ -417,6 +416,11 @@ export function UserPreferencesDirective( p[key] = _.extend(val, scope.preferences[key]); }); + + if (orig['editor:theme'] != null) { + p['editor:theme'] = {...orig['editor:theme'], theme: JSON.stringify(authThemes.syncWithApplicationTheme(scope.activeTheme, orig['editor:theme'].theme))}; + } + return p; } }, diff --git a/scripts/apps/users/tests/sdUserPreferences.spec.ts b/scripts/apps/users/tests/sdUserPreferences.spec.ts index f4a9367688..360b1f1046 100644 --- a/scripts/apps/users/tests/sdUserPreferences.spec.ts +++ b/scripts/apps/users/tests/sdUserPreferences.spec.ts @@ -45,7 +45,12 @@ describe('sdUserPreferences directive', () => { }, 'editor:theme': { type: 'string', - theme: {}, + theme: { + theme: 'default', + headline: 'medium', + abstract: 'medium', + body: 'medium', + }, }, }; From 374d3e6bc68e2d3aa39ba93c1ec283b017d4512d Mon Sep 17 00:00:00 2001 From: dzonidoo Date: Wed, 30 Aug 2023 23:39:55 +0200 Subject: [PATCH 4/4] fix test --- scripts/apps/users/tests/sdUserPreferences.spec.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/scripts/apps/users/tests/sdUserPreferences.spec.ts b/scripts/apps/users/tests/sdUserPreferences.spec.ts index 360b1f1046..af3ab15215 100644 --- a/scripts/apps/users/tests/sdUserPreferences.spec.ts +++ b/scripts/apps/users/tests/sdUserPreferences.spec.ts @@ -45,12 +45,12 @@ describe('sdUserPreferences directive', () => { }, 'editor:theme': { type: 'string', - theme: { + theme: JSON.stringify({ theme: 'default', headline: 'medium', abstract: 'medium', body: 'medium', - }, + }), }, }; @@ -131,7 +131,7 @@ describe('sdUserPreferences directive', () => { expect(modal.confirm).not.toHaveBeenCalled(); callArgs = preferencesService.update.calls.allArgs(); - expect(callArgs.length).toEqual(2); + expect(callArgs.length).toEqual(1); callArgs = callArgs[0][0] || {}; // first arg of the first call arg = callArgs['categories:preferred'] || {}; @@ -164,7 +164,7 @@ describe('sdUserPreferences directive', () => { // check if the API was called and with what data callArgs = preferencesService.update.calls.allArgs(); - expect(callArgs.length).toEqual(2); + expect(callArgs.length).toEqual(1); callArgs = callArgs[0][0] || {}; // first arg of the first call arg = callArgs['categories:preferred'] || {};