Nenhuma descrição

utils.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. 'use strict';
  2. import { Dimensions, Platform } from 'react-native';
  3. const domMutationObserveScript = `
  4. var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
  5. var observer = new MutationObserver(updateSize);
  6. observer.observe(document, {
  7. subtree: true,
  8. attributes: true
  9. });
  10. `;
  11. const updateSizeWithMessage = element =>
  12. `
  13. var updateSizeInterval = null;
  14. var height = 0;
  15. var lastHeight = 0;
  16. var heightTheSameTimes = 0;
  17. var maxHeightTheSameTimes = 5;
  18. var forceRefreshDelay = 1000;
  19. var forceRefreshTimeout;
  20. function updateSize(event) {
  21. if (!window.hasOwnProperty('ReactNativeWebView') || !window.ReactNativeWebView.hasOwnProperty('postMessage')) {
  22. !updateSizeInterval && (updateSizeInterval = setInterval(updateSize, 200));
  23. return;
  24. }
  25. clearInterval(updateSizeInterval)
  26. height = ${element}.offsetHeight || window.innerHeight;
  27. width = ${element}.offsetWidth || window.innerWidth;
  28. window.ReactNativeWebView.postMessage(JSON.stringify({ width: width, height: height }));
  29. // Make additional height checks (required to fix issues wit twitter embeds)
  30. clearTimeout(forceRefreshTimeout);
  31. if (lastHeight !== height) {
  32. heightTheSameTimes = 1;
  33. } else {
  34. heightTheSameTimes++;
  35. }
  36. lastHeight = height;
  37. if (heightTheSameTimes <= maxHeightTheSameTimes) {
  38. forceRefreshTimeout = setTimeout(
  39. updateSize,
  40. heightTheSameTimes * forceRefreshDelay
  41. );
  42. }
  43. }
  44. `;
  45. // add viewport setting to meta for WKWebView
  46. const makeScalePageToFit = zoomable => `
  47. var meta = document.createElement('meta');
  48. meta.setAttribute('name', 'viewport');
  49. meta.setAttribute('content', 'width=device-width, user-scalable=${
  50. zoomable ? 'yes' : 'no'
  51. }'); document.getElementsByTagName('head')[0].appendChild(meta);
  52. `;
  53. const getBaseScript = ({ style, zoomable }) =>
  54. `
  55. ;
  56. if (!document.getElementById("rnahw-wrapper")) {
  57. var wrapper = document.createElement('div');
  58. wrapper.id = 'rnahw-wrapper';
  59. while (document.body.firstChild instanceof Node) {
  60. wrapper.appendChild(document.body.firstChild);
  61. }
  62. document.body.appendChild(wrapper);
  63. }
  64. var width = ${getWidth(style)};
  65. ${updateSizeWithMessage('wrapper')}
  66. window.addEventListener('load', updateSize);
  67. window.addEventListener('resize', updateSize);
  68. ${domMutationObserveScript}
  69. ${Platform.OS === 'ios' ? makeScalePageToFit(zoomable) : ''}
  70. updateSize();
  71. `;
  72. const appendFilesToHead = ({ files, script }) =>
  73. files.reduceRight((combinedScript, file) => {
  74. const { rel, type, href } = file;
  75. return `
  76. var link = document.createElement('link');
  77. link.rel = '${rel}';
  78. link.type = '${type}';
  79. link.href = '${href}';
  80. document.head.appendChild(link);
  81. ${combinedScript}
  82. `;
  83. }, script);
  84. const screenWidth = Dimensions.get('window').width;
  85. const bodyStyle = `
  86. body {
  87. margin: 0;
  88. padding: 0;
  89. }
  90. `;
  91. const appendStylesToHead = ({ style, script }) => {
  92. const currentStyles = style ? bodyStyle + style : bodyStyle;
  93. // Escape any single quotes or newlines in the CSS with .replace()
  94. const escaped = currentStyles.replace(/\'/g, "\\'").replace(/\n/g, '\\n');
  95. return `
  96. var styleElement = document.createElement('style');
  97. styleElement.innerHTML = '${escaped}';
  98. document.head.appendChild(styleElement);
  99. ${script}
  100. `;
  101. };
  102. const getInjectedSource = ({ html, script }) => `
  103. ${html}
  104. <script>
  105. ${script}
  106. </script>
  107. `;
  108. const getScript = ({ files, customStyle, customScript, style, zoomable }) => {
  109. let script = getBaseScript({ style, zoomable });
  110. script = files && files.length > 0 ? appendFilesToHead({ files, script }) : script;
  111. script = appendStylesToHead({ style: customStyle, script });
  112. customScript && (script = customScript + script);
  113. return script;
  114. };
  115. export const getWidth = style => {
  116. return style && style.width ? style.width : screenWidth;
  117. };
  118. export const isSizeChanged = ({ height, previousHeight, width, previousWidth }) => {
  119. if (!height || !width) {
  120. return;
  121. }
  122. return height !== previousHeight || width !== previousWidth;
  123. };
  124. export const reduceData = props => {
  125. const { source } = props;
  126. const script = getScript(props);
  127. const { html, baseUrl } = source;
  128. if (html) {
  129. return { currentSource: { baseUrl, html: getInjectedSource({ html, script }) } };
  130. } else {
  131. return {
  132. currentSource: source,
  133. script
  134. };
  135. }
  136. };
  137. export const shouldUpdate = ({ prevProps, nextProps }) => {
  138. if (!(prevProps && nextProps)) {
  139. return true;
  140. }
  141. for (const prop in nextProps) {
  142. if (nextProps[prop] !== prevProps[prop]) {
  143. if (typeof nextProps[prop] === 'object' && typeof prevProps[prop] === 'object') {
  144. if (shouldUpdate({ prevProps: prevProps[prop], nextProps: nextProps[prop] })) {
  145. return true;
  146. }
  147. } else {
  148. return true;
  149. }
  150. }
  151. }
  152. return false;
  153. };