Skip to content

Commit

Permalink
Merge branch 'v5' into beta
Browse files Browse the repository at this point in the history
  • Loading branch information
imzbf committed Nov 11, 2024
2 parents 82e5861 + 4975782 commit a873d39
Show file tree
Hide file tree
Showing 16 changed files with 98 additions and 43 deletions.
6 changes: 3 additions & 3 deletions packages/MdEditor/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const Editor = forwardRef((props: EditorProps, ref: ForwardedRef<unknown>) => {
noHighlight = defaultProps.noHighlight,
noImgZoomIn = defaultProps.noImgZoomIn,
language = defaultProps.language,
inputBoxWitdh = defaultProps.inputBoxWitdh,
inputBoxWidth = defaultProps.inputBoxWidth,
sanitizeMermaid = defaultProps.sanitizeMermaid,
transformImgUrl = defaultProps.transformImgUrl,
codeFoldable = defaultProps.codeFoldable,
Expand Down Expand Up @@ -190,8 +190,8 @@ const Editor = forwardRef((props: EditorProps, ref: ForwardedRef<unknown>) => {
theme={props.theme}
noImgZoomIn={noImgZoomIn}
onDrop={props.onDrop}
inputBoxWitdh={inputBoxWitdh}
onInputBoxWitdhChange={props.onInputBoxWitdhChange}
inputBoxWidth={inputBoxWidth}
onInputBoxWidthChange={props.onInputBoxWidthChange}
sanitizeMermaid={sanitizeMermaid}
transformImgUrl={transformImgUrl}
codeFoldable={codeFoldable}
Expand Down
2 changes: 1 addition & 1 deletion packages/MdEditor/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ export const defaultProps = {
autoDetectCode: false,
noHighlight: false,
noImgZoomIn: false,
inputBoxWitdh: '50%',
inputBoxWidth: '50%',
sanitizeMermaid: (h: string) => Promise.resolve(h),
transformImgUrl: (t: string) => t,
codeFoldable: true,
Expand Down
9 changes: 5 additions & 4 deletions packages/MdEditor/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {
PREVIEW_ONLY_CHANGED
} from './static/event-name';
import { ContentExposeParam } from './layouts/Content/type';
import { CDN_IDS } from './static';

/**
* 键盘监听
Expand Down Expand Up @@ -165,12 +166,12 @@ export const useExpansion = (staticProps: StaticProps) => {
...css,
rel: 'stylesheet',
href: editorExtensions.cropper!.css,
id: `${prefix}-cropperCss`
id: CDN_IDS.croppercss
});
appendHandler('script', {
...js,
src: editorExtensions.cropper!.js,
id: `${prefix}-cropper`
id: CDN_IDS.cropperjs
});
}

Expand All @@ -181,7 +182,7 @@ export const useExpansion = (staticProps: StaticProps) => {
appendHandler('script', {
...standaloneJs,
src: editorExtensions.prettier!.standaloneJs,
id: `${prefix}-prettier`
id: CDN_IDS.prettier
});
}

Expand All @@ -191,7 +192,7 @@ export const useExpansion = (staticProps: StaticProps) => {
appendHandler('script', {
...parserMarkdownJs,
src: editorExtensions.prettier!.parserMarkdownJs,
id: `${prefix}-prettierMD`
id: CDN_IDS.prettierMD
});
}
}, [noPrettier, noUploadImg]);
Expand Down
7 changes: 4 additions & 3 deletions packages/MdEditor/layouts/Content/hooks/useHighlight.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useContext, useEffect, useRef, useState } from 'react';
import { prefix, configOption } from '~/config';
import { configOption } from '~/config';
import { EditorContext } from '~/context';
import { appendHandler, updateHandler } from '~/utils/dom';
import { CDN_IDS } from '~/static';
import { ContentPreviewProps } from '../props';

/**
Expand All @@ -25,7 +26,7 @@ const useHighlight = (props: ContentPreviewProps) => {
updateHandler('link', {
...highlight.css,
rel: 'stylesheet',
id: `${prefix}-hlCss`
id: CDN_IDS.hlcss
});
}, [highlight.css, props.noHighlight]);

Expand All @@ -39,7 +40,7 @@ const useHighlight = (props: ContentPreviewProps) => {
'script',
{
...highlight.js,
id: `${prefix}-hljs`,
id: CDN_IDS.hljs,
onload() {
hljsRef.current = window.hljs;
setHljsInited(true);
Expand Down
8 changes: 5 additions & 3 deletions packages/MdEditor/layouts/Content/hooks/useKatex.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { useEffect, useRef, useState } from 'react';
import { prefix, configOption } from '~/config';
import { configOption } from '~/config';
import { appendHandler } from '~/utils/dom';
import { CDN_IDS } from '~~/MdEditor/static';

import { ContentPreviewProps } from '../props';

/**
Expand All @@ -27,7 +29,7 @@ const useKatex = (props: ContentPreviewProps) => {
'script',
{
src: editorExtensions.katex!.js,
id: `${prefix}-katex`,
id: CDN_IDS.katexjs,
onload() {
katexRef.current = window.katex;
setKatexInited(true);
Expand All @@ -38,7 +40,7 @@ const useKatex = (props: ContentPreviewProps) => {
appendHandler('link', {
rel: 'stylesheet',
href: editorExtensions.katex!.css,
id: `${prefix}-katexCss`
id: CDN_IDS.katexcss
});
}, [props.noKatex]);

Expand Down
6 changes: 0 additions & 6 deletions packages/MdEditor/layouts/Content/hooks/useMarkdownIt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import KatexPlugin from '../markdownIt/katex';
import AdmonitionPlugin from '../markdownIt/admonition';
import HeadingPlugin from '../markdownIt/heading';
import CodePlugin from '../markdownIt/code';
import XSSPlugin from '../markdownIt/xss';
import TaskListPlugin from '../markdownIt/task';
import { ContentPreviewProps } from '../props';

Expand Down Expand Up @@ -137,11 +136,6 @@ const useMarkdownIt = (props: ContentPreviewProps, previewOnly: boolean) => {
customIconRef
}
},
{
type: 'xss',
plugin: XSSPlugin,
options: {}
},
{
type: 'sub',
plugin: SubPlugin,
Expand Down
5 changes: 3 additions & 2 deletions packages/MdEditor/layouts/Content/hooks/useMermaid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { prefix, configOption } from '~/config';
import { EditorContext } from '~/context';
import { appendHandler } from '~/utils/dom';
import { mermaidCache } from '~/utils/cache';
import { CDN_IDS } from '~~/MdEditor/static';

import { ContentPreviewProps } from '../props';

Expand Down Expand Up @@ -52,7 +53,7 @@ const useMermaid = (props: ContentPreviewProps) => {
...editorExtensionsAttrs.mermaid?.js,
rel: 'modulepreload',
href: jsSrc,
id: `${prefix}-mermaid-m`
id: CDN_IDS.mermaidM
});

import(
Expand All @@ -69,7 +70,7 @@ const useMermaid = (props: ContentPreviewProps) => {
{
...editorExtensionsAttrs.mermaid?.js,
src: jsSrc,
id: `${prefix}-mermaid`,
id: CDN_IDS.mermaid,
onload() {
mermaidRef.current = window.mermaid;
configMermaid();
Expand Down
18 changes: 9 additions & 9 deletions packages/MdEditor/layouts/Content/hooks/useResize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ const useResize = (
resizeRef: RefObject<HTMLDivElement>
) => {
const [inputWrapperStyle, setInputWrapperStyle] = useState<CSSProperties>({
width: props.inputBoxWitdh
width: props.inputBoxWidth
});

const [resizeOperateStyle, setResizeOperateStyle] = useState<CSSProperties>({
left: props.inputBoxWitdh
left: props.inputBoxWidth
});

/**
Expand All @@ -25,7 +25,7 @@ const useResize = (
props.setting.preview || props.setting.htmlPreview
);

const resizedWidth = useRef<string | number | undefined>(props.inputBoxWitdh);
const resizedWidth = useRef<string | number | undefined>(props.inputBoxWidth);

useEffect(() => {
// 挂载后计算宽度的数值
Expand Down Expand Up @@ -60,7 +60,7 @@ const useResize = (
});

resizedWidth.current = ibw;
props.onInputBoxWitdhChange?.(ibw);
props.onInputBoxWidthChange?.(ibw);
};

const resizeMousedown = () => {
Expand Down Expand Up @@ -92,23 +92,23 @@ const useResize = (
}, [contentRef, props, resizeRef]);

useEffect(() => {
if (props.inputBoxWitdh) {
resizedWidth.current = props.inputBoxWitdh;
if (props.inputBoxWidth) {
resizedWidth.current = props.inputBoxWidth;
setInputWrapperStyle((prevState) => {
return {
...prevState,
width: props.inputBoxWitdh
width: props.inputBoxWidth
};
});

setResizeOperateStyle((prevState) => {
return {
...prevState,
left: props.inputBoxWitdh
left: props.inputBoxWidth
};
});
}
}, [props.inputBoxWitdh]);
}, [props.inputBoxWidth]);

useEffect(() => {
const po = props.setting.previewOnly;
Expand Down
4 changes: 1 addition & 3 deletions packages/MdEditor/layouts/Content/markdownIt/xss/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const MdWhiteList: xss.IFilterXSSOptions['whiteList'] = {
]
};

const XSSPlugin = (md: markdownit, options: XSSPluginType) => {
export const XSSPlugin = (md: markdownit, options: XSSPluginType) => {
const { extendedWhiteList = {}, xss: xssOption = {} } = options;
let xssIns: xss.FilterXSS;

Expand Down Expand Up @@ -87,5 +87,3 @@ const XSSPlugin = (md: markdownit, options: XSSPluginType) => {
}
});
};

export default XSSPlugin;
4 changes: 2 additions & 2 deletions packages/MdEditor/layouts/Content/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ export type ContentProps = Readonly<
* @returns
*/
onDrop?: (event: DragEvent) => void;
inputBoxWitdh: string;
onInputBoxWitdhChange?: (width: string) => void;
inputBoxWidth: string;
onInputBoxWidthChange?: (width: string) => void;
transformImgUrl: (text: string) => string | Promise<string>;
} & ContentPreviewProps
>;
5 changes: 3 additions & 2 deletions packages/MdEditor/layouts/Toolbar/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RefObject, useCallback, useContext, useEffect, useRef, useState } from 'react';
import { prefix, configOption } from '~/config';
import { configOption } from '~/config';
import { EditorContext } from '~/context';
import { appendHandler } from '~/utils/dom';
import { ToolDirective } from '~/utils/content-help';
Expand All @@ -10,6 +10,7 @@ import {
UPLOAD_IMAGE
} from '~/static/event-name';
import bus from '~/utils/event-bus';
import { CDN_IDS } from '~/static';

import { ToolbarProps } from './';
import { HoverData } from './TableShape';
Expand Down Expand Up @@ -69,7 +70,7 @@ export const useSreenfull = (props: ToolbarProps) => {
{
...editorExtensionsAttrs.screenfull?.js,
src: editorExtensions.screenfull!.js,
id: `${prefix}-screenfull`,
id: CDN_IDS.screenfull,
onload() {
// 复制实例
screenfull.current = window.screenfull;
Expand Down
31 changes: 28 additions & 3 deletions packages/MdEditor/layouts/Toolbar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1081,7 +1081,31 @@ const Toolbar = (props: ToolbarProps) => {
},
[
props,
ult.toolbarTips,
ult.toolbarTips?.bold,
ult.toolbarTips?.underline,
ult.toolbarTips?.italic,
ult.toolbarTips?.strikeThrough,
ult.toolbarTips?.sub,
ult.toolbarTips?.sup,
ult.toolbarTips?.quote,
ult.toolbarTips?.unorderedList,
ult.toolbarTips?.orderedList,
ult.toolbarTips?.task,
ult.toolbarTips?.codeRow,
ult.toolbarTips?.code,
ult.toolbarTips?.link,
ult.toolbarTips?.image,
ult.toolbarTips?.revoke,
ult.toolbarTips?.next,
ult.toolbarTips?.save,
ult.toolbarTips?.prettier,
ult.toolbarTips?.pageFullscreen,
ult.toolbarTips?.fullscreen,
ult.toolbarTips?.catalog,
ult.toolbarTips?.preview,
ult.toolbarTips?.previewOnly,
ult.toolbarTips?.htmlPreview,
ult.toolbarTips?.github,
emitHandler,
TitleDropdown,
setModalData,
Expand All @@ -1094,8 +1118,9 @@ const Toolbar = (props: ToolbarProps) => {
MermaidDropdown,
KatexDropdown,
theme,
language,
previewTheme
codeTheme,
previewTheme,
language
]
);

Expand Down
15 changes: 15 additions & 0 deletions packages/MdEditor/static/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { prefix } from '../config';

export const CDN_IDS: Record<string, string> = {
hljs: `${prefix}-hljs`,
hlcss: `${prefix}-hlCss`,
prettier: `${prefix}-prettier`,
prettierMD: `${prefix}-prettierMD`,
cropperjs: `${prefix}-cropper`,
croppercss: `${prefix}-cropperCss`,
screenfull: `${prefix}-screenfull`,
mermaidM: `${prefix}-mermaid-m`,
mermaid: `${prefix}-mermaid`,
katexjs: `${prefix}-katex`,
katexcss: `${prefix}-katexCss`
};
4 changes: 2 additions & 2 deletions packages/MdEditor/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,11 +471,11 @@ export interface EditorProps extends MdPreviewProps {
*
* @example '100px'/'50%'
*/
inputBoxWitdh?: string;
inputBoxWidth?: string;
/**
* 输入框宽度变化事件
*/
onInputBoxWitdhChange?: (width: string) => void;
onInputBoxWidthChange?: (width: string) => void;
/**
* 替换粘贴的图片链接
*
Expand Down
2 changes: 2 additions & 0 deletions packages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export { default as MdModal } from './MdEditor/components/Modal';
export { default as StrIcon } from './MdEditor/components/Icon/Str';
export { default as NormalFooterToolbar } from './NormalFooterToolbar';

export * from '~/layouts/Content/markdownIt/xss';
export * from './config';
export * from './util';

export * from '~/type';
15 changes: 15 additions & 0 deletions packages/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { CDN_IDS } from './MdEditor/static';

/**
* 清空组件带来的副作用,例如
* 1. 使用CDN嵌入的链接,为了保证多个组件能够正常使用,组件在卸载时不会主动移除
*/
export const clearSideEffects = () => {
(Object.keys(CDN_IDS) as Array<keyof typeof CDN_IDS>).forEach((key) => {
const ele = document.getElementById(CDN_IDS[key]);

if (ele) {
ele.remove();
}
});
};

0 comments on commit a873d39

Please sign in to comment.