Нема описа

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. function getWidth(style) {
  22. return style && style.width ? style.width : screenWidth;
  23. }
  24. const bodyStyle = `
  25. body {
  26. margin: 0;
  27. padding: 0;
  28. }
  29. `;
  30. function appendStylesToHead(styles, script) {
  31. const currentStyles = styles ? bodyStyle + styles : bodyStyle;
  32. // Escape any single quotes or newlines in the CSS with .replace()
  33. const escaped = currentStyles.replace(/\'/g, "\\'").replace(/\n/g, '\\n');
  34. return `
  35. var styleElement = document.createElement('style');
  36. styleElement.innerHTML = '${escaped}';
  37. document.head.appendChild(styleElement);
  38. ${script}
  39. `;
  40. }
  41. function getReloadRelatedData(props) {
  42. const { hasIframe, files, customStyle, customScript, style } = props;
  43. return {
  44. hasIframe,
  45. files,
  46. customStyle,
  47. customScript,
  48. style
  49. };
  50. }
  51. function isChanged(newValue, oldValue) {
  52. return !Immutable.is(Immutable.fromJS(newValue), Immutable.fromJS(oldValue));
  53. }
  54. function getScript(props, getBaseScript, getIframeBaseScript) {
  55. const { hasIframe, files, customStyle, customScript, style } = getReloadRelatedData(props);
  56. const baseScript = getBaseScript(style);
  57. let script = hasIframe ? baseScript : getIframeBaseScript(style);
  58. script = files ? appendFilesToHead(files, baseScript) : baseScript;
  59. script = appendStylesToHead(customStyle, script);
  60. customScript && (script = customScript + script);
  61. return script;
  62. }
  63. function getSize(nextStyle, prevStyle) {
  64. if (!nextStyle) {
  65. return null;
  66. }
  67. if (isChanged(nextStyle, prevStyle)) {
  68. let size = null;
  69. const { height, width } = nextStyle;
  70. height && Object.assign(size, { height });
  71. width && Object.assign(size, { width });
  72. return size;
  73. } else {
  74. return null;
  75. }
  76. }
  77. function isScriptChanged(nextProps, props) {
  78. return nextProps && props && isChanged(getReloadRelatedData(nextProps), getReloadRelatedData(props));
  79. }
  80. function handleSizeUpdated(height, width, onSizeUpdated) {
  81. onSizeUpdated &&
  82. onSizeUpdated({
  83. height,
  84. width
  85. });
  86. }
  87. const domMutationObserveScript = `
  88. var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
  89. var observer = new MutationObserver(updateSize);
  90. observer.observe(document, {
  91. subtree: true,
  92. attributes: true
  93. });
  94. `;
  95. export { getSize, isScriptChanged, getWidth, getScript, handleSizeUpdated, domMutationObserveScript };