Skip to content

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.

refactor: Improve the plug-in system and add hooks plug-ins

robinc20838ae14e7
diff --git a/ui/src/components/Editor/index.tsx b/ui/src/components/Editor/index.tsx
index b6529300..3756de0a 100644
--- a/ui/src/components/Editor/index.tsx
+++ b/ui/src/components/Editor/index.tsx
@@ -89,12 +89,6 @@ const MDEditor: ForwardRefRenderFunction<EditorRef, Props> = (
getHtml,
}));
- const handleChange = (val) => {
- const { ch } = editor.getCursor();
-
- editor.replaceSelection(`${ch ? '\n' : ''}\`\`\`mermaid\n${val}\n\`\`\`\n`);
- };
-
useEffect(() => {
if (!editor) {
return;
@@ -111,7 +105,7 @@ const MDEditor: ForwardRefRenderFunction<EditorRef, Props> = (
<PluginRender
type="editor"
className="toolbar-wrap px-3 d-flex align-items-center flex-wrap"
- onChange={handleChange}
+ editor={editor}
previewElement={previewRef.current?.element}>
<Heading />
<Bold />
diff --git a/ui/src/components/Editor/utils/index.ts b/ui/src/components/Editor/utils/index.ts
index 19cba8b2..a7f3cc3f 100644
--- a/ui/src/components/Editor/utils/index.ts
+++ b/ui/src/components/Editor/utils/index.ts
@@ -3,7 +3,6 @@ import { useEffect, useState } from 'react';
import type { Editor, Position } from 'codemirror';
import type CodeMirror from 'codemirror';
import 'codemirror/lib/codemirror.css';
-import 'katex/dist/katex.min.css';
export function createEditorUtils(
codemirror: typeof CodeMirror,
@@ -91,45 +90,6 @@ export function htmlRender(el: HTMLElement | null) {
}
});
- // import('mermaid').then(({ default: mermaid }) => {
- // mermaid.initialize({ startOnLoad: false });
-
- // el.querySelectorAll('.language-mermaid').forEach((pre) => {
- // const flag = Date.now();
- // mermaid.render(
- // `theGraph${flag}`,
- // pre.textContent || '',
- // function (svgCode) {
- // const p = document.createElement('p');
- // p.className = 'text-center';
- // p.innerHTML = svgCode;
-
- // pre.parentNode?.replaceChild(p, pre);
- // },
- // );
- // });
- // });
- import('katex/contrib/auto-render/auto-render').then(
- ({ default: render }) => {
- render(el, {
- delimiters: [
- { left: '$$', right: '$$', display: true },
- { left: '$$<br>', right: '<br>$$', display: true },
- {
- left: '\\begin{equation}',
- right: '\\end{equation}',
- display: true,
- },
- { left: '\\begin{align}', right: '\\end{align}', display: true },
- { left: '\\begin{alignat}', right: '\\end{alignat}', display: true },
- { left: '\\begin{gather}', right: '\\end{gather}', display: true },
- { left: '\\(', right: '\\)', display: false },
- { left: '\\[', right: '\\]', display: true },
- ],
- });
- },
- );
-
// change table style
el.querySelectorAll('table').forEach((table) => {
diff --git a/ui/src/components/PluginRender/index.tsx b/ui/src/components/PluginRender/index.tsx
index 5baef52e..bc6a903e 100644
--- a/ui/src/components/PluginRender/index.tsx
+++ b/ui/src/components/PluginRender/index.tsx
@@ -1,6 +1,6 @@
import React, { FC, ReactNode, memo } from 'react';
-import PluginKite, { Plugin, PluginType } from '@/utils/pluginKit';
+import PluginKit, { Plugin, PluginType } from '@/utils/pluginKit';
/**
* Note:Please set at least either of the `slug_name` and `type` attributes, otherwise no plugins will be rendered.
*
@@ -27,7 +27,7 @@ const Index: FC<Props> = ({
...props
}) => {
const pluginSlice: Plugin[] = [];
- const plugins = PluginKite.getPlugins();
+ const plugins = PluginKit.getPlugins();
plugins.forEach((plugin) => {
if (type && slug_name) {
diff --git a/ui/src/pages/Questions/Detail/components/Question/index.tsx b/ui/src/pages/Questions/Detail/components/Question/index.tsx
index 22042919..1bb938ee 100644
--- a/ui/src/pages/Questions/Detail/components/Question/index.tsx
+++ b/ui/src/pages/Questions/Detail/components/Question/index.tsx
@@ -14,6 +14,7 @@ import {
Icon,
ImgViewer,
} from '@/components';
+import { useRenderHtmlPlugin } from '@/utils/pluginKit';
import { formatCount, guard } from '@/utils';
import { following } from '@/services';
import { pathFactory } from '@/router/pathFactory';
@@ -33,6 +34,8 @@ const Index: FC<Props> = ({ data, initPage, hasAnswer, isLogged }) => {
const [followed, setFollowed] = useState(data?.is_followed);
const ref = useRef<HTMLDivElement>(null);
+ useRenderHtmlPlugin(ref.current);
+
const handleFollow = (e) => {
e.preventDefault();
if (!guard.tryNormalLogged(true)) {
diff --git a/ui/src/utils/pluginKit/index.ts b/ui/src/utils/pluginKit/index.ts
index 3ca00491..aca9375e 100644
--- a/ui/src/utils/pluginKit/index.ts
+++ b/ui/src/utils/pluginKit/index.ts
@@ -3,6 +3,8 @@ import { NamedExoticComponent, FC } from 'react';
import builtin from '@/plugins/builtin';
import * as allPlugins from '@/plugins';
+import { initI18nResource } from './utils';
+
/**
* This information is to be defined for all components.
* It may be used for feature upgrades or version compatibility processing.
@@ -26,6 +28,10 @@ export interface PluginInfo {
export interface Plugin {
info: PluginInfo;
component: NamedExoticComponent | FC;
+ i18nConfig?;
+ hooks?: {
+ useRender?: Array<(element: HTMLElement | null) => void>;
+ };
}
class Plugins {
@@ -73,6 +79,9 @@ class Plugins {
if (!bool) {
return;
}
+ if (plugin.i18nConfig) {
+ initI18nResource(plugin.i18nConfig);
+ }
this.plugins.push(plugin);
}
@@ -80,10 +89,6 @@ class Plugins {
return this.plugins.find((p) => p.info.slug_name === slug_name);
}
- getPluginsByType(type: PluginType) {
- return this.plugins.filter((p) => p.info.type === type);
- }
-
getPlugins() {
return this.plugins;
}
@@ -91,4 +96,15 @@ class Plugins {
const plugins = new Plugins();
+const useRenderHtmlPlugin = (element: HTMLElement | null) => {
+ plugins
+ .getPlugins()
+ .filter((plugin) => plugin.hooks?.useRender)
+ .forEach((plugin) => {
+ plugin.hooks?.useRender?.forEach((hook) => {
+ hook(element);
+ });
+ });
+};
+export { useRenderHtmlPlugin };
export default plugins;