You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

262 lines
8.6 KiB

Summary: fix slowness due to layout thrashing when reloading a large … (#12661) * Summary: fix slowness due to layout thrashing when reloading a large set of status updates in order to limit the maximum size of a status in a list view (e.g. the home timeline), so as to avoid having to scroll all the way through an abnormally large status update (see https://github.com/tootsuite/mastodon/pull/8205), the following steps are taken: •the element containing the status is rendered in the browser •its height is calculated, to determine if it exceeds the maximum height threshold. Unfortunately for performance, these steps are carried out in the componentDidMount(/Update) method, which also performs style modifications on the element. The combination of height request and style modification during javascript evaluation in the browser leads to layout-thrashing, where the elements are repeatedly re-laid-out (see https://developers.google.com/web/fundamentals/performance/rendering/avoid-large-complex-layouts-and-layout-thrashing & https://developer.mozilla.org/en-US/docs/Mozilla/Firefox/Performance_best_practices_for_Firefox_fe_engineers). The solution implemented here is to memoize the collapsed state in Redux the first time the status is seen (e.g. when fetched as part of a small batch, to populate the home timeline) , so that on subsequent re-renders, the value can be queried, rather than recalculated. This strategy is derived from https://github.com/tootsuite/mastodon/pull/4439 & https://github.com/tootsuite/mastodon/pull/4909, and should resolve https://github.com/tootsuite/mastodon/issues/12455. Andrew Lin (https://github.com/onethreeseven) is thanked for his assistance in root cause analysis and solution brainstorming * remove getSnapshotBeforeUpdate from status * remove componentWillUnmount from status * persist last-intersected status update and restore when ScrollableList is restored e.g. when navigating from home-timeline to a status conversational thread and <Back again * cache currently-viewing status id to avoid calling redux with identical value * refactor collapse toggle to pass explicit boolean
4 years ago
7 years ago
Summary: fix slowness due to layout thrashing when reloading a large … (#12661) * Summary: fix slowness due to layout thrashing when reloading a large set of status updates in order to limit the maximum size of a status in a list view (e.g. the home timeline), so as to avoid having to scroll all the way through an abnormally large status update (see https://github.com/tootsuite/mastodon/pull/8205), the following steps are taken: •the element containing the status is rendered in the browser •its height is calculated, to determine if it exceeds the maximum height threshold. Unfortunately for performance, these steps are carried out in the componentDidMount(/Update) method, which also performs style modifications on the element. The combination of height request and style modification during javascript evaluation in the browser leads to layout-thrashing, where the elements are repeatedly re-laid-out (see https://developers.google.com/web/fundamentals/performance/rendering/avoid-large-complex-layouts-and-layout-thrashing & https://developer.mozilla.org/en-US/docs/Mozilla/Firefox/Performance_best_practices_for_Firefox_fe_engineers). The solution implemented here is to memoize the collapsed state in Redux the first time the status is seen (e.g. when fetched as part of a small batch, to populate the home timeline) , so that on subsequent re-renders, the value can be queried, rather than recalculated. This strategy is derived from https://github.com/tootsuite/mastodon/pull/4439 & https://github.com/tootsuite/mastodon/pull/4909, and should resolve https://github.com/tootsuite/mastodon/issues/12455. Andrew Lin (https://github.com/onethreeseven) is thanked for his assistance in root cause analysis and solution brainstorming * remove getSnapshotBeforeUpdate from status * remove componentWillUnmount from status * persist last-intersected status update and restore when ScrollableList is restored e.g. when navigating from home-timeline to a status conversational thread and <Back again * cache currently-viewing status id to avoid calling redux with identical value * refactor collapse toggle to pass explicit boolean
4 years ago
7 years ago
7 years ago
7 years ago
Summary: fix slowness due to layout thrashing when reloading a large … (#12661) * Summary: fix slowness due to layout thrashing when reloading a large set of status updates in order to limit the maximum size of a status in a list view (e.g. the home timeline), so as to avoid having to scroll all the way through an abnormally large status update (see https://github.com/tootsuite/mastodon/pull/8205), the following steps are taken: •the element containing the status is rendered in the browser •its height is calculated, to determine if it exceeds the maximum height threshold. Unfortunately for performance, these steps are carried out in the componentDidMount(/Update) method, which also performs style modifications on the element. The combination of height request and style modification during javascript evaluation in the browser leads to layout-thrashing, where the elements are repeatedly re-laid-out (see https://developers.google.com/web/fundamentals/performance/rendering/avoid-large-complex-layouts-and-layout-thrashing & https://developer.mozilla.org/en-US/docs/Mozilla/Firefox/Performance_best_practices_for_Firefox_fe_engineers). The solution implemented here is to memoize the collapsed state in Redux the first time the status is seen (e.g. when fetched as part of a small batch, to populate the home timeline) , so that on subsequent re-renders, the value can be queried, rather than recalculated. This strategy is derived from https://github.com/tootsuite/mastodon/pull/4439 & https://github.com/tootsuite/mastodon/pull/4909, and should resolve https://github.com/tootsuite/mastodon/issues/12455. Andrew Lin (https://github.com/onethreeseven) is thanked for his assistance in root cause analysis and solution brainstorming * remove getSnapshotBeforeUpdate from status * remove componentWillUnmount from status * persist last-intersected status update and restore when ScrollableList is restored e.g. when navigating from home-timeline to a status conversational thread and <Back again * cache currently-viewing status id to avoid calling redux with identical value * refactor collapse toggle to pass explicit boolean
4 years ago
Summary: fix slowness due to layout thrashing when reloading a large … (#12661) * Summary: fix slowness due to layout thrashing when reloading a large set of status updates in order to limit the maximum size of a status in a list view (e.g. the home timeline), so as to avoid having to scroll all the way through an abnormally large status update (see https://github.com/tootsuite/mastodon/pull/8205), the following steps are taken: •the element containing the status is rendered in the browser •its height is calculated, to determine if it exceeds the maximum height threshold. Unfortunately for performance, these steps are carried out in the componentDidMount(/Update) method, which also performs style modifications on the element. The combination of height request and style modification during javascript evaluation in the browser leads to layout-thrashing, where the elements are repeatedly re-laid-out (see https://developers.google.com/web/fundamentals/performance/rendering/avoid-large-complex-layouts-and-layout-thrashing & https://developer.mozilla.org/en-US/docs/Mozilla/Firefox/Performance_best_practices_for_Firefox_fe_engineers). The solution implemented here is to memoize the collapsed state in Redux the first time the status is seen (e.g. when fetched as part of a small batch, to populate the home timeline) , so that on subsequent re-renders, the value can be queried, rather than recalculated. This strategy is derived from https://github.com/tootsuite/mastodon/pull/4439 & https://github.com/tootsuite/mastodon/pull/4909, and should resolve https://github.com/tootsuite/mastodon/issues/12455. Andrew Lin (https://github.com/onethreeseven) is thanked for his assistance in root cause analysis and solution brainstorming * remove getSnapshotBeforeUpdate from status * remove componentWillUnmount from status * persist last-intersected status update and restore when ScrollableList is restored e.g. when navigating from home-timeline to a status conversational thread and <Back again * cache currently-viewing status id to avoid calling redux with identical value * refactor collapse toggle to pass explicit boolean
4 years ago
Summary: fix slowness due to layout thrashing when reloading a large … (#12661) * Summary: fix slowness due to layout thrashing when reloading a large set of status updates in order to limit the maximum size of a status in a list view (e.g. the home timeline), so as to avoid having to scroll all the way through an abnormally large status update (see https://github.com/tootsuite/mastodon/pull/8205), the following steps are taken: •the element containing the status is rendered in the browser •its height is calculated, to determine if it exceeds the maximum height threshold. Unfortunately for performance, these steps are carried out in the componentDidMount(/Update) method, which also performs style modifications on the element. The combination of height request and style modification during javascript evaluation in the browser leads to layout-thrashing, where the elements are repeatedly re-laid-out (see https://developers.google.com/web/fundamentals/performance/rendering/avoid-large-complex-layouts-and-layout-thrashing & https://developer.mozilla.org/en-US/docs/Mozilla/Firefox/Performance_best_practices_for_Firefox_fe_engineers). The solution implemented here is to memoize the collapsed state in Redux the first time the status is seen (e.g. when fetched as part of a small batch, to populate the home timeline) , so that on subsequent re-renders, the value can be queried, rather than recalculated. This strategy is derived from https://github.com/tootsuite/mastodon/pull/4439 & https://github.com/tootsuite/mastodon/pull/4909, and should resolve https://github.com/tootsuite/mastodon/issues/12455. Andrew Lin (https://github.com/onethreeseven) is thanked for his assistance in root cause analysis and solution brainstorming * remove getSnapshotBeforeUpdate from status * remove componentWillUnmount from status * persist last-intersected status update and restore when ScrollableList is restored e.g. when navigating from home-timeline to a status conversational thread and <Back again * cache currently-viewing status id to avoid calling redux with identical value * refactor collapse toggle to pass explicit boolean
4 years ago
  1. import React from 'react';
  2. import ImmutablePropTypes from 'react-immutable-proptypes';
  3. import PropTypes from 'prop-types';
  4. import { FormattedMessage } from 'react-intl';
  5. import Permalink from './permalink';
  6. import classnames from 'classnames';
  7. import PollContainer from 'mastodon/containers/poll_container';
  8. import Icon from 'mastodon/components/icon';
  9. import { autoPlayGif } from 'mastodon/initial_state';
  10. const MAX_HEIGHT = 642; // 20px * 32 (+ 2px padding at the top)
  11. export default class StatusContent extends React.PureComponent {
  12. static contextTypes = {
  13. router: PropTypes.object,
  14. };
  15. static propTypes = {
  16. status: ImmutablePropTypes.map.isRequired,
  17. expanded: PropTypes.bool,
  18. showThread: PropTypes.bool,
  19. onExpandedToggle: PropTypes.func,
  20. onClick: PropTypes.func,
  21. collapsable: PropTypes.bool,
  22. onCollapsedToggle: PropTypes.func,
  23. };
  24. state = {
  25. hidden: true,
  26. };
  27. _updateStatusLinks () {
  28. const node = this.node;
  29. if (!node) {
  30. return;
  31. }
  32. const links = node.querySelectorAll('a');
  33. for (var i = 0; i < links.length; ++i) {
  34. let link = links[i];
  35. if (link.classList.contains('status-link')) {
  36. continue;
  37. }
  38. link.classList.add('status-link');
  39. let mention = this.props.status.get('mentions').find(item => link.href === item.get('url'));
  40. if (mention) {
  41. link.addEventListener('click', this.onMentionClick.bind(this, mention), false);
  42. link.setAttribute('title', mention.get('acct'));
  43. } else if (link.textContent[0] === '#' || (link.previousSibling && link.previousSibling.textContent && link.previousSibling.textContent[link.previousSibling.textContent.length - 1] === '#')) {
  44. link.addEventListener('click', this.onHashtagClick.bind(this, link.text), false);
  45. } else {
  46. link.setAttribute('title', link.href);
  47. link.classList.add('unhandled-link');
  48. }
  49. link.setAttribute('target', '_blank');
  50. link.setAttribute('rel', 'noopener noreferrer');
  51. }
  52. if (this.props.status.get('collapsed', null) === null) {
  53. let collapsed =
  54. this.props.collapsable
  55. && this.props.onClick
  56. && node.clientHeight > MAX_HEIGHT
  57. && this.props.status.get('spoiler_text').length === 0;
  58. if(this.props.onCollapsedToggle) this.props.onCollapsedToggle(collapsed);
  59. this.props.status.set('collapsed', collapsed);
  60. }
  61. }
  62. handleMouseEnter = ({ currentTarget }) => {
  63. if (autoPlayGif) {
  64. return;
  65. }
  66. const emojis = currentTarget.querySelectorAll('.custom-emoji');
  67. for (var i = 0; i < emojis.length; i++) {
  68. let emoji = emojis[i];
  69. emoji.src = emoji.getAttribute('data-original');
  70. }
  71. }
  72. handleMouseLeave = ({ currentTarget }) => {
  73. if (autoPlayGif) {
  74. return;
  75. }
  76. const emojis = currentTarget.querySelectorAll('.custom-emoji');
  77. for (var i = 0; i < emojis.length; i++) {
  78. let emoji = emojis[i];
  79. emoji.src = emoji.getAttribute('data-static');
  80. }
  81. }
  82. componentDidMount () {
  83. this._updateStatusLinks();
  84. }
  85. componentDidUpdate () {
  86. this._updateStatusLinks();
  87. }
  88. onMentionClick = (mention, e) => {
  89. if (this.context.router && e.button === 0 && !(e.ctrlKey || e.metaKey)) {
  90. e.preventDefault();
  91. this.context.router.history.push(`/accounts/${mention.get('id')}`);
  92. }
  93. }
  94. onHashtagClick = (hashtag, e) => {
  95. hashtag = hashtag.replace(/^#/, '');
  96. if (this.context.router && e.button === 0 && !(e.ctrlKey || e.metaKey)) {
  97. e.preventDefault();
  98. this.context.router.history.push(`/timelines/tag/${hashtag}`);
  99. }
  100. }
  101. handleMouseDown = (e) => {
  102. this.startXY = [e.clientX, e.clientY];
  103. }
  104. handleMouseUp = (e) => {
  105. if (!this.startXY) {
  106. return;
  107. }
  108. const [ startX, startY ] = this.startXY;
  109. const [ deltaX, deltaY ] = [Math.abs(e.clientX - startX), Math.abs(e.clientY - startY)];
  110. let element = e.target;
  111. while (element) {
  112. if (element.localName === 'button' || element.localName === 'a' || element.localName === 'label') {
  113. return;
  114. }
  115. element = element.parentNode;
  116. }
  117. if (deltaX + deltaY < 5 && e.button === 0 && this.props.onClick) {
  118. this.props.onClick();
  119. }
  120. this.startXY = null;
  121. }
  122. handleSpoilerClick = (e) => {
  123. e.preventDefault();
  124. if (this.props.onExpandedToggle) {
  125. // The parent manages the state
  126. this.props.onExpandedToggle();
  127. } else {
  128. this.setState({ hidden: !this.state.hidden });
  129. }
  130. }
  131. setRef = (c) => {
  132. this.node = c;
  133. }
  134. render () {
  135. const { status } = this.props;
  136. if (status.get('content').length === 0) {
  137. return null;
  138. }
  139. const hidden = this.props.onExpandedToggle ? !this.props.expanded : this.state.hidden;
  140. const renderReadMore = this.props.onClick && status.get('collapsed');
  141. const renderViewThread = this.props.showThread && status.get('in_reply_to_id') && status.get('in_reply_to_account_id') === status.getIn(['account', 'id']);
  142. const content = { __html: status.get('contentHtml') };
  143. const spoilerContent = { __html: status.get('spoilerHtml') };
  144. const classNames = classnames('status__content', {
  145. 'status__content--with-action': this.props.onClick && this.context.router,
  146. 'status__content--with-spoiler': status.get('spoiler_text').length > 0,
  147. 'status__content--collapsed': renderReadMore,
  148. });
  149. const showThreadButton = (
  150. <button className='status__content__read-more-button' onClick={this.props.onClick}>
  151. <FormattedMessage id='status.show_thread' defaultMessage='Show thread' />
  152. </button>
  153. );
  154. const readMoreButton = (
  155. <button className='status__content__read-more-button' onClick={this.props.onClick} key='read-more'>
  156. <FormattedMessage id='status.read_more' defaultMessage='Read more' /><Icon id='angle-right' fixedWidth />
  157. </button>
  158. );
  159. if (status.get('spoiler_text').length > 0) {
  160. let mentionsPlaceholder = '';
  161. const mentionLinks = status.get('mentions').map(item => (
  162. <Permalink to={`/accounts/${item.get('id')}`} href={item.get('url')} key={item.get('id')} className='mention'>
  163. @<span>{item.get('username')}</span>
  164. </Permalink>
  165. )).reduce((aggregate, item) => [...aggregate, item, ' '], []);
  166. const toggleText = hidden ? <FormattedMessage id='status.show_more' defaultMessage='Show more' /> : <FormattedMessage id='status.show_less' defaultMessage='Show less' />;
  167. if (hidden) {
  168. mentionsPlaceholder = <div>{mentionLinks}</div>;
  169. }
  170. return (
  171. <div className={classNames} ref={this.setRef} tabIndex='0' onMouseDown={this.handleMouseDown} onMouseUp={this.handleMouseUp} onMouseEnter={this.handleMouseEnter} onMouseLeave={this.handleMouseLeave}>
  172. <p style={{ marginBottom: hidden && status.get('mentions').isEmpty() ? '0px' : null }}>
  173. <span dangerouslySetInnerHTML={spoilerContent} className='translate' />
  174. {' '}
  175. <button tabIndex='0' className={`status__content__spoiler-link ${hidden ? 'status__content__spoiler-link--show-more' : 'status__content__spoiler-link--show-less'}`} onClick={this.handleSpoilerClick}>{toggleText}</button>
  176. </p>
  177. {mentionsPlaceholder}
  178. <div tabIndex={!hidden ? 0 : null} className={`status__content__text ${!hidden ? 'status__content__text--visible' : ''} translate`} dangerouslySetInnerHTML={content} />
  179. {!hidden && !!status.get('poll') && <PollContainer pollId={status.get('poll')} />}
  180. {renderViewThread && showThreadButton}
  181. </div>
  182. );
  183. } else if (this.props.onClick) {
  184. const output = [
  185. <div className={classNames} ref={this.setRef} tabIndex='0' onMouseDown={this.handleMouseDown} onMouseUp={this.handleMouseUp} key='status-content' onMouseEnter={this.handleMouseEnter} onMouseLeave={this.handleMouseLeave}>
  186. <div className='status__content__text status__content__text--visible translate' dangerouslySetInnerHTML={content} />
  187. {!!status.get('poll') && <PollContainer pollId={status.get('poll')} />}
  188. {renderViewThread && showThreadButton}
  189. </div>,
  190. ];
  191. if (renderReadMore) {
  192. output.push(readMoreButton);
  193. }
  194. return output;
  195. } else {
  196. return (
  197. <div className={classNames} ref={this.setRef} tabIndex='0' onMouseEnter={this.handleMouseEnter} onMouseLeave={this.handleMouseLeave}>
  198. <div className='status__content__text status__content__text--visible translate' dangerouslySetInnerHTML={content} />
  199. {!!status.get('poll') && <PollContainer pollId={status.get('poll')} />}
  200. {renderViewThread && showThreadButton}
  201. </div>
  202. );
  203. }
  204. }
  205. }