A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.
feat(Customize): demo with customize
haitao(lj)e8826be2768e
diff --git a/ui/src/common/interface.ts b/ui/src/common/interface.tsindex 843bb687..e9ea4cce 100644--- a/ui/src/common/interface.ts+++ b/ui/src/common/interface.ts@@ -321,6 +321,7 @@ export interface SiteSettings {general: AdminSettingsGeneral;interface: AdminSettingsInterface;login: AdminSettingsLogin;+ custom_css_html: AdminSettingsCustom;}export interface AdminSettingBranding {diff --git a/ui/src/components/Customize/index.tsx b/ui/src/components/Customize/index.tsxnew file mode 100644index 00000000..4f54bccd--- /dev/null+++ b/ui/src/components/Customize/index.tsx@@ -0,0 +1,84 @@+import { FC, memo, useEffect } from 'react';++import { customizeStore } from '@/stores';++const getElementByAttr = (attr: string, elName: string) => {+ let el = document.querySelector(`[${attr}]`);+ if (!el) {+ el = document.createElement(elName);+ el.setAttribute(attr, '');+ }+ return el;+};++// eslint-disable-next-line @typescript-eslint/no-unused-vars+const textToDf = (t) => {+ const dummyDoc = document.createElement('div');+ dummyDoc.innerHTML = t;+ const frag = document.createDocumentFragment();+ while (dummyDoc.childNodes.length) {+ frag.appendChild(dummyDoc.children[0]);+ }+ return frag;+};++const injectCustomCSS = (t: string) => {+ if (!t) {+ return;+ }+ const el = getElementByAttr('data-custom-css', 'style');+ el.textContent = t;+ document.head.insertBefore(el, document.head.lastChild);+};++const injectCustomHead = (t: string) => {+ if (!t) {+ return;+ }+ setTimeout(() => {+ const el = getElementByAttr('data-custom-head', 'style');+ el.textContent = t;+ document.head.appendChild(el);+ }, 200);+};++const injectCustomHeader = (t: string) => {+ if (!t) {+ return;+ }+ // const frag = textToDf(t);+ t = ' Customize Header ';+ document.body.insertBefore(+ document.createComment(t),+ document.body.firstChild,+ );+};++const injectCustomFooter = (t: string) => {+ if (!t) {+ return;+ }+ // FIXME+ t = ' Customize Footer ';+ // const frag = textToDf(t);+ document.documentElement.appendChild(document.createComment(t));+};++const Index: FC = () => {+ const { custom_css, custom_head, custom_header, custom_footer } =+ customizeStore((state) => state);+ useEffect(() => {+ injectCustomCSS(custom_css);+ injectCustomHead(custom_head);+ injectCustomHeader(custom_header);+ injectCustomFooter(custom_footer);+ }, []);+ return (+ <>+ {null}+ {/* App customize */}+ </>+ );+};++export default memo(Index);diff --git a/ui/src/components/SchemaForm/index.tsx b/ui/src/components/SchemaForm/index.tsxindex 73a94407..a004deb6 100644--- a/ui/src/components/SchemaForm/index.tsx+++ b/ui/src/components/SchemaForm/index.tsx@@ -58,6 +58,7 @@ export interface UISchema {| 'url'| 'week';empty?: string;+ className?: string | string[];validator?: (value,formData?,@@ -459,6 +460,7 @@ const SchemaForm: ForwardRefRenderFunction<IRef, IProps> = (onChange={handleInputChange}isInvalid={formData[key].isInvalid}rows={options?.rows || 3}+ className={classnames(options.className)}/><Form.Control.Feedback type="invalid">{formData[key]?.errorMsg}diff --git a/ui/src/components/index.ts b/ui/src/components/index.tsindex f8fa4181..8ce3b751 100644--- a/ui/src/components/index.ts+++ b/ui/src/components/index.ts@@ -27,6 +27,7 @@ import BrandUpload from './BrandUpload';import SchemaForm, { JSONSchema, UISchema, initFormData } from './SchemaForm';import Labels from './LabelsCard';import DiffContent from './DiffContent';+import Customize from './Customize';export {Avatar,@@ -60,5 +61,6 @@ export {initFormData,Labels,DiffContent,+ Customize,};export type { EditorRef, JSONSchema, UISchema };diff --git a/ui/src/pages/Admin/CssAndHtml/index.tsx b/ui/src/pages/Admin/CssAndHtml/index.tsxindex b050d87e..f2ec26dd 100644--- a/ui/src/pages/Admin/CssAndHtml/index.tsx+++ b/ui/src/pages/Admin/CssAndHtml/index.tsx@@ -42,24 +42,28 @@ const Index: FC = () => {'ui:widget': 'textarea','ui:options': {rows: 10,+ className: ['fs-14', 'font-monospace'],},},custom_head: {'ui:widget': 'textarea','ui:options': {rows: 10,+ className: ['fs-14', 'font-monospace'],},},custom_header: {'ui:widget': 'textarea','ui:options': {rows: 10,+ className: ['fs-14', 'font-monospace'],},},custom_footer: {'ui:widget': 'textarea','ui:options': {rows: 10,+ className: ['fs-14', 'font-monospace'],},},};@@ -82,6 +86,9 @@ const Index: FC = () => {msg: t('update', { keyPrefix: 'toast' }),variant: 'success',});+ setTimeout(() => {+ window.location.reload();+ }, 3000);}).catch((err) => {if (err.isError) {diff --git a/ui/src/pages/Layout/index.tsx b/ui/src/pages/Layout/index.tsxindex 7726574d..462b9beb 100644--- a/ui/src/pages/Layout/index.tsx+++ b/ui/src/pages/Layout/index.tsx@@ -5,7 +5,7 @@ import { Helmet, HelmetProvider } from 'react-helmet-async';import { SWRConfig } from 'swr';import { toastStore, brandingStore, pageTagStore } from '@/stores';-import { Header, Footer, Toast } from '@/components';+import { Header, Footer, Toast, Customize } from '@/components';const Layout: FC = () => {const { msg: toastMsg, variant, clear: toastClear } = toastStore();@@ -41,6 +41,7 @@ const Layout: FC = () => {</div><Toast msg={toastMsg} variant={variant} onClose={closeToast} /><Footer />+ <Customize /></SWRConfig></HelmetProvider>);diff --git a/ui/src/stores/customize.ts b/ui/src/stores/customize.tsnew file mode 100644index 00000000..209f6024--- /dev/null+++ b/ui/src/stores/customize.ts@@ -0,0 +1,30 @@+import create from 'zustand';++interface IType {+ custom_css: string;+ custom_head: string;+ custom_header: string;+ custom_footer: string;+ update: (params: {+ custom_css?: string;+ custom_head?: string;+ custom_header?: string;+ custom_footer?: string;+ }) => void;+}++const loginSetting = create<IType>((set) => ({+ custom_css: '',+ custom_head: '',+ custom_header: '',+ custom_footer: '',+ update: (params) =>+ set((state) => {+ return {+ ...state,+ ...params,+ };+ }),+}));++export default loginSetting;diff --git a/ui/src/stores/index.ts b/ui/src/stores/index.tsindex c1d1b58e..03bad593 100644--- a/ui/src/stores/index.ts+++ b/ui/src/stores/index.ts@@ -6,6 +6,7 @@ import siteInfoStore from './siteInfo';import interfaceStore from './interface';import brandingStore from './branding';import pageTagStore from './pageTags';+import customizeStore from './customize';export {toastStore,@@ -15,4 +16,5 @@ export {brandingStore,pageTagStore,loginSettingStore,+ customizeStore,};diff --git a/ui/src/utils/guard.ts b/ui/src/utils/guard.tsindex 315384fb..2a99561b 100644--- a/ui/src/utils/guard.ts+++ b/ui/src/utils/guard.ts@@ -5,6 +5,7 @@ import {interfaceStore,brandingStore,loginSettingStore,+ customizeStore,} from '@/stores';import { RouteAlias } from '@/router/alias';import Storage from '@/utils/storage';@@ -256,6 +257,7 @@ export const initAppSettingsStore = async () => {interfaceStore.getState().update(appSettings.interface);brandingStore.getState().update(appSettings.branding);loginSettingStore.getState().update(appSettings.login);+ customizeStore.getState().update(appSettings.custom_css_html);}};