-
Notifications
You must be signed in to change notification settings - Fork 5
/
MCTextFieldWebView.m
256 lines (219 loc) · 8.66 KB
/
MCTextFieldWebView.m
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
//
// MCTextFieldWebView.m
// BackToTheMac
//
// Created by Drew McCormack on 15/11/10.
// Copyright 2010 The Mental Faculty. All rights reserved.
//
#import "MCTextFieldWebView.h"
@implementation MCTextFieldWebView
@synthesize textFieldDelegate;
@synthesize fontSize;
-(id)initWithFrame:(NSRect)frameRect
{
NSString *frameName = [NSString stringWithFormat:@"Frame for MCTextFieldWebView %p", self];
NSString *groupName = [NSString stringWithFormat:@"Group for MCTextFieldWebView %p", self];
if ( (self = [super initWithFrame:frameRect frameName:frameName groupName:groupName]) ) {
// Paths to files
NSString *pathToResources = [[NSBundle bundleForClass:[self class]] pathForResource:@"MCTextFieldWebView Resources" ofType:@""];
NSString *pathToHTML = [pathToResources stringByAppendingPathComponent:@"main.html"];
NSString *skeletonHTML = [NSString stringWithContentsOfFile:pathToHTML usedEncoding:nil error:NULL];
NSURL *baseURL = [NSURL fileURLWithPath:pathToHTML];
// Initialize web view
self.drawsBackground = NO;
// Begin load
finishedLoading = NO;
failedLoad = NO;
self.frameLoadDelegate = self;
self.editingDelegate = self;
self.policyDelegate = self;
[self.mainFrame loadHTMLString:skeletonHTML baseURL:baseURL];
// Wait for load to complete
while ( !finishedLoading ) {
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.0]];
}
// Handle failed load
if ( failedLoad ) {
self = nil;
}
else {
self.fontSize = 16.0;
self.fontFamily = @"Helvetica";
self.textAlignment = NSLeftTextAlignment;
self.textColor = [NSColor blackColor];
}
}
return self;
}
#pragma mark Loading Delegate Methods
-(void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
finishedLoading = YES;
}
-(void)webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame
{
NSLog(@"MCTextFieldWebView did fail load: %@", error);
finishedLoading = YES;
failedLoad = YES;
}
#pragma mark Editing Delegate Methods
-(void)webViewDidBeginEditing:(NSNotification *)notif
{
if ( [textFieldDelegate respondsToSelector:@selector(textFieldWebViewDidBeginEditing:)] ) {
[textFieldDelegate textFieldWebViewDidBeginEditing:self];
}
}
-(void)webViewDidChange:(NSNotification *)notif
{
if ( [textFieldDelegate respondsToSelector:@selector(textFieldWebViewHTMLContentDidChange:)] ) {
[textFieldDelegate textFieldWebViewHTMLContentDidChange:self];
}
}
-(void)webViewDidEndEditing:(NSNotification *)notif
{
if ( [textFieldDelegate respondsToSelector:@selector(textFieldWebViewDidEndEditing:)] ) {
[textFieldDelegate textFieldWebViewDidEndEditing:self];
}
}
-(void)removeFontAttributesFromDOMHTMLElement:(DOMHTMLElement *)element
{
NSString *color = [element.style.color copy];
[element setAttribute:@"style" value:nil];
element.style.color = color;
}
-(BOOL)webView:(WebView *)webView shouldInsertNode:(DOMNode *)node replacingDOMRange:(DOMRange *)range givenAction:(WebViewInsertAction)action
{
if ( action == WebViewInsertActionPasted ) {
DOMDocumentFragment *documentFragment = (id)node;
DOMNodeList *children = [documentFragment childNodes];
for ( NSUInteger c = 0; c < children.length; ++c ) {
DOMNode *child = [children item:c];
if ( [child isKindOfClass:[DOMHTMLElement class]] ) {
DOMHTMLElement *domChild = (id)child;
[self removeFontAttributesFromDOMHTMLElement:domChild];
DOMNodeList *elements = [domChild getElementsByTagName:@"*"];
for ( NSUInteger i = 0; i < [elements length]; ++i ) {
DOMHTMLElement *el = (id)[elements item:i];
[self removeFontAttributesFromDOMHTMLElement:el];
}
}
}
[self replaceSelectionWithNode:documentFragment];
return NO;
}
return YES;
}
#pragma mark Navigation Delegate Methods
-(void)webView:(WebView *)webView decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id <WebPolicyDecisionListener>)listener
{
NSNumber *navType = [actionInformation objectForKey:WebActionNavigationTypeKey];
if ( [navType integerValue] == WebNavigationTypeLinkClicked ) {
[listener ignore];
[[NSWorkspace sharedWorkspace] openURL:request.URL];
}
else {
[listener use];
}
}
#pragma mark Accessors
-(void)setHtmlString:(NSString *)newString
{
DOMHTMLElement *textContainerElement = (id)[self.mainFrame.DOMDocument getElementById:@"textcontainer"];
textContainerElement.innerHTML = [newString copy];
}
-(NSString *)htmlString
{
DOMHTMLElement *textContainerElement = (id)[self.mainFrame.DOMDocument getElementById:@"textcontainer"];
return textContainerElement.innerHTML;
}
-(NSString *)plainString
{
DOMHTMLElement *textContainerElement = (id)[self.mainFrame.DOMDocument getElementById:@"textcontainer"];
return textContainerElement.innerText;
}
-(void)setFontFamily:(NSString *)newFamily
{
DOMHTMLElement *textContainerElement = (id)[self.mainFrame.DOMDocument getElementById:@"textcontainer"];
DOMCSSStyleDeclaration *style = textContainerElement.style;
[style setFontFamily:[newFamily copy]];
}
-(NSString *)fontFamily
{
DOMHTMLElement *textContainerElement = (id)[self.mainFrame.DOMDocument getElementById:@"textcontainer"];
DOMCSSStyleDeclaration *style = textContainerElement.style;
return [style fontFamily];
}
-(void)updateFontSize
{
DOMHTMLElement *textContainerElement = (id)[self.mainFrame.DOMDocument getElementById:@"textcontainer"];
DOMCSSStyleDeclaration *style = textContainerElement.style;
[style setFontSize:[NSString stringWithFormat:@"%fpx", fontSize]];
}
-(void)setFontSize:(CGFloat)size
{
fontSize = size;
[self updateFontSize];
}
-(void)setTextColor:(NSColor *)newColor
{
if ( !newColor ) newColor = [NSColor blackColor];
NSColor *rgbColor = [newColor colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
CGFloat red = [rgbColor redComponent];
CGFloat green = [rgbColor greenComponent];
CGFloat blue = [rgbColor blueComponent];
NSString *colorString = [NSString stringWithFormat:@"rgb(%d,%d,%d)", (int)(red * 255), (int)(green * 255), (int)(blue * 255)];
DOMHTMLElement *textContainerElement = (id)[self.mainFrame.DOMDocument getElementById:@"textcontainer"];
DOMCSSStyleDeclaration *style = textContainerElement.style;
style.color = colorString;
}
-(NSColor *)textColor
{
DOMHTMLElement *textContainerElement = (id)[self.mainFrame.DOMDocument getElementById:@"textcontainer"];
DOMCSSStyleDeclaration *style = textContainerElement.style;
NSString *colorString = style.color;
NSScanner *scanner = [NSScanner scannerWithString:colorString];
NSCharacterSet *skipSet = [NSCharacterSet characterSetWithCharactersInString:@"rgb(, "];
float red, green, blue;
[scanner setCharactersToBeSkipped:skipSet];
[scanner scanFloat:&red];
[scanner scanFloat:&green];
[scanner scanFloat:&blue];
return [NSColor colorWithCalibratedRed:red green:green blue:blue alpha:1.0f];
}
-(void)setTextAlignment:(NSTextAlignment)newAlignment
{
DOMHTMLElement *textContainerElement = (id)[self.mainFrame.DOMDocument getElementById:@"textcontainer"];
DOMCSSStyleDeclaration *style = textContainerElement.style;
NSString *alignString;
switch ( newAlignment ) {
case NSLeftTextAlignment:
alignString = @"left";
break;
case NSRightTextAlignment:
alignString = @"right";
break;
case NSCenterTextAlignment:
alignString = @"center";
break;
default:
alignString = @"center";
break;
}
style.textAlign = alignString;
}
-(NSTextAlignment)textAlignment
{
DOMHTMLElement *textContainerElement = (id)[self.mainFrame.DOMDocument getElementById:@"textcontainer"];
DOMCSSStyleDeclaration *style = textContainerElement.style;
NSString *alignString = style.textAlign;
NSTextAlignment alignment = NSCenterTextAlignment;
if ( [alignString isEqualToString:@"left"] )
alignment = NSLeftTextAlignment;
else if ( [alignString isEqualToString:@"center"] )
alignment = NSCenterTextAlignment;
else if ( [alignString isEqualToString:@"right"] )
alignment = NSRightTextAlignment;
return alignment;
}
@end