Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bundle): added empty row placeholder #506

Merged
merged 7 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
type RenderPreview,
type ToolbarGroupData,
type UseMarkdownEditorProps,
WysywigPlaceholderOptions,
logger,
markupToolbarConfigs,
useMarkdownEditor,
Expand Down Expand Up @@ -46,7 +47,7 @@

const b = block('playground');
const fileUploadHandler: FileUploadHandler = async (file) => {
console.info('[Playground] Uploading file: ' + file.name);

Check warning on line 50 in demo/components/Playground.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement
await randomDelay(1000, 3000);
return {url: URL.createObjectURL(file)};
};
Expand Down Expand Up @@ -79,6 +80,7 @@
breaks?: boolean;
linkify?: boolean;
linkifyTlds?: string | string[];
placeholderOptions?: WysywigPlaceholderOptions;
sanitizeHtml?: boolean;
prepareRawMarkup?: boolean;
splitModeOrientation?: 'horizontal' | 'vertical' | false;
Expand Down Expand Up @@ -112,8 +114,8 @@
>;

logger.setLogger({
metrics: console.info,

Check warning on line 117 in demo/components/Playground.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement
action: (data) => console.info(`Action: ${data.action}`, data),

Check warning on line 118 in demo/components/Playground.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement
...console,
});

Expand All @@ -139,6 +141,7 @@
wysiwygCommandMenuConfig,
markupConfigExtensions,
markupToolbarConfig,
placeholderOptions,
escapeConfig,
enableSubmitInPreview,
hidePreviewAfterSubmit,
Expand All @@ -156,7 +159,7 @@
}, [mdRaw]);

const renderPreview = useCallback<RenderPreview>(
({getValue, md, directiveSyntax}) => (

Check warning on line 162 in demo/components/Playground.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

'directiveSyntax' is already declared in the upper scope on line 150 column 9
<SplitModePreview
getValue={getValue}
allowHTML={md.html}
Expand Down Expand Up @@ -185,6 +188,9 @@
needToSetDimensionsForUploadedImages,
renderPreview: renderPreviewDefined ? renderPreview : undefined,
fileUploadHandler,
wysiwygConfig: {
placeholderOptions: placeholderOptions,
},
experimental: {
...experimental,
directiveSyntax,
Expand Down Expand Up @@ -276,14 +282,14 @@
setEditorMode(mode);
}
const onToolbarAction = ({id, editorMode: type}: ToolbarActionData) => {
console.info(`The '${id}' action is performed in the ${type}-editor.`);

Check warning on line 285 in demo/components/Playground.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement
};
function onChangeSplitModeEnabled({splitModeEnabled}: {splitModeEnabled: boolean}) {
props.onChangeSplitModeEnabled?.(splitModeEnabled);
console.info(`Split mode enabled: ${splitModeEnabled}`);

Check warning on line 289 in demo/components/Playground.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement
}
function onChangeToolbarVisibility({visible}: {visible: boolean}) {
console.info('Toolbar visible: ' + visible);

Check warning on line 292 in demo/components/Playground.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement
}

mdEditor.on('cancel', onCancel);
Expand All @@ -303,7 +309,7 @@
mdEditor.off('change-split-mode-enabled', onChangeSplitModeEnabled);
mdEditor.off('change-toolbar-visibility', onChangeToolbarVisibility);
};
}, [mdEditor]);

Check warning on line 312 in demo/components/Playground.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

React Hook useEffect has a missing dependency: 'props'. Either include it or remove the dependency array. However, 'props' will change when *any* prop changes, so the preferred fix is to destructure the 'props' object outside of the useEffect call and refer to those specific props inside useEffect

return (
<div className={b()}>
Expand Down
6 changes: 6 additions & 0 deletions src/bundle/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ export type RenderPreview = (params: RenderPreviewParams) => ReactNode;

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

export type WysywigPlaceholderOptions = {
makhnatkin marked this conversation as resolved.
Show resolved Hide resolved
value?: string | (() => string);
behavior: 'empty-doc' | 'empty-row';
makhnatkin marked this conversation as resolved.
Show resolved Hide resolved
};

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

// [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
26 changes: 22 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, WysywigPlaceholderOptions} 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?: WysywigPlaceholderOptions;
/**
* If we need to set dimensions for uploaded images
*
Expand Down Expand Up @@ -63,9 +64,26 @@ 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;
let isPlaceholderShown = false;

if (opts.placeholderOptions?.behavior === 'empty-row') {
isPlaceholderShown = !node.text && parent?.type.name === 'doc';
makhnatkin marked this conversation as resolved.
Show resolved Hide resolved
} else {
isPlaceholderShown =
!node.text && parent?.type.name === BaseNode.Doc && parent.childCount === 1;
}

let text = i18nPlaceholder('doc_empty');

if (opts.placeholderOptions?.value) {
if (typeof opts.placeholderOptions?.value === 'string') {
text = opts.placeholderOptions?.value;
} else {
text = opts.placeholderOptions?.value();
}
}

return isPlaceholderShown ? text : null;
makhnatkin marked this conversation as resolved.
Show resolved Hide resolved
},
...opts.baseSchema,
},
Expand Down
Loading