完成了撤回、重做功能

This commit is contained in:
梦凌汐 2024-12-19 17:54:25 +08:00
parent 09c45e5af8
commit 273ecc77b0
4 changed files with 69 additions and 33 deletions

View File

@ -22,7 +22,8 @@ private:
Insert,
Delete,
Command
} inputstatus_ = InputStatus::Insert;
};
InputStatus inputstatus_;
static const int MAX_DIVISION_TIME = 1200; // 1.2s
int lastInputTime_ = 0;
@ -55,18 +56,20 @@ public:
content_ = "";
cursor_.setVisibility(false);
actionmanager_.setOriginContent(content_);
inputstatus_ = InputStatus::Insert;
};
void setRuleName(std::string ruleName) { ruleName_ = ruleName; }
std::string getRuleName() { return ruleName_; }
void setContent(std::string content) {
void setContent(std::string content, bool firstTime = false) {
content_ = content;
coloredContent_ = SyntaxHighlighter(ruleName_).highlight(content_);
textArea_.setText(coloredContent_);
lineedContent_ = split(content_);
actionmanager_.setOriginContent(content_);
if(firstTime) actionmanager_.setOriginContent(content_);
}
std::string getContent() { return content_; }
void setTitle(std::string title) {
@ -131,7 +134,12 @@ public:
int currentChar = cursor_.getLeft() - left - 1 + textArea_.getViewLeft();
int lineLength = lineedContent_[currentLine].size();
if(key == 72 + 256) { // up
if(key >= 256 && key < 512) {
if(inputstatus_ != InputStatus::Command) {
inputstatus_ = InputStatus::Command;
actionmanager_.updateContent(content_);
}
if (key == 72 + 256) { // up
moveUp();
} else if(key == 80 + 256) { // down
moveDown();
@ -139,6 +147,7 @@ public:
moveLeft();
} else if(key == 77 + 256) { // right
moveRight();
}
} else {
if(key == 224) return;
if(currentChar > lineLength) {
@ -151,7 +160,8 @@ public:
if(key == 8) { // backspace
if(inputstatus_ != InputStatus::Delete) {
inputstatus_ = InputStatus::Delete;
actionmanager_.updateContent(content_);
std::string rst = actionmanager_.updateContent(getContent()) ? "success" : "fail";
// MessageBoxA(NULL, rst.c_str(), "Update Content", MB_OK);
}
// 如果是第一个字符,则把当前行内容加到上一行末尾
if(currentChar == 0) {

View File

@ -24,7 +24,7 @@ int main() {
content = content.substr(0, content.size() - 1); // remove last newline
editor.setRuleName("cpp");
editor.setContent(content);
editor.setContent(content, true);
editor.draw();
@ -33,8 +33,8 @@ int main() {
int scan = _getch(), opt;
if(scan == 224) {
opt = _getch() + 256;
} else if(scan == 26) {
opt = 26 + 512;
} else if(scan == 26 || scan == 25) {
opt = scan + 512;
} else {
opt = scan;
}

View File

@ -4,6 +4,7 @@
#include "../mystl/my_stack.h"
#include <string>
#include <iostream>
#include <windows.h>
enum class EditActiontype {
Initialize,
@ -23,6 +24,18 @@ private:
MyStack<MyVector<EditAction>> redoStack_;
std::string content_;
std::string applyEditAction(MyVector<EditAction> &actions, std::string &content) {
std::string newContent = content;
for(int i = 0; i < actions.size(); i++) {
if(actions[i].type == EditActiontype::Insert) {
newContent.insert(actions[i].pos, actions[i].content);
} else if(actions[i].type == EditActiontype::Delete) {
newContent.erase(actions[i].pos, actions[i].content.size());
}
}
return newContent;
}
public:
MyVector<EditAction> calculateEditActions(const std::string &str1, const std::string &str2) { // 规定Editor只会在删除或者插入时提交因此只会同时存在一种操作
int m = str1.length();
@ -89,54 +102,56 @@ public:
return actions;
}
std::string applyEditAction(MyVector<EditAction> &actions, std::string &content) {
std::string newContent = content;
for(int i = 0; i < actions.size(); i++) {
if(actions[i].type == EditActiontype::Insert) {
newContent.insert(actions[i].pos, actions[i].content);
} else if(actions[i].type == EditActiontype::Delete) {
newContent.erase(actions[i].pos, actions[i].content.size());
}
}
return newContent;
}
ActionManager() {
}
ActionManager(const std::string &content) {
ActionManager(std::string content) {
content_ = content;
}
void updateContent(const std::string &content) {
bool updateContent(const std::string content) {
// std::string oldContentLength = std::to_string(content_.length());
// std::string newContentLength = std::to_string(content.length());
// std::string message = "Length of old content is " + oldContentLength + " and new content is " + newContentLength;
// MessageBoxA(0, message.c_str(), "Update Content", MB_OK);
if(content == content_) {
return false;
}
redoStack_.clear();
MyVector<EditAction> actions = calculateEditActions(content, content_);
content_ = content;
undoStack_.push(actions);
return true;
}
void undo() {
bool undo() {
if (undoStack_.empty()) {
return;
return false;
}
MyVector<EditAction> actions = undoStack_.top();
undoStack_.pop();
std::string newContent = applyEditAction(actions, content_);
redoStack_.push(calculateEditActions(newContent, content_));
content_ = newContent;
return true;
}
void redo() {
bool redo() {
if (redoStack_.empty()) {
return;
return false;
}
MyVector<EditAction> actions = redoStack_.top();
redoStack_.pop();
std::string newContent = applyEditAction(actions, content_);
undoStack_.push(calculateEditActions(newContent, content_));
content_ = newContent;
return true;
}
void setOriginContent(std::string content) {
// std::string oldContentLength = std::to_string(content_.length());
// std::string newContentLength = std::to_string(content.length());
// std::string message = "Length of old content is " + oldContentLength + " and new content is " + newContentLength;
// MessageBoxA(0, message.c_str(), "Set Origin Content", MB_OK);
content_ = content;
undoStack_.clear();
redoStack_.clear();

View File

@ -6,6 +6,17 @@ using namespace std;
int main() {
ActionManager am;
std::string s_ = "hello,world!";
am.setOriginContent(s_);
s_ = s_.substr(0, 5);
printf("%s\n", s_.c_str());
printf("%s\n", am.getContent().c_str());
am.updateContent(s_);
printf("%s\n", am.getContent().c_str());
am.undo();
printf("%s\n", am.getContent().c_str());
MyVector<EditAction> v;
std::string s0 = "i can see you, but u dont love me";
v = am.calculateEditActions("i can see you, but u dont love me", "i cant hug u, but i love you");