Skip to content

Commit 9bfce5d

Browse files
committed
Language tags added but buggy. Check icon not appearing when selected. Cannot unselect lang tag.
1 parent e11cf18 commit 9bfce5d

File tree

4 files changed

+67
-17
lines changed

4 files changed

+67
-17
lines changed

src/CodeSnippetDisplay.tsx

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,7 +1334,11 @@ export class CodeSnippetDisplay extends React.Component<
13341334
}
13351335
};
13361336

1337-
filterSnippets = (searchValue: string, filterTags: string[]): void => {
1337+
filterSnippets = (
1338+
searchValue: string,
1339+
filterTags: string[],
1340+
languageTags: string[]
1341+
): void => {
13381342
// filter with search
13391343
let matchIndices: number[][] = [];
13401344
const matchResults: StringExt.IMatchResult[] = [];
@@ -1435,7 +1439,11 @@ export class CodeSnippetDisplay extends React.Component<
14351439
} else {
14361440
filteredSnippets.forEach(snippet => {
14371441
const matchResult = StringExt.matchSumOfSquares(
1438-
(snippet.language + snippet.name).toLowerCase(),
1442+
(
1443+
snippet.language +
1444+
snippet.name +
1445+
snippet.code.join('\n')
1446+
).toLowerCase(),
14391447
searchValue.replace(' ', '').toLowerCase()
14401448
);
14411449

@@ -1462,13 +1470,17 @@ export class CodeSnippetDisplay extends React.Component<
14621470
matchResults.forEach(res => matchIndices.push(res.indices));
14631471
}
14641472

1465-
// filter with tags
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
14661475
if (filterTags.length !== 0) {
14671476
const newMatchIndices = matchIndices.slice();
14681477
filteredSnippets = filteredSnippets.filter((codeSnippet, id) => {
14691478
return filterTags.some(tag => {
14701479
if (codeSnippet.tags) {
1471-
if (codeSnippet.tags.includes(tag)) {
1480+
if (
1481+
codeSnippet.tags.includes(tag) ||
1482+
codeSnippet.language === tag
1483+
) {
14721484
return true;
14731485
}
14741486
}
@@ -1477,7 +1489,20 @@ export class CodeSnippetDisplay extends React.Component<
14771489
const indexToDelete = newMatchIndices.indexOf(matchedIndex);
14781490
newMatchIndices.splice(indexToDelete, 1);
14791491
return false;
1480-
});
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+
})*/;
14811506
});
14821507
matchIndices = newMatchIndices;
14831508
}
@@ -1495,8 +1520,11 @@ export class CodeSnippetDisplay extends React.Component<
14951520
);
14961521
};
14971522

1498-
getActiveTags(): string[] {
1523+
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
14991526
const tags: string[] = [];
1527+
const languages: string[] = [];
15001528
for (const codeSnippet of this.props.codeSnippets) {
15011529
if (codeSnippet.tags) {
15021530
for (const tag of codeSnippet.tags) {
@@ -1505,8 +1533,11 @@ export class CodeSnippetDisplay extends React.Component<
15051533
}
15061534
}
15071535
}
1536+
if (!languages.includes(codeSnippet.language)) {
1537+
languages.push(codeSnippet.language);
1538+
}
15081539
}
1509-
return tags;
1540+
return [tags, languages];
15101541
}
15111542

15121543
private deleteCommand(codeSnippet: ICodeSnippet): void {
@@ -1613,7 +1644,7 @@ export class CodeSnippetDisplay extends React.Component<
16131644
editSnip.className = CODE_SNIPPET_MORE_OTPIONS_EDIT;
16141645
editSnip.textContent = 'Edit snippet';
16151646
editSnip.onclick = (): void => {
1616-
const allTags = this.getActiveTags();
1647+
const allTags = this.getActiveTags()[0];
16171648
this.props.openCodeSnippetEditor({
16181649
name: codeSnippet.name,
16191650
description: codeSnippet.description,
@@ -1672,7 +1703,7 @@ export class CodeSnippetDisplay extends React.Component<
16721703
code: [],
16731704
id: this.state.codeSnippets.length,
16741705
selectedTags: [],
1675-
allTags: this.getActiveTags(),
1706+
allTags: this.getActiveTags()[0],
16761707
fromScratch: true
16771708
});
16781709
}}
@@ -1682,7 +1713,8 @@ export class CodeSnippetDisplay extends React.Component<
16821713
</header>
16831714
<div className={CODE_SNIPPET_HEADER_BOX}>
16841715
<FilterTools
1685-
tags={this.getActiveTags()}
1716+
languages={this.getActiveTags()[1]}
1717+
tags={this.getActiveTags()[0]}
16861718
onFilter={this.filterSnippets}
16871719
/>
16881720
</div>

