Skip to content

Commit

Permalink
feat(bundle): pass md options to renderPreview callback (#435)
Browse files Browse the repository at this point in the history
  • Loading branch information
d3m1d0v authored Oct 21, 2024
1 parent 8499538 commit 2676b45
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 22 deletions.
12 changes: 6 additions & 6 deletions demo/Playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,18 @@ export const Playground = React.memo<PlaygroundProps>((props) => {
}, [mdRaw]);

const renderPreview = useCallback<RenderPreview>(
({getValue}) => (
({getValue, md}) => (
<SplitModePreview
getValue={getValue}
allowHTML={allowHTML}
linkify={linkify}
linkifyTlds={linkifyTlds}
breaks={breaks}
allowHTML={md.html}
linkify={md.linkify}
linkifyTlds={md.linkifyTlds}
breaks={md.breaks}
needToSanitizeHtml={sanitizeHtml}
plugins={plugins}
/>
),
[allowHTML, breaks, linkify, linkifyTlds, sanitizeHtml],
[sanitizeHtml],
);

const mdEditor = useMarkdownEditor({
Expand Down
12 changes: 6 additions & 6 deletions demo/PresetDemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,18 @@ export const PresetDemo = React.memo<PresetDemoProps>((props) => {
const [mdRaw, setMdRaw] = React.useState<MarkupString>('');

const renderPreview = useCallback<RenderPreview>(
({getValue}) => (
({getValue, md}) => (
<SplitModePreview
getValue={getValue}
allowHTML={allowHTML}
linkify={linkify}
linkifyTlds={linkifyTlds}
breaks={breaks}
allowHTML={md.html}
linkify={md.linkify}
linkifyTlds={md.linkifyTlds}
breaks={md.breaks}
needToSanitizeHtml
plugins={plugins}
/>
),
[allowHTML, breaks, linkify, linkifyTlds],
[],
);

const mdEditor = useMarkdownEditor({
Expand Down
20 changes: 11 additions & 9 deletions src/bundle/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import type {FileUploadHandler} from '../utils/upload';
import type {
MarkdownEditorMode as EditorMode,
MarkdownEditorPreset as EditorPreset,
MarkdownEditorMdOptions,
MarkdownEditorOptions,
MarkdownEditorMarkupConfig as MarkupConfig,
RenderPreview,
Expand Down Expand Up @@ -77,6 +78,7 @@ export interface EditorInt
readonly splitModeEnabled: boolean;
readonly splitMode: SplitMode;
readonly preset: EditorPreset;
readonly mdOptions: Readonly<MarkdownEditorMdOptions>;

/** @internal used in demo for dev-tools */
readonly _wysiwygView?: PMEditorView;
Expand Down Expand Up @@ -132,11 +134,9 @@ export class EditorImpl extends SafeEventEmitter<EventMapInt> implements EditorI
#markupEditor?: MarkupEditor;
#markupConfig: MarkupConfig;
#escapeConfig?: EscapeConfig;
#mdOptions: Readonly<MarkdownEditorMdOptions>;

readonly #preset: EditorPreset;
#allowHTML?: boolean;
#linkify?: boolean;
#linkifyTlds?: string | string[];
#extensions?: WysiwygEditorOptions['extensions'];
#renderStorage: ReactRenderStorage;
#fileUploadHandler?: FileUploadHandler;
Expand Down Expand Up @@ -209,6 +209,10 @@ export class EditorImpl extends SafeEventEmitter<EventMapInt> implements EditorI
return this.#preset;
}

get mdOptions(): Readonly<MarkdownEditorMdOptions> {
return this.#mdOptions;
}

get renderPreview(): RenderPreview | undefined {
return this.#renderPreview;
}
Expand All @@ -233,9 +237,9 @@ export class EditorImpl extends SafeEventEmitter<EventMapInt> 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),
Expand Down Expand Up @@ -303,9 +307,7 @@ export class EditorImpl extends SafeEventEmitter<EventMapInt> 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};

Expand Down
1 change: 1 addition & 0 deletions src/bundle/MarkdownEditorView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ export const MarkdownEditorView = React.forwardRef<HTMLDivElement, MarkdownEdito
{editor.renderPreview?.({
getValue: editor.getValue,
mode: 'preview',
md: editor.mdOptions,
})}
</div>
{settings}
Expand Down
6 changes: 5 additions & 1 deletion src/bundle/SplitModeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ const SplitModeView = React.forwardRef<HTMLDivElement, SplitModeProps>(({editor}
<Label icon={<Eye />} className={b('preview-sign')} size={'m'}>
{i18n('split-mode-text')}
</Label>
{editor.renderPreview?.({getValue: editor.getValue, mode: 'split'})}
{editor.renderPreview?.({
getValue: editor.getValue,
mode: 'split',
md: editor.mdOptions,
})}
</div>
);
});
Expand Down
1 change: 1 addition & 0 deletions src/bundle/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type MarkdownEditorSplitMode = false | 'horizontal' | 'vertical';
export type RenderPreviewParams = {
getValue: () => MarkupString;
mode: 'preview' | 'split';
md: Readonly<MarkdownEditorMdOptions>;
};
export type RenderPreview = (params: RenderPreviewParams) => ReactNode;

Expand Down

0 comments on commit 2676b45

Please sign in to comment.