Skip to content

Commit 4e410a1

Browse files
committed
Fix #98 #99
1 parent 3e0e596 commit 4e410a1

File tree

3 files changed

+57
-13
lines changed

3 files changed

+57
-13
lines changed

src/CodeSnippetDisplay.tsx

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,17 +1072,31 @@ export class CodeSnippetDisplay extends React.Component<
10721072
props: ICodeSnippetDisplayProps,
10731073
state: ICodeSnippetDisplayState
10741074
): ICodeSnippetDisplayState {
1075-
if (
1076-
props.codeSnippets !== state.codeSnippets &&
1077-
state.searchValue === '' &&
1078-
state.filterTags.length === 0
1079-
) {
1075+
if (state.searchValue === '' && state.filterTags.length === 0) {
10801076
return {
10811077
codeSnippets: props.codeSnippets,
10821078
searchValue: '',
10831079
filterTags: []
10841080
};
10851081
}
1082+
1083+
if (state.searchValue !== '' || state.filterTags.length !== 0) {
1084+
const newSnippets = props.codeSnippets.filter(codeSnippet => {
1085+
return (
1086+
(state.searchValue !== '' &&
1087+
codeSnippet.name.toLowerCase().includes(state.searchValue)) ||
1088+
(state.searchValue !== '' &&
1089+
codeSnippet.language.toLowerCase().includes(state.searchValue)) ||
1090+
(codeSnippet.tags &&
1091+
codeSnippet.tags.some(tag => state.filterTags.includes(tag)))
1092+
);
1093+
});
1094+
return {
1095+
codeSnippets: newSnippets,
1096+
searchValue: state.searchValue,
1097+
filterTags: state.filterTags
1098+
};
1099+
}
10861100
return null;
10871101
}
10881102

@@ -1158,12 +1172,19 @@ export class CodeSnippetDisplay extends React.Component<
11581172
editor.dispose();
11591173
}
11601174

1175+
// deleting snippets when there is one snippet active
11611176
contentsService.delete('snippets/' + codeSnippet.name + '.json');
11621177
this.props._codeSnippetWidgetModel.deleteSnippet(codeSnippet.id);
11631178
this.props._codeSnippetWidgetModel.updateSnippetContents();
1164-
this.setState({
1165-
codeSnippets: this.props._codeSnippetWidgetModel.snippets
1166-
});
1179+
1180+
// active tags after delete
1181+
const activeTags = this.getActiveTags();
1182+
1183+
// filterTags: only the tags that are still being used
1184+
this.setState(state => ({
1185+
codeSnippets: this.props._codeSnippetWidgetModel.snippets,
1186+
filterTags: state.filterTags.filter(tag => activeTags.includes(tag))
1187+
}));
11671188
}
11681189
});
11691190
}

src/CodeSnippetWidget.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,15 @@ export class CodeSnippetWidget extends ReactWidget {
337337
*/
338338
const source = event.source;
339339
if (source instanceof CodeSnippetDisplay) {
340+
if (
341+
source.state.searchValue !== '' ||
342+
source.state.filterTags.length !== 0
343+
) {
344+
alert(
345+
"Sorry, in the current version, you can't move snippets within explorer while filtering or searching"
346+
);
347+
return;
348+
}
340349
event.dropAction = 'move';
341350
if (event.mimeData.hasData('snippet/id')) {
342351
const srcIdx = event.mimeData.getData('snippet/id') as number;

src/FilterTools.tsx

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface IFilterSnippetProps {
99

1010
interface IFilterSnippetState {
1111
show: boolean;
12-
filteredTags: string[];
12+
selectedTags: string[];
1313
searchValue: string;
1414
}
1515

@@ -31,7 +31,7 @@ export class FilterTools extends React.Component<
3131
> {
3232
constructor(props: IFilterSnippetProps) {
3333
super(props);
34-
this.state = { show: false, filteredTags: [], searchValue: '' };
34+
this.state = { show: false, selectedTags: [], searchValue: '' };
3535
this.createFilterBox = this.createFilterBox.bind(this);
3636
this.renderFilterOption = this.renderFilterOption.bind(this);
3737
this.renderTags = this.renderTags.bind(this);
@@ -40,6 +40,20 @@ export class FilterTools extends React.Component<
4040
this.filterSnippets = this.filterSnippets.bind(this);
4141
}
4242

43+
componentDidMount() {
44+
this.setState({ show: false, selectedTags: [], searchValue: '' });
45+
}
46+
47+
componentDidUpdate(prevProps: IFilterSnippetProps) {
48+
if (prevProps !== this.props) {
49+
this.setState(state => ({
50+
selectedTags: state.selectedTags.filter(tag =>
51+
this.props.tags.includes(tag)
52+
)
53+
}));
54+
}
55+
}
56+
4357
createFilterBox(): void {
4458
const filterArrow = document.querySelector(`.${FILTER_ARROW_UP}`);
4559

@@ -78,10 +92,10 @@ export class FilterTools extends React.Component<
7892

7993
this.setState(
8094
state => ({
81-
filteredTags: this.handleClickHelper(
95+
selectedTags: this.handleClickHelper(
8296
target,
8397
parent,
84-
state.filteredTags,
98+
state.selectedTags,
8599
clickedTag
86100
)
87101
}),
@@ -138,7 +152,7 @@ export class FilterTools extends React.Component<
138152
};
139153

140154
filterSnippets(): void {
141-
this.props.onFilter(this.state.searchValue, this.state.filteredTags);
155+
this.props.onFilter(this.state.searchValue, this.state.selectedTags);
142156
}
143157

144158
renderFilterOption(): JSX.Element {

0 commit comments

Comments
 (0)