customize.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import { ColorScheme } from '@growi/core';
  2. import { DevidedPagePath } from '@growi/core/dist/models';
  3. import { getForcedColorScheme } from '@growi/core/dist/utils';
  4. import { DefaultThemeMetadata, PresetThemesMetadatas } 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. xssService: any;
  20. lastLoadedAt?: Date;
  21. customCss?: string;
  22. customTitleTemplate!: string;
  23. theme: string;
  24. themeHref: string;
  25. forcedColorScheme?: ColorScheme;
  26. constructor(crowi) {
  27. this.configManager = crowi.configManager;
  28. this.s2sMessagingService = crowi.s2sMessagingService;
  29. this.appService = crowi.appService;
  30. this.xssService = crowi.xssService;
  31. }
  32. /**
  33. * @inheritdoc
  34. */
  35. shouldHandleS2sMessage(s2sMessage) {
  36. const { eventName, updatedAt } = s2sMessage;
  37. if (eventName !== 'customizeServiceUpdated' || updatedAt == null) {
  38. return false;
  39. }
  40. return this.lastLoadedAt == null || this.lastLoadedAt < new Date(s2sMessage.updatedAt);
  41. }
  42. /**
  43. * @inheritdoc
  44. */
  45. async handleS2sMessage(s2sMessage) {
  46. const { configManager } = this;
  47. logger.info('Reset customized value by pubsub notification');
  48. await configManager.loadConfigs();
  49. this.initCustomCss();
  50. this.initCustomTitle();
  51. this.initGrowiTheme();
  52. }
  53. async publishUpdatedMessage() {
  54. const { s2sMessagingService } = this;
  55. if (s2sMessagingService != null) {
  56. const s2sMessage = new S2sMessage('customizeServiceUpdated', { updatedAt: new Date() });
  57. try {
  58. await s2sMessagingService.publish(s2sMessage);
  59. }
  60. catch (e) {
  61. logger.error('Failed to publish update message with S2sMessagingService: ', e.message);
  62. }
  63. }
  64. }
  65. /**
  66. * initialize custom css strings
  67. */
  68. initCustomCss() {
  69. const rawCss = this.configManager.getConfig('crowi', 'customize:css') || '';
  70. // uglify and store
  71. this.customCss = uglifycss.processString(rawCss);
  72. this.lastLoadedAt = new Date();
  73. }
  74. getCustomCss() {
  75. return this.customCss;
  76. }
  77. getCustomScript() {
  78. return this.configManager.getConfig('crowi', 'customize:script');
  79. }
  80. getCustomNoscript() {
  81. return this.configManager.getConfig('crowi', 'customize:noscript');
  82. }
  83. initCustomTitle() {
  84. let configValue = this.configManager.getConfig('crowi', 'customize:title');
  85. if (configValue == null || configValue.trim().length === 0) {
  86. configValue = '{{pagename}} - {{sitename}}';
  87. }
  88. this.customTitleTemplate = configValue;
  89. this.lastLoadedAt = new Date();
  90. }
  91. generateCustomTitle(pageOrPath) {
  92. const path = pageOrPath.path || pageOrPath;
  93. const dPagePath = new DevidedPagePath(path, true, true);
  94. const customTitle = this.customTitleTemplate
  95. .replace('{{sitename}}', this.appService.getAppTitle())
  96. .replace('{{pagepath}}', path)
  97. .replace('{{page}}', dPagePath.latter) // for backward compatibility
  98. .replace('{{pagename}}', dPagePath.latter);
  99. return this.xssService.process(customTitle);
  100. }
  101. generateCustomTitleForFixedPageName(title) {
  102. // replace
  103. const customTitle = this.customTitleTemplate
  104. .replace('{{sitename}}', this.appService.getAppTitle())
  105. .replace('{{page}}', title)
  106. .replace('{{pagepath}}', title)
  107. .replace('{{pagename}}', title);
  108. return this.xssService.process(customTitle);
  109. }
  110. async initGrowiTheme(): Promise<void> {
  111. const theme = this.configManager.getConfig('crowi', 'customize:theme');
  112. this.theme = theme;
  113. const resultForThemePlugin = await growiPluginService.findThemePlugin(theme);
  114. if (resultForThemePlugin != null) {
  115. this.forcedColorScheme = getForcedColorScheme(resultForThemePlugin.themeMetadata.schemeType);
  116. this.themeHref = resultForThemePlugin.themeHref;
  117. }
  118. // retrieve preset theme
  119. else {
  120. // import preset-themes manifest
  121. const presetThemesManifest = await import('@growi/preset-themes/dist/themes/manifest.json').then(imported => imported.default);
  122. const themeMetadata = PresetThemesMetadatas.find(p => p.name === theme);
  123. this.forcedColorScheme = getForcedColorScheme(themeMetadata?.schemeType);
  124. const manifestKey = themeMetadata?.manifestKey ?? DefaultThemeMetadata.manifestKey;
  125. if (themeMetadata == null || !(themeMetadata.manifestKey in presetThemesManifest)) {
  126. logger.warn(`Use default theme because the key for '${theme} does not exist in preset-themes manifest`);
  127. }
  128. this.themeHref = `/static/preset-themes/${presetThemesManifest[manifestKey].file}`; // configured by express.static
  129. }
  130. }
  131. }
  132. module.exports = CustomizeService;