import { IDocumentWidget } from '@jupyterlab/docregistry'; import { nullTranslator, TranslationBundle } from '@jupyterlab/translation'; import React from 'react'; import { WidgetAdapter } from '../adapters/adapter'; import { VirtualDocument } from '../virtual/document'; export function getBreadcrumbs( document: VirtualDocument, adapter: WidgetAdapter, trans?: TranslationBundle, collapse = true ): JSX.Element[] { if (!trans) { trans = nullTranslator.load(''); } return document.ancestry.map((document: VirtualDocument) => { if (!document.parent) { let path = document.path; if ( !document.has_lsp_supported_file && document.file_extension && path.endsWith(document.file_extension) ) { path = path.slice(0, -document.file_extension.length - 1); } const full_path = path; if (collapse) { let parts = path.split('/'); if (parts.length > 2) { path = parts[0] + '/.../' + parts[parts.length - 1]; } } return ( {path} ); } if (!document.virtual_lines.size) { return Empty document; } try { if (adapter.has_multiple_editors) { let first_line = document.virtual_lines.get(0)!; let last_line = document.virtual_lines.get( document.last_virtual_line - 1 )!; let first_cell = adapter.get_editor_index(first_line.editor); let last_cell = adapter.get_editor_index(last_line.editor); let cell_locator = first_cell === last_cell ? trans!.__('cell %1', first_cell + 1) : trans!.__('cells: %1-%2', first_cell + 1, last_cell + 1); return ( {document.language} ({cell_locator}) ); } } catch (e) { console.warn('LSP: could not display document cell location', e); } return {document.language}; }); } /** * @deprecated please use getBreadcrumbs instead; `get_breadcrumbs` will be removed in 4.0 */ export function get_breadcrumbs( document: VirtualDocument, adapter: WidgetAdapter, collapse = true ) { return getBreadcrumbs(document, adapter, undefined, collapse); } export function focus_on(node: HTMLElement) { if (!node) { return; } node.scrollIntoView(); node.focus(); } export function DocumentLocator(props: { document: VirtualDocument; adapter: WidgetAdapter; trans?: TranslationBundle; }) { let { document, adapter } = props; let target: HTMLElement | null = null; if (adapter.has_multiple_editors) { let first_line = document.virtual_lines.get(0); if (first_line) { target = adapter.get_editor_wrapper(first_line.editor); } else { console.warn('Could not get first line of ', document); } } let breadcrumbs = getBreadcrumbs(document, adapter, props.trans); return (
(target ? focus_on(target) : null)} > {breadcrumbs}
); }