ScrollSyncHelper.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * This class is copied from Microsoft/vscode repository
  3. * @see https://github.com/Microsoft/vscode/blob/0532a3429a18688a0c086a4212e7e5b4888b2a48/extensions/markdown/media/main.js
  4. */
  5. class ScrollSyncHelper {
  6. /**
  7. * @typedef {{ element: Element, line: number }} CodeLineElement
  8. */
  9. constructor() {
  10. }
  11. getCodeLineElements(parentElement) {
  12. /** @type {CodeLineElement[]} */
  13. let elements;
  14. if (!elements) {
  15. elements = Array.prototype.map.call(
  16. parentElement.getElementsByClassName('code-line'),
  17. element => {
  18. const line = +element.getAttribute('data-line');
  19. return { element, line }
  20. })
  21. .filter(x => !isNaN(x.line));
  22. }
  23. return elements;
  24. }
  25. /**
  26. * Find the html elements that map to a specific target line in the editor.
  27. *
  28. * If an exact match, returns a single element. If the line is between elements,
  29. * returns the element prior to and the element after the given line.
  30. *
  31. * @param {Element} element
  32. * @param {number} targetLine
  33. *
  34. * @returns {{ previous: CodeLineElement, next?: CodeLineElement }}
  35. */
  36. getElementsForSourceLine(element, targetLine) {
  37. const lines = this.getCodeLineElements(element);
  38. let previous = lines[0] || null;
  39. for (const entry of lines) {
  40. if (entry.line === targetLine) {
  41. return { previous: entry, next: null };
  42. } else if (entry.line > targetLine) {
  43. return { previous, next: entry };
  44. }
  45. previous = entry;
  46. }
  47. return { previous };
  48. }
  49. /**
  50. * Find the html elements that are at a specific pixel offset on the page.
  51. *
  52. * @param {Element} parentElement
  53. * @param {number} offset
  54. *
  55. * @returns {{ previous: CodeLineElement, next?: CodeLineElement }}
  56. */
  57. getLineElementsAtPageOffset(parentElement, offset) {
  58. const lines = this.getCodeLineElements(parentElement);
  59. const position = offset - parentElement.scrollTop + this.getParentElementOffset(parentElement);
  60. let lo = -1;
  61. let hi = lines.length - 1;
  62. while (lo + 1 < hi) {
  63. const mid = Math.floor((lo + hi) / 2);
  64. const bounds = lines[mid].element.getBoundingClientRect();
  65. if (bounds.top + bounds.height >= position) {
  66. hi = mid;
  67. } else {
  68. lo = mid;
  69. }
  70. }
  71. const hiElement = lines[hi];
  72. if (hi >= 1 && hiElement.element.getBoundingClientRect().top > position) {
  73. const loElement = lines[lo];
  74. const bounds = loElement.element.getBoundingClientRect();
  75. let previous = { element: loElement.element, line: loElement.line };
  76. if (bounds.height > 0) {
  77. previous.line += (position - bounds.top) / (bounds.height);
  78. }
  79. const next = { element: hiElement.element, line: hiElement.line, fractional: 0 };
  80. return { previous, next };
  81. }
  82. const bounds = hiElement.element.getBoundingClientRect();
  83. const previous = { element: hiElement.element, line: hiElement.line + (position - bounds.top) / (bounds.height) };
  84. return { previous };
  85. }
  86. getEditorLineNumberForPageOffset(parentElement, offset) {
  87. const { previous, next } = this.getLineElementsAtPageOffset(parentElement, offset);
  88. if (previous) {
  89. if (next) {
  90. const betweenProgress = (offset - parentElement.scrollTop - previous.element.getBoundingClientRect().top) / (next.element.getBoundingClientRect().top - previous.element.getBoundingClientRect().top);
  91. return previous.line + betweenProgress * (next.line - previous.line);
  92. } else {
  93. return previous.line;
  94. }
  95. }
  96. return null;
  97. }
  98. /**
  99. * return the sum of the offset position of parent element and paddingTop
  100. * @param {Element} parentElement
  101. */
  102. getParentElementOffset(parentElement) {
  103. const offsetY = parentElement.getBoundingClientRect().top;
  104. // get paddingTop
  105. const style = window.getComputedStyle(parentElement, null);
  106. const paddingTop = +(style.paddingTop.replace('px', ''));
  107. return offsetY + paddingTop;
  108. }
  109. /**
  110. * Attempt to scroll preview element for a source line in the editor.
  111. *
  112. * @param {Element} previewElement
  113. * @param {number} line
  114. */
  115. scrollPreview(previewElement, line) {
  116. const { previous, next } = this.getElementsForSourceLine(previewElement, line);
  117. if (previous) {
  118. let scrollTo = 0;
  119. if (next) {
  120. // Between two elements. Go to percentage offset between them.
  121. const betweenProgress = (line - previous.line) / (next.line - previous.line);
  122. const elementOffset = next.element.getBoundingClientRect().top - previous.element.getBoundingClientRect().top;
  123. scrollTo = previous.element.getBoundingClientRect().top + betweenProgress * elementOffset;
  124. } else {
  125. scrollTo = previous.element.getBoundingClientRect().top;
  126. }
  127. scrollTo -= this.getParentElementOffset(previewElement);
  128. previewElement.scroll(0, previewElement.scrollTop + scrollTo);
  129. }
  130. }
  131. /**
  132. * Attempt to reveal the element that is overflowing from previewElement.
  133. *
  134. * @param {Element} previewElement
  135. * @param {number} line
  136. */
  137. scrollPreviewToRevealOverflowing(previewElement, line) {
  138. const { previous, next } = this.getElementsForSourceLine(previewElement, line);
  139. if (previous) {
  140. const parentElementOffset = this.getParentElementOffset(previewElement);
  141. const prevElmTop = previous.element.getBoundingClientRect().top - parentElementOffset;
  142. const prevElmBottom = previous.element.getBoundingClientRect().bottom - parentElementOffset;
  143. let scrollTo = null;
  144. if (prevElmTop < 0) {
  145. // set the top of 'previous.element' to the top of 'previewElement'
  146. scrollTo = previewElement.scrollTop + prevElmTop;
  147. }
  148. else if (prevElmBottom > previewElement.clientHeight) {
  149. // set the bottom of 'previous.element' to the bottom of 'previewElement'
  150. scrollTo = previewElement.scrollTop + prevElmBottom - previewElement.clientHeight + 20;
  151. }
  152. if (scrollTo == null) {
  153. return;
  154. }
  155. previewElement.scroll(0, scrollTo);
  156. }
  157. }
  158. /**
  159. * Attempt to scroll Editor component for the offset of the element in the Preview component.
  160. *
  161. * @param {Editor} editor
  162. * @param {Element} previewElement
  163. * @param {number} offset
  164. */
  165. scrollEditor(editor, previewElement, offset) {
  166. let line = this.getEditorLineNumberForPageOffset(previewElement, offset);
  167. line = Math.floor(line);
  168. editor.setScrollTopByLine(line);
  169. }
  170. }
  171. // singleton pattern
  172. const instance = new ScrollSyncHelper();
  173. Object.freeze(instance);
  174. export default instance;