Skip to content

Commit 90797d6

Browse files
authored
Merge pull request #134 from kpinnipa/cell-saving
Cell saving and Snippet Preview
2 parents fbeb2dd + c3a9066 commit 90797d6

11 files changed

+48
-20
lines changed

.DS_Store

2 KB
Binary file not shown.

schema/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{
44
"command": "codeSnippet:save-as-snippet",
55
"keys": ["Accel Shift A"],
6-
"selector": ".jp-Cell"
6+
"selector": ".jp-Notebook"
77
},
88
{
99
"command": "codeSnippet:save-as-snippet",

snippets/gdp_calculator.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"name":"gdp_calculator","description":"Calculate total GDP, in trillions of dollars, by region, over time","language":"R","code":["gdp_regions <- nations %>%"," mutate(gdp = gdp_percap * population,"," gdp_tn = gdp/1000000000000) %>%"," group_by(region, year) %>%"," summarize(total_gdp_tn = sum(gdp_tn, na.rm = TRUE))",""],"id":11,"tags":["Countries Project"]}
1+
{"name":"gdp_calculator","description":"Calculate total GDP, in trillions of dollars, by region, over time","language":"R","code":["gdp_regions <- nations %>%"," mutate(gdp = gdp_percap * population,"," gdp_tn = gdp/1000000000000) %>%"," group_by(region, year) %>%"," summarize(total_gdp_tn = sum(gdp_tn, na.rm = TRUE))",""],"id":10,"tags":["Countries Project"]}

snippets/generate_hundred.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"name":"generate_hundred","description":"Scala program to print numbers from 1 to 100 using for loop with until to determine loop range.","language":"Scala","code":["object ExampleForLoop2 {"," def main(args: Array[String]) {"," var counter: Int=0;"," "," for(counter <- 1 until 101)"," print(counter + \" \");"," "," // to print new line"," println();"," }","}"],"id":13,"tags":["math"]}
1+
{"name":"generate_hundred","description":"Scala program to print numbers from 1 to 100 using for loop with until to determine loop range.","language":"Scala","code":["object ExampleForLoop2 {"," def main(args: Array[String]) {"," var counter: Int=0;"," "," for(counter <- 1 until 101)"," print(counter + \" \");"," "," // to print new line"," println();"," }","}"],"id":12,"tags":["math"]}

snippets/life_exp_eur_asia.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"name":"life_exp_eur_asia","description":"Find countries in North America or Europe and Central Asia with a life expectancy in 2016 of 75 to 80.","language":"R","code":["eur_na_75_80 <- longevity %>%"," filter(life_expect > 75 & life_expect < 80 & (region == \"Europe & Central Asia\" | region == \"North America\")) %>%"," arrange(desc(life_expect))"],"id":9,"tags":["Countries Project"]}
1+
{"name":"life_exp_eur_asia","description":"Find countries in North America or Europe and Central Asia with a life expectancy in 2016 of 75 to 80.","language":"R","code":["eur_na_75_80 <- longevity %>%"," filter(life_expect > 75 & life_expect < 80 & (region == \"Europe & Central Asia\" | region == \"North America\")) %>%"," arrange(desc(life_expect))"],"id":8,"tags":["Countries Project"]}

snippets/parallel_strings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"name":"parallel_strings","description":"A simple queue function to generate four random strings in parallel.","language":"Python","code":["import multiprocessing as mp","import random","import string","","random.seed(123)","","# Define an output queue","output = mp.Queue()","","# define a example function","def rand_string(length, output):"," \"\"\" Generates a random string of numbers, lower- and uppercase chars. \"\"\""," rand_str = ''.join(random.choice("," string.ascii_lowercase "," + string.ascii_uppercase "," + string.digits)"," for i in range(length))"," output.put(rand_str)","","# Setup a list of processes that we want to run","processes = [mp.Process(target=rand_string, args=(5, output)) for x in range(4)]","","# Run processes","for p in processes:"," p.start()","","# Exit the completed processes","for p in processes:"," p.join()","","# Get process results from the output queue","results = [output.get() for p in processes]","","print(results)"],"id":8,"tags":["multiprocessing"]}
1+
{"name":"parallel_strings","description":"A simple queue function to generate four random strings in parallel.","language":"Python","code":["import multiprocessing as mp","import random","import string","","random.seed(123)","","# Define an output queue","output = mp.Queue()","","# define a example function","def rand_string(length, output):"," \"\"\" Generates a random string of numbers, lower- and uppercase chars. \"\"\""," rand_str = ''.join(random.choice("," string.ascii_lowercase "," + string.ascii_uppercase "," + string.digits)"," for i in range(length))"," output.put(rand_str)","","# Setup a list of processes that we want to run","processes = [mp.Process(target=rand_string, args=(5, output)) for x in range(4)]","","# Run processes","for p in processes:"," p.start()","","# Exit the completed processes","for p in processes:"," p.join()","","# Get process results from the output queue","results = [output.get() for p in processes]","","print(results)"],"id":16,"tags":["multiprocessing"]}

