Skip to content

Commit 69b6468

Browse files
iam-vipinCopilot
andauthored
[WIKI-829] fix: add option to only show placeholder on empty editor (#8232)
* feat: add placeholderOnEmpty functionality to editor components * Update packages/editor/src/core/extensions/placeholder.ts Co-authored-by: Copilot <[email protected]> * refactor: rename placeholderOnEmpty to showPlaceholderOnEmpty across editor components * chore : make optional --------- Co-authored-by: Copilot <[email protected]>
1 parent 2f45bfb commit 69b6468

File tree

8 files changed

+23
-2
lines changed

8 files changed

+23
-2
lines changed

apps/web/core/components/editor/lite-text/editor.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export const LiteTextEditor = React.forwardRef(function LiteTextEditor(
7272
placeholder = t("issue.comments.placeholder"),
7373
disabledExtensions: additionalDisabledExtensions = [],
7474
editorClassName = "",
75+
showPlaceholderOnEmpty = true,
7576
...rest
7677
} = props;
7778
// states
@@ -154,6 +155,7 @@ export const LiteTextEditor = React.forwardRef(function LiteTextEditor(
154155
}),
155156
}}
156157
placeholder={placeholder}
158+
showPlaceholderOnEmpty={showPlaceholderOnEmpty}
157159
containerClassName={cn(containerClassName, "relative", {
158160
"p-2": !editable,
159161
})}

packages/editor/src/core/components/editors/editor-wrapper.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export function EditorWrapper(props: Props) {
4141
handleEditorReady,
4242
autofocus,
4343
placeholder,
44+
showPlaceholderOnEmpty,
4445
tabIndex,
4546
value,
4647
} = props;
@@ -67,6 +68,7 @@ export function EditorWrapper(props: Props) {
6768
handleEditorReady,
6869
autofocus,
6970
placeholder,
71+
showPlaceholderOnEmpty,
7072
tabIndex,
7173
value,
7274
});

packages/editor/src/core/extensions/extensions.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type TArguments = Pick<
4747
| "isTouchDevice"
4848
| "mentionHandler"
4949
| "placeholder"
50+
| "showPlaceholderOnEmpty"
5051
| "tabIndex"
5152
| "extendedEditorProps"
5253
> & {
@@ -65,6 +66,7 @@ export const CoreEditorExtensions = (args: TArguments): Extensions => {
6566
isTouchDevice = false,
6667
mentionHandler,
6768
placeholder,
69+
showPlaceholderOnEmpty,
6870
tabIndex,
6971
editable,
7072
extendedEditorProps,
@@ -108,7 +110,7 @@ export const CoreEditorExtensions = (args: TArguments): Extensions => {
108110
TableCell,
109111
TableRow,
110112
CustomMentionExtension(mentionHandler),
111-
CustomPlaceholderExtension({ placeholder }),
113+
CustomPlaceholderExtension({ placeholder, showPlaceholderOnEmpty }),
112114
CharacterCount,
113115
CustomColorExtension,
114116
CustomTextAlignExtension,

packages/editor/src/core/extensions/placeholder.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import type { IEditorProps } from "@/types";
66

77
type TArgs = {
88
placeholder: IEditorProps["placeholder"];
9+
showPlaceholderOnEmpty: IEditorProps["showPlaceholderOnEmpty"];
910
};
1011

1112
export const CustomPlaceholderExtension = (args: TArgs) => {
12-
const { placeholder } = args;
13+
const { placeholder, showPlaceholderOnEmpty = false } = args;
1314

1415
return Placeholder.configure({
1516
placeholder: ({ editor, node }) => {
@@ -29,6 +30,13 @@ export const CustomPlaceholderExtension = (args: TArgs) => {
2930

3031
if (shouldHidePlaceholder) return "";
3132

33+
if (showPlaceholderOnEmpty) {
34+
const isDocumentEmpty = editor.state.doc.textContent.length === 0;
35+
if (!isDocumentEmpty) {
36+
return "";
37+
}
38+
}
39+
3240
if (placeholder) {
3341
if (typeof placeholder === "string") return placeholder;
3442
else return placeholder(editor.isFocused, editor.getHTML());

packages/editor/src/core/hooks/use-collaborative-editor.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export const useCollaborativeEditor = (props: TCollaborativeEditorHookProps) =>
3333
mentionHandler,
3434
onEditorFocus,
3535
placeholder,
36+
showPlaceholderOnEmpty,
3637
realtimeConfig,
3738
serverHandler,
3839
tabIndex,
@@ -119,6 +120,7 @@ export const useCollaborativeEditor = (props: TCollaborativeEditorHookProps) =>
119120
onEditorFocus,
120121
onTransaction,
121122
placeholder,
123+
showPlaceholderOnEmpty,
122124
provider,
123125
tabIndex,
124126
});

packages/editor/src/core/hooks/use-editor.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export const useEditor = (props: TEditorHookProps) => {
4040
onEditorFocus,
4141
onTransaction,
4242
placeholder,
43+
showPlaceholderOnEmpty,
4344
tabIndex,
4445
provider,
4546
value,
@@ -70,6 +71,7 @@ export const useEditor = (props: TEditorHookProps) => {
7071
isTouchDevice,
7172
mentionHandler,
7273
placeholder,
74+
showPlaceholderOnEmpty,
7375
tabIndex,
7476
provider,
7577
}),

packages/editor/src/core/types/editor.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ export type IEditorProps = {
164164
onEnterKeyPress?: (e?: any) => void;
165165
onTransaction?: () => void;
166166
placeholder?: string | ((isFocused: boolean, value: string) => string);
167+
showPlaceholderOnEmpty?: boolean;
167168
tabIndex?: number;
168169
value?: string | null;
169170
extendedEditorProps: IEditorPropsExtended;

packages/editor/src/core/types/hook.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export type TEditorHookProps = TCoreHookProps &
2929
| "onChange"
3030
| "onTransaction"
3131
| "placeholder"
32+
| "showPlaceholderOnEmpty"
3233
| "tabIndex"
3334
| "value"
3435
> & {
@@ -50,6 +51,7 @@ export type TCollaborativeEditorHookProps = TCoreHookProps &
5051
| "onChange"
5152
| "onTransaction"
5253
| "placeholder"
54+
| "showPlaceholderOnEmpty"
5355
| "tabIndex"
5456
> &
5557
Pick<

0 commit comments

Comments
 (0)