UNPKG

2.28 kBPlain TextView Raw
1/*
2 * Copyright (c) Jupyter Development Team.
3 * Distributed under the terms of the Modified BSD License.
4 */
5
6import { Widget } from '@lumino/widgets';
7import { Message } from '@lumino/messaging';
8import { Notebook } from './widget';
9import { NotebookActions } from './actions';
10
11const NOTEBOOK_FOOTER_CLASS = 'jp-Notebook-footer';
12
13/**
14 * A footer widget added after the last cell of the notebook.
15 */
16export class NotebookFooter extends Widget {
17 /**
18 * Construct a footer widget.
19 */
20 constructor(protected notebook: Notebook) {
21 super({ node: document.createElement('button') });
22 const trans = notebook.translator.load('jupyterlab');
23 this.addClass(NOTEBOOK_FOOTER_CLASS);
24 this.node.setAttribute('tabindex', '-1');
25 this.node.innerText = trans.__('Click to add a cell.');
26 }
27
28 /**
29 * Handle incoming events.
30 */
31 handleEvent(event: Event): void {
32 switch (event.type) {
33 case 'click':
34 this.onClick();
35 break;
36 case 'keydown':
37 if ((event as KeyboardEvent).key === 'ArrowUp') {
38 this.onArrowUp();
39 break;
40 }
41 }
42 }
43
44 /**
45 * On single click (mouse event), insert a cell below (at the end of the notebook as default behavior).
46 */
47 protected onClick(): void {
48 if (this.notebook.widgets.length > 0) {
49 this.notebook.activeCellIndex = this.notebook.widgets.length - 1;
50 }
51 NotebookActions.insertBelow(this.notebook);
52 // Focus on the created cell.
53 void NotebookActions.focusActiveCell(this.notebook);
54 }
55
56 /**
57 * On arrow up key pressed (keydown keyboard event).
58 * @deprecated To be removed in v5, this is a no-op
59 */
60 protected onArrowUp(): void {
61 // The specific behavior has been removed in https://github.com/jupyterlab/jupyterlab/pull/14796
62 }
63
64 /*
65 * Handle `after-detach` messages for the widget.
66 */
67 protected onAfterAttach(msg: Message): void {
68 super.onAfterAttach(msg);
69 this.node.addEventListener('click', this);
70 this.node.addEventListener('keydown', this);
71 }
72
73 /**
74 * Handle `before-detach` messages for the widget.
75 */
76 protected onBeforeDetach(msg: Message): void {
77 this.node.removeEventListener('click', this);
78 this.node.removeEventListener('keydown', this);
79 super.onBeforeDetach(msg);
80 }
81}