This repository was archived by the owner on Dec 12, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathXSourceNoteWindowController.m
More file actions
337 lines (259 loc) · 11.2 KB
/
XSourceNoteWindowController.m
File metadata and controls
337 lines (259 loc) · 11.2 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
//
// XSourceNoteWindowController.m
// XSourceNote
//
// Created by everettjf on 10/2/15.
// Copyright © 2015 everettjf. All rights reserved.
//
#import "XSourceNoteWindowController.h"
#import "XSourceNoteModel.h"
#import "XSourceNoteUtil.h"
#import "XSourceNotePreferencesWindowController.h"
#import "XSourceNoteStorage.h"
#import "XSourceNoteTableView.h"
#import "XSourceNoteTableCellView.h"
#import "XSourceNoteFormatter.h"
@interface XSourceNoteWindowController () <NSTableViewDelegate,NSTableViewDataSource, NSTextViewDelegate, NSWindowDelegate>
@property (weak) IBOutlet NSTabView *tabView;
@property (nonatomic,strong) XSourceNotePreferencesWindowController *preferencesWindowController;
// Information
@property (weak) IBOutlet NSTextField *rootPathTextField;
@property (weak) IBOutlet NSTextField *repoTextField;
@property (weak) IBOutlet NSTextField *revisionTextField;
@property (weak) IBOutlet NSTextField *projectNameTextField;
@property (weak) IBOutlet NSTextField *officialSiteTextField;
@property (unsafe_unretained) IBOutlet NSTextView *descriptionTextView;
// Project Note
@property (unsafe_unretained) IBOutlet NSTextView *projectNoteTextView;
// Summarize
@property (unsafe_unretained) IBOutlet NSTextView *summarizeTextView;
// Lines Note
@property (weak) IBOutlet XSourceNoteTableView *lineNoteTableView;
// Tool
@property (unsafe_unretained) IBOutlet NSTextView *filePrefixTextView;
@property (unsafe_unretained) IBOutlet NSTextView *currentNoteView;
@property (unsafe_unretained) IBOutlet NSTextView *currentSourceView;
@property (weak) IBOutlet NSTextField *currentSourceTextField;
@property (strong) NSArray *notes;
@property (copy) NSString *currentNoteUniqueID;
@property (assign) NSInteger currentNoteIndex;
@end
@implementation XSourceNoteWindowController
- (void)windowDidLoad {
[super windowDidLoad];
self.window.level = NSFloatingWindowLevel;
self.window.hidesOnDeactivate = YES;
self.notes = @[];
self.lineNoteTableView.deleteKeyAction = @selector(onDeleteLineNote:);
self.currentNoteView.delegate = self;
[self refreshTabFields];
[self refreshNotes];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(notificationLineNotesChange:) name:XSourceNoteModelLineNotesChanged object:nil];
[self startSaveTimer];
self.currentNoteView.editable = NO;
self.currentSourceView.editable = NO;
}
- (void)dealloc{
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
- (void)startSaveTimer{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self _saveCurrent];
[self startSaveTimer];
});
}
-(void) notificationLineNotesChange:(NSNotification*)obj{
[self refreshNotes];
}
-(NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
XSourceNoteTableCellView *cellView = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
if([tableColumn.identifier isEqualToString:@"LineColumn"]){
XSourceNoteEntityObject *note = [self.notes objectAtIndex:row];
cellView.note = note;
}
return cellView;
}
-(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView{
return self.notes.count;
}
-(void)refreshNotes{
[[XSourceNoteModel sharedModel]fetchAllNotes:^(NSArray *notes) {
NSMutableArray *dataset = [NSMutableArray new];
[dataset addObject:[XSourceNoteBasicInformationEntity new]];
[dataset addObject:[XSourceNoteProjectNoteEntity new]];
[dataset addObject:[XSourceNoteProjectSummarizeEntity new]];
[dataset addObject:[XSourceNoteProjectToolEntity new]];
for (XSourceNoteEntityObject *note in notes) {
[dataset addObject:note];
}
self.notes = dataset;
[self.lineNoteTableView reloadData];
}];
}
- (IBAction)onTableViewClicked:(id)sender {
_currentNoteIndex = self.lineNoteTableView.selectedRow;
XSourceNoteEntityObject *note = [self _selectedNote];
if(!note)return;
switch (note.type) {
case XSourceNoteEntityTypeBasicInformation:
[self.tabView selectTabViewItemAtIndex:0];
break;
case XSourceNoteEntityTypeProjectNote:
[self.tabView selectTabViewItemAtIndex:1];
break;
case XSourceNoteEntityTypeSummarize:
[self.tabView selectTabViewItemAtIndex:3];
break;
case XSourceNoteEntityTypeTool:
[self.tabView selectTabViewItemAtIndex:4];
break;
case XSourceNoteEntityTypeLineNote:{
[self.tabView selectTabViewItemAtIndex:2];
[self _saveCurrent];
XSourceNoteLineEntity *lineNote = (id)note;
[self _showNewNote:lineNote];
self.currentNoteView.editable = YES;
self.currentNoteUniqueID = lineNote.uniqueID;
NSLog(@"locate file : %@ , %@", lineNote.localPath, @(lineNote.begin));
[XSourceNoteUtil openSourceFile:lineNote.localPath highlightLineNumber:lineNote.begin];
[self refreshNotes];
break;
}
default:
break;
}
}
-(XSourceNoteEntityObject*)_selectedNote{
NSInteger selectedRow = _currentNoteIndex;
if(selectedRow < 0 || selectedRow >= self.notes.count){
return nil;
}
return [self.notes objectAtIndex:selectedRow];
}
- (IBAction)helpClicked:(id)sender {
NSString *githubURLString = @"http://github.com/everettjf/XSourceNote";
NSString *versionString = [[NSBundle bundleForClass:[XSourceNoteWindowController class]]objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
NSString *xcodeVersion = [[NSBundle mainBundle]objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
NSAlert *alert = [[NSAlert alloc] init];
[alert addButtonWithTitle:@"OK"];
[alert addButtonWithTitle:@"Source on GitHub"];
[alert setMessageText:@"XSourceNote"];
[alert setInformativeText:[NSString stringWithFormat:@"Author:everettjf\nGitHub:%@\nVersion:%@\nXcode:%@",
githubURLString,
versionString,
xcodeVersion
]];
[alert setAlertStyle:NSWarningAlertStyle];
NSModalResponse resp = [alert runModal];
if(resp == NSAlertSecondButtonReturn){
// Star
[[NSWorkspace sharedWorkspace]openURL:[NSURL URLWithString:githubURLString]];
}
}
- (IBAction)showPreferencesClicked:(id)sender {
self.preferencesWindowController = [[XSourceNotePreferencesWindowController alloc]init];
[self.preferencesWindowController.window makeKeyAndOrderFront:sender];
}
- (void)refreshTabFields{
XSourceNoteStorage *st = [XSourceNoteStorage sharedStorage];
self.rootPathTextField.stringValue = st.rootPath;
self.projectNameTextField.stringValue = st.projectName;
self.officialSiteTextField.stringValue = st.projectSite;
self.repoTextField.stringValue = st.projectRepo;
self.revisionTextField.stringValue = st.projectRevision;
self.descriptionTextView.string = st.projectDescription;
self.projectNoteTextView.string = st.projectNote;
self.summarizeTextView.string = st.projectSummarize;
self.filePrefixTextView.string = st.filePrefix;
}
- (void)onDeleteLineNote:(id)sender{
XSourceNoteEntityObject *obj = [self _selectedNote];
if(!obj)return;
if(obj.type != XSourceNoteEntityTypeLineNote)return;
XSourceNoteLineEntity *note = (id)obj;
if(note.content && ![note.content isEqualToString:@""]){
NSAlert *alert = [[NSAlert alloc] init];
[alert addButtonWithTitle:@"Confirm"];
[alert addButtonWithTitle:@"Cancel"];
[alert setMessageText:@"Current note is not empty, please confirm to delete."];
[alert setAlertStyle:NSWarningAlertStyle];
if ([alert runModal] != NSAlertFirstButtonReturn) {
return;
}
}
[[XSourceNoteModel sharedModel]removeLineNote:note];
[self refreshNotes];
}
- (void)_showNewNote:(XSourceNoteLineEntity*)note{
self.window.title = [note title];
// show new
XSNote * fetchedNote = [[XSourceNoteStorage sharedStorage]fetchLineNote:note.uniqueID];
NSString *content = fetchedNote.content;
if(!content) content = @"";
NSString *code = note.code;
if(!code)code = @"";
self.currentNoteView.string = content;
self.currentSourceView.string = code;
self.currentSourceTextField.stringValue = note.source;
}
- (void)_saveCurrent{
if(self.currentNoteUniqueID){
NSString *content = self.currentNoteView.string;
[[XSourceNoteStorage sharedStorage]updateLineNote:self.currentNoteUniqueID content:content];
}
XSourceNoteStorage *st = [XSourceNoteStorage sharedStorage];
st.rootPath = [self.rootPathTextField.stringValue copy];
st.projectName = [self.projectNameTextField.stringValue copy];
st.projectSite = [self.officialSiteTextField.stringValue copy];
st.projectRepo = [self.repoTextField.stringValue copy];
st.projectRevision = [self.revisionTextField.stringValue copy];
st.projectDescription = [self.descriptionTextView.string copy];
st.projectNote = [self.projectNoteTextView.string copy];
st.projectSummarize = [self.summarizeTextView.string copy];
st.filePrefix = [self.filePrefixTextView.string copy];
}
- (IBAction)exportToMarkdown:(id)sender {
XSourceNoteStorage *store = [XSourceNoteStorage sharedStorage];
NSSavePanel *save = [NSSavePanel savePanel];
save.nameFieldStringValue = [NSString stringWithFormat:@"%@",store.projectName];
save.message = @"Save to ?";
save.allowsOtherFileTypes = YES;
save.allowedFileTypes = @[@"md",@"markdown"];
save.extensionHidden = YES;
save.canCreateDirectories = YES;
[save beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) {
if(result != NSFileHandlingPanelOKButton)
return;
NSString *filePath = [[save URL]path];
if([[XSourceNoteFormatter sharedFormatter]saveTo:filePath]){
NSAlert *alert = [[NSAlert alloc] init];
[alert addButtonWithTitle:@"OK"];
alert.messageText = @"Succeed";
[alert setAlertStyle:NSInformationalAlertStyle];
[alert runModal];
}else{
NSAlert *alert = [[NSAlert alloc] init];
[alert addButtonWithTitle:@"OK"];
alert.messageText = @"Failed";
[alert setAlertStyle:NSCriticalAlertStyle];
[alert runModal];
}
}];
}
- (IBAction)selectRootPathClicked:(id)sender {
NSOpenPanel *open = [NSOpenPanel openPanel];
open.canChooseFiles = NO;
open.canChooseDirectories = YES;
open.allowsMultipleSelection = NO;
if([open runModal] != NSModalResponseOK){
return;
}
NSString *path = [open.URL path];
XSourceNoteStorage *st = [XSourceNoteStorage sharedStorage];
st.rootPath = path;
self.rootPathTextField.stringValue = st.rootPath;
}
- (void)windowWillClose:(NSNotification *)notification{
[self _saveCurrent];
}
@end