snippets/progress_bar.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"name":"progress_bar","description":"Create a progress bar.","language":"Python","code":["class ProgressBar():"," def __init__(self, width=50):"," self.pointer = 0"," self.width = width",""," def __call__(self,x):"," # x in percent"," self.pointer = int(self.width*(x/100.0))"," return \"|\" + \"#\"*self.pointer + \"-\"*(self.width-self.pointer)+\\"," \"|\\n %d percent done\" % int(x) "],"id":10,"tags":["Time"]}
1+
{"name":"progress_bar","description":"Create a progress bar.","language":"Python","code":["class ProgressBar():"," def __init__(self, width=50):"," self.pointer = 0"," self.width = width",""," def __call__(self,x):"," # x in percent"," self.pointer = int(self.width*(x/100.0))"," return \"|\" + \"#\"*self.pointer + \"-\"*(self.width-self.pointer)+\\"," \"|\\n %d percent done\" % int(x) "],"id":9,"tags":["Time"]}

snippets/sum_array.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"name":"sum_array","description":"Scala program of array. Declare, print, and calculate sum of all elements.","language":"Scala","code":["object ExampleArray1 {"," "," def main(args: Array[String]) {"," "," var numbers = Array(10,20,30,40,50);"," var N:Int=0;"," "," //print all array elements"," println(\"All array elements: \");"," for ( N <- numbers ) {"," println(N);"," }"," //calculating SUM of all elements"," var sum: Int=0;"," for ( N <- numbers ) {"," sum+=N;"," } "," println(\"Sum of all array elements: \"+sum);",""," }","}"],"id":12,"tags":["math"]}
1+
{"name":"sum_array","description":"Scala program of array. Declare, print, and calculate sum of all elements.","language":"Scala","code":["object ExampleArray1 {"," "," def main(args: Array[String]) {"," "," var numbers = Array(10,20,30,40,50);"," var N:Int=0;"," "," //print all array elements"," println(\"All array elements: \");"," for ( N <- numbers ) {"," println(N);"," }"," //calculating SUM of all elements"," var sum: Int=0;"," for ( N <- numbers ) {"," sum+=N;"," } "," println(\"Sum of all array elements: \"+sum);",""," }","}"],"id":11,"tags":["math"]}

src/CodeSnippetDisplay.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -546,11 +546,19 @@ export class CodeSnippetDisplay extends React.Component<
546546
private _setPreviewPosition(id: string): void {
547547
const intID = parseInt(id, 10);
548548
const realTarget = document.getElementsByClassName(TITLE_CLASS)[intID];
549+
const newTarget = document.getElementsByClassName(CODE_SNIPPET_ITEM)[intID];
549550
// distDown is the number of pixels to shift the preview down
550-
let distDown: number = realTarget.getBoundingClientRect().top - 43;
551-
if (realTarget.getBoundingClientRect().top > window.screen.height / 2) {
552-
distDown = distDown - 66;
553-
}
551+
const distDown: number = realTarget.getBoundingClientRect().top - 43; //this is bumping it up
552+
const elementSnippet = newTarget as HTMLElement;
553+
const heightSnippet = elementSnippet.clientHeight;
554+
const heightPreview = heightSnippet.toString(10) + 'px';
555+
document.documentElement.style.setProperty(
556+
'--preview-max-height',
557+
heightPreview
558+
);
559+
// if (realTarget.getBoundingClientRect().top > window.screen.height / 2) {
560+
// distDown = distDown - 66; //this is bumping it up further if it's close to the end of the screen
561+
// }
554562
const final = distDown.toString(10) + 'px';
555563
document.documentElement.style.setProperty('--preview-distance', final);
556564
}

src/index.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,31 @@ function activateCodeSnippet(
163163
iconClass: 'some-css-icon-class',
164164
execute: () => {
165165
const highlightedCode = getSelectedText();
166-
167-
CodeSnippetInputDialog(
168-
codeSnippetWidget,
169-
highlightedCode.split('\n'),
170-
-1
171-
);
166+
if (highlightedCode === '') {
167+
//if user just right-clicks the whole cell to save
168+
const curr = document.getElementsByClassName(
169+
'jp-Cell jp-mod-selected'
170+
)[1];
171+
const text = curr as HTMLElement;
172+
const textContent = text.innerText;
173+
const arrayInput = textContent.split('\n');
174+
const indexedInput = arrayInput.slice(1);
175+
for (let i = 0; i < indexedInput.length; i++) {
176+
for (let j = 0; j < indexedInput[i].length; j++) {
177+
if (indexedInput[i].charCodeAt(j) === 8203) {
178+
indexedInput[i] = '';
179+
}
180+
}
181+
}
182+
CodeSnippetInputDialog(codeSnippetWidget, indexedInput, -1);
183+
} else {
184+
CodeSnippetInputDialog(
185+
codeSnippetWidget,
186+
highlightedCode.split('\n'),
187+
-1
188+
);
189+
}
190+
// if highlightedCode is empty, check the code of the entire cell.
172191
}
173192
});
174193

style/index.css

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
:root {
22
--preview-distance: 0px;
3+
--preview-max-height: 75px;
34
--more-options-top: 0px;
45
--more-options-left: 0px;
56
}
@@ -178,9 +179,9 @@
178179
background: var(--jp-layout-color1);
179180
padding: 5px;
180181
width: 180px;
181-
/*height: fit-content;
182-
max-height: 150px;*/
183-
height: 106px;
182+
height: fit-content;
183+
max-height: var(--preview-max-height);
184+
/*height: 106px;*/
184185
box-sizing: border-box;
185186
box-shadow: var(--jp-elevation-z2);
186187
word-wrap: break-word;

0 commit comments

Comments
 (0)