-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.js
165 lines (139 loc) · 4.17 KB
/
renderer.js
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// renderer.js
const { ipcRenderer } = require('electron');
let currentFilePath = null;
let editor;
let isRTL = true;
document.addEventListener('DOMContentLoaded', () => {
const editor = CodeMirror.fromTextArea(document.getElementById('editor'), {
mode: 'markdown',
lineNumbers: true,
lineWrapping: true,
direction: 'rtl',
theme: 'default',
rtlMoveVisually: true,
viewportMargin: Infinity,
});
let isAutoSaveEnabled = true;
let autoSaveTimer = null;
setDirection(isRTL);
ipcRenderer.on('toggle-text-direction', () => {
isRTL = !isRTL;
setDirection(isRTL);
});
function setDirection(rtl) {
const direction = rtl ? 'rtl' : 'ltr';
document.documentElement.dir = direction;
editor.setOption('direction', direction);
// Adjust CodeMirror sizer margin
const sizer = editor.display.sizer;
if (rtl) {
sizer.style.marginLeft = '';
sizer.style.marginRight = 'auto';
} else {
sizer.style.marginRight = '';
sizer.style.marginLeft = 'auto';
}
// Force CodeMirror to update its layout
editor.refresh();
}
async function saveFile() {
if (currentFilePath) {
const result = await ipcRenderer.invoke('save-file', { filePath: currentFilePath, content: editor.getValue() });
if (!result.success) {
console.error('Failed to save file:', result.error);
}
} else {
await saveAs();
}
}
async function saveAs() {
const result = await ipcRenderer.invoke('save-file-dialog');
if (!result.canceled) {
currentFilePath = result.filePath;
const saveResult = await ipcRenderer.invoke('save-file', { filePath: currentFilePath, content: editor.getValue() });
if (!saveResult.success) {
console.error('Failed to save file:', saveResult.error);
}
}
}
// Handle Ctrl+S shortcut
document.addEventListener('keydown', async (e) => {
if (e.ctrlKey && e.key === 's') {
e.preventDefault();
await saveFile();
}
});
// Auto-save functionality
function startAutoSave() {
if (autoSaveTimer) clearTimeout(autoSaveTimer);
autoSaveTimer = setTimeout(async () => {
if (currentFilePath && isAutoSaveEnabled) {
await saveFile();
console.log('Auto-saved');
}
}, 3000); // Auto-save after 3 seconds of inactivity
}
editor.on('change', () => {
if (isAutoSaveEnabled) {
startAutoSave();
}
console.log('Editor content changed');
});
ipcRenderer.on('menu-save', saveFile);
ipcRenderer.on('menu-save-as', saveAs);
ipcRenderer.on('menu-open', async () => {
const result = await ipcRenderer.invoke('open-file-dialog');
if (result) {
currentFilePath = result.filePath;
editor.setValue(result.content);
}
});
ipcRenderer.on('toggle-auto-save', (event, enabled) => {
isAutoSaveEnabled = enabled;
console.log('Auto-save toggled:', enabled);
if (!enabled && autoSaveTimer) {
clearTimeout(autoSaveTimer);
}
});
// Add undo and redo handlers
ipcRenderer.on('undo', () => {
editor.undo();
});
ipcRenderer.on('redo', () => {
editor.redo();
});
// Add copy and paste handlers (optional, as CodeMirror handles these by default)
ipcRenderer.on('copy', () => {
const selectedText = editor.getSelection();
if (selectedText) {
navigator.clipboard.writeText(selectedText);
}
});
ipcRenderer.on('paste', async () => {
try {
const text = await navigator.clipboard.readText();
editor.replaceSelection(text);
} catch (err) {
console.error('Failed to paste:', err);
}
});
// Ensure the editor takes up the full height
const resizeEditor = () => {
editor.setSize(null, window.innerHeight);
};
// Initial resize
resizeEditor();
// Resize on window resize
window.addEventListener('resize', resizeEditor);
// editor.setValue('# سلام دنیا\n\nاین یک ویرایشگر مارکداون راست به چپ است.');
ipcRenderer.on('new-file', () => {
editor.setValue('');
currentFilePath = null;
});
editor.on('focus', () => {
console.log('Editor focused');
});
editor.on('blur', () => {
console.log('Editor blurred');
});
});