Skip to content

Commit

Permalink
feat(bundle): added empty row placeholder (#506)
Browse files Browse the repository at this point in the history
  • Loading branch information
PMAWorks authored Dec 17, 2024
1 parent 4355519 commit dc049af
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
6 changes: 6 additions & 0 deletions demo/components/Playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
type RenderPreview,
type ToolbarGroupData,
type UseMarkdownEditorProps,
WysiwygPlaceholderOptions,
logger,
markupToolbarConfigs,
useMarkdownEditor,
Expand Down Expand Up @@ -79,6 +80,7 @@ export type PlaygroundProps = {
breaks?: boolean;
linkify?: boolean;
linkifyTlds?: string | string[];
placeholderOptions?: WysiwygPlaceholderOptions;
sanitizeHtml?: boolean;
prepareRawMarkup?: boolean;
splitModeOrientation?: 'horizontal' | 'vertical' | false;
Expand Down Expand Up @@ -139,6 +141,7 @@ export const Playground = React.memo<PlaygroundProps>((props) => {
wysiwygCommandMenuConfig,
markupConfigExtensions,
markupToolbarConfig,
placeholderOptions,
escapeConfig,
enableSubmitInPreview,
hidePreviewAfterSubmit,
Expand Down Expand Up @@ -185,6 +188,9 @@ export const Playground = React.memo<PlaygroundProps>((props) => {
needToSetDimensionsForUploadedImages,
renderPreview: renderPreviewDefined ? renderPreview : undefined,
fileUploadHandler,
wysiwygConfig: {
placeholderOptions: placeholderOptions,
},
experimental: {
...experimental,
directiveSyntax,
Expand Down
12 changes: 12 additions & 0 deletions src/bundle/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ export type RenderPreview = (params: RenderPreviewParams) => ReactNode;

export type ParseInsertedUrlAsImage = (text: string) => {imageUrl: string; title?: string} | null;

export type WysiwygPlaceholderOptions = {
value?: string | (() => string);
/** Default – empty-doc
Values:
- 'empty-doc' – The placeholder will only be shown when the document is completely empty;
- 'empty-row-top-level' – The placeholder will be displayed in an empty line that is at the top level of the document structure;
- 'empty-row' – The placeholder will be shown in any empty line within the document, regardless of its nesting level.
*/
behavior?: 'empty-doc' | 'empty-row-top-level' | 'empty-row';
};

export type MarkdownEditorMdOptions = {
html?: boolean;
breaks?: boolean;
Expand Down Expand Up @@ -148,6 +159,7 @@ export type MarkdownEditorWysiwygConfig = {
extensions?: Extension;
extensionOptions?: ExtensionsOptions;
escapeConfig?: EscapeConfig;
placeholderOptions?: WysiwygPlaceholderOptions;
};

// [major] TODO: remove generic type
Expand Down
1 change: 1 addition & 0 deletions src/bundle/useMarkdownEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export function useMarkdownEditor<T extends object = {}>(
editor.emit('submit', null);
return true;
},
placeholderOptions: wysiwygConfig.placeholderOptions,
mdBreaks: breaks,
fileUploadHandler: uploadFile,
needToSetDimensionsForUploadedImages,
Expand Down
22 changes: 18 additions & 4 deletions src/bundle/wysiwyg-preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type {FileUploadHandler} from '../utils/upload';

import {wCommandMenuConfigByPreset, wSelectionMenuConfigByPreset} from './config/wysiwyg';
import {emojiDefs} from './emoji';
import type {MarkdownEditorPreset} from './types';
import type {MarkdownEditorPreset, WysiwygPlaceholderOptions} from './types';

const DEFAULT_IGNORED_KEYS = ['Tab', 'Shift-Tab'] as const;

Expand All @@ -27,6 +27,7 @@ export type BundlePresetOptions = ExtensionsOptions &
preset: MarkdownEditorPreset;
mdBreaks?: boolean;
fileUploadHandler?: FileUploadHandler;
placeholderOptions?: WysiwygPlaceholderOptions;
/**
* If we need to set dimensions for uploaded images
*
Expand Down Expand Up @@ -63,9 +64,22 @@ export const BundlePreset: ExtensionAuto<BundlePresetOptions> = (builder, opts)
baseSchema: {
paragraphKey: f.toPM(A.Text),
paragraphPlaceholder: (node: Node, parent?: Node | null) => {
const isDocEmpty =
!node.text && parent?.type.name === BaseNode.Doc && parent.childCount === 1;
return isDocEmpty ? i18nPlaceholder('doc_empty') : null;
const {value, behavior} = opts.placeholderOptions || {};

const emptyEntries = {
'empty-row': !node.text,
'empty-row-top-level': !node.text && parent?.type.name === BaseNode.Doc,
'empty-doc':
!node.text && parent?.type.name === BaseNode.Doc && parent.childCount === 1,
};

const showPlaceholder = emptyEntries[behavior || 'empty-doc'];

if (!showPlaceholder) return null;

return typeof value === 'function'
? value()
: value ?? i18nPlaceholder('doc_empty');
},
...opts.baseSchema,
},
Expand Down

0 comments on commit dc049af

Please sign in to comment.