Skip to content

Commit

Permalink
Github Issue #165: Fixed the undo and redo routine to correctly updat…
Browse files Browse the repository at this point in the history
…e the rawDocument with old and new texts. Also corrected the position of text cursor after redo action was performed. Also added checks to stop undo/redo actions if out of limit
  • Loading branch information
AngryFender committed Jul 20, 2024
1 parent a014bd4 commit f61df66
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 39 deletions.
64 changes: 40 additions & 24 deletions mkedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ void MkEdit::keyPressEvent(QKeyEvent *event)
case Qt::Key_Enter:
case Qt::Key_Return: undoData.editType = multiEdit; break;
case Qt::Key_D: if( event->modifiers() == Qt::CTRL) {undoData.editType = multiEdit;}break;
case Qt::Key_Z: if( event->modifiers() == Qt::CTRL) {undoData.editType = multiEdit;}break;
case Qt::Key_Y: if( event->modifiers() == Qt::CTRL) {undoData.editType = multiEdit;}break;
case Qt::Key_Z: if( event->modifiers() == Qt::CTRL) {undoData.editType = undoRedo;undoData.undoRedoSkip = true;}break;
case Qt::Key_Y: if( event->modifiers() == Qt::CTRL) {undoData.editType = undoRedo;undoData.undoRedoSkip = true;}break;
case Qt::Key_Backspace:{
QString blockText = this->textCursor().block().text();
if((textCursor().positionInBlock() == 0) || (blockText.left(3)=="```")){
Expand All @@ -183,8 +183,12 @@ void MkEdit::keyPressEvent(QKeyEvent *event)
case Qt::Key_QuoteLeft: undoData.editType = multiEdit; break;
}

if(textCursor().hasSelection()){
undoData.editType = multiEdit;
if(textCursor().hasSelection() && undoData.editType != undoRedo){
if(selectRange.selectionFirstStartBlock == selectRange.selectionEndBlock){
undoData.editType = singleEdit;
}else{
undoData.editType = multiEdit;
}
}
clearMkEffects(undoData.editType);
QTextEdit::keyPressEvent(event);
Expand All @@ -205,25 +209,31 @@ void MkEdit::keyPressEvent(QKeyEvent *event)
}break;
case Qt::Key_D: if( event->modifiers() == Qt::CTRL) {emit duplicateLine(this->textCursor().blockNumber());; fileSaveNow(); return;}break;
case Qt::Key_Z: if( event->modifiers() == Qt::CTRL) {
emit undoStackUndoSignal();
undoData.editType = (undoData.viewEditTypeStore? *undoData.viewEditTypeStore: multiEdit);
undoData.undoRedoSkip = true;
fileSaveTimer.stop();
postUndoSetup();
emit fileSaveRaw();
applyMkEffects();
showSelectionAfterUndo();
bool success = false;
emit undoStackUndoSignal(success);
if(success){
undoData.editType = (undoData.viewEditTypeStore? *undoData.viewEditTypeStore: multiEdit);
undoData.undoRedoSkip = true;
fileSaveTimer.stop();
postUndoSetup();
emit fileSaveRaw();
applyMkEffects();
showSelectionAfterUndo();
}
return;
}break;
case Qt::Key_Y: if( event->modifiers() == Qt::CTRL) {
emit undoStackRedoSignal();
undoData.editType = (undoData.viewEditTypeStore? *undoData.viewEditTypeStore: multiEdit);
undoData.undoRedoSkip = true;
fileSaveTimer.stop();
postUndoSetup();
emit fileSaveRaw();
applyMkEffects();
showSelectionAfterRedo();
bool success = false;
emit undoStackRedoSignal(success);
if(success){
undoData.editType = (undoData.viewEditTypeStore? *undoData.viewEditTypeStore: multiEdit);
undoData.undoRedoSkip = true;
fileSaveTimer.stop();
postUndoSetup();
emit fileSaveRaw();
applyMkEffects();
showSelectionAfterRedo();
}
return;
}break;
default: break;
Expand Down Expand Up @@ -288,14 +298,15 @@ void MkEdit::showSelectionAfterUndo(){
void MkEdit::showSelectionAfterRedo()
{
SelectRange &range = undoRedoSelectRange;
QTextCursor textCursor = this->textCursor();
int currentBlock = range.currentBlockNo;
int pos = range.currentposInBlock;

//first show all the Markdown symbols in the editor
emit cursorPosChanged(&range);

int cursorPos = this->document()->findBlockByNumber(range.currentBlockNo).position() + range.currentposInBlock ;
textCursor.setPosition(cursorPos);
this->setTextCursor(textCursor);
QTextCursor cursor = this->textCursor();
cursor.setPosition(this->document()->findBlockByNumber(currentBlock).position()+pos);
this->setTextCursor(cursor);

if(range.isCheckBox){
this->verticalScrollBar()->setSliderPosition(range.scrollValue);
Expand Down Expand Up @@ -425,6 +436,9 @@ QRect MkEdit::getVisibleRect()
void MkEdit::clearMkEffects(EditType editType)
{
undoData.scrollValue = this->verticalScrollBar()->sliderPosition(); //this is important
if(editType == undoRedo){
return;
}

QTextCursor cursor = this->textCursor();
int blockNumber = cursor.blockNumber();
Expand Down Expand Up @@ -461,6 +475,7 @@ void MkEdit::clearMkEffects(EditType editType)
void MkEdit::applyMkEffects()
{
switch(undoData.editType){
case undoRedo: break;
case singleEdit: emit applyMkSingleBlock(this->textCursor().blockNumber()); break;
case checkbox:
case enterPressed:
Expand All @@ -484,6 +499,7 @@ void MkEdit::applyMkEffects()
void MkEdit::updateRawDocument()
{
switch(undoData.editType){
case EditType::undoRedo: break;
case EditType::singleEdit:
if(undoData.oldSelectRange.currentBlockNo == this->textCursor().blockNumber()){
emit saveSingleRawBlock(textCursor().blockNumber()); return;
Expand Down
4 changes: 2 additions & 2 deletions mkedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ private slots:
void checkIfCursorInBlock(bool &isBlock, QTextCursor &cursor);

void undoStackPushSignal(QUndoCommand *);
void undoStackUndoSignal();
void undoStackRedoSignal();
void undoStackUndoSignal(bool &success);
void undoStackRedoSignal(bool &success);
void undoStackClear();
void escapeFocus(QWidget*view);
};
Expand Down
27 changes: 17 additions & 10 deletions mktextdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void MkTextDocument::setUndoRedoText(const int blockNo, const QString &text)
cursor.insertText(text);

QTextCursor rawCursor(&rawDocument);
rawCursor.setPosition(findBlockByNumber(blockNo).position());
rawCursor.setPosition(rawDocument.findBlockByNumber(blockNo).position());
rawCursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::MoveAnchor);
rawCursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
rawCursor.insertText(text);
Expand All @@ -59,7 +59,7 @@ void MkTextDocument::setUndoEnterPressedText(const int blockNo, const QString &t
cursor.deleteChar();

QTextCursor rawCursor(&rawDocument);
rawCursor.setPosition(findBlockByNumber(blockNo).position());
rawCursor.setPosition(rawDocument.findBlockByNumber(blockNo).position());
rawCursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::MoveAnchor);
rawCursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
rawCursor.insertText(text);
Expand Down Expand Up @@ -164,6 +164,7 @@ void MkTextDocument::applyAllMkDataHandle(int blockNumber)
for(int num = 0; num < this->blockCount(); num++){
this->selectRange.hideBlocks.insert(num);
}
this->selectRange.hideBlocks.erase(blockNumber);
hideMKSymbolsFromPreviousSelectedBlocks(&this->selectRange);

this->selectRange.showBlocks.insert(blockNumber);
Expand Down Expand Up @@ -1293,14 +1294,20 @@ void MkTextDocument::undoStackPush(QUndoCommand *edit)
undoStack.push(edit);
}

void MkTextDocument::undoStackUndo()
void MkTextDocument::undoStackUndo(bool &success)
{
undoStack.undo();
if(undoStack.canUndo()){
undoStack.undo();
success = true;
}
}

void MkTextDocument::undoStackRedo()
void MkTextDocument::undoStackRedo(bool &success)
{
undoStack.redo();
if(undoStack.canRedo()){
undoStack.redo();
success = true;
}
}

void MkTextDocument::resetFormatLocation()
Expand Down Expand Up @@ -1398,6 +1405,7 @@ EditCommand::EditCommand(UndoData &data)
this->oldBlock = data.oldBlock;

switch(editType){
case undoRedo: return;
case singleEdit:
this->newBlock = data.newBlock;
this->oldBlock = data.oldBlock;
Expand All @@ -1414,6 +1422,7 @@ EditCommand::EditCommand(UndoData &data)
void EditCommand::undo()
{
switch(editType){
case undoRedo: return;
case singleEdit: doc->setUndoRedoText(oldSelectRange.currentBlockNo, this->oldBlock); break;
case checkbox:
case enterPressed:
Expand All @@ -1429,18 +1438,16 @@ void EditCommand::redo()
isConstructorRedo = false;
}else{
switch(editType){
case undoRedo: return;
case singleEdit: doc->setUndoRedoText(this->blockNo, this->newBlock);break;
case checkbox:
case enterPressed:
case multiEdit: doc->setUndoRedoText(text); break;
}

*this->viewEditTypeStore = editType;
QTextCursor cursor = this->view->textCursor();
cursor.setPosition(this->view->document()->findBlockByNumber(this->blockNo).position());
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor, this->posInBlock);

this->view->setTextCursor(cursor);
viewSelectRangeStore->hasSelection = false;
viewSelectRangeStore->currentBlockNo = this->blockNo;
viewSelectRangeStore->currentposInBlock = this->posInBlock;
viewSelectRangeStore->isCheckBox = this->oldSelectRange.isCheckBox;
Expand Down
7 changes: 4 additions & 3 deletions mktextdocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ public slots:
void cursorUpdateHandle(const int blockNo, const int characterNo);

void undoStackPush(QUndoCommand *edit);
void undoStackUndo();
void undoStackRedo();
void undoStackUndo(bool &success);
void undoStackRedo(bool &success);

signals:
void disconnectCursorPos(bool override = false);
Expand Down Expand Up @@ -235,7 +235,8 @@ public slots:
};

enum EditType{
singleEdit = 0,
undoRedo = 0,
singleEdit,
checkbox,
enterPressed,
multiEdit,
Expand Down

0 comments on commit f61df66

Please sign in to comment.