Path: blob/main/extensions/markdown-language-features/preview-src/scroll-sync.ts
5240 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 {22if (this._detailParentElements.some(x => !x.open)) {23return false;24}2526const style = window.getComputedStyle(this.element);27if (style.display === 'none' || style.visibility === 'hidden') {28return false;29}3031const bounds = this.element.getBoundingClientRect();32if (bounds.height === 0 || bounds.width === 0) {33return false;34}3536return true;37}38}3940const getCodeLineElements = (() => {41let cachedElements: CodeLineElement[] | undefined;42let cachedVersion = -1;43return (documentVersion: number) => {44if (!cachedElements || documentVersion !== cachedVersion) {45cachedVersion = documentVersion;46cachedElements = [new CodeLineElement(document.body, -1)];47for (const element of document.getElementsByClassName(codeLineClass)) {48if (!(element instanceof HTMLElement)) {49continue;50}5152const line = +element.getAttribute('data-line')!;53if (isNaN(line)) {54continue;55}565758if (element.tagName === 'CODE' && element.parentElement && element.parentElement.tagName === 'PRE') {59// Fenced code blocks are a special case since the `code-line` can only be marked on60// the `<code>` element and not the parent `<pre>` element.61cachedElements.push(new CodeLineElement(element.parentElement, line, element));62} else if (element.tagName === 'UL' || element.tagName === 'OL') {63// Skip adding list elements since the first child has the same code line (and should be preferred)64} else {65cachedElements.push(new CodeLineElement(element, line));66}67}68}69return cachedElements;70};71})();7273/**74* Find the html elements that map to a specific target line in the editor.75*76* If an exact match, returns a single element. If the line is between elements,77* returns the element prior to and the element after the given line.78*/79export function getElementsForSourceLine(targetLine: number, documentVersion: number): { previous: CodeLineElement; next?: CodeLineElement } {80const lineNumber = Math.floor(targetLine);81const lines = getCodeLineElements(documentVersion);82let previous = lines[0] || null;83for (const entry of lines) {84if (entry.line === lineNumber) {85return { previous: entry, next: undefined };86} else if (entry.line > lineNumber) {87return { previous, next: entry };88}89previous = entry;90}91return { previous };92}9394/**95* Find the html elements that are at a specific pixel offset on the page.96*/97export function getLineElementsAtPageOffset(offset: number, documentVersion: number): { previous: CodeLineElement; next?: CodeLineElement } {98const lines = getCodeLineElements(documentVersion).filter(x => x.isVisible);99const position = offset - window.scrollY;100let lo = -1;101let hi = lines.length - 1;102while (lo + 1 < hi) {103const mid = Math.floor((lo + hi) / 2);104const bounds = getElementBounds(lines[mid]);105if (bounds.top + bounds.height >= position) {106hi = mid;107}108else {109lo = mid;110}111}112const hiElement = lines[hi];113const hiBounds = getElementBounds(hiElement);114if (hi >= 1 && hiBounds.top > position) {115const loElement = lines[lo];116return { previous: loElement, next: hiElement };117}118if (hi > 1 && hi < lines.length && hiBounds.top + hiBounds.height > position) {119return { previous: hiElement, next: lines[hi + 1] };120}121return { previous: hiElement };122}123124function getElementBounds({ element }: CodeLineElement): { top: number; height: number } {125const myBounds = element.getBoundingClientRect();126127// Some code line elements may contain other code line elements.128// In those cases, only take the height up to that child.129const codeLineChild = element.querySelector(`.${codeLineClass}`);130if (codeLineChild) {131const childBounds = codeLineChild.getBoundingClientRect();132const height = Math.max(1, (childBounds.top - myBounds.top));133return {134top: myBounds.top,135height: height136};137}138139return myBounds;140}141142/**143* Attempt to reveal the element for a source line in the editor.144*/145export function scrollToRevealSourceLine(line: number, documentVersion: number, settingsManager: SettingsManager) {146if (!settingsManager.settings?.scrollPreviewWithEditor) {147return;148}149150if (line <= 0) {151window.scroll(window.scrollX, 0);152return;153}154155const { previous, next } = getElementsForSourceLine(line, documentVersion);156if (!previous) {157return;158}159let scrollTo = 0;160const rect = getElementBounds(previous);161const previousTop = rect.top;162if (next && next.line !== previous.line) {163// Between two elements. Go to percentage offset between them.164const betweenProgress = (line - previous.line) / (next.line - previous.line);165const previousEnd = previousTop + rect.height;166const betweenHeight = next.element.getBoundingClientRect().top - previousEnd;167scrollTo = previousEnd + betweenProgress * betweenHeight;168} else {169const progressInElement = line - Math.floor(line);170scrollTo = previousTop + (rect.height * progressInElement);171}172window.scroll(window.scrollX, Math.max(1, window.scrollY + scrollTo));173}174175export function getEditorLineNumberForPageOffset(offset: number, documentVersion: number): number | null {176const { previous, next } = getLineElementsAtPageOffset(offset, documentVersion);177if (previous) {178if (previous.line < 0) {179return 0;180}181const previousBounds = getElementBounds(previous);182const offsetFromPrevious = (offset - window.scrollY - previousBounds.top);183if (next) {184const progressBetweenElements = offsetFromPrevious / (getElementBounds(next).top - previousBounds.top);185return previous.line + progressBetweenElements * (next.line - previous.line);186} else {187const progressWithinElement = offsetFromPrevious / (previousBounds.height);188return previous.line + progressWithinElement;189}190}191return null;192}193194/**195* Try to find the html element by using a fragment id196*/197export function getLineElementForFragment(fragment: string, documentVersion: number): CodeLineElement | undefined {198return getCodeLineElements(documentVersion).find((element) => {199return element.element.id === fragment;200});201}202203function* getParentsWithTagName<T extends HTMLElement>(element: HTMLElement, tagName: string): Iterable<T> {204for (let parent = element.parentElement; parent; parent = parent.parentElement) {205if (parent.tagName === tagName) {206yield parent as T;207}208}209}210211212