Skip to content
This repository has been archived by the owner on Jul 30, 2023. It is now read-only.

Commit

Permalink
Check selected format
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Feb 10, 2019
1 parent e99ed87 commit 12a8a64
Showing 1 changed file with 50 additions and 4 deletions.
54 changes: 50 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import {

import '../style/index.css';

interface JupytextSection {
formats?: string;
}

const JUPYTEXT_FORMATS = [
{
'formats': 'ipynb,auto:light',
Expand All @@ -34,7 +38,11 @@ const JUPYTEXT_FORMATS = [
'label': 'Pair Notebook with R Markdown'
},
{
'formats': 'unpair',
'formats': 'custom',
'label': 'Custom pairing'
},
{
'formats': 'none',
'label': 'Unpair Notebook'
}
]
Expand All @@ -55,11 +63,49 @@ const extension: JupyterLabPlugin<void> = {
const command: string = 'jupytext:' + formats
app.commands.addCommand(command, {
label: args['label'],
execute: args => {
isToggled: () => {
if (!notebook_tracker.currentWidget)
return false;

if (!notebook_tracker.currentWidget.context.model.metadata.has('jupytext'))
return formats == 'none';

const jupytext: JupytextSection = notebook_tracker.currentWidget.context.model.metadata.get('jupytext') as unknown as JupytextSection;
if (!jupytext.formats)
return formats == 'none'

if (formats == 'custom')
return (jupytext.formats && ['ipynb,auto:light', 'ipynb,auto:percent', 'ipynb,auto:hydrogen', 'ipynb,md', 'ipynb,Rmd'].indexOf(jupytext.formats) < 0)

return jupytext.formats == formats
},
execute: () => {
console.log('Jupytext: executing command=' + command)
if (formats == 'custom') {
alert("Please edit the notebook metadata directly if you wish a custom configuration.")
return
}

const jupytext: JupytextSection = notebook_tracker.currentWidget.context.model.metadata.get('jupytext') as unknown as JupytextSection;

if (formats == 'none') {
if (!notebook_tracker.currentWidget.context.model.metadata.has('jupytext'))
return

if (jupytext.formats) {
delete jupytext.formats
}

// TODO: This is always true! How can we check that the JupytextSection is really empty, ie. no other
// field like 'comment_magics', 'split_at_heading', etc...
if (jupytext == {})
notebook_tracker.currentWidget.context.model.metadata.delete('jupytext')
return
}

if (formats == 'unpair')
notebook_tracker.currentWidget.context.model.metadata.set('jupytext', {})
// set desired format
if (jupytext)
jupytext.formats = formats
else
notebook_tracker.currentWidget.context.model.metadata.set('jupytext', { formats })
}
Expand Down

0 comments on commit 12a8a64

Please sign in to comment.