diff --git a/.DS_Store b/.DS_Store index ab8d775..da33467 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/schema/settings.json b/schema/settings.json index 88710a4..4f4d934 100644 --- a/schema/settings.json +++ b/schema/settings.json @@ -3,7 +3,7 @@ { "command": "codeSnippet:save-as-snippet", "keys": ["Accel Shift A"], - "selector": ".jp-Cell" + "selector": ".jp-Notebook" }, { "command": "codeSnippet:save-as-snippet", diff --git a/snippets/gdp_calculator.json b/snippets/gdp_calculator.json index 7c2b9d4..5e5485f 100644 --- a/snippets/gdp_calculator.json +++ b/snippets/gdp_calculator.json @@ -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"]} \ No newline at end of file +{"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"]} \ No newline at end of file diff --git a/snippets/generate_hundred.json b/snippets/generate_hundred.json index 1e1a6f7..83b8f7b 100644 --- a/snippets/generate_hundred.json +++ b/snippets/generate_hundred.json @@ -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"]} \ No newline at end of file +{"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"]} \ No newline at end of file diff --git a/snippets/life_exp_eur_asia.json b/snippets/life_exp_eur_asia.json index 432ec0c..b7cbc44 100644 --- a/snippets/life_exp_eur_asia.json +++ b/snippets/life_exp_eur_asia.json @@ -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"]} \ No newline at end of file +{"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"]} \ No newline at end of file diff --git a/snippets/parallel_strings.json b/snippets/parallel_strings.json index 1282421..c119dc6 100644 --- a/snippets/parallel_strings.json +++ b/snippets/parallel_strings.json @@ -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"]} \ No newline at end of file +{"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"]} \ No newline at end of file diff --git a/snippets/progress_bar.json b/snippets/progress_bar.json index 73f002d..1b12d9f 100644 --- a/snippets/progress_bar.json +++ b/snippets/progress_bar.json @@ -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"]} \ No newline at end of file +{"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"]} \ No newline at end of file diff --git a/snippets/sum_array.json b/snippets/sum_array.json index 155b0a1..e141d29 100644 --- a/snippets/sum_array.json +++ b/snippets/sum_array.json @@ -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"]} \ No newline at end of file +{"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"]} \ No newline at end of file diff --git a/src/CodeSnippetDisplay.tsx b/src/CodeSnippetDisplay.tsx index ded4d5f..95f73ad 100644 --- a/src/CodeSnippetDisplay.tsx +++ b/src/CodeSnippetDisplay.tsx @@ -535,11 +535,19 @@ export class CodeSnippetDisplay extends React.Component< private _setPreviewPosition(id: string): void { const intID = parseInt(id, 10); const realTarget = document.getElementsByClassName(TITLE_CLASS)[intID]; + const newTarget = document.getElementsByClassName(CODE_SNIPPET_ITEM)[intID]; // distDown is the number of pixels to shift the preview down - let distDown: number = realTarget.getBoundingClientRect().top - 43; - if (realTarget.getBoundingClientRect().top > window.screen.height / 2) { - distDown = distDown - 66; - } + const distDown: number = realTarget.getBoundingClientRect().top - 43; //this is bumping it up + const elementSnippet = newTarget as HTMLElement; + const heightSnippet = elementSnippet.clientHeight; + const heightPreview = heightSnippet.toString(10) + 'px'; + document.documentElement.style.setProperty( + '--preview-max-height', + heightPreview + ); + // if (realTarget.getBoundingClientRect().top > window.screen.height / 2) { + // distDown = distDown - 66; //this is bumping it up further if it's close to the end of the screen + // } const final = distDown.toString(10) + 'px'; document.documentElement.style.setProperty('--preview-distance', final); } diff --git a/src/index.ts b/src/index.ts index 8dd8f1d..91c21ce 100644 --- a/src/index.ts +++ b/src/index.ts @@ -163,12 +163,31 @@ function activateCodeSnippet( iconClass: 'some-css-icon-class', execute: () => { const highlightedCode = getSelectedText(); - - CodeSnippetInputDialog( - codeSnippetWidget, - highlightedCode.split('\n'), - -1 - ); + if (highlightedCode === '') { + //if user just right-clicks the whole cell to save + const curr = document.getElementsByClassName( + 'jp-Cell jp-mod-selected' + )[1]; + const text = curr as HTMLElement; + const textContent = text.innerText; + const arrayInput = textContent.split('\n'); + const indexedInput = arrayInput.slice(1); + for (let i = 0; i < indexedInput.length; i++) { + for (let j = 0; j < indexedInput[i].length; j++) { + if (indexedInput[i].charCodeAt(j) === 8203) { + indexedInput[i] = ''; + } + } + } + CodeSnippetInputDialog(codeSnippetWidget, indexedInput, -1); + } else { + CodeSnippetInputDialog( + codeSnippetWidget, + highlightedCode.split('\n'), + -1 + ); + } + // if highlightedCode is empty, check the code of the entire cell. } }); diff --git a/style/index.css b/style/index.css index e9a8857..a4be386 100644 --- a/style/index.css +++ b/style/index.css @@ -1,5 +1,6 @@ :root { --preview-distance: 0px; + --preview-max-height: 75px; --more-options-top: 0px; --more-options-left: 0px; } @@ -178,9 +179,9 @@ background: var(--jp-layout-color1); padding: 5px; width: 180px; - /*height: fit-content; - max-height: 150px;*/ - height: 106px; + height: fit-content; + max-height: var(--preview-max-height); + /*height: 106px;*/ box-sizing: border-box; box-shadow: var(--jp-elevation-z2); word-wrap: break-word;