-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14.DesignanOnlineTextEditor.py
More file actions
84 lines (60 loc) · 2.24 KB
/
14.DesignanOnlineTextEditor.py
File metadata and controls
84 lines (60 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Build a simple in-memory text editor where multiple users can create,
# edit, undo, and redo changes to their text documents.
# The editor should maintain each user’s document history and
# support fast undo/redo operations using the Command Pattern, without any external database.
class Command:
def __init__(self, execute_func, undo_func):
self.execute_func = execute_func
self.undo_func = undo_func
def execute(self):
self.execute_func()
def undo(self):
self.undo_func()
class TextEditor:
def __init__(self):
self.documents = {}
self.undo_stack = []
self.redo_stack = []
def create_doc(self, user, doc_name, text = ""):
if user not in self.documents:
self.documents[user] = {}
self.documents[user][doc_name] = text
print(f"Document '{doc_name}' created for {user}.")
def edit_doc(self, user, doc_name, new_text):
prev_text = self.documents[user][doc_name]
def execute():
self.documents[user][doc_name] = new_text
def undo():
self.documents[user][doc_name] = prev_text
cmd = Command(execute, undo)
cmd.execute()
self.undo_stack.append(cmd)
self.redo_stack.clear()
print(f"Edited '{doc_name}' for {user}.")
def undo(self):
if not self.undo_stack:
print("Nothing to undo.")
return
cmd = self.undo_stack.pop()
cmd.undo()
self.redo_stack.append(cmd)
print("Undo operation performed.")
def redo(self):
if not self.redo_stack:
print("Nothing to redo.")
return
cmd = self.redo_stack.pop()
cmd.execute()
self.undo_stack.append(cmd)
print("Redo operation performed.")
def show_doc(self, user, doc_name):
print(f"Current Text of '{doc_name}': {self.documents[user][doc_name]}")
if __name__ == "__main__":
editor = TextEditor()
editor.create_doc("user1", "doc1", "Hello")
editor.edit_doc("user1", "doc1", "Hello World!")
editor.show_doc("user1", "doc1")
editor.undo()
editor.show_doc("user1", "doc1")
editor.redo()
editor.show_doc("user1", "doc1")