Browse Source

Update common.js

Updated the following functions:
appendStylesToHead
getScript

Added:
onWidthUpdated
onHeightWidthUpdated
ShaMan123 6 years ago
parent
commit
c33fa90a22
No account linked to committer's email address
1 changed files with 36 additions and 9 deletions
  1. 36
    9
      autoHeightWebView/common.js

+ 36
- 9
autoHeightWebView/common.js View File

5
     return script;
5
     return script;
6
   }
6
   }
7
   return files.reduceRight((file, combinedScript) => {
7
   return files.reduceRight((file, combinedScript) => {
8
-    const { rel, type, href } = file;
8
+      const { rel, type, href } = file;
9
     return `
9
     return `
10
             var link  = document.createElement('link');
10
             var link  = document.createElement('link');
11
             link.rel  = '${rel}';
11
             link.rel  = '${rel}';
17
   }, script);
17
   }, script);
18
 }
18
 }
19
 
19
 
20
-function appendStylesToHead(styles, script) {
21
-  if (!styles) {
22
-    return script;
23
-  }
20
+function appendStylesToHead(styles, script, shouldResizeWidth) {
21
+    var bodyStyle;
22
+    if (shouldResizeWidth) {
23
+        bodyStyle = `
24
+            body {
25
+                display: flex;
26
+                justify-content: center;
27
+                align-items: center;
28
+            }`;
29
+    }
30
+    else {
31
+        bodyStyle = '';
32
+    }
33
+    
34
+    if (!styles) {
35
+        styles = bodyStyle;
36
+    }
37
+    else {
38
+        styles += bodyStyle;
39
+    }
24
   // Escape any single quotes or newlines in the CSS with .replace()
40
   // Escape any single quotes or newlines in the CSS with .replace()
25
-  const escaped = styles.replace(/\'/g, "\\'").replace(/\n/g, '\\n');
41
+    const escaped = styles.replace(/\'/g, "\\'").replace(/\n/g, '\\n');
42
+    
26
   return `
43
   return `
27
           var styleElement = document.createElement('style');
44
           var styleElement = document.createElement('style');
28
           var styleText = document.createTextNode('${escaped}');
45
           var styleText = document.createTextNode('${escaped}');
33
 }
50
 }
34
 
51
 
35
 function getScript(props, baseScript, iframeBaseScript) {
52
 function getScript(props, baseScript, iframeBaseScript) {
36
-  const { hasIframe, files, customStyle } = props;
53
+    const { hasIframe, files, customStyle, shouldResizeWidth } = props;
37
   let script = hasIframe ? iframeBaseScript : baseScript;
54
   let script = hasIframe ? iframeBaseScript : baseScript;
38
   script = files ? appendFilesToHead(files, baseScript) : baseScript;
55
   script = files ? appendFilesToHead(files, baseScript) : baseScript;
39
-  script = customStyle ? appendStylesToHead(customStyle, script) : script;
56
+    script = appendStylesToHead(customStyle, script, shouldResizeWidth);
40
   return script;
57
   return script;
41
 }
58
 }
42
 
59
 
44
   props.onHeightUpdated && props.onHeightUpdated(height);
61
   props.onHeightUpdated && props.onHeightUpdated(height);
45
 }
62
 }
46
 
63
 
64
+function onWidthUpdated(width, props) {
65
+    props.onWidthUpdated && props.onWidthUpdated(width);
66
+}
67
+
68
+function onHeightWidthUpdated(height, width, props) {
69
+    onHeightUpdated(height, props);
70
+    onWidthUpdated(width, props);
71
+    props.onHeightWidthUpdated && props.onHeightWidthUpdated(height, width);
72
+}
73
+
47
 const domMutationObserveScript = 
74
 const domMutationObserveScript = 
48
 `
75
 `
49
 var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
76
 var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
54
 });
81
 });
55
 `;
82
 `;
56
 
83
 
57
-export { getScript, onHeightUpdated, domMutationObserveScript };
84
+export { getScript, onHeightUpdated, onWidthUpdated, onHeightWidthUpdated, domMutationObserveScript };