Skip to content

Commit 34ac2fd

Browse files
committed
refactor: Improve save functionality and prevent unnecessary saving
1 parent 4cd1111 commit 34ac2fd

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

packages/markdown-editor/src/components/markdown-editor/toolbar/commands/save.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { EditorView, KeyBinding } from '@codemirror/view'
22
import { ToolbarCommand } from './type'
33
import { CustomEventDetail } from '@/types'
44
import { markdownCustomEventName } from '@/index'
5-
import toast from 'react-hot-toast'
65

76
export const saveKeymap: KeyBinding = {
87
linux: 'Ctrl-s',
@@ -39,8 +38,17 @@ const save: ToolbarCommand = {
3938
execute: saveExecute,
4039
}
4140

41+
const prevDoc: Map<string, string> = new Map()
42+
4243
export function saveExecute(view: EditorView) {
4344
const doc = view.state.doc.toString()
45+
46+
const currentUrl = window.location.href
47+
if (prevDoc.has(currentUrl) && prevDoc.get(currentUrl) === doc) {
48+
return
49+
}
50+
prevDoc.set(currentUrl, doc)
51+
4452
const event = new CustomEvent<CustomEventDetail['updateItemEvent']>(
4553
markdownCustomEventName.updateItemEvent,
4654
{

packages/markdown-editor/src/components/markdown-preview/markdown-preview.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import cn from 'clsx'
22
import { useConfig } from '@/contexts'
3-
import { useEffect, useRef, useState, type ReactElement } from 'react'
3+
import { useEffect, useRef, type ReactElement } from 'react'
44
import { MDXRemote } from 'next-mdx-remote'
55
import { getComponents } from '@/mdx-components'
66
import { useMarkdownEditor } from '@/contexts/markdown-editor'

0 commit comments

Comments
 (0)