attempt to fix cursor position for UNO/REDO

This commit is contained in:
Gamosoft 2025-12-14 15:16:06 +01:00
parent 554d5c2537
commit 21b6092502
1 changed files with 35 additions and 14 deletions

View File

@ -2057,8 +2057,8 @@ function noteApp() {
this.currentImage = ''; // Clear image viewer when loading a note this.currentImage = ''; // Clear image viewer when loading a note
this.lastSaved = false; this.lastSaved = false;
// Initialize undo/redo history for this note // Initialize undo/redo history for this note (with cursor at start)
this.undoHistory = [data.content]; this.undoHistory = [{ content: data.content, cursorPos: 0 }];
this.redoHistory = []; this.redoHistory = [];
// Update browser URL and history // Update browser URL and history
@ -2634,15 +2634,18 @@ function noteApp() {
}, CONFIG.AUTOSAVE_DELAY); }, CONFIG.AUTOSAVE_DELAY);
}, },
// Push current content to undo history // Push current content to undo history (with cursor position)
pushToHistory() { pushToHistory() {
const editor = document.getElementById('note-editor');
const cursorPos = editor ? editor.selectionStart : 0;
// Only push if content actually changed // Only push if content actually changed
if (this.undoHistory.length > 0 && if (this.undoHistory.length > 0 &&
this.undoHistory[this.undoHistory.length - 1] === this.noteContent) { this.undoHistory[this.undoHistory.length - 1].content === this.noteContent) {
return; return;
} }
this.undoHistory.push(this.noteContent); this.undoHistory.push({ content: this.noteContent, cursorPos });
// Limit history size // Limit history size
if (this.undoHistory.length > this.maxHistorySize) { if (this.undoHistory.length > this.maxHistorySize) {
@ -2657,26 +2660,35 @@ function noteApp() {
undo() { undo() {
if (!this.currentNote || this.undoHistory.length <= 1) return; if (!this.currentNote || this.undoHistory.length <= 1) return;
const editor = document.getElementById('note-editor');
// Pop current state to redo history // Pop current state to redo history
const currentContent = this.undoHistory.pop(); const currentState = this.undoHistory.pop();
this.redoHistory.push(currentContent); this.redoHistory.push(currentState);
// Get previous state // Get previous state
const previousContent = this.undoHistory[this.undoHistory.length - 1]; const previousState = this.undoHistory[this.undoHistory.length - 1];
// Apply previous state // Apply previous state
this.isUndoRedo = true; this.isUndoRedo = true;
this.noteContent = previousContent; this.noteContent = previousState.content;
// Recalculate stats with new content // Recalculate stats with new content
if (this.statsPluginEnabled) { if (this.statsPluginEnabled) {
this.calculateStats(); this.calculateStats();
} }
// Save the undone state // Restore cursor position from the state we're going back to
this.$nextTick(() => { this.$nextTick(() => {
this.saveNote(); this.saveNote();
this.isUndoRedo = false; this.isUndoRedo = false;
if (editor) {
setTimeout(() => {
const newPos = Math.min(previousState.cursorPos, this.noteContent.length);
editor.setSelectionRange(newPos, newPos);
editor.focus();
}, 0);
}
}); });
}, },
@ -2684,25 +2696,34 @@ function noteApp() {
redo() { redo() {
if (!this.currentNote || this.redoHistory.length === 0) return; if (!this.currentNote || this.redoHistory.length === 0) return;
const editor = document.getElementById('note-editor');
// Pop from redo history // Pop from redo history
const nextContent = this.redoHistory.pop(); const nextState = this.redoHistory.pop();
// Push to undo history // Push to undo history
this.undoHistory.push(nextContent); this.undoHistory.push(nextState);
// Apply next state // Apply next state
this.isUndoRedo = true; this.isUndoRedo = true;
this.noteContent = nextContent; this.noteContent = nextState.content;
// Recalculate stats with new content // Recalculate stats with new content
if (this.statsPluginEnabled) { if (this.statsPluginEnabled) {
this.calculateStats(); this.calculateStats();
} }
// Save the redone state // Restore cursor position from the state we're going forward to
this.$nextTick(() => { this.$nextTick(() => {
this.saveNote(); this.saveNote();
this.isUndoRedo = false; this.isUndoRedo = false;
if (editor) {
setTimeout(() => {
const newPos = Math.min(nextState.cursorPos, this.noteContent.length);
editor.setSelectionRange(newPos, newPos);
editor.focus();
}, 0);
}
}); });
}, },