Nenhuma descrição

common.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. return (height && height !== oldHeight) || (width && width !== oldWidth);
  98. }
  99. export const domMutationObserveScript = `
  100. var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
  101. var observer = new MutationObserver(updateSize);
  102. observer.observe(document, {
  103. subtree: true,
  104. attributes: true
  105. });
  106. `;
  107. export const getCurrentSize = `
  108. function getSize(container) {
  109. var height = container.offsetHeight || document.body.offsetHeight;
  110. var width = container.offsetWidth || document.body.offsetWidth;
  111. return {
  112. height: height,
  113. width: width
  114. };
  115. }
  116. `;