-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEditorView.j
More file actions
192 lines (175 loc) · 5.64 KB
/
EditorView.j
File metadata and controls
192 lines (175 loc) · 5.64 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
// (c) 2010-2011 by Anton Korenyushkin
@implementation EditorView : CPControl
{
DOMElement iframe;
JSObject editor;
CPString stringValue;
id delegate @accessors(readonly);
}
- (id)initWithFrame:(CGRect)frame syntax:(CPString)syntax readOnly:(BOOL)readOnly // public
{
if (self = [super initWithFrame: frame]) {
stringValue = "";
iframe = document.createElement("iframe");
iframe.style.width = "100%";
iframe.style.height = "100%";
iframe.style.border = "none";
iframe.frameBorder = "0";
iframe.src = [[CPBundle mainBundle] pathForResource:"Editor.html"];
iframe.onload = function () {
var doc = iframe.contentDocument;
doc.onkeydown = function (event) {
if (CPPlatformActionKeyMask == CPCommandKeyMask ? event.metaKey : event.ctrlKey) {
[[[self window] platformWindow] keyEvent:event];
if (BoundKeys.indexOf(String.fromCharCode(event.keyCode).toLowerCase()) != -1)
return false;
} else if (event.keyCode == CPEscapeKeyCode ||
(event.altKey && (event.keyCode == CPUpArrowKeyCode || event.keyCode == CPDownArrowKeyCode))) {
[[[self window] platformWindow] keyEvent:event];
return false;
}
};
var win = iframe.contentWindow;
editor = win.ace.edit(doc.body);
editor.setReadOnly(readOnly);
var canon = win.require("pilot/canon");
canon.removeCommand("gotoline");
canon.removeCommand("find");
var session = editor.getSession();
session.setTabSize(2);
var modeName = {
js: "javascript",
html: "html",
css: "css"
}[syntax];
if (modeName)
session.setMode(new (win.require("ace/mode/" + modeName).Mode)());
session.setValue(stringValue);
editor.gotoLine(1);
session.addEventListener(
"change",
function (event) {
[self textDidChange:[CPNotification notificationWithName:CPControlTextDidChangeNotification object:self]];
[[CPRunLoop currentRunLoop] limitDateForMode:CPDefaultRunLoopMode];
});
editor.addEventListener(
"blur",
function (event) {
[self refocus];
});
doc.body.onclick = function () {
if ([CPApp modalWindow]) {
editor.renderer.hideCursor();
} else {
var window = [self window];
[window makeKeyWindow];
[window makeFirstResponder:self];
}
};
editor.focus();
};
_DOMElement.appendChild(iframe);
[[CPNotificationCenter defaultCenter] addObserver:self
selector:@selector(refocus)
name:CPMenuDidEndTrackingNotification
object:[CPApp mainMenu]];
}
return self;
}
- (void)refocus // private
{
var window = [self window];
if (!(editor && [window isKeyWindow] && [window firstResponder] === self))
return;
editor.focus();
setTimeout(
function () {
if ([window isKeyWindow])
iframe.focus();
},
0);
}
- (CPString)stringValue // public
{
return editor ? editor.getSession().getValue() : stringValue;
}
- (void)setStringValue:(CPString)aStringValue // public
{
if (!editor) {
stringValue = aStringValue;
return;
}
editor.getSession().setValue(aStringValue);
editor.gotoLine(1);
}
- (void)setLineNumber:(unsigned)lineNumber // public
{
editor.gotoLine(lineNumber);
}
- (void)find:(CPString)searchString // public
{
editor.find(searchString);
}
- (void)selectRange:(JSObject)range // private
{
if (range)
editor.textView.setSelection(range, YES);
}
- (void)findNext // public
{
editor.findNext();
}
- (void)findPrevious // public
{
editor.findPrevious();
}
- (void)setDelegate:(id)aDelegate // public
{
var defaultCenter = [CPNotificationCenter defaultCenter];
if (delegate)
[defaultCenter removeObserver:delegate name:CPControlTextDidChangeNotification object:self];
delegate = aDelegate;
if ([delegate respondsToSelector:@selector(controlTextDidChange:)])
[defaultCenter addObserver:delegate
selector:@selector(controlTextDidChange:)
name:CPControlTextDidChangeNotification
object:self];
}
- (BOOL)acceptsFirstResponder // public
{
return YES;
}
- (BOOL)becomeFirstResponder // public
{
iframe.focus();
if (editor)
editor.focus();
return YES;
}
- (BOOL)resignFirstResponder // public
{
if (editor)
editor.renderer.hideCursor();
return YES;
}
- (void)becomeKeyWindow // public
{
[self becomeFirstResponder];
}
- (void)resignKeyWindow // public
{
if (editor) {
editor.renderer.hideCursor();
if (CPBrowserIsEngine(CPGeckoBrowserEngine)) {
var callback = function (event) {
if (console)
console.log("hack");
editor.blur();
};
editor.addEventListener("focus", callback);
setTimeout(function () { editor.removeEventListener("focus", callback); }, 100);
iframe.blur();
}
}
}
@end