Skip to content

Commit 8a11d12

Browse files
committed
Improve Snippets - add run menu for snippets, remove apply/cancel buttons for snippets page
1 parent 8065991 commit 8a11d12

File tree

3 files changed

+59
-16
lines changed

3 files changed

+59
-16
lines changed

resource/snippets/run.svg

Lines changed: 6 additions & 0 deletions
Loading

src/common/vpPopupPage.js

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,15 @@ define([
6767
page.appendLine('</div>'); // body end
6868

6969
// button box
70-
page.appendFormatLine('<div class="{0}">', VP_PP_BUTTON_BOX);
71-
page.appendFormatLine('<button type="button" class="{0}">{1}</button>'
72-
, VP_PP_BUTTON_CANCEL, 'Cancel');
73-
page.appendFormatLine('<button type="button" class="{0}">{1}</button>'
74-
, VP_PP_BUTTON_APPLY, 'Apply');
75-
page.appendLine('</div>');
70+
// Snippets menu don't use buttons
71+
if (title != 'Snippets') {
72+
page.appendFormatLine('<div class="{0}">', VP_PP_BUTTON_BOX);
73+
page.appendFormatLine('<button type="button" class="{0}">{1}</button>'
74+
, VP_PP_BUTTON_CANCEL, 'Cancel');
75+
page.appendFormatLine('<button type="button" class="{0}">{1}</button>'
76+
, VP_PP_BUTTON_APPLY, 'Apply');
77+
page.appendLine('</div>');
78+
}
7679

7780
page.appendLine('</div>'); // container end
7881
page.appendLine('</div>'); // VP_PP end
@@ -100,6 +103,18 @@ define([
100103
$(this.wrapSelector()).remove();
101104
}
102105

106+
PopupPage.prototype.apply = function() {
107+
if (this.pageThis) {
108+
var code = this.pageThis.generateCode(false, false);
109+
$(vpCommon.wrapSelector('#' + this.targetId)).val(code);
110+
$(vpCommon.wrapSelector('#' + this.targetId)).trigger({
111+
type: 'popup_apply',
112+
title: this.config.title,
113+
code: code
114+
});
115+
}
116+
}
117+
103118
PopupPage.prototype.bindEvent = function() {
104119
var that = this;
105120

@@ -115,15 +130,7 @@ define([
115130

116131
// click apply
117132
$(document).on('click', this.wrapSelector('.' + VP_PP_BUTTON_APPLY), function() {
118-
if (that.pageThis) {
119-
var code = that.pageThis.generateCode(false, false);
120-
$(vpCommon.wrapSelector('#' + that.targetId)).val(code);
121-
$(vpCommon.wrapSelector('#' + that.targetId)).trigger({
122-
type: 'popup_apply',
123-
title: that.config.title,
124-
code: code
125-
});
126-
}
133+
that.apply();
127134
that.close();
128135
});
129136
}

src/file_io/udf.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,20 @@ define([
379379
$(document).on('click', this.wrapSelector('.vp-sn-item-menu-item'), function(evt) {
380380
var menu = $(this).data('menu');
381381
var title = $(this).closest('.vp-sn-item').data('title');
382-
if (menu == 'duplicate') {
382+
if (menu == 'run') {
383+
var item = $(this).closest('.vp-sn-item');
384+
var title = $(item).data('title');
385+
386+
// get codemirror
387+
that.codemirrorList[title].save();
388+
var code = that.codemirrorList[title].getValue();
389+
$(vpCommon.wrapSelector('#vp_appsCode')).val(code);
390+
$(vpCommon.wrapSelector('#vp_appsCode')).trigger({
391+
type: 'popup_apply',
392+
title: 'Snippets',
393+
code: code
394+
});
395+
} else if (menu == 'duplicate') {
383396
var dupNo = 1;
384397
var timestamp = new Date().getTime();
385398
var dupTitle = title + '_dup' + dupNo;
@@ -561,6 +574,10 @@ define([
561574
item.appendFormatLine('<i class="{0}"></i>', 'fa fa-circle vp-sn-imported-item');
562575
}
563576
item.appendFormatLine('<div class="{0}">', 'vp-sn-item-menu');
577+
item.appendFormatLine('<div class="{0}" data-menu="{1}" title="{2}">'
578+
, 'vp-sn-item-menu-item', 'run', 'Run');
579+
item.appendFormatLine('<img src="{0}"/>', '/nbextensions/visualpython/resource/snippets/run.svg');
580+
item.appendLine('</div>');
564581
item.appendFormatLine('<div class="{0}" data-menu="{1}" title="{2}">'
565582
, 'vp-sn-item-menu-item', 'duplicate', 'Duplicate');
566583
item.appendFormatLine('<img src="{0}"/>', '/nbextensions/visualpython/resource/snippets/duplicate.svg');
@@ -662,6 +679,19 @@ define([
662679
// save codemirror value to origin textarea
663680
// this.vp_userCode.save();
664681

682+
// selected snippet
683+
var selected = $(this.wrapSelector('.vp-sn-item-header.selected'));
684+
if (selected) {
685+
var item = $(selected).closest('.vp-sn-item');
686+
var title = $(item).data('title');
687+
688+
// get codemirror
689+
this.codemirrorList[title].save();
690+
var code = this.codemirrorList[title].getValue();
691+
sbCode.append(code);
692+
}
693+
694+
665695
if (addCell) this.cellExecute(sbCode.toString(), exec);
666696

667697
return sbCode.toString();

0 commit comments

Comments
 (0)