Няма описание

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. function updateSize(event) {
  16. if (!window.hasOwnProperty('ReactNativeWebView') || !window.ReactNativeWebView.hasOwnProperty('postMessage')) {
  17. !updateSizeInterval && (updateSizeInterval = setInterval(updateSize, 200));
  18. return;
  19. }
  20. clearInterval(updateSizeInterval)
  21. height = ${element}.offsetHeight || document.documentElement.offsetHeight;
  22. width = ${element}.offsetWidth || document.documentElement.offsetWidth;
  23. window.ReactNativeWebView.postMessage(JSON.stringify({ width: width, height: height }));
  24. }
  25. `;
  26. // add viewport setting to meta for WKWebView
  27. const makeScalePageToFit = zoomable => `
  28. var meta = document.createElement('meta');
  29. meta.setAttribute('name', 'viewport');
  30. meta.setAttribute('content', 'width=device-width, user-scalable=${
  31. zoomable ? 'yes' : 'no'
  32. }'); document.getElementsByTagName('head')[0].appendChild(meta);
  33. `;
  34. const getBaseScript = ({ style, zoomable }) =>
  35. `
  36. ;
  37. if (!document.getElementById("rnahw-wrapper")) {
  38. var wrapper = document.createElement('div');
  39. wrapper.id = 'rnahw-wrapper';
  40. while (document.body.firstChild instanceof Node) {
  41. wrapper.appendChild(document.body.firstChild);
  42. }
  43. document.body.appendChild(wrapper);
  44. }
  45. var width = ${getWidth(style)};
  46. ${updateSizeWithMessage('wrapper')}
  47. window.addEventListener('load', updateSize);
  48. window.addEventListener('resize', updateSize);
  49. ${domMutationObserveScript}
  50. ${makeScalePageToFit(zoomable)}
  51. updateSize();
  52. `;
  53. const appendFilesToHead = ({ files, script }) =>
  54. files.reduceRight((combinedScript, file) => {
  55. const { rel, type, href } = file;
  56. return `
  57. var link = document.createElement('link');
  58. link.rel = '${rel}';
  59. link.type = '${type}';
  60. link.href = '${href}';
  61. document.head.appendChild(link);
  62. ${combinedScript}
  63. `;
  64. }, script);
  65. const screenWidth = Dimensions.get('window').width;
  66. const bodyStyle = `
  67. body {
  68. margin: 0;
  69. padding: 0;
  70. }
  71. `;
  72. const appendStylesToHead = ({ style, script }) => {
  73. const currentStyles = style ? bodyStyle + style : bodyStyle;
  74. // Escape any single quotes or newlines in the CSS with .replace()
  75. const escaped = currentStyles.replace(/\'/g, "\\'").replace(/\n/g, '\\n');
  76. return `
  77. var styleElement = document.createElement('style');
  78. styleElement.innerHTML = '${escaped}';
  79. document.head.appendChild(styleElement);
  80. ${script}
  81. `;
  82. };
  83. const getInjectedSource = ({ html, script }) => `
  84. ${html}
  85. <script>
  86. ${script}
  87. </script>
  88. `;
  89. const getScript = ({ files, customStyle, customScript, style, zoomable }) => {
  90. let script = getBaseScript({ style, zoomable });
  91. script = files && files.length > 0 ? appendFilesToHead({ files, script }) : script;
  92. script = appendStylesToHead({ style: customStyle, script });
  93. customScript && (script = customScript + script);
  94. return script;
  95. };
  96. export const getWidth = style => {
  97. return style && style.width ? style.width : screenWidth;
  98. };
  99. export const isSizeChanged = ({ height, previousHeight, width, previousWidth }) => {
  100. if (!height || !width) {
  101. return;
  102. }
  103. return height !== previousHeight || width !== previousWidth;
  104. };
  105. export const reduceData = props => {
  106. const { source } = props;
  107. const script = getScript(props);
  108. const { html, baseUrl } = source;
  109. if (html) {
  110. return { currentSource: { baseUrl, html: getInjectedSource({ html, script }) } };
  111. } else {
  112. return {
  113. currentSource: source,
  114. script
  115. };
  116. }
  117. };
  118. export const shouldUpdate = ({ prevProps, nextProps }) => {
  119. if (!(prevProps && nextProps)) {
  120. return true;
  121. }
  122. for (const prop in nextProps) {
  123. if (nextProps[prop] !== prevProps[prop]) {
  124. if (typeof nextProps[prop] === 'object' && typeof prevProps[prop] === 'object') {
  125. if (shouldUpdate({ prevProps: prevProps[prop], nextProps: nextProps[prop] })) {
  126. return true;
  127. }
  128. } else {
  129. return true;
  130. }
  131. }
  132. }
  133. return false;
  134. };