@@ -1304,22 +1304,24 @@ export class CodeSnippetDisplay extends React.Component<
13041304 return null ;
13051305 }
13061306
1307- // This function will be called to process user input for a narrowed search
1308- // using various options. The user will enter a single search term which will
1309- // searched across multiple options (name, lang, code).
1310- filterSnippetsHelper = ( searchValue : string , options : string [ ] ) => {
1307+ filterSnippets = ( searchValue : string , filterTags : string [ ] ) : void => {
1308+ // filter with search
1309+ let matchIndices : number [ ] [ ] = [ ] ;
13111310 const matchResults : StringExt . IMatchResult [ ] = [ ] ;
1312- const filteredSnippets = this . props . codeSnippets ;
1313- //let language: boolean = false;
1314- //let code: boolean = false;
1311+ let filteredSnippets = this . props . codeSnippets ;
13151312 const filteredSnippetsScore : {
13161313 score : number ;
13171314 snippet : ICodeSnippet ;
13181315 } [ ] = [ ] ;
13191316 if ( searchValue !== '' ) {
1317+ // language, title, code
13201318 filteredSnippets . forEach ( snippet => {
13211319 const matchResult = StringExt . matchSumOfSquares (
1322- ( snippet . language + snippet . name ) . toLowerCase ( ) ,
1320+ (
1321+ snippet . language +
1322+ snippet . name +
1323+ snippet . code . join ( '\n' )
1324+ ) . toLowerCase ( ) ,
13231325 searchValue . replace ( ' ' , '' ) . toLowerCase ( )
13241326 ) ;
13251327
@@ -1331,131 +1333,6 @@ export class CodeSnippetDisplay extends React.Component<
13311333 } ) ;
13321334 }
13331335 } ) ;
1334- }
1335- } ;
1336-
1337- filterSnippets = (
1338- searchValue : string ,
1339- filterTags : string [ ] ,
1340- languageTags : string [ ]
1341- ) : void => {
1342- // filter with search
1343- let matchIndices : number [ ] [ ] = [ ] ;
1344- const matchResults : StringExt . IMatchResult [ ] = [ ] ;
1345- let filteredSnippets = this . props . codeSnippets ;
1346- let name = false ;
1347- //let language: boolean = false;
1348- //let code: boolean = false;
1349- const filteredSnippetsScore : {
1350- score : number ;
1351- snippet : ICodeSnippet ;
1352- } [ ] = [ ] ;
1353- // if (searchValue !== '') {
1354- // filteredSnippets.forEach(snippet => {
1355- // const matchResult = StringExt.matchSumOfSquares(
1356- // (snippet.language + snippet.name).toLowerCase(),
1357- // searchValue.replace(' ', '').toLowerCase()
1358- // );
1359-
1360- // if (matchResult) {
1361- // matchResults.push(matchResult);
1362- // filteredSnippetsScore.push({
1363- // score: matchResult.score,
1364- // snippet: snippet
1365- // });
1366- // }
1367- // });
1368- if ( searchValue !== '' ) {
1369- // title/code/language
1370- if ( searchValue . includes ( '/*' ) ) {
1371- const searchVals : string [ ] = searchValue . split ( '/*' ) ;
1372- if ( searchVals [ 0 ] !== undefined ) {
1373- name = true ;
1374- filteredSnippets . forEach ( snippet => {
1375- //search by name
1376- const matchResult = StringExt . matchSumOfSquares (
1377- snippet . name . toLowerCase ( ) ,
1378- searchVals [ 0 ] . replace ( ' ' , '' ) . toLowerCase ( )
1379- ) ;
1380-
1381- if ( matchResult ) {
1382- matchResults . push ( matchResult ) ;
1383- filteredSnippetsScore . push ( {
1384- score : matchResult . score ,
1385- snippet : snippet
1386- } ) ;
1387- }
1388- } ) ;
1389- }
1390-
1391- if ( searchVals [ 1 ] !== undefined ) {
1392- // THIS DOESNT WORK. Have to check a snippet language and remove.
1393- //search by language
1394- console . log ( 'language' ) ;
1395- let filtered : ICodeSnippet [ ] = filteredSnippets ;
1396- if ( name ) {
1397- //if a name is entered, narrow by both name and language
1398- filtered = [ ] ;
1399- filteredSnippetsScore . forEach ( snippetScore =>
1400- filtered . push ( snippetScore . snippet )
1401- ) ;
1402- }
1403- filtered . forEach ( snippet => {
1404- const matchResult2 = StringExt . matchSumOfSquares (
1405- snippet . language . toLowerCase ( ) ,
1406- searchVals [ 1 ] . replace ( ' ' , '' ) . toLowerCase ( )
1407- ) ;
1408-
1409- if ( matchResult2 ) {
1410- matchResults . push ( matchResult2 ) ;
1411- filteredSnippetsScore . push ( {
1412- score : matchResult2 . score ,
1413- snippet : snippet
1414- } ) ;
1415- }
1416- } ) ;
1417- }
1418- // if (searchVals[2] !== undefined) {
1419- // // search for search term through the code lines
1420- // console.log(searchVals[2]);
1421- // filteredSnippets.forEach(snippet => {
1422- // for (let snippetLine in snippet.code) {
1423- // // go through each line of the code
1424- // let matchResult3 = StringExt.matchSumOfSquares(
1425- // snippetLine.toLowerCase(),
1426- // searchVals[2].toLowerCase()
1427- // );
1428-
1429- // if (matchResult3) {
1430- // matchResults.push(matchResult3);
1431- // filteredSnippetsScore.push({
1432- // score: matchResult3.score,
1433- // snippet: snippet
1434- // });
1435- // }
1436- // }
1437- // });
1438- // }
1439- } else {
1440- filteredSnippets . forEach ( snippet => {
1441- const matchResult = StringExt . matchSumOfSquares (
1442- (
1443- snippet . language +
1444- snippet . name +
1445- snippet . code . join ( '\n' )
1446- ) . toLowerCase ( ) ,
1447- searchValue . replace ( ' ' , '' ) . toLowerCase ( )
1448- ) ;
1449-
1450- if ( matchResult ) {
1451- matchResults . push ( matchResult ) ;
1452- filteredSnippetsScore . push ( {
1453- score : matchResult . score ,
1454- snippet : snippet
1455- } ) ;
1456- }
1457- } ) ;
1458- }
14591336
14601337 // sort snippets by its score
14611338 filteredSnippetsScore . sort ( ( a , b ) => a . score - b . score ) ;
@@ -1470,8 +1347,6 @@ export class CodeSnippetDisplay extends React.Component<
14701347 matchResults . forEach ( res => matchIndices . push ( res . indices ) ) ;
14711348 }
14721349
1473- // filter with tags --- create a helper function with this code and call on both tags & languages
1474- // 2 if statements filter by cell tags then language tags
14751350 if ( filterTags . length !== 0 ) {
14761351 const newMatchIndices = matchIndices . slice ( ) ;
14771352 filteredSnippets = filteredSnippets . filter ( ( codeSnippet , id ) => {
@@ -1489,20 +1364,7 @@ export class CodeSnippetDisplay extends React.Component<
14891364 const indexToDelete = newMatchIndices . indexOf ( matchedIndex ) ;
14901365 newMatchIndices . splice ( indexToDelete , 1 ) ;
14911366 return false ;
1492- } ) /*||
1493- languageTags.some(lang => {
1494- if (codeSnippet.language) {
1495- if (codeSnippet.language === lang) {
1496- //if a snippet's language matches one of the selected ones
1497- return true;
1498- }
1499- }
1500- // if the snippet does not have the tag, remove its mathed index
1501- const matchedIndex = matchIndices[id];
1502- const indexToDelete = newMatchIndices.indexOf(matchedIndex);
1503- newMatchIndices.splice(indexToDelete, 1);
1504- return false;
1505- })*/ ;
1367+ } ) ;
15061368 } ) ;
15071369 matchIndices = newMatchIndices ;
15081370 }
@@ -1521,8 +1383,6 @@ export class CodeSnippetDisplay extends React.Component<
15211383 } ;
15221384
15231385 getActiveTags ( ) : [ string [ ] , string [ ] ] {
1524- //TODO: go through snippet languages and add those to a list & pass into FilterTools,
1525- // in filterTools add a new language section and add list of lang tags there
15261386 const tags : string [ ] = [ ] ;
15271387 const languages : string [ ] = [ ] ;
15281388 for ( const codeSnippet of this . props . codeSnippets ) {
@@ -1714,7 +1574,7 @@ export class CodeSnippetDisplay extends React.Component<
17141574 < div className = { CODE_SNIPPET_HEADER_BOX } >
17151575 < FilterTools
17161576 languages = { this . getActiveTags ( ) [ 1 ] }
1717- tags = { this . getActiveTags ( ) [ 0 ] }
1577+ allTags = { this . getActiveTags ( ) }
17181578 onFilter = { this . filterSnippets }
17191579 />
17201580 </ div >
0 commit comments