src/CodeSnippetEditorTags.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ export class CodeSnippetEditorTags extends React.Component<
182182
{hasTags
183183
? this.state.tags.map((tag: string, index: number) =>
184184
((): JSX.Element => {
185+
console.log(this.state.tags);
185186
if (!this.state.selectedTags) {
186187
return (
187188
<ul

src/CodeSnippetInputDialog.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ export function showInputDialog(
121121
);
122122
} else {
123123
const tags = result.value.slice(3);
124+
//tags.push(result.value[2]);
124125
const newSnippet: ICodeSnippet = {
125126
name: result.value[0].replace(' ', ''),
126127
description: result.value[1],

src/FilterTools.tsx

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@
44
import { InputGroup, checkIcon } from '@jupyterlab/ui-components';
55

66
import React from 'react';
7+
//import { filter } from '@lumino/algorithm';
78

89
interface IFilterSnippetProps {
10+
languages: string[];
911
tags: string[];
10-
onFilter: (searchValue: string, filterTags: string[]) => void;
12+
onFilter: (
13+
searchValue: string,
14+
filterTags: string[],
15+
languageTags: string[]
16+
) => void;
1117
}
1218

1319
interface IFilterSnippetState {
@@ -71,10 +77,10 @@ export class FilterTools extends React.Component<
7177
filterOption.classList.toggle('idle');
7278
}
7379

74-
renderTags(): JSX.Element {
80+
renderTags(tags: string[]): JSX.Element {
7581
return (
7682
<div className={FILTER_TAGS}>
77-
{this.props.tags.sort().map((tag: string, index: number) => {
83+
{tags.sort().map((tag: string, index: number) => {
7884
if (this.state.selectedTags.includes(tag)) {
7985
return this.renderAppliedTag(tag, index.toString());
8086
} else {
@@ -122,7 +128,7 @@ export class FilterTools extends React.Component<
122128
const target = event.target as HTMLElement;
123129
const clickedTag = target.innerText;
124130
const parent = target.parentElement;
125-
131+
console.log('ok');
126132
this.setState(
127133
(state) => ({
128134
selectedTags: this.handleClickHelper(
@@ -150,6 +156,7 @@ export class FilterTools extends React.Component<
150156
const idx = currentTags.indexOf(clickedTag);
151157
currentTags.splice(idx, 1);
152158
}
159+
console.log(currentTags);
153160
return currentTags.sort();
154161
}
155162

@@ -158,16 +165,25 @@ export class FilterTools extends React.Component<
158165
};
159166

160167
filterSnippets(): void {
161-
this.props.onFilter(this.state.searchValue, this.state.selectedTags);
168+
this.props.onFilter(this.state.searchValue, this.state.selectedTags, [
169+
'Scala'
170+
]); // hmmmmm selected Lang tags?
162171
}
163172

164173
renderFilterOption(): JSX.Element {
174+
//make lang tags/cell tags a dropdown
165175
return (
166176
<div className={`${FILTER_OPTION} idle`}>
167177
<div className={FILTER_TITLE}>
168-
<span>cell tags</span>
178+
<span>language tags</span>
179+
</div>
180+
{this.renderTags(this.props.languages)}
181+
<div className={FILTER_TITLE}>
182+
<span>snippet tags</span>
169183
</div>
170-
{this.renderTags()}
184+
{this.renderTags(
185+
this.props.tags.filter(tag => !this.props.languages.includes(tag))
186+
)}
171187
</div>
172188
);
173189
}

0 commit comments

Comments
 (0)