Skip to content

Commit

Permalink
move slide type out to notebook plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
bollwyvl committed Jan 10, 2023
1 parent 643907f commit e81055f
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 4 deletions.
89 changes: 85 additions & 4 deletions js/jupyterlab-deck/src/notebook/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
import { JupyterFrontEnd, JupyterFrontEndPlugin } from '@jupyterlab/application';
import { INotebookTools } from '@jupyterlab/notebook';
import { ICommandPalette, IPaletteItem } from '@jupyterlab/apputils';
import { INotebookTools, INotebookTracker } from '@jupyterlab/notebook';
import { LabIcon } from '@jupyterlab/ui-components';

import { NS, IDeckManager } from '../tokens';
import { ICONS } from '../icons';
import {
NS,
IDeckManager,
CommandIds,
CATEGORY,
META,
TSlideType,
SLIDE_TYPES,
} from '../tokens';

import { NotebookDeckExtension } from './extension';
import { NotebookPresenter } from './presenter';

export const notebookPlugin: JupyterFrontEndPlugin<void> = {
id: `${NS}:notebooks`,
requires: [INotebookTools, IDeckManager],
requires: [INotebookTracker, INotebookTools, IDeckManager],
optional: [ICommandPalette],
autoStart: true,
activate: (
app: JupyterFrontEnd,
notebooks: INotebookTracker,
notebookTools: INotebookTools,
decks: IDeckManager
decks: IDeckManager,
palette?: ICommandPalette
) => {
const { commands } = app;
const { __ } = decks;
const presenter = new NotebookPresenter({
manager: decks,
notebookTools,
Expand All @@ -27,5 +42,71 @@ export const notebookPlugin: JupyterFrontEndPlugin<void> = {
'Notebook',
new NotebookDeckExtension({ commands, presenter })
);

commands.addCommand(CommandIds.setSlideType, {
label: (args: any) => __(`Slide Type: ${args.slideType}`),
isVisible: () => !!notebooks.activeCell,
icon: (args: any): LabIcon => {
const slideType = args.slideType || 'null';
return ICONS.slideshow[slideType] || ICONS.deckStart;
},
execute: (args: any) => {
const { activeCell } = notebooks;
if (!activeCell) {
return;
}

const slideType = args.slideType || null;

let cells = [activeCell];

const { currentWidget } = notebooks;

if (currentWidget) {
const selection = notebooks.currentWidget?.content.getContiguousSelection();

if (selection && selection.head != null && selection.anchor != null) {
cells = currentWidget.content.widgets.slice(
selection.anchor,
selection.head + 1
);
}
}

for (const cell of cells) {
let meta = {
...((cell.model.metadata.get(META.slideshow) as Record<string, any>) || {}),
};

if (slideType == null || slideType == 'null') {
delete meta[META.slideType];
} else {
meta[META.slideType] = slideType;
}

if (!Object.keys(meta)) {
cell.model.metadata.delete(META.slideshow);
} else {
cell.model.metadata.set(META.slideshow, meta);
}
}
},
});

if (palette) {
const category = __(CATEGORY);
const command = CommandIds.setSlideType;
const isPalette = true;
function makeSlideTypeItem(slideType: TSlideType): IPaletteItem {
return {
category,
command,
args: { slideType, isPalette },
};
}
for (const slideType of SLIDE_TYPES) {
palette.addItem(makeSlideTypeItem(slideType));
}
}
},
};
2 changes: 2 additions & 0 deletions js/jupyterlab-deck/src/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ export namespace CommandIds {
/* layover */
export const showLayover = 'deck:show-layover';
export const hideLayover = 'deck:hide-layover';
/* notebook */
export const setSlideType = 'deck:set-slide-type';
}

export namespace META {
Expand Down

0 comments on commit e81055f

Please sign in to comment.