customize.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import path from 'path';
  2. import type { ColorScheme } from '@growi/core';
  3. import { getForcedColorScheme } from '@growi/core/dist/utils';
  4. import { DefaultThemeMetadata, PresetThemesMetadatas, manifestPath } from '@growi/preset-themes';
  5. import uglifycss from 'uglifycss';
  6. import { growiPluginService } from '~/features/growi-plugin/server/services';
  7. import loggerFactory from '~/utils/logger';
  8. import type Crowi from '../crowi';
  9. import S2sMessage from '../models/vo/s2s-message';
  10. import type { IConfigManagerForApp } from './config-manager';
  11. import type { S2sMessageHandlable } from './s2s-messaging/handlable';
  12. const logger = loggerFactory('growi:service:CustomizeService');
  13. /**
  14. * the service class of CustomizeService
  15. */
  16. class CustomizeService implements S2sMessageHandlable {
  17. configManager: IConfigManagerForApp;
  18. s2sMessagingService: any;
  19. appService: any;
  20. lastLoadedAt?: Date;
  21. customCss?: string;
  22. customTitleTemplate!: string;
  23. theme: string;
  24. themeHref: string;
  25. forcedColorScheme?: ColorScheme;
  26. constructor(crowi: Crowi) {
  27. this.configManager = crowi.configManager;
  28. this.s2sMessagingService = crowi.s2sMessagingService;
  29. this.appService = crowi.appService;
  30. }
  31. /**
  32. * @inheritdoc
  33. */
  34. shouldHandleS2sMessage(s2sMessage) {
  35. const { eventName, updatedAt } = s2sMessage;
  36. if (eventName !== 'customizeServiceUpdated' || updatedAt == null) {
  37. return false;
  38. }
  39. return this.lastLoadedAt == null || this.lastLoadedAt < new Date(s2sMessage.updatedAt);
  40. }
  41. /**
  42. * @inheritdoc
  43. */
  44. async handleS2sMessage(s2sMessage) {
  45. const { configManager } = this;
  46. logger.info('Reset customized value by pubsub notification');
  47. await configManager.loadConfigs();
  48. this.initCustomCss();
  49. this.initCustomTitle();
  50. this.initGrowiTheme();
  51. }
  52. async publishUpdatedMessage() {
  53. const { s2sMessagingService } = this;
  54. if (s2sMessagingService != null) {
  55. const s2sMessage = new S2sMessage('customizeServiceUpdated', { updatedAt: new Date() });
  56. try {
  57. await s2sMessagingService.publish(s2sMessage);
  58. }
  59. catch (e) {
  60. logger.error('Failed to publish update message with S2sMessagingService: ', e.message);
  61. }
  62. }
  63. }
  64. /**
  65. * initialize custom css strings
  66. */
  67. initCustomCss() {
  68. const rawCss = this.configManager.getConfig('customize:css') || '';
  69. // uglify and store
  70. this.customCss = uglifycss.processString(rawCss);
  71. this.lastLoadedAt = new Date();
  72. }
  73. getCustomCss() {
  74. return this.customCss;
  75. }
  76. getCustomScript() {
  77. return this.configManager.getConfig('customize:script');
  78. }
  79. getCustomNoscript() {
  80. return this.configManager.getConfig('customize:noscript');
  81. }
  82. initCustomTitle() {
  83. let configValue = this.configManager.getConfig('customize:title');
  84. if (configValue == null || configValue.trim().length === 0) {
  85. configValue = '{{pagename}} - {{sitename}}';
  86. }
  87. this.customTitleTemplate = configValue;
  88. this.lastLoadedAt = new Date();
  89. }
  90. async initGrowiTheme(): Promise<void> {
  91. const theme = this.configManager.getConfig('customize:theme');
  92. this.theme = theme;
  93. const resultForThemePlugin = await growiPluginService.findThemePlugin(theme);
  94. if (resultForThemePlugin != null) {
  95. this.forcedColorScheme = getForcedColorScheme(resultForThemePlugin.themeMetadata.schemeType);
  96. this.themeHref = resultForThemePlugin.themeHref;
  97. }
  98. // retrieve preset theme
  99. else {
  100. // import preset-themes manifest
  101. const presetThemesManifest = await import(path.join('@growi/preset-themes', manifestPath)).then(imported => imported.default);
  102. const themeMetadata = PresetThemesMetadatas.find(p => p.name === theme);
  103. this.forcedColorScheme = getForcedColorScheme(themeMetadata?.schemeType);
  104. const manifestKey = themeMetadata?.manifestKey ?? DefaultThemeMetadata.manifestKey;
  105. if (themeMetadata == null || !(themeMetadata.manifestKey in presetThemesManifest)) {
  106. logger.warn(`Use default theme because the key for '${theme} does not exist in preset-themes manifest`);
  107. }
  108. this.themeHref = `/static/preset-themes/${presetThemesManifest[manifestKey].file}`; // configured by express.static
  109. }
  110. }
  111. }
  112. module.exports = CustomizeService;