-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathDocumentEditor.py
57 lines (43 loc) · 1.72 KB
/
DocumentEditor.py
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
import os
from typing import List
class DocumentEditor:
def __init__(self, file_path: str):
self.file_path = file_path
self.file_type = os.path.splitext(file_path)[1]
self.file_content = self._read_file()
def _read_file(self):
with open(self.file_path, 'r') as f:
return f.read()
def _write_file(self):
with open(self.file_path, 'w') as f:
f.write(self.file_content)
def insert_text(self, text: str, position: int):
self.file_content = self.file_content[:position] + text + self.file_content[position:]
self._write_file()
def delete_text(self, start: int, end: int):
self.file_content = self.file_content[:start] + self.file_content[end:]
self._write_file()
def format_text(self, start: int, end: int, format_type: str):
# Implement text formatting (bold, italic, underline, etc.)
pass
def insert_image(self, image_path: str, position: int):
# Implement image insertion
pass
def insert_hyperlink(self, link: str, position: int):
# Implement hyperlink insertion
pass
def get_file_content(self):
return self.file_content
class DocumentEditorManager:
def __init__(self):
self.editors = {}
def create_editor(self, file_path: str) -> str:
editor_id = str(len(self.editors))
self.editors[editor_id] = DocumentEditor(file_path)
return editor_id
def delete_editor(self, editor_id: str):
del self.editors[editor_id]
def get_editor(self, editor_id: str) -> DocumentEditor:
return self.editors[editor_id]
def get_all_editors(self) -> List[DocumentEditor]:
return list(self.editors.values())