Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/CodeSnippetDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,7 @@ export class CodeSnippetDisplay extends React.Component<
// deleting snippets when there is one snippet active
contentsService.delete('snippets/' + codeSnippet.name + '.json');
this.props._codeSnippetWidgetModel.deleteSnippet(codeSnippet.id);
this.props._codeSnippetWidgetModel.reorderSnippet();
this.props._codeSnippetWidgetModel.updateSnippetContents();

// active tags after delete
Expand Down
3 changes: 3 additions & 0 deletions src/CodeSnippetWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,9 @@ export class CodeSnippetWidget extends ReactWidget {
event.dropAction = 'copy';
CodeSnippetInputDialog(this, data, idx);
}

// Reorder snippet just to make sure id's are in order.
this._codeSnippetWidgetModel.reorderSnippet();
}

// move code snippet within code snippet explorer
Expand Down
7 changes: 7 additions & 0 deletions src/CodeSnippetWidgetModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ export class CodeSnippetWidgetModel implements ICodeSnippetWidgetModel {
this._snippets = snippetList;
}

reorderSnippet(): void {
this.sortSnippets();
for (let i = 0; i < this._snippets.length; i++) {
this._snippets[i].id = i;
}
}

addSnippet(newSnippet: ICodeSnippet, index: number): void {
// append a new snippet created from input form to the end
if (newSnippet.id === -1) {
Expand Down
77 changes: 40 additions & 37 deletions src/FilterTools.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,26 @@ export class FilterTools extends React.Component<
this.createFilterBox = this.createFilterBox.bind(this);
this.renderFilterOption = this.renderFilterOption.bind(this);
this.renderTags = this.renderTags.bind(this);
this.renderTag = this.renderTag.bind(this);
this.renderAppliedTag = this.renderAppliedTag.bind(this);
this.renderUnappliedTag = this.renderUnappliedTag.bind(this);
this.handleClick = this.handleClick.bind(this);
this.filterSnippets = this.filterSnippets.bind(this);
}

componentDidMount() {
this.setState({ show: false, selectedTags: [], searchValue: '' });
this.setState({
show: false,
selectedTags: [],
searchValue: ''
});
}

componentDidUpdate(prevProps: IFilterSnippetProps) {
if (prevProps !== this.props) {
this.setState(state => ({
selectedTags: state.selectedTags.filter(tag =>
this.props.tags.includes(tag)
)
selectedTags: state.selectedTags
.filter(tag => this.props.tags.includes(tag))
.sort()
}));
}
}
Expand All @@ -66,14 +71,39 @@ export class FilterTools extends React.Component<
renderTags(): JSX.Element {
return (
<div className={FILTER_TAGS}>
{this.props.tags.map((tag: string, index: number) =>
this.renderTag(tag, index.toString())
)}
{this.props.tags.sort().map((tag: string, index: number) => {
if (this.state.selectedTags.includes(tag)) {
return this.renderAppliedTag(tag, index.toString());
} else {
return this.renderUnappliedTag(tag, index.toString());
}
})}
</div>
);
}

renderAppliedTag(tag: string, index: string): JSX.Element {
return (
<div
className={`${FILTER_TAG} tag applied-tag`}
id={'filter' + '-' + tag + '-' + index}
key={'filter' + '-' + tag + '-' + index}
>
<button onClick={this.handleClick}>{tag}</button>
<checkIcon.react
className={FILTER_CHECK}
tag="span"
elementPosition="center"
height="18px"
width="18px"
marginLeft="5px"
marginRight="-3px"
/>
</div>
);
}

renderTag(tag: string, index: string): JSX.Element {
renderUnappliedTag(tag: string, index: string): JSX.Element {
return (
<div
className={`${FILTER_TAG} tag unapplied-tag`}
Expand All @@ -93,7 +123,6 @@ export class FilterTools extends React.Component<
this.setState(
state => ({
selectedTags: this.handleClickHelper(
target,
parent,
state.selectedTags,
clickedTag
Expand All @@ -104,47 +133,21 @@ export class FilterTools extends React.Component<
}

handleClickHelper(
target: HTMLElement,
parent: HTMLElement,
currentTags: string[],
clickedTag: string
): string[] {
if (parent.classList.contains('unapplied-tag')) {
parent.classList.replace('unapplied-tag', 'applied-tag');
const iconContainer = checkIcon.element({
className: FILTER_CHECK,
tag: 'span',
elementPosition: 'center',
height: '18px',
width: '18px',
marginLeft: '5px',
marginRight: '-3px'
});
const color = getComputedStyle(document.documentElement).getPropertyValue(
'--jp-ui-font-color1'
);
target.style.color = color;
if (parent.children.length === 1) {
parent.appendChild(iconContainer);
}

currentTags.splice(-1, 0, clickedTag);
} else if (parent.classList.contains('applied-tag')) {
parent.classList.replace('applied-tag', 'unapplied-tag');
const color = getComputedStyle(document.documentElement).getPropertyValue(
'--jp-ui-font-color2'
);
target.style.color = color;

if (parent.children.length !== 1) {
// remove check icon
parent.removeChild(parent.children.item(1));
}

const idx = currentTags.indexOf(clickedTag);
currentTags.splice(idx, 1);
}
return currentTags;
return currentTags.sort();
}

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