ScrollSyncHelper.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. getCodeLineElements(parentElement) {
  10. /** @type {CodeLineElement[]} */
  11. let elements;
  12. if (!elements) {
  13. elements = Array.prototype.map.call(
  14. parentElement.getElementsByClassName('code-line'),
  15. (element) => {
  16. const line = +element.getAttribute('data-line');
  17. return { element, line };
  18. },
  19. )
  20. .filter((x) => { return !Number.isNaN(x.line) });
  21. }
  22. return elements;
  23. }
  24. /**
  25. * Find the html elements that map to a specific target line in the editor.
  26. *
  27. * If an exact match, returns a single element. If the line is between elements,
  28. * returns the element prior to and the element after the given line.
  29. *
  30. * @param {Element} element
  31. * @param {number} targetLine
  32. *
  33. * @returns {{ previous: CodeLineElement, next?: CodeLineElement }}
  34. */
  35. getElementsForSourceLine(element, targetLine) {
  36. const lines = this.getCodeLineElements(element);
  37. let previous = lines[0] || null;
  38. for (const entry of lines) {
  39. if (entry.line === targetLine) {
  40. return { previous: entry, next: null };
  41. }
  42. 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. }
  68. else {
  69. lo = mid;
  70. }
  71. }
  72. const hiElement = lines[hi];
  73. if (hi >= 1 && hiElement.element.getBoundingClientRect().top > position) {
  74. const loElement = lines[lo];
  75. const bounds = loElement.element.getBoundingClientRect();
  76. const previous = { element: loElement.element, line: loElement.line };
  77. if (bounds.height > 0) {
  78. previous.line += (position - bounds.top) / (bounds.height);
  79. }
  80. const next = { element: hiElement.element, line: hiElement.line, fractional: 0 };
  81. return { previous, next };
  82. }
  83. const bounds = hiElement.element.getBoundingClientRect();
  84. const previous = { element: hiElement.element, line: hiElement.line + (position - bounds.top) / (bounds.height) };
  85. return { previous };
  86. }
  87. getEditorLineNumberForPageOffset(parentElement, offset) {
  88. const { previous, next } = this.getLineElementsAtPageOffset(parentElement, offset);
  89. if (previous) {
  90. if (next) {
  91. const betweenProgress = (
  92. offset - parentElement.scrollTop - previous.element.getBoundingClientRect().top)
  93. / (next.element.getBoundingClientRect().top - previous.element.getBoundingClientRect().top);
  94. return previous.line + betweenProgress * (next.line - previous.line);
  95. }
  96. return previous.line;
  97. }
  98. return null;
  99. }
  100. /**
  101. * return the sum of the offset position of parent element and paddingTop
  102. * @param {Element} parentElement
  103. */
  104. getParentElementOffset(parentElement) {
  105. const offsetY = parentElement.getBoundingClientRect().top;
  106. // get paddingTop
  107. const style = window.getComputedStyle(parentElement, null);
  108. const paddingTop = +(style.paddingTop.replace('px', ''));
  109. return offsetY + paddingTop;
  110. }
  111. /**
  112. * Attempt to scroll preview element for a source line in the editor.
  113. *
  114. * @param {Element} previewElement
  115. * @param {number} line
  116. */
  117. scrollPreview(previewElement, line) {
  118. const { previous, next } = this.getElementsForSourceLine(previewElement, line);
  119. if (previous) {
  120. let scrollTo = 0;
  121. if (next) {
  122. // Between two elements. Go to percentage offset between them.
  123. const betweenProgress = (line - previous.line) / (next.line - previous.line);
  124. const elementOffset = next.element.getBoundingClientRect().top - previous.element.getBoundingClientRect().top;
  125. scrollTo = previous.element.getBoundingClientRect().top + betweenProgress * elementOffset;
  126. }
  127. else {
  128. scrollTo = previous.element.getBoundingClientRect().top;
  129. }
  130. scrollTo -= this.getParentElementOffset(previewElement);
  131. previewElement.scrollTop += scrollTo;
  132. }
  133. }
  134. /**
  135. * Attempt to reveal the element that is overflowing from previewElement.
  136. *
  137. * @param {Element} previewElement
  138. * @param {number} line
  139. */
  140. scrollPreviewToRevealOverflowing(previewElement, line) {
  141. // eslint-disable-next-line no-unused-vars
  142. const { previous, next } = this.getElementsForSourceLine(previewElement, line);
  143. if (previous) {
  144. const parentElementOffset = this.getParentElementOffset(previewElement);
  145. const prevElmTop = previous.element.getBoundingClientRect().top - parentElementOffset;
  146. const prevElmBottom = previous.element.getBoundingClientRect().bottom - parentElementOffset;
  147. let scrollTo = null;
  148. if (prevElmTop < 0) {
  149. // set the top of 'previous.element' to the top of 'previewElement'
  150. scrollTo = previewElement.scrollTop + prevElmTop;
  151. }
  152. else if (prevElmBottom > previewElement.clientHeight) {
  153. // set the bottom of 'previous.element' to the bottom of 'previewElement'
  154. scrollTo = previewElement.scrollTop + prevElmBottom - previewElement.clientHeight + 20;
  155. }
  156. if (scrollTo == null) {
  157. return;
  158. }
  159. previewElement.scrollTop = scrollTo;
  160. }
  161. }
  162. /**
  163. * Attempt to scroll Editor component for the offset of the element in the Preview component.
  164. *
  165. * @param {Editor} editor
  166. * @param {Element} previewElement
  167. * @param {number} offset
  168. */
  169. scrollEditor(editor, previewElement, offset) {
  170. let line = this.getEditorLineNumberForPageOffset(previewElement, offset);
  171. line = Math.floor(line);
  172. editor.setScrollTopByLine(line);
  173. }
  174. }
  175. // singleton pattern
  176. const instance = new ScrollSyncHelper();
  177. Object.freeze(instance);
  178. export default instance;