Skip to content

Commit cbf2468

Browse files
authored
Merge pull request #112 from jahn96/master
Bug fixes
2 parents 32d0f35 + 756a068 commit cbf2468

File tree

4 files changed

+51
-37
lines changed

4 files changed

+51
-37
lines changed

src/CodeSnippetDisplay.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,6 +1169,7 @@ export class CodeSnippetDisplay extends React.Component<
11691169
// deleting snippets when there is one snippet active
11701170
contentsService.delete('snippets/' + codeSnippet.name + '.json');
11711171
this.props._codeSnippetWidgetModel.deleteSnippet(codeSnippet.id);
1172+
this.props._codeSnippetWidgetModel.reorderSnippet();
11721173
this.props._codeSnippetWidgetModel.updateSnippetContents();
11731174

11741175
// active tags after delete

src/CodeSnippetWidget.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,9 @@ export class CodeSnippetWidget extends ReactWidget {
358358
event.dropAction = 'copy';
359359
CodeSnippetInputDialog(this, data, idx);
360360
}
361+
362+
// Reorder snippet just to make sure id's are in order.
363+
this._codeSnippetWidgetModel.reorderSnippet();
361364
}
362365

363366
// move code snippet within code snippet explorer

src/CodeSnippetWidgetModel.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ export class CodeSnippetWidgetModel implements ICodeSnippetWidgetModel {
2626
this._snippets = snippetList;
2727
}
2828

29+
reorderSnippet(): void {
30+
this.sortSnippets();
31+
for (let i = 0; i < this._snippets.length; i++) {
32+
this._snippets[i].id = i;
33+
}
34+
}
35+
2936
addSnippet(newSnippet: ICodeSnippet, index: number): void {
3037
// append a new snippet created from input form to the end
3138
if (newSnippet.id === -1) {

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)