Skip to content

Commit

Permalink
feat(bundle): added empty row placeholder (gravity-ui#506)
Browse files Browse the repository at this point in the history
  • Loading branch information
PMAWorks committed Dec 18, 2024
1 parent c36b8b5 commit 0fa5029
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 @@ -80,6 +81,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 @@ -141,6 +143,7 @@ export const Playground = React.memo<PlaygroundProps>((props) => {
wysiwygCommandMenuConfig,
markupConfigExtensions,
markupToolbarConfig,
placeholderOptions,
escapeConfig,
enableSubmitInPreview,
hidePreviewAfterSubmit,
Expand Down Expand Up @@ -187,6 +190,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 @@ -28,6 +28,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;
preserveEmptyRows?: boolean;
Expand Down Expand Up @@ -152,6 +163,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 @@ -66,6 +66,7 @@ export function useMarkdownEditor<T extends object = {}>(
return true;
},
preserveEmptyRows: preserveEmptyRows,
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 @@ -28,6 +28,7 @@ export type BundlePresetOptions = ExtensionsOptions &
mdBreaks?: boolean;
preserveEmptyRows?: boolean;
fileUploadHandler?: FileUploadHandler;
placeholderOptions?: WysiwygPlaceholderOptions;
/**
* If we need to set dimensions for uploaded images
*
Expand Down Expand Up @@ -64,9 +65,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');
},
preserveEmptyRows: opts.preserveEmptyRows,
...opts.baseSchema,
Expand Down

0 comments on commit 0fa5029

Please sign in to comment.