Uploaded image for project: 'Weex (Retired)'
  1. Weex (Retired)
  2. WEEX-233

More enhanced about <web> component

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • None
    • None
    • Android, iOS

    Description

      0301 Modified version

      0302 Modified version
      0307 Modified version

      Hi, I'm Tw93(侑夕) from Fliggy, I'd like to discuss the enhancements about component, let it take over where we can't implement through weex.

      Target

      <web> is used to display web content that specified by src attribute in weex page,We also can use webview module to control WebView behavior such as goBack, goForward and reload.

      But it's not enough in most businesses,I think a more enhanced web component should have the following:

      • Support to send messages from Weex to a html in component.
      • Support to send messages from a html in component to Weex.
      • Support to render html source.

      Previously, component was an island that only rendered a remote url, but enabled it to communicate through the context native to the webView. Then I'm going to describe the solution that I came up with.

      My solution

      Code

      Demo && Theory

      Details

      Support to send messages from Weex to a html in component.

      • Native:(W3c MessageEvent)
        // Weex postMessage to web
      • -(void)postMessage:(NSDictionary *)data
        Unknown macro: {WXSDKInstance *instance = [WXSDKEngine topInstance]; NSMutableString *bundleUrlOrigin = @""; if (instance.pageName) { NSString *bundleUrl = [instance.scriptURL absoluteString]; NSURL *url = [NSURL URLWithString:bundleUrl]; bundleUrlOrigin = [NSString stringWithFormat:@"%@://%@%@", url.scheme, url.host, url.port ? [NSString stringWithFormat:@":%@", url.port] : @""]; }

         NSDictionary *initDic = @{ @"type" : @"message", @"data" : data, @"origin" : bundleUrlOrigin }; NSString *json = [WXUtility JSONString}
        ())", json];
        [_jsContext evaluateScript:code];
        }

      • How to use:
        // Weex
        const webview = weex.requireModule('webview');
        // recommend
        this.$refs['wxc-web'].postMessage({ detail: "a message" });
        //not recommend
        //webview.postMessage(this.$refs.webview, {detail:"a message"});
         
        // Web
        window.addEventListener('message',function(e) { console.log(e.data,e.type,e.origin); // { detail: "a message" }
        ,"message","*"
        },false)

      Support to send messages from a html in component to Weex.

       
       
      //Weex catch postMessage event from web
      _jsContext[@"postMessage"] = ^() {
       
      NSArray *args = [JSContext currentArguments];
       
      if (args && args.count < 2)

      { return; }

       
      NSDictionary *data = [args[0] toDictionary];
      NSString *origin = [args[1] toString];
       
      if (data == nil) { return; }

       
      NSDictionary *initDic = @

      { @"type" : @"message", @"data" : data, @"origin" : origin }

      ;
       
      [weakSelf fireEvent:@"message" params:initDic];
      };
       

      • How to use:
        // Weex
        <web @message="onMessage"></web>
         
        onMessage (e) { console.log(e.data,e.type,e.origin); // { detail: "a message" }
        ,"message","*"
        },
         
        // Web iframe
        window.parent.postMessage({ detail: "a message" }, '*');

      Support to render html source.

      • Native:
        [_webview loadHTMLString:_source baseURL:nil];
         
      • How to use:
        <web source='<p style="text-align: center;"><img align="absmiddle" src="http://img03.taobaocdn.com/imgextra/i3/1124701655/TB2zmmIcpXXXXbIXpXXXXXXXXXX_!!1124701655.jpg"/></p>'></web>

      Welcome to put forward any suggestion about the solution or other requirements for the component, Thanks!

      Attachments

        Activity

          People

            cxfeng Adam Feng
            tw93 Tang Wei
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - 72h
                72h
                Remaining:
                Remaining Estimate - 72h
                72h
                Logged:
                Time Spent - Not Specified
                Not Specified