From 34eac092acd118620c2653731e283ca8aa7fc383 Mon Sep 17 00:00:00 2001 From: Bibiko Date: Wed, 8 Dec 2010 22:51:45 +0000 Subject: =?UTF-8?q?=E2=80=A2=20added=20Save=20Page=20As=20and=20View=20Sou?= =?UTF-8?q?rce=20to=20HTML=20output=20window's=20context=20menu?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/SPBundleHTMLOutputController.h | 3 ++ Source/SPBundleHTMLOutputController.m | 81 ++++++++++++++++++++++++++++++++++- 2 files changed, 82 insertions(+), 2 deletions(-) diff --git a/Source/SPBundleHTMLOutputController.h b/Source/SPBundleHTMLOutputController.h index 9ad79a1e..0ad38241 100644 --- a/Source/SPBundleHTMLOutputController.h +++ b/Source/SPBundleHTMLOutputController.h @@ -45,4 +45,7 @@ - (void)displayHTMLContent:(NSString *)content withOptions:(NSDictionary *)displayOptions; - (void)displayURLString:(NSString *)url withOptions:(NSDictionary *)displayOptions; +- (void)showSourceCode; +- (void)saveDocument; + @end diff --git a/Source/SPBundleHTMLOutputController.m b/Source/SPBundleHTMLOutputController.m index b89606f9..a630eb06 100644 --- a/Source/SPBundleHTMLOutputController.m +++ b/Source/SPBundleHTMLOutputController.m @@ -24,6 +24,7 @@ #import "SPBundleHTMLOutputController.h" +#import "SPAlertSheets.h" @@ -121,7 +122,6 @@ - (void)keyDown:(NSEvent *)theEvent { long allFlags = (NSShiftKeyMask|NSControlKeyMask|NSAlternateKeyMask|NSCommandKeyMask); - NSString *characters = [theEvent characters]; NSString *charactersIgnMod = [theEvent charactersIgnoringModifiers]; unichar insertedCharacter = [characters characterAtIndex:0]; @@ -157,11 +157,63 @@ } +/** + * Sheet did end method + */ +- (void)sheetDidEnd:(id)sheet returnCode:(NSInteger)returnCode contextInfo:(NSString *)contextInfo +{ + + // Order out current sheet to suppress overlapping of sheets + if ([sheet respondsToSelector:@selector(orderOut:)]) + [sheet orderOut:nil]; + else if ([sheet respondsToSelector:@selector(window)]) + [[sheet window] orderOut:nil]; + + if([contextInfo isEqualToString:@"saveDocument"]) { + if (returnCode == NSOKButton) { + NSString *sourceCode = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].outerHTML"]; + NSError *err = nil; + [sourceCode writeToFile:[sheet filename] + atomically:YES + encoding:NSUTF8StringEncoding + error:&err]; + if(err != nil) { + SPBeginAlertSheet(NSLocalizedString(@"Error", @"error"), NSLocalizedString(@"OK", @"OK button"), nil, nil, [self window], self, nil, nil, + [NSString stringWithFormat:@"%@", [err localizedDescription]]); + } + + } + } +} + - (IBAction)printDocument:(id)sender { [[[[webView mainFrame] frameView] documentView] print:sender]; } +- (void)showSourceCode +{ + NSString *sourceCode = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].outerHTML"]; + SPBundleHTMLOutputController *c = [[SPBundleHTMLOutputController alloc] init]; + [c displayHTMLContent:[NSString stringWithFormat:@"
%@
", [sourceCode HTMLEscapeString]] withOptions:nil]; + [[NSApp delegate] addHTMLOutputController:c]; +} + +- (void)saveDocument +{ + NSSavePanel *panel = [NSSavePanel savePanel]; + + [panel setRequiredFileType:@"html"]; + + [panel setExtensionHidden:NO]; + [panel setAllowsOtherFileTypes:YES]; + [panel setCanSelectHiddenExtension:YES]; + [panel setCanCreateDirectories:YES]; + + [panel beginSheetForDirectory:nil file:@"output" modalForWindow:[self window] modalDelegate:self didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:) contextInfo:@"saveDocument"]; +} + + #pragma mark - - (void)windowWillClose:(NSNotification *)notification @@ -170,11 +222,36 @@ [webView close]; [self setInitHTMLSourceString:@""]; windowUUID = @""; - // [[notification object] release]; } #pragma mark - +- (NSArray *)webView:(WebView *)sender contextMenuItemsForElement:(NSDictionary *)element defaultMenuItems:(NSArray *)defaultMenuItems +{ + NSMutableArray *webViewMenuItems = [[defaultMenuItems mutableCopy] autorelease]; + + [webViewMenuItems addObject:[NSMenuItem separatorItem]]; + + NSMenuItem *anItem; + anItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"View Source", @"view html source code menu item title") action:@selector(showSourceCode) keyEquivalent:@""]; + [anItem setEnabled:YES]; + [anItem setTarget:self]; + [webViewMenuItems addObject:anItem]; + [anItem release]; + anItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Save Page As…", @"save page as menu item title") action:@selector(saveDocument) keyEquivalent:@""]; + [anItem setEnabled:YES]; + [anItem setTarget:self]; + [webViewMenuItems addObject:anItem]; + [anItem release]; + anItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Print Page…", @"print page menu item title") action:@selector(printDocument:) keyEquivalent:@""]; + [anItem setEnabled:YES]; + [anItem setTarget:self]; + [webViewMenuItems addObject:anItem]; + [anItem release]; + + return webViewMenuItems; +} + - (WebView *)webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request { if(request != nil) { -- cgit v1.2.3