Fix note saves and drafts across asynchronous edits
This commit is contained in:
1 parent
8ce4a11b33
commit
7e52d9a00b
4 files changed
+161
-37
No files matched your search
+4
-14
@@ -4,6 +4,8 @@ import { EditorView } from '@codemirror/view'
|
||||
export interface PasteImagesOptions {
|
||||
// 上传文件,返回写入 Markdown 的链接
|
||||
upload: (file: File) => Promise<string>
|
||||
// 按笔记身份修改占位,上传结束时该笔记可能已切到后台
|
||||
replace: (text: string, replacement: string) => void
|
||||
onError: (err: unknown) => void
|
||||
// 上传开始/结束,供保存队列在上传期间暂缓保存(避免把临时的 blob: 地址写入文件)
|
||||
onPending: (delta: 1 | -1) => void
|
||||
@@ -33,9 +35,9 @@ export function insertImageFiles(view: EditorView, files: File[], pos?: number):
|
||||
opts.onPending(1)
|
||||
opts
|
||||
.upload(file)
|
||||
.then((link) => replaceUrl(view, urls[i], link))
|
||||
.then((link) => opts.replace(urls[i], link))
|
||||
.catch((err) => {
|
||||
removePlaceholder(view, urls[i])
|
||||
opts.replace(``, '')
|
||||
opts.onError(err)
|
||||
})
|
||||
.finally(() => {
|
||||
@@ -71,15 +73,3 @@ export function pasteImages(opts: PasteImagesOptions): Extension {
|
||||
}),
|
||||
]
|
||||
}
|
||||
|
||||
// 占位可能已被用户编辑移动,按 blob 地址在当前文档中查找
|
||||
function replaceUrl(view: EditorView, url: string, link: string) {
|
||||
const at = view.state.doc.toString().indexOf(url)
|
||||
if (at >= 0) view.dispatch({ changes: { from: at, to: at + url.length, insert: link } })
|
||||
}
|
||||
|
||||
function removePlaceholder(view: EditorView, url: string) {
|
||||
const placeholder = ``
|
||||
const at = view.state.doc.toString().indexOf(placeholder)
|
||||
if (at >= 0) view.dispatch({ changes: { from: at, to: at + placeholder.length, insert: '' } })
|
||||
}
|
||||
Reference in new issue
Block a user