ScrollSyncHelper.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. this.isSyncScrollToPreviewFired = false;
  11. this.isSyncScrollToEditorFired = false;
  12. }
  13. getCodeLineElements(parentElement) {
  14. /** @type {CodeLineElement[]} */
  15. let elements;
  16. if (!elements) {
  17. elements = Array.prototype.map.call(
  18. parentElement.getElementsByClassName('code-line'),
  19. element => {
  20. const line = +element.getAttribute('data-line');
  21. return { element, line }
  22. })
  23. .filter(x => !isNaN(x.line));
  24. }
  25. return elements;
  26. }
  27. /**
  28. * Find the html elements that map to a specific target line in the editor.
  29. *
  30. * If an exact match, returns a single element. If the line is between elements,
  31. * returns the element prior to and the element after the given line.
  32. *
  33. * @param {Element} parentElement
  34. * @param {number} targetLine
  35. *
  36. * @returns {{ previous: CodeLineElement, next?: CodeLineElement }}
  37. */
  38. getElementsForSourceLine(parentElement, targetLine) {
  39. const lines = this.getCodeLineElements(parentElement);
  40. let previous = lines[0] || null;
  41. for (const entry of lines) {
  42. if (entry.line === targetLine) {
  43. return { previous: entry, next: null };
  44. } else if (entry.line > targetLine) {
  45. return { previous, next: entry };
  46. }
  47. previous = entry;
  48. }
  49. return { previous };
  50. }
  51. /**
  52. * Find the html elements that are at a specific pixel offset on the page.
  53. *
  54. * @param {Element} parentElement
  55. * @param {number} offset
  56. *
  57. * @returns {{ previous: CodeLineElement, next?: CodeLineElement }}
  58. */
  59. getLineElementsAtPageOffset(parentElement, offset) {
  60. const lines = this.getCodeLineElements(parentElement);
  61. const position = offset - parentElement.scrollTop + this.getParentElementOffset(parentElement);
  62. let lo = -1;
  63. let hi = lines.length - 1;
  64. while (lo + 1 < hi) {
  65. const mid = Math.floor((lo + hi) / 2);
  66. const bounds = lines[mid].element.getBoundingClientRect();
  67. if (bounds.top + bounds.height >= position) {
  68. hi = mid;
  69. } else {
  70. lo = mid;
  71. }
  72. }
  73. const hiElement = lines[hi];
  74. if (hi >= 1 && hiElement.element.getBoundingClientRect().top > position) {
  75. const loElement = lines[lo];
  76. const bounds = loElement.element.getBoundingClientRect();
  77. const previous = { element: loElement.element, line: loElement.line + (position - bounds.top) / (bounds.height) };
  78. const next = { element: hiElement.element, line: hiElement.line, fractional: 0 };
  79. return { previous, next };
  80. }
  81. const bounds = hiElement.element.getBoundingClientRect();
  82. const previous = { element: hiElement.element, line: hiElement.line + (position - bounds.top) / (bounds.height) };
  83. return { previous };
  84. }
  85. getEditorLineNumberForPageOffset(parentElement, offset) {
  86. const { previous, next } = this.getLineElementsAtPageOffset(parentElement, offset);
  87. if (previous) {
  88. if (next) {
  89. const betweenProgress = (offset - parentElement.scrollTop - previous.element.getBoundingClientRect().top) / (next.element.getBoundingClientRect().top - previous.element.getBoundingClientRect().top);
  90. return previous.line + betweenProgress * (next.line - previous.line);
  91. } else {
  92. return previous.line;
  93. }
  94. }
  95. return null;
  96. }
  97. getParentElementOffset(parentElement) {
  98. // get paddingTop
  99. const style = window.getComputedStyle(parentElement, null);
  100. const paddingTop = +(style.paddingTop.replace('px', ''));
  101. return paddingTop + parentElement.getBoundingClientRect().top;
  102. }
  103. /**
  104. * Attempt to scroll preview element for a source line in the editor.
  105. *
  106. * @param {Element} previewElement
  107. * @param {number} line
  108. */
  109. scrollPreview(previewElement, line) {
  110. // turn off the flag
  111. if (this.isSyncScrollToEditorFired) {
  112. this.isSyncScrollToEditorFired = false;
  113. return;
  114. }
  115. const { previous, next } = this.getElementsForSourceLine(previewElement, line);
  116. // marker.update(previous && previous.element);
  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. // turn on the flag
  129. this.isSyncScrollToPreviewFired = true;
  130. previewElement.scroll(0, previewElement.scrollTop + scrollTo);
  131. }
  132. }
  133. /**
  134. * Attempt to reveal the element that is overflowing from previewElement.
  135. *
  136. * @param {Element} previewElement
  137. * @param {number} line
  138. */
  139. scrollPreviewToRevealOverflowing(previewElement, line) {
  140. // turn off the flag
  141. if (this.isSyncScrollToEditorFired) {
  142. this.isSyncScrollToEditorFired = false;
  143. return;
  144. }
  145. const { previous, next } = this.getElementsForSourceLine(previewElement, line);
  146. // marker.update(previous && previous.element);
  147. if (previous) {
  148. const parentElementOffset = this.getParentElementOffset(previewElement);
  149. const prevElmTop = previous.element.getBoundingClientRect().top - parentElementOffset;
  150. const prevElmBottom = previous.element.getBoundingClientRect().bottom - parentElementOffset;
  151. let scrollTo = null;
  152. if (prevElmTop < 0) {
  153. // set the top of 'previous.element' to the top of 'previewElement'
  154. scrollTo = previewElement.scrollTop + prevElmTop;
  155. }
  156. else if (prevElmBottom > previewElement.clientHeight) {
  157. // set the bottom of 'previous.element' to the bottom of 'previewElement'
  158. scrollTo = previewElement.scrollTop + prevElmBottom - previewElement.clientHeight + 20;
  159. }
  160. if (scrollTo == null) {
  161. return;
  162. }
  163. // turn on the flag
  164. this.isSyncScrollToPreviewFired = true;
  165. previewElement.scroll(0, scrollTo);
  166. }
  167. }
  168. /**
  169. * Attempt to scroll Editor component for the offset of the element in the Preview component.
  170. *
  171. * @param {Editor} editor
  172. * @param {Element} previewElement
  173. * @param {number} offset
  174. */
  175. scrollEditor(editor, previewElement, offset) {
  176. // turn off the flag
  177. if (this.isSyncScrollToPreviewFired) {
  178. this.isSyncScrollToPreviewFired = false;
  179. return;
  180. }
  181. let line = this.getEditorLineNumberForPageOffset(previewElement, offset);
  182. line = Math.floor(line);
  183. // turn on flag
  184. this.isSyncScrollToEditorFired = true;
  185. editor.setScrollTopByLine(line);
  186. }
  187. }
  188. // singleton pattern
  189. const instance = new ScrollSyncHelper();
  190. export default instance;