Skip to content

Commit 756a068

Browse files
committed
Fix the issue when creating a new snippet while filtering/searching
1 parent 33c8c83 commit 756a068

File tree

3 files changed

+43
-41
lines changed

3 files changed

+43
-41
lines changed

src/CodeSnippetDisplay.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,9 +1166,6 @@ export class CodeSnippetDisplay extends React.Component<
11661166
editor.dispose();
11671167
}
11681168

1169-
console.log('delete snippet');
1170-
console.log(codeSnippet.id);
1171-
console.log(this.props._codeSnippetWidgetModel._snippets);
11721169
// deleting snippets when there is one snippet active
11731170
contentsService.delete('snippets/' + codeSnippet.name + '.json');
11741171
this.props._codeSnippetWidgetModel.deleteSnippet(codeSnippet.id);

src/CodeSnippetWidget.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,6 @@ export class CodeSnippetWidget extends ReactWidget {
331331
idx = parseInt(snippet.id);
332332
}
333333

334-
console.log(this._codeSnippetWidgetModel.snippets);
335334
/**
336335
* moving snippets inside the snippet panel
337336
*/
@@ -359,6 +358,9 @@ export class CodeSnippetWidget extends ReactWidget {
359358
event.dropAction = 'copy';
360359
CodeSnippetInputDialog(this, data, idx);
361360
}
361+
362+
// Reorder snippet just to make sure id's are in order.
363+
this._codeSnippetWidgetModel.reorderSnippet();
362364
}
363365

364366
// move code snippet within code snippet explorer

src/FilterTools.tsx

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,26 @@ export class FilterTools extends React.Component<
3535
this.createFilterBox = this.createFilterBox.bind(this);
3636
this.renderFilterOption = this.renderFilterOption.bind(this);
3737
this.renderTags = this.renderTags.bind(this);
38-
this.renderTag = this.renderTag.bind(this);
38+
this.renderAppliedTag = this.renderAppliedTag.bind(this);
39+
this.renderUnappliedTag = this.renderUnappliedTag.bind(this);
3940
this.handleClick = this.handleClick.bind(this);
4041
this.filterSnippets = this.filterSnippets.bind(this);
4142
}
4243

4344
componentDidMount() {
44-
this.setState({ show: false, selectedTags: [], searchValue: '' });
45+
this.setState({
46+
show: false,
47+
selectedTags: [],
48+
searchValue: ''
49+
});
4550
}
4651

4752
componentDidUpdate(prevProps: IFilterSnippetProps) {
4853
if (prevProps !== this.props) {
4954
this.setState(state => ({
50-
selectedTags: state.selectedTags.filter(tag =>
51-
this.props.tags.includes(tag)
52-
)
55+
selectedTags: state.selectedTags
56+
.filter(tag => this.props.tags.includes(tag))
57+
.sort()
5358
}));
5459
}
5560
}
@@ -66,14 +71,39 @@ export class FilterTools extends React.Component<
6671
renderTags(): JSX.Element {
6772
return (
6873
<div className={FILTER_TAGS}>
69-
{this.props.tags.map((tag: string, index: number) =>
70-
this.renderTag(tag, index.toString())
71-
)}
74+
{this.props.tags.sort().map((tag: string, index: number) => {
75+
if (this.state.selectedTags.includes(tag)) {
76+
return this.renderAppliedTag(tag, index.toString());
77+
} else {
78+
return this.renderUnappliedTag(tag, index.toString());
79+
}
80+
})}
81+
</div>
82+
);
83+
}
84+
85+
renderAppliedTag(tag: string, index: string): JSX.Element {
86+
return (
87+
<div
88+
className={`${FILTER_TAG} tag applied-tag`}
89+
id={'filter' + '-' + tag + '-' + index}
90+
key={'filter' + '-' + tag + '-' + index}
91+
>
92+
<button onClick={this.handleClick}>{tag}</button>
93+
<checkIcon.react
94+
className={FILTER_CHECK}
95+
tag="span"
96+
elementPosition="center"
97+
height="18px"
98+
width="18px"
99+
marginLeft="5px"
100+
marginRight="-3px"
101+
/>
72102
</div>
73103
);
74104
}
75105

76-
renderTag(tag: string, index: string): JSX.Element {
106+
renderUnappliedTag(tag: string, index: string): JSX.Element {
77107
return (
78108
<div
79109
className={`${FILTER_TAG} tag unapplied-tag`}
@@ -93,7 +123,6 @@ export class FilterTools extends React.Component<
93123
this.setState(
94124
state => ({
95125
selectedTags: this.handleClickHelper(
96-
target,
97126
parent,
98127
state.selectedTags,
99128
clickedTag
@@ -104,47 +133,21 @@ export class FilterTools extends React.Component<
104133
}
105134

106135
handleClickHelper(
107-
target: HTMLElement,
108136
parent: HTMLElement,
109137
currentTags: string[],
110138
clickedTag: string
111139
): string[] {
112140
if (parent.classList.contains('unapplied-tag')) {
113141
parent.classList.replace('unapplied-tag', 'applied-tag');
114-
const iconContainer = checkIcon.element({
115-
className: FILTER_CHECK,
116-
tag: 'span',
117-
elementPosition: 'center',
118-
height: '18px',
119-
width: '18px',
120-
marginLeft: '5px',
121-
marginRight: '-3px'
122-
});
123-
const color = getComputedStyle(document.documentElement).getPropertyValue(
124-
'--jp-ui-font-color1'
125-
);
126-
target.style.color = color;
127-
if (parent.children.length === 1) {
128-
parent.appendChild(iconContainer);
129-
}
130142

131143
currentTags.splice(-1, 0, clickedTag);
132144
} else if (parent.classList.contains('applied-tag')) {
133145
parent.classList.replace('applied-tag', 'unapplied-tag');
134-
const color = getComputedStyle(document.documentElement).getPropertyValue(
135-
'--jp-ui-font-color2'
136-
);
137-
target.style.color = color;
138-
139-
if (parent.children.length !== 1) {
140-
// remove check icon
141-
parent.removeChild(parent.children.item(1));
142-
}
143146

144147
const idx = currentTags.indexOf(clickedTag);
145148
currentTags.splice(idx, 1);
146149
}
147-
return currentTags;
150+
return currentTags.sort();
148151
}
149152

150153
handleSearch = (event: React.ChangeEvent<HTMLInputElement>): void => {

0 commit comments

Comments
 (0)