-
Notifications
You must be signed in to change notification settings - Fork 0
/
AceEditor.j
150 lines (106 loc) · 3.07 KB
/
AceEditor.j
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
@import <AppKit/CPTextField.j>
var AceEditorFileChangeNotification = @"AceEditorFileChangeNotification",
AceEditorMarkFileUnsavedNotification = @"AceEditorMarkFileUnsavedNotification";
@implementation AceEditor : CPView
{
DOMElement editor;
id _delegate @accessors(getter=delegate);
CPDictionary _activeFile @accessors(getter=activeFile);
}
-(id) initWithFrame:(CGRect)aFrame
{
self = [super initWithFrame:aFrame];
if( self )
{
[self setAutoresizingMask:CPViewWidthSizable|CPViewHeightSizable];
_DOMElement.attr('id', "ace");
_DOMElement.css("fontSize", 14);
editor = nil;
_delegate = nil;
_activeFile = nil;
}
return self;
}
-(void) setDelegate:(id)aDelegate
{
if(_delegate == aDelegate)
return ;
var defaultCenter = [CPNotificationCenter defaultCenter];
if([_delegate respondsToSelector:@selector(aceEditorTextDidChange:)])
{
[defaultCenter removeObserver:_delegate
name:AceEditorFileChangeNotification
object:self];
}
_delegate = aDelegate;
if([_delegate respondsToSelector:@selector(aceEditorTextDidChange:)])
{
[defaultCenter addObserver:_delegate
selector:@selector(aceEditorTextDidChange:)
name:AceEditorFileChangeNotification
object:self];
}
}
-(void) mouseDown:(CPEvent)theEvent
{
[[self window] makeFirstResponder:self];
}
-(BOOL) acceptsFirstResponder
{
return YES;
}
-(CPString) text
{
if(editor)
return editor.getValue();
return @"";
}
-(BOOL) swallowsKey
{
return YES;
}
-(void) setFile:(CPDictionary)fileInfo
{
if(editor)
{
_activeFile = fileInfo;
var fileName = [fileInfo objectForKey:@"name"];
if([fileName hasSuffix:".j"])
editor.getSession().setMode("ace/mode/objectivej");
else if([fileName hasSuffix:".js"] || [fileName hasSuffix:".json"] )
editor.getSession().setMode("ace/mode/javascript");
else if([fileName hasSuffix:".css"])
editor.getSession().setMode("ace/mode/css");
else if([fileName hasSuffix:".html"])
editor.getSession().setMode("ace/mode/html");
editor.setValue([_activeFile objectForKey:@"contents"]);
editor.gotoLine(0);
}
}
-(void) setup
{
if(!editor)
{
editor = ace.edit("ace");
editor.setTheme("ace/theme/tomorrow_night_bright");
editor.getSession().setUseWorker(false);
editor.getSession().setMode("ace/mode/objectivej");
editor.getSession().on('change', function(e) {
var newText = [self text];
var oldText = [_activeFile objectForKey:@"contents"];
if(![oldText isEqualToString:newText])
{
[_activeFile setObject:[self text] forKey:@"contents"];
var unsaved = [_activeFile objectForKey:@"unsaved"];
[_activeFile setObject:YES forKey:@"unsaved"];
if(!unsaved)
{
[[CPNotificationCenter defaultCenter] postNotificationName:AceEditorMarkFileUnsavedNotification object:self];
}
[[CPNotificationCenter defaultCenter] postNotificationName:AceEditorFileChangeNotification object:self];
}
});
editor.setShowPrintMargin(false);
}
}
@end