From 2676b45dce71cb1083757f876564f99949454c2b Mon Sep 17 00:00:00 2001 From: Yuriy Demidov Date: Mon, 21 Oct 2024 15:46:09 +0300 Subject: [PATCH] feat(bundle): pass md options to renderPreview callback (#435) --- demo/Playground.tsx | 12 ++++++------ demo/PresetDemo.tsx | 12 ++++++------ src/bundle/Editor.ts | 20 +++++++++++--------- src/bundle/MarkdownEditorView.tsx | 1 + src/bundle/SplitModeView.tsx | 6 +++++- src/bundle/types.ts | 1 + 6 files changed, 30 insertions(+), 22 deletions(-) diff --git a/demo/Playground.tsx b/demo/Playground.tsx index 6f0140f8..a012a4f7 100644 --- a/demo/Playground.tsx +++ b/demo/Playground.tsx @@ -145,18 +145,18 @@ export const Playground = React.memo((props) => { }, [mdRaw]); const renderPreview = useCallback( - ({getValue}) => ( + ({getValue, md}) => ( ), - [allowHTML, breaks, linkify, linkifyTlds, sanitizeHtml], + [sanitizeHtml], ); const mdEditor = useMarkdownEditor({ diff --git a/demo/PresetDemo.tsx b/demo/PresetDemo.tsx index ab0941cd..213131f6 100644 --- a/demo/PresetDemo.tsx +++ b/demo/PresetDemo.tsx @@ -64,18 +64,18 @@ export const PresetDemo = React.memo((props) => { const [mdRaw, setMdRaw] = React.useState(''); const renderPreview = useCallback( - ({getValue}) => ( + ({getValue, md}) => ( ), - [allowHTML, breaks, linkify, linkifyTlds], + [], ); const mdEditor = useMarkdownEditor({ diff --git a/src/bundle/Editor.ts b/src/bundle/Editor.ts index a333ddd3..2498c52a 100644 --- a/src/bundle/Editor.ts +++ b/src/bundle/Editor.ts @@ -22,6 +22,7 @@ import type {FileUploadHandler} from '../utils/upload'; import type { MarkdownEditorMode as EditorMode, MarkdownEditorPreset as EditorPreset, + MarkdownEditorMdOptions, MarkdownEditorOptions, MarkdownEditorMarkupConfig as MarkupConfig, RenderPreview, @@ -77,6 +78,7 @@ export interface EditorInt readonly splitModeEnabled: boolean; readonly splitMode: SplitMode; readonly preset: EditorPreset; + readonly mdOptions: Readonly; /** @internal used in demo for dev-tools */ readonly _wysiwygView?: PMEditorView; @@ -132,11 +134,9 @@ export class EditorImpl extends SafeEventEmitter implements EditorI #markupEditor?: MarkupEditor; #markupConfig: MarkupConfig; #escapeConfig?: EscapeConfig; + #mdOptions: Readonly; readonly #preset: EditorPreset; - #allowHTML?: boolean; - #linkify?: boolean; - #linkifyTlds?: string | string[]; #extensions?: WysiwygEditorOptions['extensions']; #renderStorage: ReactRenderStorage; #fileUploadHandler?: FileUploadHandler; @@ -209,6 +209,10 @@ export class EditorImpl extends SafeEventEmitter implements EditorI return this.#preset; } + get mdOptions(): Readonly { + return this.#mdOptions; + } + get renderPreview(): RenderPreview | undefined { return this.#renderPreview; } @@ -233,9 +237,9 @@ export class EditorImpl extends SafeEventEmitter implements EditorI mdPreset, initialContent: this.#markup, extensions: this.#extensions, - allowHTML: this.#allowHTML, - linkify: this.#linkify, - linkifyTlds: this.#linkifyTlds, + allowHTML: this.#mdOptions.html, + linkify: this.#mdOptions.linkify, + linkifyTlds: this.#mdOptions.linkifyTlds, escapeConfig: this.#escapeConfig, onChange: () => this.emit('rerender-toolbar', null), onDocChange: () => this.emit('change', null), @@ -303,9 +307,7 @@ export class EditorImpl extends SafeEventEmitter implements EditorI this.#markup = initial.markup ?? ''; this.#preset = opts.preset ?? 'full'; - this.#linkify = md.linkify; - this.#linkifyTlds = md.linkifyTlds; - this.#allowHTML = md.html; + this.#mdOptions = md; this.#extensions = wysiwygConfig.extensions; this.#markupConfig = {...opts.markupConfig}; diff --git a/src/bundle/MarkdownEditorView.tsx b/src/bundle/MarkdownEditorView.tsx index ae8f5117..697b9e30 100644 --- a/src/bundle/MarkdownEditorView.tsx +++ b/src/bundle/MarkdownEditorView.tsx @@ -239,6 +239,7 @@ export const MarkdownEditorView = React.forwardRef {settings} diff --git a/src/bundle/SplitModeView.tsx b/src/bundle/SplitModeView.tsx index 8330e8df..b0dfcd72 100644 --- a/src/bundle/SplitModeView.tsx +++ b/src/bundle/SplitModeView.tsx @@ -106,7 +106,11 @@ const SplitModeView = React.forwardRef(({editor} - {editor.renderPreview?.({getValue: editor.getValue, mode: 'split'})} + {editor.renderPreview?.({ + getValue: editor.getValue, + mode: 'split', + md: editor.mdOptions, + })} ); }); diff --git a/src/bundle/types.ts b/src/bundle/types.ts index 2670a62a..4dc06ca7 100644 --- a/src/bundle/types.ts +++ b/src/bundle/types.ts @@ -18,6 +18,7 @@ export type MarkdownEditorSplitMode = false | 'horizontal' | 'vertical'; export type RenderPreviewParams = { getValue: () => MarkupString; mode: 'preview' | 'split'; + md: Readonly; }; export type RenderPreview = (params: RenderPreviewParams) => ReactNode;