customize.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // eslint-disable-next-line no-unused-vars
  2. import { ColorScheme, DevidedPagePath, getForcedColorScheme } from '@growi/core';
  3. import { DefaultThemeMetadata, PresetThemesMetadatas } from '@growi/preset-themes';
  4. import uglifycss from 'uglifycss';
  5. import { growiPluginService } from '~/features/growi-plugin/services';
  6. import loggerFactory from '~/utils/logger';
  7. import S2sMessage from '../models/vo/s2s-message';
  8. import type { ConfigManager } from './config-manager';
  9. import type { S2sMessageHandlable } from './s2s-messaging/handlable';
  10. const logger = loggerFactory('growi:service:CustomizeService');
  11. /**
  12. * the service class of CustomizeService
  13. */
  14. class CustomizeService implements S2sMessageHandlable {
  15. configManager: ConfigManager;
  16. s2sMessagingService: any;
  17. appService: any;
  18. xssService: 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. this.xssService = crowi.xssService;
  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('crowi', '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('crowi', 'customize:script');
  78. }
  79. getCustomNoscript() {
  80. return this.configManager.getConfig('crowi', 'customize:noscript');
  81. }
  82. initCustomTitle() {
  83. let configValue = this.configManager.getConfig('crowi', '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. generateCustomTitle(pageOrPath) {
  91. const path = pageOrPath.path || pageOrPath;
  92. const dPagePath = new DevidedPagePath(path, true, true);
  93. const customTitle = this.customTitleTemplate
  94. .replace('{{sitename}}', this.appService.getAppTitle())
  95. .replace('{{pagepath}}', path)
  96. .replace('{{page}}', dPagePath.latter) // for backward compatibility
  97. .replace('{{pagename}}', dPagePath.latter);
  98. return this.xssService.process(customTitle);
  99. }
  100. generateCustomTitleForFixedPageName(title) {
  101. // replace
  102. const customTitle = this.customTitleTemplate
  103. .replace('{{sitename}}', this.appService.getAppTitle())
  104. .replace('{{page}}', title)
  105. .replace('{{pagepath}}', title)
  106. .replace('{{pagename}}', title);
  107. return this.xssService.process(customTitle);
  108. }
  109. async initGrowiTheme(): Promise<void> {
  110. const theme = this.configManager.getConfig('crowi', 'customize:theme');
  111. this.theme = theme;
  112. const resultForThemePlugin = await growiPluginService.findThemePlugin(theme);
  113. if (resultForThemePlugin != null) {
  114. this.forcedColorScheme = getForcedColorScheme(resultForThemePlugin.themeMetadata.schemeType);
  115. this.themeHref = resultForThemePlugin.themeHref;
  116. }
  117. // retrieve preset theme
  118. else {
  119. // import preset-themes manifest
  120. const presetThemesManifest = await import('@growi/preset-themes/dist/themes/manifest.json').then(imported => imported.default);
  121. const themeMetadata = PresetThemesMetadatas.find(p => p.name === theme);
  122. this.forcedColorScheme = getForcedColorScheme(themeMetadata?.schemeType);
  123. const manifestKey = themeMetadata?.manifestKey ?? DefaultThemeMetadata.manifestKey;
  124. if (themeMetadata == null || !(themeMetadata.manifestKey in presetThemesManifest)) {
  125. logger.warn(`Use default theme because the key for '${theme} does not exist in preset-themes manifest`);
  126. }
  127. this.themeHref = `/static/preset-themes/${presetThemesManifest[manifestKey].file}`; // configured by express.static
  128. }
  129. }
  130. }
  131. module.exports = CustomizeService;