fix: disable editor history conditionally (#5133)

This commit is contained in:
Aaryan Khandelwal 2024-07-19 15:31:22 +05:30 committed by GitHub
parent e7948eabf2
commit cb21dcbcef
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 34 additions and 27 deletions

View file

@ -34,6 +34,7 @@ export const EditorWrapper: React.FC<Props> = (props) => {
const editor = useEditor({ const editor = useEditor({
editorClassName, editorClassName,
enableHistory: true,
extensions, extensions,
fileHandler, fileHandler,
forwardedRef, forwardedRef,

View file

@ -30,23 +30,25 @@ import { isValidHttpUrl } from "@/helpers/common";
import { DeleteImage, IMentionHighlight, IMentionSuggestion, RestoreImage, UploadImage } from "@/types"; import { DeleteImage, IMentionHighlight, IMentionSuggestion, RestoreImage, UploadImage } from "@/types";
type TArguments = { type TArguments = {
mentionConfig: { enableHistory: boolean;
mentionSuggestions?: () => Promise<IMentionSuggestion[]>;
mentionHighlights?: () => Promise<IMentionHighlight[]>;
};
fileConfig: { fileConfig: {
deleteFile: DeleteImage; deleteFile: DeleteImage;
restoreFile: RestoreImage; restoreFile: RestoreImage;
cancelUploadImage?: () => void; cancelUploadImage?: () => void;
uploadFile: UploadImage; uploadFile: UploadImage;
}; };
mentionConfig: {
mentionSuggestions?: () => Promise<IMentionSuggestion[]>;
mentionHighlights?: () => Promise<IMentionHighlight[]>;
};
placeholder?: string | ((isFocused: boolean, value: string) => string); placeholder?: string | ((isFocused: boolean, value: string) => string);
tabIndex?: number; tabIndex?: number;
}; };
export const CoreEditorExtensions = ({ export const CoreEditorExtensions = ({
mentionConfig, enableHistory,
fileConfig: { deleteFile, restoreFile, cancelUploadImage, uploadFile }, fileConfig: { deleteFile, restoreFile, cancelUploadImage, uploadFile },
mentionConfig,
placeholder, placeholder,
tabIndex, tabIndex,
}: TArguments) => [ }: TArguments) => [
@ -70,11 +72,11 @@ export const CoreEditorExtensions = ({
codeBlock: false, codeBlock: false,
horizontalRule: false, horizontalRule: false,
blockquote: false, blockquote: false,
history: false,
dropcursor: { dropcursor: {
color: "rgba(var(--color-text-100))", color: "rgba(var(--color-text-100))",
width: 1, width: 1,
}, },
...(enableHistory ? {} : { history: false }),
}), }),
CustomQuoteExtension, CustomQuoteExtension,
DropHandlerExtension(uploadFile), DropHandlerExtension(uploadFile),

View file

@ -87,6 +87,7 @@ export const useDocumentEditor = (props: DocumentEditorProps) => {
id, id,
editorProps, editorProps,
editorClassName, editorClassName,
enableHistory: false,
fileHandler, fileHandler,
handleEditorReady, handleEditorReady,
forwardedRef, forwardedRef,

View file

@ -24,42 +24,44 @@ export type TFileHandler = {
}; };
export interface CustomEditorProps { export interface CustomEditorProps {
id?: string;
fileHandler: TFileHandler;
initialValue?: string;
editorClassName: string; editorClassName: string;
// undefined when prop is not passed, null if intentionally passed to stop
// swr syncing
value?: string | null | undefined;
provider?: CollaborationProvider;
onChange?: (json: object, html: string) => void;
extensions?: any;
editorProps?: EditorProps; editorProps?: EditorProps;
enableHistory: boolean;
extensions?: any;
fileHandler: TFileHandler;
forwardedRef?: MutableRefObject<EditorRefApi | null>; forwardedRef?: MutableRefObject<EditorRefApi | null>;
handleEditorReady?: (value: boolean) => void;
id?: string;
initialValue?: string;
mentionHandler: { mentionHandler: {
highlights: () => Promise<IMentionHighlight[]>; highlights: () => Promise<IMentionHighlight[]>;
suggestions?: () => Promise<IMentionSuggestion[]>; suggestions?: () => Promise<IMentionSuggestion[]>;
}; };
handleEditorReady?: (value: boolean) => void; onChange?: (json: object, html: string) => void;
placeholder?: string | ((isFocused: boolean, value: string) => string); placeholder?: string | ((isFocused: boolean, value: string) => string);
provider?: CollaborationProvider;
tabIndex?: number; tabIndex?: number;
// undefined when prop is not passed, null if intentionally passed to stop
// swr syncing
value?: string | null | undefined;
} }
export const useEditor = ({ export const useEditor = ({
id = "",
editorProps = {},
initialValue,
editorClassName, editorClassName,
value, editorProps = {},
enableHistory,
extensions = [], extensions = [],
fileHandler, fileHandler,
onChange,
forwardedRef, forwardedRef,
tabIndex,
handleEditorReady, handleEditorReady,
provider, id = "",
initialValue,
mentionHandler, mentionHandler,
onChange,
placeholder, placeholder,
provider,
tabIndex,
value,
}: CustomEditorProps) => { }: CustomEditorProps) => {
const editor = useCustomEditor({ const editor = useCustomEditor({
editorProps: { editorProps: {
@ -68,16 +70,17 @@ export const useEditor = ({
}, },
extensions: [ extensions: [
...CoreEditorExtensions({ ...CoreEditorExtensions({
mentionConfig: { enableHistory,
mentionSuggestions: mentionHandler.suggestions ?? (() => Promise.resolve<IMentionSuggestion[]>([])),
mentionHighlights: mentionHandler.highlights ?? [],
},
fileConfig: { fileConfig: {
uploadFile: fileHandler.upload, uploadFile: fileHandler.upload,
deleteFile: fileHandler.delete, deleteFile: fileHandler.delete,
restoreFile: fileHandler.restore, restoreFile: fileHandler.restore,
cancelUploadImage: fileHandler.cancel, cancelUploadImage: fileHandler.cancel,
}, },
mentionConfig: {
mentionSuggestions: mentionHandler.suggestions ?? (() => Promise.resolve<IMentionSuggestion[]>([])),
mentionHighlights: mentionHandler.highlights ?? [],
},
placeholder, placeholder,
tabIndex, tabIndex,
}), }),