-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainForm.cs
More file actions
399 lines (322 loc) · 11.8 KB
/
MainForm.cs
File metadata and controls
399 lines (322 loc) · 11.8 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
using System.Globalization;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
namespace C__Yaml_Parser
{
public partial class frmMain : Form
{
private const string KDocEnd = "---";
private const string KDocStart = "---";
private CultureInfo mEnCulture = new CultureInfo("en-US");
private CultureInfo mRoCulture = new CultureInfo("ro-RO");
private EditorYaml _editorYaml = new();
private string KDefaultFolder =
Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),
"" // Add subfolder here
);
private string KTempFolder =
Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),
"STAGING_temp"
);
private string[] s_AllowedExtensions = [
".md",
".markdown",
".txt",
];
public frmMain()
{
InitializeComponent();
cmbLang.SelectedIndex = 0;
// Add _editorYaml to form
_editorYaml.Dock = DockStyle.Fill;
panelYaml.Controls.Clear();
panelYaml.Controls.Add(_editorYaml);
_editorYaml.BringToFront();
OnBrowse(KDefaultFolder);
this.CenterToScreen();
}
/*
On list selection changed:
- load file name
- load contents
- try to detect front matter data in the .md file
if found -> load data into the interface
- author
- title
- layout
- date
- categories
- etc.
if not found?
- load defaults
*/
private void OnBrowse(string folder)
{
// clear the list
listFiles.Items.Clear();
// clear selection details
ClearSelectionDetails();
// toggle editor states
ToggleEditors(false);
if (string.IsNullOrEmpty(folder))
return;
// folder must exist
if (!Directory.Exists(folder))
return;
LoadFilesFromFolder(folder);
txtFolder.Text = folder;
}
private void LoadFilesFromFolder(string folder)
{
// load actual files
DirectoryInfo di = new DirectoryInfo(folder);
IEnumerable<FileInfo> files = di.EnumerateFiles();
if (chkMarkdownAndText.Checked)
{
files = files.Where(
f => s_AllowedExtensions.Contains(f.Extension)
);
}
foreach (FileInfo file in files)
listFiles.Items.Add(file.FullName);
if (chkIncludeSubfolders.Checked)
{
// load subfolders
IEnumerable<DirectoryInfo> subdirs = di.EnumerateDirectories();
foreach (DirectoryInfo sdi in subdirs)
{
LoadFilesFromFolder(sdi.FullName);
}
}
}
private void ClearSelectionDetails()
{
txtFileName.Clear();
_editorYaml.Clear();
txtContents.Clear();
}
private void ToggleEditors(bool on)
{
txtFileName.Enabled = false;
_editorYaml.Enabled = on;
//txtContents.Enabled = false;
btnSave.Enabled = on;
}
private void LoadSelectionData(string fileName)
{
string contents = txtContents.Text;
_editorYaml.FileName = fileName;
try
{
var data = MarkdownExtensions.GetFrontMatter<FrontMatterData>(contents);
_editorYaml.LoadData(data);
}
catch (Exception ex)
{
DialogResult res;
res = MessageBox.Show(
ex.ToString(),
"Exception Thrown",
MessageBoxButtons.OK,
MessageBoxIcon.Information
);
_editorYaml.LoadDefaults();
}
}
private void LoadFileContents(string fullFileName)
{
txtContents.Clear();
txtContents.Text = File.ReadAllText(fullFileName);
}
private void btnBrowse_Click(object sender, EventArgs e)
{
// show dialog to select folder
FolderBrowserDialog fbd = new FolderBrowserDialog();
DialogResult res = fbd.ShowDialog();
if (res == DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
{
OnBrowse(fbd.SelectedPath);
}
}
private void chkMarkdown_CheckedChanged(object sender, EventArgs e)
{
// reloads files from folder loading all or .md files only
OnBrowse(txtFolder.Text);
}
private void listFiles_SelectedIndexChanged(object sender, EventArgs e)
{
if (listFiles.SelectedIndex == -1)
return;
if (listFiles.SelectedItem is not string selected)
return;
string fileName = Path.GetFileName(selected.ToString());
string ext = Path.GetExtension(fileName).ToLowerInvariant();
// make sure we're loading only text-files
if (!s_AllowedExtensions.Contains(ext))
{
_editorYaml.Clear();
ToggleEditors(false);
txtFileName.Text = fileName;
txtContents.Text = string.Empty;
return;
}
LoadFileContents(selected.ToString());
LoadSelectionData(fileName);
ToggleEditors(true);
// set file name last because it gets cleared when settin FM data
txtFileName.Text = fileName;
}
private void btnSave_Click(object sender, EventArgs e)
{
string ext = Path.GetExtension(txtFileName.Text).ToLowerInvariant();
if (!s_AllowedExtensions.Contains(ext))
{
string message = string.Format("Saving to a {0} file is not allowed", ext);
string title = "Cannot save";
MessageBox.Show(message, title);
return;
}
// make sure we have a file name
if (string.IsNullOrEmpty(txtFileName.Text))
return;
// create the front matter structure
FrontMatterData fm = new FrontMatterData();
_editorYaml.SaveData(ref fm);
// serialize structure
var serializer = new SerializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build();
var yaml = serializer.Serialize(fm);
// write to file
if (!Directory.Exists(KTempFolder))
{
Directory.CreateDirectory(KTempFolder);
}
string fullFileName = Path.Combine(KTempFolder, txtFileName.Text);
using (StreamWriter writer = new StreamWriter(fullFileName))
{
writer.WriteLine(KDocStart);
writer.WriteLine(yaml);
writer.WriteLine(KDocEnd);
writer.WriteLine(txtContents.Text);
}
MessageBox.Show("File saved successfully.");
}
private void listFiles_DragEnter(object sender, DragEventArgs e)
{
// set the drag drop effects based on the selection
e.Effect = DragDropEffects.None;
if (e.Data is null)
return;
if (!e.Data.GetDataPresent(DataFormats.FileDrop))
return;
if (e.Data.GetData(DataFormats.FileDrop) is not string[] dropItems ||
dropItems.Length == 0)
return;
foreach (string item in dropItems)
{
if (Directory.Exists(item))
{
e.Effect = DragDropEffects.All;
continue;
}
string ext = Path.GetExtension(item).ToLowerInvariant();
if (chkMarkdownAndText.Checked)
{
if (s_AllowedExtensions.Contains(ext))
{
e.Effect = DragDropEffects.All;
continue;
}
}
else
{
e.Effect = DragDropEffects.All;
continue;
}
}
}
private void listFiles_DragDrop(object sender, DragEventArgs e)
{
if (e.Data is null)
return;
if (!e.Data.GetDataPresent(DataFormats.FileDrop))
return;
if (e.Data.GetData(DataFormats.FileDrop) is not string[] dropItems ||
dropItems.Length == 0)
return;
foreach (string item in dropItems)
{
if (Directory.Exists(item))
{
DirectoryInfo di = new DirectoryInfo(item);
IEnumerable<FileInfo> files = di.EnumerateFiles();
if (chkMarkdownAndText.Checked)
{
files = files.Where(
f => s_AllowedExtensions.Contains(f.Extension)
);
}
foreach (FileInfo file in files)
listFiles.Items.Add(file.FullName);
continue;
}
string ext = Path.GetExtension(item).ToLowerInvariant();
if (chkMarkdownAndText.Checked)
{
if (s_AllowedExtensions.Contains(ext))
{
listFiles.Items.Add(item);
continue;
}
}
else
{
listFiles.Items.Add(item);
continue;
}
}
}
private void btnClearList_Click(object sender, EventArgs e)
{
DialogResult res = MessageBox.Show("Clear list?", "Confirmation", MessageBoxButtons.YesNo);
if (res == DialogResult.No)
return;
listFiles.Items.Clear();
txtFolder.Text = string.Empty;
ClearSelectionDetails();
ToggleEditors(false);
}
private void cmbLang_SelectedIndexChanged(object sender, EventArgs e)
{
switch (cmbLang.SelectedIndex)
{
case 0: // EN
Thread.CurrentThread.CurrentUICulture = mEnCulture;
break;
case 1: // RO
Thread.CurrentThread.CurrentUICulture = mRoCulture;
break;
default:
Thread.CurrentThread.CurrentUICulture = mEnCulture;
break;
}
UpdateLocale();
}
private void UpdateLocale()
{
this.Text = Locale.APPLICATION_NAME;
lblListOfFiles.Text = Locale.LBL_LIST_OF_FILES;
lblFileName.Text = Locale.LBL_FILE_NAME;
lblContents.Text = Locale.LBL_CONTENTS;
btnBrowse.Text = Locale.BTN_ADD_FOLDER;
chkIncludeSubfolders.Text = Locale.CHK_INCLUDE_SUBFOLDERS;
chkMarkdownAndText.Text = Locale.CHK_MD_AND_TEXT;
lblLang.Text = Locale.LBL_LANGUAGE;
_editorYaml.UpdateLocale();
btnSave.Text = Locale.BTN_SAVE;
}
}
}