mirror of
https://github.com/MeowLynxSea/ceditor.git
synced 2025-07-09 10:54:37 +00:00
完成了撤回、重做功能
This commit is contained in:
parent
09c45e5af8
commit
273ecc77b0
@ -22,7 +22,8 @@ private:
|
|||||||
Insert,
|
Insert,
|
||||||
Delete,
|
Delete,
|
||||||
Command
|
Command
|
||||||
} inputstatus_ = InputStatus::Insert;
|
};
|
||||||
|
InputStatus inputstatus_;
|
||||||
static const int MAX_DIVISION_TIME = 1200; // 1.2s
|
static const int MAX_DIVISION_TIME = 1200; // 1.2s
|
||||||
int lastInputTime_ = 0;
|
int lastInputTime_ = 0;
|
||||||
|
|
||||||
@ -55,18 +56,20 @@ public:
|
|||||||
content_ = "";
|
content_ = "";
|
||||||
cursor_.setVisibility(false);
|
cursor_.setVisibility(false);
|
||||||
actionmanager_.setOriginContent(content_);
|
actionmanager_.setOriginContent(content_);
|
||||||
|
inputstatus_ = InputStatus::Insert;
|
||||||
};
|
};
|
||||||
|
|
||||||
void setRuleName(std::string ruleName) { ruleName_ = ruleName; }
|
void setRuleName(std::string ruleName) { ruleName_ = ruleName; }
|
||||||
std::string getRuleName() { return ruleName_; }
|
std::string getRuleName() { return ruleName_; }
|
||||||
|
|
||||||
void setContent(std::string content) {
|
void setContent(std::string content, bool firstTime = false) {
|
||||||
content_ = content;
|
content_ = content;
|
||||||
coloredContent_ = SyntaxHighlighter(ruleName_).highlight(content_);
|
coloredContent_ = SyntaxHighlighter(ruleName_).highlight(content_);
|
||||||
textArea_.setText(coloredContent_);
|
textArea_.setText(coloredContent_);
|
||||||
lineedContent_ = split(content_);
|
lineedContent_ = split(content_);
|
||||||
actionmanager_.setOriginContent(content_);
|
if(firstTime) actionmanager_.setOriginContent(content_);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string getContent() { return content_; }
|
std::string getContent() { return content_; }
|
||||||
|
|
||||||
void setTitle(std::string title) {
|
void setTitle(std::string title) {
|
||||||
@ -131,14 +134,20 @@ public:
|
|||||||
int currentChar = cursor_.getLeft() - left - 1 + textArea_.getViewLeft();
|
int currentChar = cursor_.getLeft() - left - 1 + textArea_.getViewLeft();
|
||||||
int lineLength = lineedContent_[currentLine].size();
|
int lineLength = lineedContent_[currentLine].size();
|
||||||
|
|
||||||
if(key == 72 + 256) { // up
|
if(key >= 256 && key < 512) {
|
||||||
moveUp();
|
if(inputstatus_ != InputStatus::Command) {
|
||||||
} else if(key == 80 + 256) { // down
|
inputstatus_ = InputStatus::Command;
|
||||||
moveDown();
|
actionmanager_.updateContent(content_);
|
||||||
} else if(key == 75 + 256) { // left
|
}
|
||||||
moveLeft();
|
if (key == 72 + 256) { // up
|
||||||
} else if(key == 77 + 256) { // right
|
moveUp();
|
||||||
moveRight();
|
} else if(key == 80 + 256) { // down
|
||||||
|
moveDown();
|
||||||
|
} else if(key == 75 + 256) { // left
|
||||||
|
moveLeft();
|
||||||
|
} else if(key == 77 + 256) { // right
|
||||||
|
moveRight();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if(key == 224) return;
|
if(key == 224) return;
|
||||||
if(currentChar > lineLength) {
|
if(currentChar > lineLength) {
|
||||||
@ -151,7 +160,8 @@ public:
|
|||||||
if(key == 8) { // backspace
|
if(key == 8) { // backspace
|
||||||
if(inputstatus_ != InputStatus::Delete) {
|
if(inputstatus_ != InputStatus::Delete) {
|
||||||
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) {
|
if(currentChar == 0) {
|
||||||
|
@ -24,7 +24,7 @@ int main() {
|
|||||||
content = content.substr(0, content.size() - 1); // remove last newline
|
content = content.substr(0, content.size() - 1); // remove last newline
|
||||||
|
|
||||||
editor.setRuleName("cpp");
|
editor.setRuleName("cpp");
|
||||||
editor.setContent(content);
|
editor.setContent(content, true);
|
||||||
|
|
||||||
editor.draw();
|
editor.draw();
|
||||||
|
|
||||||
@ -33,8 +33,8 @@ int main() {
|
|||||||
int scan = _getch(), opt;
|
int scan = _getch(), opt;
|
||||||
if(scan == 224) {
|
if(scan == 224) {
|
||||||
opt = _getch() + 256;
|
opt = _getch() + 256;
|
||||||
} else if(scan == 26) {
|
} else if(scan == 26 || scan == 25) {
|
||||||
opt = 26 + 512;
|
opt = scan + 512;
|
||||||
} else {
|
} else {
|
||||||
opt = scan;
|
opt = scan;
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#include "../mystl/my_stack.h"
|
#include "../mystl/my_stack.h"
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <windows.h>
|
||||||
|
|
||||||
enum class EditActiontype {
|
enum class EditActiontype {
|
||||||
Initialize,
|
Initialize,
|
||||||
@ -23,6 +24,18 @@ private:
|
|||||||
MyStack<MyVector<EditAction>> redoStack_;
|
MyStack<MyVector<EditAction>> redoStack_;
|
||||||
std::string content_;
|
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:
|
public:
|
||||||
MyVector<EditAction> calculateEditActions(const std::string &str1, const std::string &str2) { // 规定Editor只会在删除或者插入时提交,因此只会同时存在一种操作
|
MyVector<EditAction> calculateEditActions(const std::string &str1, const std::string &str2) { // 规定Editor只会在删除或者插入时提交,因此只会同时存在一种操作
|
||||||
int m = str1.length();
|
int m = str1.length();
|
||||||
@ -89,54 +102,56 @@ public:
|
|||||||
return actions;
|
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() {
|
||||||
}
|
}
|
||||||
ActionManager(const std::string &content) {
|
ActionManager(std::string content) {
|
||||||
content_ = 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();
|
redoStack_.clear();
|
||||||
MyVector<EditAction> actions = calculateEditActions(content, content_);
|
MyVector<EditAction> actions = calculateEditActions(content, content_);
|
||||||
content_ = content;
|
content_ = content;
|
||||||
undoStack_.push(actions);
|
undoStack_.push(actions);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void undo() {
|
bool undo() {
|
||||||
if (undoStack_.empty()) {
|
if (undoStack_.empty()) {
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
MyVector<EditAction> actions = undoStack_.top();
|
MyVector<EditAction> actions = undoStack_.top();
|
||||||
undoStack_.pop();
|
undoStack_.pop();
|
||||||
std::string newContent = applyEditAction(actions, content_);
|
std::string newContent = applyEditAction(actions, content_);
|
||||||
redoStack_.push(calculateEditActions(newContent, content_));
|
redoStack_.push(calculateEditActions(newContent, content_));
|
||||||
content_ = newContent;
|
content_ = newContent;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void redo() {
|
bool redo() {
|
||||||
if (redoStack_.empty()) {
|
if (redoStack_.empty()) {
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
MyVector<EditAction> actions = redoStack_.top();
|
MyVector<EditAction> actions = redoStack_.top();
|
||||||
redoStack_.pop();
|
redoStack_.pop();
|
||||||
std::string newContent = applyEditAction(actions, content_);
|
std::string newContent = applyEditAction(actions, content_);
|
||||||
undoStack_.push(calculateEditActions(newContent, content_));
|
undoStack_.push(calculateEditActions(newContent, content_));
|
||||||
content_ = newContent;
|
content_ = newContent;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setOriginContent(std::string content) {
|
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;
|
content_ = content;
|
||||||
undoStack_.clear();
|
undoStack_.clear();
|
||||||
redoStack_.clear();
|
redoStack_.clear();
|
||||||
|
@ -6,6 +6,17 @@ using namespace std;
|
|||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
ActionManager am;
|
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;
|
MyVector<EditAction> v;
|
||||||
std::string s0 = "i can see you, but u dont love me";
|
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");
|
v = am.calculateEditActions("i can see you, but u dont love me", "i cant hug u, but i love you");
|
||||||
|
Loading…
Reference in New Issue
Block a user