From ebcff58bef30183dda0df93a1843a8373d097f8b Mon Sep 17 00:00:00 2001 From: Kiran Date: Wed, 21 Oct 2020 12:52:18 -0700 Subject: [PATCH 1/2] Multi-cell saving working on right click/context menu. --- src/index.ts | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/index.ts b/src/index.ts index 91c21ce..15cd03e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -165,21 +165,24 @@ function activateCodeSnippet( const highlightedCode = getSelectedText(); 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] = ''; + const curr = document.getElementsByClassName('jp-Cell jp-mod-selected'); + const resultArray = []; + for (let i = 1; i < curr.length; i++) { + const text = curr[i] 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] = ''; + } } + resultArray.push(indexedInput[i]); } } - CodeSnippetInputDialog(codeSnippetWidget, indexedInput, -1); + console.log(resultArray); + CodeSnippetInputDialog(codeSnippetWidget, resultArray, -1); } else { CodeSnippetInputDialog( codeSnippetWidget, From cfad9e5ec5b622a117e85861e8e8d41fb03a2227 Mon Sep 17 00:00:00 2001 From: Kiran Date: Fri, 23 Oct 2020 12:26:15 -0700 Subject: [PATCH 2/2] Reduced multi cell iteration complexity. --- src/index.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/index.ts b/src/index.ts index 15cd03e..3e4ff4a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -164,24 +164,24 @@ function activateCodeSnippet( execute: () => { const highlightedCode = getSelectedText(); if (highlightedCode === '') { - //if user just right-clicks the whole cell to save + //if user just right-clicks cell(s) to save const curr = document.getElementsByClassName('jp-Cell jp-mod-selected'); const resultArray = []; for (let i = 1; i < curr.length; i++) { + //loop through each cell const text = curr[i] 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] = ''; - } + // looping through each line in cell + if (indexedInput[i].charCodeAt(0) === 8203) { + //check if first char in line is invalid + indexedInput[i] = ''; //replace invalid line with empty string } - resultArray.push(indexedInput[i]); + resultArray.push(indexedInput[i]); //push cell code lines into result } } - console.log(resultArray); CodeSnippetInputDialog(codeSnippetWidget, resultArray, -1); } else { CodeSnippetInputDialog(