on master help sign in
Add runHTMLWithBaseURL to jsaddle-wkwebview
commit7eb50cb73a7cbc31ec16916f85a3a89164b4908b authorHamish Mackenzie <Hamish.K.Mackenzie@googlemail.com> dateWed 13 Jun 2018, 22:55 (8 years ago) parented907e5
4 files changed+56−29
jsaddle-wkwebview/cbits/WKWebView.m+1−1 jsaddle-wkwebview/jsaddle-wkwebview.cabal+14−11 jsaddle-wkwebview/src-ghc/Language/Javascript/JSaddle/WKWebView.hs+29−16 jsaddle-wkwebview/src/Language/Javascript/JSaddle/WKWebView/Internal.hs+12−1
jsaddle-wkwebview/cbits/WKWebView.m+1−1
@@ -97,7 +97,7 @@ void completeSync(JSaddleHandler *handler, const char * _Nonnull s) {
9797
9898void loadHTMLStringWithBaseURL(WKWebView *webView, const char * _Nonnull html, const char * _Nonnull url) {
9999 NSString *htmlString = [NSString stringWithCString:html encoding:NSUTF8StringEncoding];
100− NSURL *baseURL = [NSURL fileURLWithPath:[[NSString alloc] initWithCString:url encoding:NSUTF8StringEncoding]];
100+ NSURL *baseURL = [NSURL URLWithString:[[NSString alloc] initWithCString:url encoding:NSUTF8StringEncoding]];
101101 dispatch_async(dispatch_get_main_queue(), ^{
102102 [webView loadHTMLString:htmlString baseURL:baseURL];
103103 });
jsaddle-wkwebview/jsaddle-wkwebview.cabal+14−11
@@ -25,26 +25,29 @@ source-repository head
2525library
2626 exposed-modules:
2727 Language.Javascript.JSaddle.WKWebView
28− Language.Javascript.JSaddle.WKWebView.Internal
2928 build-depends:
30− aeson >=0.8.0.2 && <1.3,
31− base <5,
32− bytestring >=0.10.6.0 && <0.11,
33− directory,
34− jsaddle >= 0.9.4.0 && <0.10,
35− text,
36− data-default,
37− containers
29+ base <5
3830 default-language: Haskell2010
3931 hs-source-dirs: src
4032 frameworks: Foundation, WebKit
4133 ghc-options: -ferror-spans -Wall
42− c-sources:
43− cbits/WKWebView.m
4434 if impl(ghcjs)
4535 hs-source-dirs: src-ghcjs
4636 else
37+ build-depends:
38+ aeson >=0.8.0.2 && <1.3,
39+ bytestring >=0.10.6.0 && <0.11,
40+ directory,
41+ jsaddle >= 0.9.4.0 && <0.10,
42+ text,
43+ data-default,
44+ containers
45+ exposed-modules:
46+ Language.Javascript.JSaddle.WKWebView.Internal
4747 hs-source-dirs: src-ghc
48+ c-sources:
49+ cbits/WKWebView.m
50+ cc-options: -Wno-everything
4851 if os(ios)
4952 frameworks: UIKit, UserNotifications
5053 if flag(include-app-delegate)
jsaddle-wkwebview/src-ghc/Language/Javascript/JSaddle/WKWebView.hs+29−16
@@ -1,9 +1,12 @@
11module Language.Javascript.JSaddle.WKWebView
22 ( jsaddleMain
3+ , jsaddleMainHTMLWithBaseURL
34 , jsaddleMainFile
45 , WKWebView(..)
56 , run
7+ , run'
68 , runWithAppConfig
9+ , runHTMLWithBaseURL
710 , runFile
811 , mainBundleResourcePath
912 , AppDelegateConfig (..)
@@ -17,7 +20,8 @@ import Data.Set (Set)
1720import qualified Data.Set as Set
1821import Data.Word
1922import Foreign.Marshal.Utils (fromBool)
20−import Language.Javascript.JSaddle.WKWebView.Internal (jsaddleMain, jsaddleMainFile, WKWebView(..), mainBundleResourcePath)
23+import Language.Javascript.JSaddle.WKWebView.Internal
24+ (jsaddleMain, jsaddleMainHTMLWithBaseURL, jsaddleMainFile, WKWebView(..), mainBundleResourcePath)
2125import System.Environment (getProgName)
2226import Foreign.C.String (CString, withCString)
2327import Foreign.StablePtr (StablePtr, newStablePtr)
@@ -96,29 +100,38 @@ instance Default AppDelegateNotificationConfig where
96100
97101-- | Run JSaddle in a WKWebView
98102run :: JSM () -> IO ()
99−run = run' Nothing def
103+run f = run' def (jsaddleMain f)
100104
101105-- | Run JSaddle in a WKWebView
102106runWithAppConfig :: AppDelegateConfig -> JSM () -> IO ()
103−runWithAppConfig = run' Nothing
107+runWithAppConfig cfg = run' cfg . jsaddleMain
104108
105109-- | Run JSaddle in a WKWebView first loading the specified file
106110-- from the mainBundle (relative to the resourcePath).
107−runFile :: ByteString -- ^ The file to navigate to.
108− -> ByteString -- ^ The path to allow read access to.
109− -> AppDelegateConfig
110− -> JSM ()
111− -> IO ()
112−runFile url allowing = run' $ Just (url, allowing)
111+runFile
112+ :: ByteString -- ^ The file to navigate to.
113+ -> ByteString -- ^ The path to allow read access to.
114+ -> AppDelegateConfig
115+ -> JSM ()
116+ -> IO ()
117+runFile url allowing cfg = run' cfg . jsaddleMainFile url allowing
113118
114−run' :: Maybe (ByteString, ByteString)
115− -> AppDelegateConfig
116− -> JSM ()
119+-- | Run JSaddle in a WKWebView first loading the specified html
120+-- as though it came from the specified URL
121+-- (calls WKWebKit function loadHTMLString).
122+runHTMLWithBaseURL
123+ :: ByteString -- ^ HTML to load.
124+ -> ByteString -- ^ pretend it came from this URL.
125+ -> AppDelegateConfig
126+ -> JSM ()
127+ -> IO ()
128+runHTMLWithBaseURL url allowing cfg = run' cfg . jsaddleMainHTMLWithBaseURL url allowing
129+
130+run' :: AppDelegateConfig
131+ -> (WKWebView -> IO ())
117132 -> IO ()
118−run' mUrl cfg f = do
119− handler <- case mUrl of
120− Just (url, allowing) -> newStablePtr (jsaddleMainFile url allowing f)
121− Nothing -> newStablePtr (jsaddleMain f)
133+run' cfg main = do
134+ handler <- newStablePtr main
122135 progName <- getProgName
123136
124137 -- AppDelegate callbacks
jsaddle-wkwebview/src/Language/Javascript/JSaddle/WKWebView/Internal.hs+12−1
@@ -1,6 +1,7 @@
11{-# LANGUAGE ForeignFunctionInterface, OverloadedStrings #-}
22module Language.Javascript.JSaddle.WKWebView.Internal
33 ( jsaddleMain
4+ , jsaddleMainHTMLWithBaseURL
45 , jsaddleMainFile
56 , WKWebView(..)
67 , mainBundleResourcePath
@@ -49,9 +50,19 @@ jsaddleMain :: JSM () -> WKWebView -> IO ()
4950jsaddleMain f webView = do
5051 pwd <- getCurrentDirectory
5152 let baseURL = encodeUtf8 $ "file://" <> T.pack pwd <> "/index.html"
53+ jsaddleMainHTMLWithBaseURL (toStrict indexHtml) baseURL f webView
54+
55+-- | Run JSaddle in WKWebView with initial html and base url
56+jsaddleMainHTMLWithBaseURL
57+ :: BS.ByteString -- ^ HTML
58+ -> BS.ByteString -- ^ Base URL
59+ -> JSM ()
60+ -> WKWebView
61+ -> IO ()
62+jsaddleMainHTMLWithBaseURL initialHTML baseURL f webView =
5263 jsaddleMain' f webView $
5364 useAsCString baseURL $ \url ->
54− useAsCString (toStrict indexHtml) $ \html ->
65+ useAsCString initialHTML $ \html ->
5566 loadHTMLStringWithBaseURL webView html url
5667
5768-- | Run JSaddle in a WKWebView first loading the specified file