Fix #5238 Notebooks should support relative links (#6289)

* Fix #5238 Notebooks should support relative links
- Added detection of relative #links inside notebooks
- Added handling of these, at least for current notebook

Not handled: open other notebook & scroll to position.
This commit is contained in:
Kevin Cunnane
2019-07-09 14:30:57 -07:00
committed by GitHub
parent 930731423d
commit aef74c6d5a
10 changed files with 101 additions and 7 deletions

View File

@@ -24,7 +24,7 @@ import { AngularDisposable } from 'sql/base/node/lifecycle';
import { CellTypes, CellType } from 'sql/workbench/parts/notebook/models/contracts';
import { ICellModel, IModelFactory, INotebookModel, NotebookContentChange } from 'sql/workbench/parts/notebook/models/modelInterfaces';
import { IConnectionManagementService } from 'sql/platform/connection/common/connectionManagement';
import { INotebookService, INotebookParams, INotebookManager, INotebookEditor, DEFAULT_NOTEBOOK_PROVIDER, SQL_NOTEBOOK_PROVIDER } from 'sql/workbench/services/notebook/common/notebookService';
import { INotebookService, INotebookParams, INotebookManager, INotebookEditor, INotebookSection, DEFAULT_NOTEBOOK_PROVIDER, SQL_NOTEBOOK_PROVIDER } from 'sql/workbench/services/notebook/common/notebookService';
import { IBootstrapParams } from 'sql/platform/bootstrap/node/bootstrapService';
import { NotebookModel } from 'sql/workbench/parts/notebook/models/notebookModel';
import { ModelFactory } from 'sql/workbench/parts/notebook/models/modelFactory';
@@ -582,4 +582,48 @@ export class NotebookComponent extends AngularDisposable implements OnInit, OnDe
}
}
getSections(): INotebookSection[] {
return this.getSectionElements();
}
private getSectionElements(): NotebookSection[] {
let headers: NotebookSection[] = [];
let el: HTMLElement = this.container.nativeElement;
let headerElements = el.querySelectorAll('h1, h2, h3, h4, h5, h6');
for (let i = 0; i < headerElements.length; i++) {
let headerEl = headerElements[i] as HTMLElement;
if (headerEl['id']) {
headers.push(new NotebookSection(headerEl));
}
}
return headers;
}
navigateToSection(id: string): void {
id = id.toLowerCase();
let section = this.getSectionElements().find(s => s.relativeUri && s.relativeUri.toLowerCase() === id);
if (section) {
// Scroll this section to the top of the header instead of just bringing header into view.
let scrollTop = jQuery(section.headerEl).offset().top;
(<HTMLElement>this.container.nativeElement).scrollTo({
top: scrollTop,
behavior: 'smooth'
});
section.headerEl.focus();
}
}
}
class NotebookSection implements INotebookSection {
constructor(public headerEl: HTMLElement) {
}
get relativeUri(): string {
return this.headerEl['id'];
}
get header(): string {
return this.headerEl.textContent;
}
}