customize.ts 4.2 KB

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