15
15
* See the License for the specific language governing permissions a* limitations under the License.
16
16
*/
17
17
18
- import { Clipboard , Dialog , showDialog } from '@jupyterlab/apputils' ;
18
+ import {
19
+ Clipboard ,
20
+ Dialog ,
21
+ InputDialog ,
22
+ showDialog ,
23
+ } from '@jupyterlab/apputils' ;
19
24
import { PathExt } from '@jupyterlab/coreutils' ;
20
25
import { DocumentWidget } from '@jupyterlab/docregistry' ;
21
26
import { FileEditor } from '@jupyterlab/fileeditor' ;
@@ -50,10 +55,9 @@ import { CodeSnippetService, ICodeSnippet } from './CodeSnippetService';
50
55
import { FilterTools } from './FilterTools' ;
51
56
import { showPreview } from './PreviewSnippet' ;
52
57
import { showMoreOptions } from './CodeSnippetMenu' ;
53
- // import {
54
- // ICodeSnippet,
55
- // CodeSnippetContentsService
56
- // } from './CodeSnippetContentsService';
58
+ // import { showCodeSnippetForm, CodeSnippetForm } from './CodeSnippetForm';
59
+
60
+ import { CodeSnippetContentsService } from './CodeSnippetContentsService' ;
57
61
58
62
import moreSVGstr from '../style/icon/jupyter_moreicon.svg' ;
59
63
import {
@@ -96,6 +100,7 @@ import {
96
100
sasIcon ,
97
101
} from './CodeSnippetLanguages' ;
98
102
import { ICodeSnippetEditorMetadata } from './CodeSnippetEditor' ;
103
+ // import { CodeSnippetContentsService } from './CodeSnippetContentsService';
99
104
100
105
/**
101
106
* The CSS class added to code snippet widget.
@@ -121,6 +126,8 @@ const CODE_SNIPPET_MORE_OTPIONS_COPY = 'jp-codeSnippet-more-options-copy';
121
126
const CODE_SNIPPET_MORE_OTPIONS_INSERT = 'jp-codeSnippet-more-options-insert' ;
122
127
const CODE_SNIPPET_MORE_OTPIONS_EDIT = 'jp-codeSnippet-more-options-edit' ;
123
128
const CODE_SNIPPET_MORE_OTPIONS_DELETE = 'jp-codeSnippet-more-options-delete' ;
129
+ const CODE_SNIPPET_MORE_OTPIONS_DOWNLOAD =
130
+ 'jp-codeSnippet-more-options-download' ;
124
131
const CODE_SNIPPET_CREATE_NEW_BTN = 'jp-createSnippetBtn' ;
125
132
const CODE_SNIPPET_NAME = 'jp-codeSnippet-name' ;
126
133
@@ -195,7 +202,7 @@ export class CodeSnippetDisplay extends React.Component<
195
202
this . handleRenameSnippet = this . handleRenameSnippet . bind ( this ) ;
196
203
}
197
204
198
- // Handle code snippet insert into an editor
205
+ // Handle code snippet insert into a notebook or document
199
206
private insertCodeSnippet = async ( snippet : ICodeSnippet ) : Promise < void > => {
200
207
const widget : Widget = this . props . getCurrentWidget ( ) ;
201
208
const snippetStr : string = snippet . code . join ( '\n' ) ;
@@ -1415,6 +1422,38 @@ export class CodeSnippetDisplay extends React.Component<
1415
1422
} ) ;
1416
1423
}
1417
1424
1425
+ private downloadCommand ( codeSnippet : ICodeSnippet ) : void {
1426
+ // Request a text
1427
+ InputDialog . getText ( {
1428
+ title : 'Download Snippet?' ,
1429
+ label : 'Directory to Download: ' ,
1430
+ placeholder : 'share/snippet' ,
1431
+ okLabel : 'Download' ,
1432
+ } ) . then ( ( value : Dialog . IResult < string > ) => {
1433
+ if ( value . button . accept ) {
1434
+ const dirs = value . value . split ( '/' ) ;
1435
+
1436
+ const codeSnippetDownloader = CodeSnippetContentsService . getInstance ( ) ;
1437
+
1438
+ let path = '' ;
1439
+ for ( let i = 0 ; i < dirs . length ; i ++ ) {
1440
+ path += dirs [ i ] + '/' ;
1441
+ codeSnippetDownloader . save ( path , { type : 'directory' } ) . catch ( ( _ ) => {
1442
+ alert ( 'Path should be a relative path' ) ;
1443
+ } ) ;
1444
+ }
1445
+
1446
+ path += codeSnippet . name + '.json' ;
1447
+
1448
+ codeSnippetDownloader . save ( path , {
1449
+ type : 'file' ,
1450
+ format : 'text' ,
1451
+ content : JSON . stringify ( codeSnippet ) ,
1452
+ } ) ;
1453
+ }
1454
+ } ) ;
1455
+ }
1456
+
1418
1457
// remove dropdown menu
1419
1458
private removeOptionsNode ( ) : void {
1420
1459
const temp = document . getElementsByClassName ( CODE_SNIPPET_MORE_OPTIONS ) [ 0 ] ;
@@ -1468,9 +1507,19 @@ export class CodeSnippetDisplay extends React.Component<
1468
1507
this . deleteCommand ( codeSnippet ) ;
1469
1508
this . removeOptionsNode ( ) ;
1470
1509
} ;
1510
+
1511
+ const downloadSnip = document . createElement ( 'div' ) ;
1512
+ downloadSnip . className = CODE_SNIPPET_MORE_OTPIONS_DOWNLOAD ;
1513
+ downloadSnip . textContent = 'Download snippet' ;
1514
+ downloadSnip . onclick = ( ) : void => {
1515
+ this . downloadCommand ( codeSnippet ) ;
1516
+ this . removeOptionsNode ( ) ;
1517
+ } ;
1518
+
1471
1519
optionsContainer . appendChild ( insertSnip ) ;
1472
1520
optionsContainer . appendChild ( copySnip ) ;
1473
1521
optionsContainer . appendChild ( editSnip ) ;
1522
+ optionsContainer . appendChild ( downloadSnip ) ;
1474
1523
optionsContainer . appendChild ( deleteSnip ) ;
1475
1524
body . append ( optionsContainer ) ;
1476
1525
return body ;
0 commit comments