Fix language auto detection/changing breaking undo/redo history

This commit is contained in:
Jonatan Heyman 2023-01-16 11:11:28 +01:00
parent 996d4338ce
commit 7a74798a11

View File

@ -1,5 +1,6 @@
import { EditorState } from "@codemirror/state"; import { EditorState } from "@codemirror/state";
import { EditorView } from "codemirror"; import { EditorView } from "codemirror";
import { redoDepth } from "@codemirror/commands";
import { getActiveNoteBlock, blockState } from "../block/note-block"; import { getActiveNoteBlock, blockState } from "../block/note-block";
import { levenshtein_distance } from "./levenshtein"; import { levenshtein_distance } from "./levenshtein";
import { HIGHLIGHTJS_TO_TOKEN } from "../languages"; import { HIGHLIGHTJS_TO_TOKEN } from "../languages";
@ -28,8 +29,12 @@ export function languageDetection(getView) {
const threshold = content.length * 0.1 const threshold = content.length * 0.1
if (levenshtein_distance(content, event.data.content) <= threshold) { if (levenshtein_distance(content, event.data.content) <= threshold) {
// the content has not changed significantly so it's safe to change the language // the content has not changed significantly so it's safe to change the language
console.log("Changing language to", newLang) if (redoDepth(state) === 0) {
changeLanguageTo(state, view.dispatch, block, newLang, true) console.log("Changing language to", newLang)
changeLanguageTo(state, view.dispatch, block, newLang, true)
} else {
console.log("Not changing language because the user has undo:ed and has redo history")
}
} else { } else {
console.log("Content has changed significantly, not setting new language") console.log("Content has changed significantly, not setting new language")
} }
@ -67,7 +72,7 @@ export function languageDetection(getView) {
} }
const content = update.state.doc.sliceString(block.content.from, block.content.to) const content = update.state.doc.sliceString(block.content.from, block.content.to)
if (content === "") { if (content === "" && redoDepth(update.state) === 0) {
// if content is cleared, set language to plaintext // if content is cleared, set language to plaintext
const view = getView() const view = getView()
const block = getActiveNoteBlock(view.state) const block = getActiveNoteBlock(view.state)