Yuki Takei пре 3 година
родитељ
комит
ee4395c6c7

+ 7 - 8
packages/app/src/components/PageEditor.tsx

@@ -69,7 +69,7 @@ const PageEditor = React.memo((): JSX.Element => {
   const { data: isTextlintEnabled } = useIsTextlintEnabled();
   const { data: isTextlintEnabled } = useIsTextlintEnabled();
   const { data: isIndentSizeForced } = useIsIndentSizeForced();
   const { data: isIndentSizeForced } = useIsIndentSizeForced();
   const { data: indentSize, mutate: mutateCurrentIndentSize } = useCurrentIndentSize();
   const { data: indentSize, mutate: mutateCurrentIndentSize } = useCurrentIndentSize();
-  const { data: isEnabledUnsavedWarning, mutate: mutateIsEnabledUnsavedWarning } = useIsEnabledUnsavedWarning();
+  const { mutate: mutateIsEnabledUnsavedWarning } = useIsEnabledUnsavedWarning();
   const { data: isUploadableFile } = useIsUploadableFile();
   const { data: isUploadableFile } = useIsUploadableFile();
   const { data: isUploadableImage } = useIsUploadableImage();
   const { data: isUploadableImage } = useIsUploadableImage();
 
 
@@ -87,18 +87,17 @@ const PageEditor = React.memo((): JSX.Element => {
   const editorRef = useRef<EditorRef>(null);
   const editorRef = useRef<EditorRef>(null);
   const previewRef = useRef<HTMLDivElement>(null);
   const previewRef = useRef<HTMLDivElement>(null);
 
 
-  const setMarkdownWithDebounce = useMemo(() => debounce(50, throttle(100, (value) => {
+  const setMarkdownWithDebounce = useMemo(() => debounce(100, throttle(150, (value: string, isClean: boolean) => {
     markdownToSave.current = value;
     markdownToSave.current = value;
     setMarkdownToPreview(value);
     setMarkdownToPreview(value);
+
     // Displays an unsaved warning alert
     // Displays an unsaved warning alert
-    if (!isEnabledUnsavedWarning) {
-      mutateIsEnabledUnsavedWarning(true);
-    }
-  })), [isEnabledUnsavedWarning, mutateIsEnabledUnsavedWarning]);
+    mutateIsEnabledUnsavedWarning(!isClean);
+  })), [mutateIsEnabledUnsavedWarning]);
 
 
 
 
-  const markdownChangedHandler = useCallback((value: string): void => {
-    setMarkdownWithDebounce(value);
+  const markdownChangedHandler = useCallback((value: string, isClean: boolean): void => {
+    setMarkdownWithDebounce(value, isClean);
   }, [setMarkdownWithDebounce]);
   }, [setMarkdownWithDebounce]);
 
 
   const save = useCallback(async(opts?: {overwriteScopesOfDescendants: boolean}) => {
   const save = useCallback(async(opts?: {overwriteScopesOfDescendants: boolean}) => {

+ 8 - 1
packages/app/src/components/PageEditor/CodeMirrorEditor.jsx

@@ -168,6 +168,9 @@ class CodeMirrorEditor extends AbstractEditor {
     // ensure to be able to resolve 'this' to use 'codemirror.commands.save'
     // ensure to be able to resolve 'this' to use 'codemirror.commands.save'
     this.getCodeMirror().codeMirrorEditor = this;
     this.getCodeMirror().codeMirrorEditor = this;
 
 
+    // mark clean
+    this.getCodeMirror().getDoc().markClean();
+
     // fold drawio section
     // fold drawio section
     this.foldDrawioSection();
     this.foldDrawioSection();
 
 
@@ -244,6 +247,9 @@ class CodeMirrorEditor extends AbstractEditor {
    */
    */
   setValue(newValue) {
   setValue(newValue) {
     this.getCodeMirror().getDoc().setValue(newValue);
     this.getCodeMirror().getDoc().setValue(newValue);
+
+    // mark clean
+    this.getCodeMirror().getDoc().markClean();
   }
   }
 
 
   /**
   /**
@@ -564,7 +570,8 @@ class CodeMirrorEditor extends AbstractEditor {
 
 
   changeHandler(editor, data, value) {
   changeHandler(editor, data, value) {
     if (this.props.onChange != null) {
     if (this.props.onChange != null) {
-      this.props.onChange(value);
+      const isClean = data.origin == null || editor.isClean();
+      this.props.onChange(value, isClean);
     }
     }
 
 
     this.updateCheatsheetStates(null, value);
     this.updateCheatsheetStates(null, value);

+ 1 - 1
packages/app/src/components/PageEditor/Editor.tsx

@@ -29,7 +29,7 @@ type EditorPropsType = {
   isUploadable?: boolean,
   isUploadable?: boolean,
   isUploadableFile?: boolean,
   isUploadableFile?: boolean,
   isTextlintEnabled?: boolean,
   isTextlintEnabled?: boolean,
-  onChange?: (newValue: string) => void,
+  onChange?: (newValue: string, isClean?: boolean) => void,
   onUpload?: (file) => void,
   onUpload?: (file) => void,
   indentSize?: number,
   indentSize?: number,
   onScroll?: ({ line: number }) => void,
   onScroll?: ({ line: number }) => void,