Skip to content

Commit

Permalink
Add user settings
Browse files Browse the repository at this point in the history
  • Loading branch information
mengyanw committed Dec 14, 2023
1 parent 53428b6 commit ca64202
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 27 deletions.
21 changes: 20 additions & 1 deletion schema/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,25 @@
"title": "jupyterlab-hints",
"description": "jupyterlab-hints settings.",
"type": "object",
"properties": {},
"properties": {
"text1": {
"type": "string",
"title": "Text 1",
"description": "Text displayed before click hint",
"default": "Click to reveal a hint"
},
"text2": {
"type": "string",
"title": "Text 2",
"description": "Text displayed confirming click hint",
"default": "Are you sure you want to reveal the hint?"
},
"blurry-level": {
"type": "number",
"title": "Blurry Level",
"description": "The blurry level of the hint",
"default": 3
}
},
"additionalProperties": false
}
35 changes: 12 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,22 @@ const plugin: JupyterFrontEndPlugin<void> = {
console.log('JupyterLab extension jupyterlab-hints is activated!');

if (settingRegistry) {
settingRegistry
.load(plugin.id)
.then(settings => {
console.log('jupyterlab-hints settings loaded:', settings.composite);
})
.catch(reason => {
console.error(
'Failed to load settings for jupyterlab-hints.',
reason
);
});
}

notebookTracker.widgetAdded.connect(
async (_, notebookPanel: NotebookPanel) => {
await notebookPanel.revealed;
const setting = await settingRegistry.load(plugin.id);
notebookTracker.widgetAdded.connect(
async (_, notebookPanel: NotebookPanel) => {
await notebookPanel.revealed;

if (notebookPanel.content.model) {
const cells = notebookPanel.content.model?.cells;
for (let i = 0; i < cells.length; i++) {
if (cells.get(i).metadata.hint) {
addHintOverlay(i, notebookPanel);
if (notebookPanel.content.model) {
const cells = notebookPanel.content.model?.cells;
for (let i = 0; i < cells.length; i++) {
if (cells.get(i).metadata.hint) {
addHintOverlay(i, notebookPanel, setting);
}
}
}
}
}
);
);
}
}
};

Expand Down
18 changes: 15 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { NotebookPanel } from '@jupyterlab/notebook';
import { ISettingRegistry } from '@jupyterlab/settingregistry';

export const addHintOverlay = (i: number, notebookPanel: NotebookPanel) => {
export const addHintOverlay = (
i: number,
notebookPanel: NotebookPanel,
settings: ISettingRegistry.ISettings
) => {
const cell = notebookPanel.content.model?.cells.get(i);
const cellWidget = notebookPanel.content.widgets.find(
widget => widget.model === cell
Expand All @@ -9,13 +14,20 @@ export const addHintOverlay = (i: number, notebookPanel: NotebookPanel) => {
// Create hint blur overlay and append to hint cell node
const overlay = document.createElement('div');
overlay.classList.add('hint-overlay');
overlay.innerText = 'Click to reveal hint';
cellWidget.node.appendChild(overlay);

// Create modal to confirm if user wants to reveal hint
const modal = document.createElement('div');
modal.classList.add('hint-modal');
modal.innerText = 'Are you sure you want to reveal the hint?';

const updateSettings = (): void => {
overlay.innerText = settings.get('text1').composite as string;
modal.innerText = settings.get('text2').composite as string;
const blurryLevel = settings.get('blurry-level').composite as number;
overlay.style.backdropFilter = `blur(${blurryLevel}px)`;
};
updateSettings();
settings.changed.connect(updateSettings);

// Remove overlay text and append modal when user clicks on overlay
overlay.addEventListener('click', () => {
Expand Down

0 comments on commit ca64202

Please sign in to comment.