2 * Copyright (C) 2015 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
26 #import "WK2WebDocumentController.h"
28 #import <WebKit/WKFrameInfo.h>
29 #import <WebKit/WKNavigationDelegate.h>
30 #import <WebKit/WKPreferencesPrivate.h>
31 #import <WebKit/WKUIDelegate.h>
32 #import <WebKit/WKWebView.h>
33 #import <WebKit/WKWebViewConfigurationPrivate.h>
34 #import <WebKit/WKWebViewPrivate.h>
35 #import <WebKit/_WKWebsiteDataStore.h>
37 @interface WK2WebDocumentController () <WKUIDelegate, NSTextFinderBarContainer>
38 @property (nonatomic, strong) WKWebView *webView;
41 @implementation WK2WebDocumentController {
42 NSTextFinder *_textFinder;
43 NSView *_textFindBarView;
47 static WKWebViewConfiguration *defaultConfiguration()
49 static WKWebViewConfiguration *configuration;
52 configuration = [[WKWebViewConfiguration alloc] init];
53 configuration.preferences._fullScreenEnabled = YES;
54 configuration.preferences._developerExtrasEnabled = YES;
60 - (IBAction)pasteAsMarkup:(id)sender
62 NSLog(@"To be implemented");
67 self.webView = [[WKWebView alloc] initWithFrame:[containerView bounds] configuration:defaultConfiguration()];
68 _webView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
69 _webView._editable = YES;
70 _webView.UIDelegate = self;
72 [containerView addSubview:_webView];
73 self.window.title = @"WebEditor [WK2]";
75 _textFinder = [[NSTextFinder alloc] init];
76 _textFinder.incrementalSearchingEnabled = YES;
77 _textFinder.incrementalSearchingShouldDimContentView = YES;
78 _textFinder.client = _webView;
79 _textFinder.findBarContainer = self;
82 - (void)loadHTMLString:(NSString *)content
84 NSStringEncoding encoding = NSUnicodeStringEncoding;
86 NSData *data = [content dataUsingEncoding:encoding];
87 CFStringEncoding cfEncoding = CFStringConvertNSStringEncodingToEncoding(encoding);
88 NSString *textEncodingName = (__bridge NSString *)CFStringConvertEncodingToIANACharSetName(cfEncoding);
90 [_webView _loadData:data MIMEType:@"text/html" characterEncodingName:textEncodingName baseURL:[NSURL URLWithString:@"x-webdoc:/klsadfgjlfsdj/"] userData:nil];
93 - (void)performTextFinderAction:(id)sender
95 [_textFinder performAction:[sender tag]];
98 - (NSView *)findBarView
100 return _textFindBarView;
103 - (void)setFindBarView:(NSView *)findBarView
105 if (_textFindBarView)
106 [_textFindBarView removeFromSuperview];
107 _textFindBarView = findBarView;
108 _findBarVisible = YES;
109 [containerView addSubview:_textFindBarView];
113 - (NSView *)contentView
118 - (BOOL)isFindBarVisible
120 return _findBarVisible;
123 - (void)setFindBarVisible:(BOOL)findBarVisible
125 _findBarVisible = findBarVisible;
127 [containerView addSubview:_textFindBarView];
129 [_textFindBarView removeFromSuperview];
134 - (void)findBarViewDidChangeHeight
141 CGRect containerBounds = [containerView bounds];
143 if (!_findBarVisible) {
144 _webView.frame = containerBounds;
146 _textFindBarView.frame = CGRectMake(containerBounds.origin.x, containerBounds.origin.y + containerBounds.size.height - _textFindBarView.frame.size.height, containerBounds.size.width, _textFindBarView.frame.size.height);
147 _webView.frame = CGRectMake(containerBounds.origin.x, containerBounds.origin.y, containerBounds.size.width, containerBounds.size.height - _textFindBarView.frame.size.height);