Yuki Takei 3 лет назад
Родитель
Сommit
4044e33abf

+ 36 - 3
packages/app/src/pages/_document.page.tsx

@@ -1,6 +1,9 @@
 /* eslint-disable @next/next/google-font-display */
 import React from 'react';
 
+import { readFileSync } from 'fs';
+
+import type { PresetThemesManifest } from '@growi/preset-themes';
 import mongoose from 'mongoose';
 import Document, {
   DocumentContext, DocumentInitialProps,
@@ -10,11 +13,31 @@ import Document, {
 import { ActivatePluginService, GrowiPluginManifestEntries } from '~/client/services/activate-plugin';
 import { CrowiRequest } from '~/interfaces/crowi-request';
 import { GrowiPlugin, GrowiPluginResourceType } from '~/interfaces/plugin';
+import { resolveFromRoot } from '~/utils/project-dir-utils';
+
+type HeadersForPresetThemesProps = {
+  manifest: PresetThemesManifest;
+}
+
+const HeadersForPresetThemes = (props: HeadersForPresetThemesProps): JSX.Element => {
+  const { manifest } = props;
+
+  const themeName = 'halloween';
+  const manifestResourceKey = `src/${themeName}.css`;
+  const href = `/static/preset-themes/${manifest[manifestResourceKey].file}`;
+
+  const elements: JSX.Element[] = [];
+
+  elements.push(
+    <link rel="stylesheet" key={`link_preset-themes-${themeName}`} href={href} />,
+  );
+
+  return <>{elements}</>;
+};
 
 type HeadersForGrowiPluginProps = {
   pluginManifestEntries: GrowiPluginManifestEntries;
 }
-
 const HeadersForGrowiPlugin = (props: HeadersForGrowiPluginProps): JSX.Element => {
   const { pluginManifestEntries } = props;
 
@@ -49,6 +72,7 @@ const HeadersForGrowiPlugin = (props: HeadersForGrowiPluginProps): JSX.Element =
 
 interface GrowiDocumentProps {
   customCss: string;
+  presetThemeManifest: PresetThemesManifest;
   pluginManifestEntries: GrowiPluginManifestEntries;
 }
 declare type GrowiDocumentInitialProps = DocumentInitialProps & GrowiDocumentProps;
@@ -61,15 +85,23 @@ class GrowiDocument extends Document<GrowiDocumentInitialProps> {
     const { customizeService } = crowi;
     const customCss: string = customizeService.getCustomCss();
 
+    // retrieve preset-theme manifest
+    const presetThemeManifestPath = resolveFromRoot('public/static/preset-themes/manifest.json');
+    const presetThemeManifestStr: string = await readFileSync(presetThemeManifestPath, 'utf-8');
+    const presetThemeManifest = JSON.parse(presetThemeManifestStr);
+
+    // retrieve plugin manifests
     const GrowiPlugin = mongoose.model<GrowiPlugin>('GrowiPlugin');
     const growiPlugins = await GrowiPlugin.find({ isEnabled: true });
     const pluginManifestEntries: GrowiPluginManifestEntries = await ActivatePluginService.retrievePluginManifests(growiPlugins);
 
-    return { ...initialProps, customCss, pluginManifestEntries };
+    return {
+      ...initialProps, customCss, presetThemeManifest, pluginManifestEntries,
+    };
   }
 
   override render(): JSX.Element {
-    const { customCss, pluginManifestEntries } = this.props;
+    const { customCss, presetThemeManifest, pluginManifestEntries } = this.props;
 
     return (
       <Html>
@@ -87,6 +119,7 @@ class GrowiDocument extends Document<GrowiDocumentInitialProps> {
           <link rel='preload' href="/static/fonts/Lato-Regular-latin-ext.woff2" as="font" type="font/woff2" />
           <link rel='preload' href="/static/fonts/Lato-Bold-latin.woff2" as="font" type="font/woff2" />
           <link rel='preload' href="/static/fonts/Lato-Bold-latin-ext.woff2" as="font" type="font/woff2" />
+          <HeadersForPresetThemes manifest={presetThemeManifest} />
           <HeadersForGrowiPlugin pluginManifestEntries={pluginManifestEntries} />
         </Head>
         <body>

+ 1 - 0
packages/preset-themes/src/index.ts

@@ -0,0 +1 @@
+export * from './interfaces/manifest';

+ 7 - 0
packages/preset-themes/src/interfaces/manifest.ts

@@ -0,0 +1,7 @@
+export type PresetThemesManifest = {
+  [key: string]: {
+    file: string,
+    src: string,
+    isEntry?: boolean,
+  }
+}