Skip to content

Commit 399e153

Browse files
authored
Uses the 'format' metadata key for SQL cells (#5)
1 parent e13c243 commit 399e153

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

src/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { ISettingRegistry } from '@jupyterlab/settingregistry';
1010
import { runIcon } from '@jupyterlab/ui-components';
1111

1212
import { requestAPI } from './handler';
13-
import { SqlWidget } from './widget';
13+
import { METADATA_SQL_FORMAT, SqlWidget } from './widget';
1414

1515
/**
1616
* Initialization data for the @jupyter/sql-cell extension.
@@ -66,7 +66,10 @@ const plugin: JupyterFrontEndPlugin<void> = {
6666
if (!model) {
6767
return false;
6868
}
69-
return model.type === 'raw' && model.getMetadata('sql-cell');
69+
return (
70+
model.type === 'raw' &&
71+
model.getMetadata('format') === METADATA_SQL_FORMAT
72+
);
7073
}
7174
});
7275

src/widget.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { CommandRegistry } from '@lumino/commands';
1010
import { Signal } from '@lumino/signaling';
1111
import * as React from 'react';
1212

13+
export const METADATA_SQL_FORMAT = 'application/sql';
14+
1315
export class SqlWidget extends ReactWidget {
1416
/**
1517
* The constructor of the widget.
@@ -39,7 +41,11 @@ export class SqlWidget extends ReactWidget {
3941
if (!model || model.type !== 'raw') {
4042
return;
4143
}
42-
model.setMetadata('sql-cell', (event.target as HTMLInputElement).checked);
44+
if ((event.target as HTMLInputElement).checked) {
45+
model.setMetadata('format', METADATA_SQL_FORMAT);
46+
} else if (model.getMetadata('format') === METADATA_SQL_FORMAT) {
47+
model.deleteMetadata('format');
48+
}
4349
}
4450

4551
/**
@@ -83,16 +89,18 @@ export class SqlWidget extends ReactWidget {
8389
disabled={this._tracker.activeCell?.model.type !== 'raw'}
8490
aria-disabled={this._tracker.activeCell?.model.type !== 'raw'}
8591
onChange={event => this._switch(event)}
86-
checked={this._tracker.activeCell?.model.getMetadata(
87-
'sql-cell'
88-
)}
92+
checked={
93+
this._tracker.activeCell?.model.getMetadata('format') ===
94+
METADATA_SQL_FORMAT
95+
}
8996
/>
9097
<span className={'slider'}></span>
9198
</label>
9299
<ToolbarButtonComponent
93100
enabled={
94101
this._tracker.activeCell?.model.type === 'raw' &&
95-
this._tracker.activeCell?.model.getMetadata('sql-cell') === true
102+
this._tracker.activeCell?.model.getMetadata('format') ===
103+
METADATA_SQL_FORMAT
96104
}
97105
icon={runIcon}
98106
onClick={this._run.bind(this)}

0 commit comments

Comments
 (0)