Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add trusted indicator #6736

Merged
merged 6 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion packages/notebook-extension/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import { Poll } from '@lumino/polling';

import { Widget } from '@lumino/widgets';

import { TrustedComponent } from './trusted';

/**
* The class for kernel status errors.
*/
Expand Down Expand Up @@ -364,6 +366,35 @@ const notebookToolsWidget: JupyterFrontEndPlugin<void> = {
}
};

/**
* A plugin that adds a Trusted indicator to the menu area
*/
const trusted: JupyterFrontEndPlugin<void> = {
id: '@jupyter-notebook/notebook-extension:trusted',
autoStart: true,
requires: [INotebookShell, ITranslator],
activate: (
app: JupyterFrontEnd,
notebookShell: INotebookShell,
translator: ITranslator
): void => {
const onChange = async () => {
const current = notebookShell.currentWidget;
if (!(current instanceof NotebookPanel)) {
return;
}

const notebook = current.content;
await current.context.ready;

const widget = TrustedComponent.create({ notebook, translator });
notebookShell.add(widget, 'menu', { rank: 11_000 });
};

notebookShell.currentChanged.connect(onChange);
}
};

/**
* Export the plugins as default.
*/
Expand All @@ -372,7 +403,8 @@ const plugins: JupyterFrontEndPlugin<any>[] = [
kernelLogo,
kernelStatus,
scrollOutput,
notebookToolsWidget
notebookToolsWidget,
trusted
];

export default plugins;
107 changes: 107 additions & 0 deletions packages/notebook-extension/src/trusted.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { ReactWidget } from '@jupyterlab/apputils';

import { Notebook, NotebookActions } from '@jupyterlab/notebook';

import { ITranslator } from '@jupyterlab/translation';

import { toArray } from '@lumino/algorithm';

import React, { useEffect, useState } from 'react';

/**
* Check if a notebook is trusted
* @param notebook The notebook to check
* @returns true if the notebook is trusted, false otherwise
*/
const isTrusted = (notebook: Notebook): boolean => {
const model = notebook.model;
if (!model) {
return false;
}
const cells = toArray(model.cells);

const trusted = cells.reduce((accum, current) => {
if (current.trusted) {
return accum + 1;
} else {
return accum;
}
}, 0);

const total = cells.length;
return trusted === total;
};

/**
* A React component to display the Trusted badge in the menu bar.
* @param notebook The Notebook
* @param translator The Translation service
*/
const TrustedButton = ({
notebook,
translator
}: {
notebook: Notebook;
translator: ITranslator;
}): JSX.Element => {
const trans = translator.load('notebook');
const [trusted, setTrusted] = useState(isTrusted(notebook));

const checkTrust = () => {
const v = isTrusted(notebook);
setTrusted(v);
};

const trust = async () => {
await NotebookActions.trust(notebook, translator);
checkTrust();
};

useEffect(() => {
notebook.modelContentChanged.connect(checkTrust);
notebook.activeCellChanged.connect(checkTrust);
checkTrust();
return () => {
notebook.modelContentChanged.disconnect(checkTrust);
notebook.activeCellChanged.disconnect(checkTrust);
};
});

return (
<button
className={'jp-NotebookTrustedStatus'}
style={!trusted ? { cursor: 'pointer' } : { cursor: 'help' }}
onClick={() => !trusted && trust()}
title={
trusted
? trans.__('JavaScript enabled for notebook display')
: trans.__('JavaScript disabled for notebook display')
}
>
{trusted ? trans.__('Trusted') : trans.__('Not Trusted')}
</button>
);
};

/**
* A namespace for TrustedComponent statics.
*/
export namespace TrustedComponent {
/**
* Create a new TrustedComponent
*
* @param notebook The notebook
* @param translator The translator
*/
export const create = ({
notebook,
translator
}: {
notebook: Notebook;
translator: ITranslator;
}): ReactWidget => {
return ReactWidget.create(
<TrustedButton notebook={notebook} translator={translator} />
);
};
}
13 changes: 13 additions & 0 deletions packages/notebook-extension/style/base.css
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,19 @@ body[data-notebook='notebooks']
animation: 0.5s fade-out forwards;
}

.jp-NotebookTrustedStatus {
background: var(--jp-layout-color1);
color: var(--jp-ui-font-color1);
margin-top: 4px;
margin-bottom: 4px;
border: solid 1px var(--jp-border-color2);
cursor: help;
}

.jp-NotebookTrustedStatus-not-trusted {
cursor: pointer;
}

@keyframes fade-out {
0% {
opacity: 1;
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ui-tests/test/mobile.spec.ts-snapshots/notebook-chromium-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ui-tests/test/mobile.spec.ts-snapshots/notebook-firefox-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.