Path: blob/main/extensions/markdown-language-features/preview-src/scroll-sync.ts
3292 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { SettingsManager } from './settings';67const codeLineClass = 'code-line';8910export class CodeLineElement {11private readonly _detailParentElements: readonly HTMLDetailsElement[];1213constructor(14readonly element: HTMLElement,15readonly line: number,16readonly codeElement?: HTMLElement,17) {18this._detailParentElements = Array.from(getParentsWithTagName<HTMLDetailsElement>(element, 'DETAILS'));19}2021get isVisible(): boolean {22return !this._detailParentElements.some(x => !x.open);23}24}2526const getCodeLineElements = (() => {27let cachedElements: CodeLineElement[] | undefined;28let cachedVersion = -1;29return (documentVersion: number) => {30if (!cachedElements || documentVersion !== cachedVersion) {31cachedVersion = documentVersion;32cachedElements = [new CodeLineElement(document.body, -1)];33for (const element of document.getElementsByClassName(codeLineClass)) {34if (!(element instanceof HTMLElement)) {35continue;36}3738const line = +element.getAttribute('data-line')!;39if (isNaN(line)) {40continue;41}424344if (element.tagName === 'CODE' && element.parentElement && element.parentElement.tagName === 'PRE') {45// Fenced code blocks are a special case since the `code-line` can only be marked on46// the `<code>` element and not the parent `<pre>` element.47cachedElements.push(new CodeLineElement(element.parentElement, line, element));48} else if (element.tagName === 'UL' || element.tagName === 'OL') {49// Skip adding list elements since the first child has the same code line (and should be preferred)50} else {51cachedElements.push(new CodeLineElement(element, line));52}53}54}55return cachedElements;56};57})();5859/**60* Find the html elements that map to a specific target line in the editor.61*62* If an exact match, returns a single element. If the line is between elements,63* returns the element prior to and the element after the given line.64*/65export function getElementsForSourceLine(targetLine: number, documentVersion: number): { previous: CodeLineElement; next?: CodeLineElement } {66const lineNumber = Math.floor(targetLine);67const lines = getCodeLineElements(documentVersion);68let previous = lines[0] || null;69for (const entry of lines) {70if (entry.line === lineNumber) {71return { previous: entry, next: undefined };72} else if (entry.line > lineNumber) {73return { previous, next: entry };74}75previous = entry;76}77return { previous };78}7980/**81* Find the html elements that are at a specific pixel offset on the page.82*/83export function getLineElementsAtPageOffset(offset: number, documentVersion: number): { previous: CodeLineElement; next?: CodeLineElement } {84const lines = getCodeLineElements(documentVersion).filter(x => x.isVisible);85const position = offset - window.scrollY;86let lo = -1;87let hi = lines.length - 1;88while (lo + 1 < hi) {89const mid = Math.floor((lo + hi) / 2);90const bounds = getElementBounds(lines[mid]);91if (bounds.top + bounds.height >= position) {92hi = mid;93}94else {95lo = mid;96}97}98const hiElement = lines[hi];99const hiBounds = getElementBounds(hiElement);100if (hi >= 1 && hiBounds.top > position) {101const loElement = lines[lo];102return { previous: loElement, next: hiElement };103}104if (hi > 1 && hi < lines.length && hiBounds.top + hiBounds.height > position) {105return { previous: hiElement, next: lines[hi + 1] };106}107return { previous: hiElement };108}109110function getElementBounds({ element }: CodeLineElement): { top: number; height: number } {111const myBounds = element.getBoundingClientRect();112113// Some code line elements may contain other code line elements.114// In those cases, only take the height up to that child.115const codeLineChild = element.querySelector(`.${codeLineClass}`);116if (codeLineChild) {117const childBounds = codeLineChild.getBoundingClientRect();118const height = Math.max(1, (childBounds.top - myBounds.top));119return {120top: myBounds.top,121height: height122};123}124125return myBounds;126}127128/**129* Attempt to reveal the element for a source line in the editor.130*/131export function scrollToRevealSourceLine(line: number, documentVersion: number, settingsManager: SettingsManager) {132if (!settingsManager.settings?.scrollPreviewWithEditor) {133return;134}135136if (line <= 0) {137window.scroll(window.scrollX, 0);138return;139}140141const { previous, next } = getElementsForSourceLine(line, documentVersion);142if (!previous) {143return;144}145let scrollTo = 0;146const rect = getElementBounds(previous);147const previousTop = rect.top;148if (next && next.line !== previous.line) {149// Between two elements. Go to percentage offset between them.150const betweenProgress = (line - previous.line) / (next.line - previous.line);151const previousEnd = previousTop + rect.height;152const betweenHeight = next.element.getBoundingClientRect().top - previousEnd;153scrollTo = previousEnd + betweenProgress * betweenHeight;154} else {155const progressInElement = line - Math.floor(line);156scrollTo = previousTop + (rect.height * progressInElement);157}158window.scroll(window.scrollX, Math.max(1, window.scrollY + scrollTo));159}160161export function getEditorLineNumberForPageOffset(offset: number, documentVersion: number): number | null {162const { previous, next } = getLineElementsAtPageOffset(offset, documentVersion);163if (previous) {164if (previous.line < 0) {165return 0;166}167const previousBounds = getElementBounds(previous);168const offsetFromPrevious = (offset - window.scrollY - previousBounds.top);169if (next) {170const progressBetweenElements = offsetFromPrevious / (getElementBounds(next).top - previousBounds.top);171return previous.line + progressBetweenElements * (next.line - previous.line);172} else {173const progressWithinElement = offsetFromPrevious / (previousBounds.height);174return previous.line + progressWithinElement;175}176}177return null;178}179180/**181* Try to find the html element by using a fragment id182*/183export function getLineElementForFragment(fragment: string, documentVersion: number): CodeLineElement | undefined {184return getCodeLineElements(documentVersion).find((element) => {185return element.element.id === fragment;186});187}188189function* getParentsWithTagName<T extends HTMLElement>(element: HTMLElement, tagName: string): Iterable<T> {190for (let parent = element.parentElement; parent; parent = parent.parentElement) {191if (parent.tagName === tagName) {192yield parent as T;193}194}195}196197198