Geen omschrijving

utils.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 lastHeight = 0;
  14. var heightTheSameTimes = 0;
  15. var maxHeightTheSameTimes = 5;
  16. var forceRefreshDelay = 1000;
  17. var forceRefreshTimeout;
  18. function updateSize(event) {
  19. if (
  20. !window.hasOwnProperty('ReactNativeWebView') ||
  21. !window.ReactNativeWebView.hasOwnProperty('postMessage')
  22. ) {
  23. setTimeout(updateSize, 200);
  24. return;
  25. }
  26. height = ${element}.offsetHeight || document.documentElement.offsetHeight;
  27. width = ${element}.offsetWidth || document.documentElement.offsetWidth;
  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. ${updateSizeWithMessage('wrapper')}
  65. window.addEventListener('load', updateSize);
  66. window.addEventListener('resize', updateSize);
  67. ${domMutationObserveScript}
  68. ${makeScalePageToFit(zoomable)}
  69. updateSize();
  70. `;
  71. const appendFilesToHead = ({ files, script }) =>
  72. files.reduceRight((combinedScript, file) => {
  73. const { rel, type, href } = file;
  74. return `
  75. var link = document.createElement('link');
  76. link.rel = '${rel}';
  77. link.type = '${type}';
  78. link.href = '${href}';
  79. document.head.appendChild(link);
  80. ${combinedScript}
  81. `;
  82. }, script);
  83. const screenWidth = Dimensions.get('window').width;
  84. const bodyStyle = `
  85. body {
  86. margin: 0;
  87. padding: 0;
  88. }
  89. `;
  90. const appendStylesToHead = ({ style, script }) => {
  91. const currentStyles = style ? bodyStyle + style : bodyStyle;
  92. // Escape any single quotes or newlines in the CSS with .replace()
  93. const escaped = currentStyles.replace(/\'/g, "\\'").replace(/\n/g, '\\n');
  94. return `
  95. var styleElement = document.createElement('style');
  96. styleElement.innerHTML = '${escaped}';
  97. document.head.appendChild(styleElement);
  98. ${script}
  99. `;
  100. };
  101. const getInjectedSource = ({ html, script }) => `
  102. ${html}
  103. <script>
  104. // prevents code colissions with global scope
  105. (() => {
  106. ${script}
  107. })();
  108. </script>
  109. `;
  110. const getScript = ({ files, customStyle, customScript, style, zoomable }) => {
  111. let script = getBaseScript({ style, zoomable });
  112. script = files && files.length > 0 ? appendFilesToHead({ files, script }) : script;
  113. script = appendStylesToHead({ style: customStyle, script });
  114. customScript && (script = customScript + script);
  115. return script;
  116. };
  117. export const getWidth = style => {
  118. return style && style.width ? style.width : screenWidth;
  119. };
  120. export const isSizeChanged = ({ height, previousHeight, width, previousWidth }) => {
  121. if (!height || !width) {
  122. return;
  123. }
  124. return height !== previousHeight || width !== previousWidth;
  125. };
  126. export const reduceData = props => {
  127. const { source } = props;
  128. const script = getScript(props);
  129. const { html, baseUrl } = source;
  130. if (html) {
  131. return { currentSource: { baseUrl, html: getInjectedSource({ html, script }) } };
  132. } else {
  133. return {
  134. currentSource: source,
  135. script
  136. };
  137. }
  138. };
  139. export const shouldUpdate = ({ prevProps, nextProps }) => {
  140. if (!(prevProps && nextProps)) {
  141. return true;
  142. }
  143. for (const prop in nextProps) {
  144. if (nextProps[prop] !== prevProps[prop]) {
  145. if (typeof nextProps[prop] === 'object' && typeof prevProps[prop] === 'object') {
  146. if (shouldUpdate({ prevProps: prevProps[prop], nextProps: nextProps[prop] })) {
  147. return true;
  148. }
  149. } else {
  150. return true;
  151. }
  152. }
  153. }
  154. return false;
  155. };