No Description

common.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. 'use strict';
  2. import { Dimensions } from 'react-native';
  3. import Immutable from 'immutable';
  4. function appendFilesToHead(files, script) {
  5. return files.reduceRight((combinedScript, file) => {
  6. const { rel, type, href } = file;
  7. return `
  8. var link = document.createElement('link');
  9. link.rel = '${rel}';
  10. link.type = '${type}';
  11. link.href = '${href}';
  12. document.head.appendChild(link);
  13. ${combinedScript}
  14. `;
  15. }, script);
  16. }
  17. const screenWidth = Dimensions.get('window').width;
  18. const bodyStyle = `
  19. body {
  20. margin: 0;
  21. padding: 0;
  22. }
  23. `;
  24. function appendStylesToHead(styles, script) {
  25. const currentStyles = styles ? bodyStyle + styles : bodyStyle;
  26. // Escape any single quotes or newlines in the CSS with .replace()
  27. const escaped = currentStyles.replace(/\'/g, "\\'").replace(/\n/g, '\\n');
  28. return `
  29. var styleElement = document.createElement('style');
  30. styleElement.innerHTML = '${escaped}';
  31. document.head.appendChild(styleElement);
  32. ${script}
  33. `;
  34. }
  35. function getReloadRelatedData(props) {
  36. const { hasIframe, files, customStyle, customScript, style, source } = props;
  37. return {
  38. source,
  39. hasIframe,
  40. files,
  41. customStyle,
  42. customScript,
  43. style
  44. };
  45. }
  46. function isChanged(newValue, oldValue) {
  47. return !Immutable.is(Immutable.fromJS(newValue), Immutable.fromJS(oldValue));
  48. }
  49. function getInjectedSource(html, script) {
  50. return `
  51. ${html}
  52. <script>
  53. ${script}
  54. </script>
  55. `;
  56. }
  57. export function getScript(props, getBaseScript) {
  58. const { files, customStyle, customScript, style } = getReloadRelatedData(props);
  59. let script = getBaseScript(style);
  60. script = files && files.length > 0 ? appendFilesToHead(files, script) : script;
  61. script = appendStylesToHead(customStyle, script);
  62. customScript && (script = customScript + script);
  63. return script;
  64. }
  65. export function getWidth(style) {
  66. return style && style.width ? style.width : screenWidth;
  67. }
  68. export function isEqual(newProps, oldProps) {
  69. return isChanged(getReloadRelatedData(newProps), getReloadRelatedData(oldProps));
  70. }
  71. export function setState(props, getBaseScript, getIframeBaseScript) {
  72. const { source, baseUrl } = props;
  73. const script = getScript(props, getBaseScript, getIframeBaseScript);
  74. let state = {};
  75. if (source.html) {
  76. let currentSource = { html: getInjectedSource(source.html, script) };
  77. baseUrl && Object.assign(currentSource, { baseUrl });
  78. Object.assign(state, { source: currentSource });
  79. } else {
  80. let currentSource = Object.assign({}, source);
  81. baseUrl && Object.assign(currentSource, { baseUrl });
  82. Object.assign(state, {
  83. source: currentSource,
  84. script
  85. });
  86. }
  87. return state;
  88. }
  89. export function handleSizeUpdated(height, width, onSizeUpdated) {
  90. onSizeUpdated &&
  91. onSizeUpdated({
  92. height,
  93. width
  94. });
  95. }
  96. export function isSizeChanged(height, oldHeight, width, oldWidth) {
  97. if (height == null || width == null) {
  98. return false;
  99. }
  100. return height !== oldHeight || width !== oldWidth;
  101. }
  102. export const domMutationObserveScript = `
  103. var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
  104. var observer = new MutationObserver(updateSize);
  105. observer.observe(document, {
  106. subtree: true,
  107. attributes: true
  108. });
  109. `;
  110. export function updateSizeWithMessage(element) {
  111. return `
  112. var updateSizeInterval = null;
  113. var height = 0;
  114. function updateSize() {
  115. if (!window.hasOwnProperty('ReactNativeWebView') || !window.ReactNativeWebView.hasOwnProperty('postMessage')) {
  116. !updateSizeInterval && (updateSizeInterval = setInterval(updateSize, 200));
  117. return;
  118. }
  119. height = ${element}.offsetHeight || window.innerHeight,
  120. width = ${element}.offsetWidth || window.innerWidth;
  121. window.ReactNativeWebView.postMessage(JSON.stringify({ width: width, height: height }));
  122. }
  123. `;
  124. }
  125. export function getStateFromProps(props, state) {
  126. const { height: oldHeight, width: oldWidth } = state;
  127. const height = props.style ? props.style.height : null;
  128. const width = props.style ? props.style.width : null;
  129. if (isSizeChanged(height, oldHeight, width, oldWidth)) {
  130. return {
  131. height: height || oldHeight,
  132. width: width || oldWidth,
  133. isSizeChanged: true
  134. };
  135. }
  136. return null;
  137. }