暂无描述

common.js 3.9KB

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