Skip to content

Commit

Permalink
Refactor breadcrumbs, introduces Breadcrumbs Service and Contribution
Browse files Browse the repository at this point in the history
Signed-off-by: Cornelius A. Ludmann <cornelius.ludmann@typefox.io>
  • Loading branch information
corneliusludmann committed Oct 19, 2019
1 parent e35e597 commit 03eb644
Show file tree
Hide file tree
Showing 28 changed files with 898 additions and 508 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,68 @@
********************************************************************************/

import * as React from 'react';
import { ReactRenderer } from '@theia/core/lib/browser';
import { ReactRenderer } from '../widgets';
import { injectable } from 'inversify';
import { Breadcrumbs } from './breadcrumbs';
import PerfectScrollbar from 'perfect-scrollbar';
import { BreadcrumbPopup } from './breadcrumb-popup';

export class BreadcrumbsListPopup extends ReactRenderer {
export const BreadcrumbPopupContainerRenderer = Symbol('BreadcrumbPopupContainerRenderer');
export interface BreadcrumbPopupContainerRenderer {
/**
* Renders the given breadcrumb and attaches it as child to the given parent element at the given anchor position.
*/
render(breadcrumbId: string, anchor: { x: number, y: number }, content: React.ReactNode, parentElement: HTMLElement): BreadcrumbPopup;
}

@injectable()
export class DefaultBreadcrumbPopupContainerRenderer implements BreadcrumbPopupContainerRenderer {
render(breadcrumbId: string, anchor: { x: number, y: number }, content: React.ReactNode, parentElement: HTMLElement): BreadcrumbPopup {
const renderer = new ReactBreadcrumbPopupContainerRenderer(breadcrumbId, anchor, content, parentElement);
renderer.render();
return renderer;
}
}

class ReactBreadcrumbPopupContainerRenderer extends ReactRenderer implements BreadcrumbPopup {

isOpen: boolean = false;

protected scrollbar: PerfectScrollbar | undefined;

constructor(
protected readonly items: { label: string, title: string, iconClass: string, action: () => void }[],
protected readonly anchor: { x: number, y: number },
host: HTMLElement
) {
super(host);
}
readonly breadcrumbId: string,
readonly anchor: { x: number, y: number },
readonly content: React.ReactNode,
host: HTMLElement,
) { super(host); }

protected doRender(): React.ReactNode {
return <div className={Breadcrumbs.Styles.BREADCRUMB_POPUP}
style={{ left: `${this.anchor.x}px`, top: `${this.anchor.y}px` }}
onBlur={_ => this.dispose()}
onBlur={this.onBlur}
tabIndex={0}
>
<ul>
{this.items.map((item, index) => <li key={index} title={item.title} onClick={_ => item.action()}>
<span className={item.iconClass}></span> <span>{item.label}</span>
</li>)}
</ul>
{this.content}
</div >;
}

protected onBlur = (event: React.FocusEvent) => {
if (event.relatedTarget && event.relatedTarget instanceof HTMLElement) {
// event.relatedTarget is the element that has the focus after this popup looses the focus.
// If a breadcrumb was clicked the following holds the breadcrumb ID of the clicked breadcrumb.
const breadcrumbId = event.relatedTarget.getAttribute('data-breadcrumb-id');
if (breadcrumbId && breadcrumbId === this.breadcrumbId) {
// This is a click on the breadcrumb that has openend this popup.
// We do not close this popup here but let the click event of the breadcrumb handle this instead
// because it needs to know that this popup is open to decide if it just closes this popup or
// also open a new popup.
return;
}
}
this.dispose();
}

render(): void {
super.render();
if (!this.scrollbar) {
Expand All @@ -61,6 +93,7 @@ export class BreadcrumbsListPopup extends ReactRenderer {
}
this.focus();
document.addEventListener('keyup', this.escFunction);
this.isOpen = true;
}

focus(): boolean {
Expand All @@ -78,6 +111,7 @@ export class BreadcrumbsListPopup extends ReactRenderer {
this.scrollbar = undefined;
}
document.removeEventListener('keyup', this.escFunction);
this.isOpen = false;
}

protected escFunction = (event: KeyboardEvent) => {
Expand Down
24 changes: 24 additions & 0 deletions packages/core/src/browser/breadcrumbs/breadcrumb-popup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/********************************************************************************
* Copyright (C) 2019 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { Disposable } from '../../common/disposable';

export interface BreadcrumbPopup extends Disposable {

breadcrumbId: string

isOpen: boolean
}
42 changes: 42 additions & 0 deletions packages/core/src/browser/breadcrumbs/breadcrumb-renderer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/********************************************************************************
* Copyright (C) 2019 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import * as React from 'react';
import { injectable } from 'inversify';
import { Breadcrumb } from './breadcrumb';
import { Breadcrumbs } from './breadcrumbs';

export const BreadcrumbRenderer = Symbol('BreadcrumbRenderer');
export interface BreadcrumbRenderer {
/**
* Renders the given breadcrumb. If `onClick` is given, it is called on breadcrumb click.
*/
render(breadcrumb: Breadcrumb, onClick?: (breadcrumb: Breadcrumb, event: React.MouseEvent) => void): React.ReactNode;
}

@injectable()
export class DefaultBreadcrumbRenderer implements BreadcrumbRenderer {
render(breadcrumb: Breadcrumb, onClick?: (breadcrumb: Breadcrumb, event: React.MouseEvent) => void): React.ReactNode {
return <li key={breadcrumb.id} title={breadcrumb.longLabel}
className={Breadcrumbs.Styles.BREADCRUMB_ITEM + (!onClick ? '' : ' ' + Breadcrumbs.Styles.BREADCRUMB_ITEM_HAS_POPUP)}
onClick={event => onClick && onClick(breadcrumb, event)}
tabIndex={0}
data-breadcrumb-id={breadcrumb.id}
>
{breadcrumb.iconClass && <span className={breadcrumb.iconClass}></span>} <span> {breadcrumb.label}</span>
</li >;
}
}
34 changes: 34 additions & 0 deletions packages/core/src/browser/breadcrumbs/breadcrumb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/********************************************************************************
* Copyright (C) 2019 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

/** A single breadcrumb in the breadcrumbs bar. */
export interface Breadcrumb {

/** An ID of this breadcrumb that should be unique in the breadcrumbs bar. */
id: string

/** The breadcrumb type. Should be the same as the contribution type `BreadcrumbsContribution#type`. */
type: symbol

/** The text that will be rendered as label. */
label: string

/** A longer text that will be used as tooltip text. */
longLabel: string

/** A CSS class for the icon. */
iconClass?: string
}
44 changes: 44 additions & 0 deletions packages/core/src/browser/breadcrumbs/breadcrumbs-contribution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/********************************************************************************
* Copyright (C) 2019 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import URI from '../../common/uri';
import { Breadcrumb } from './breadcrumb';
import { BreadcrumbPopup } from './breadcrumb-popup';

export const BreadcrumbsContribution = Symbol('BreadcrumbsContribution');
export interface BreadcrumbsContribution {

/**
* The breadcrumb type. Breadcrumbs returned by `#computeBreadcrumbs(uri)` should have this as `Breadcrumb#type`.
*/
type: symbol;

/**
* The priority of this breadcrumbs contribution. Contributions with lower priority are rendered first.
*/
priority: number;

/**
* Computes breadcrumbs for a given URI.
*/
computeBreadcrumbs(uri: URI): Promise<Breadcrumb[]>;

/**
* Opens the breadcrumb popup for the given breadcrumb at the given position.
* Parent is used as host element.
*/
openPopup(breadcrumb: Breadcrumb, position: { x: number, y: number }, parent: HTMLElement): Promise<BreadcrumbPopup | undefined>;
}
Loading

0 comments on commit 03eb644

Please sign in to comment.