-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
4,136 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
npm-debug.log | ||
node_modules | ||
dist | ||
.parcel-cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["babel-preset-solid"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,92 @@ | ||
/** @jsx jsx */ | ||
// eslint-disable-next-line no-unused-vars | ||
import { createClass, jsx } from "vanilla-jsx" | ||
import { createComputed, createEffect, createSignal, createSelector, For } from "solid-js" | ||
import { scrollIntoViewIfNeeded } from "atom-ide-base/src-commons-ui/scrollIntoView" | ||
|
||
import { $class } from "../helpers" | ||
import type { ListMovement } from "../types" | ||
import type { ListMovement, ListItem } from "../types" | ||
|
||
export default createClass({ | ||
renderView(suggestions, selectCallback) { | ||
let className = "select-list popover-list" | ||
export interface Props { | ||
suggestions: Array<ListItem> | ||
selectCallback: (suggestion: ListItem) => void | ||
movement?: ListMovement | ||
select: boolean | ||
} | ||
|
||
if (suggestions.length > 7) { | ||
className += " intentions-scroll" | ||
} | ||
export function ListElement(props: Props) { | ||
// current active index | ||
const [getActiveIndex, setActiveIndex] = createSignal(-1) | ||
// current active id | ||
const isSelected = createSelector(getActiveIndex) | ||
|
||
this.suggestions = suggestions | ||
this.suggestionsCount = suggestions.length | ||
this.suggestionsIndex = -1 | ||
this.selectCallback = selectCallback | ||
return ( | ||
<intentions-list class={className} id="intentions-list"> | ||
<ol className="list-group" ref="list"> | ||
{suggestions.map(function (suggestion) { | ||
return ( | ||
<li> | ||
<span | ||
className={suggestion[$class]} | ||
on-click={function () { | ||
selectCallback(suggestion) | ||
}} | ||
> | ||
{suggestion.title} | ||
</span> | ||
</li> | ||
) | ||
})} | ||
</ol> | ||
</intentions-list> | ||
) | ||
}, | ||
function handleSelection(active: HTMLElement, suggestion: ListItem, index: number) { | ||
// scroll into for the current selected element | ||
scrollIntoViewIfNeeded(active, false) | ||
// call its associated callback | ||
props.selectCallback(suggestion) | ||
// store it in the signal | ||
setActiveIndex(index) | ||
} | ||
|
||
move(movement: ListMovement) { | ||
let newIndex = this.suggestionsIndex | ||
function handleMove() { | ||
const suggestionsCount = props.suggestions.length | ||
|
||
if (movement === "up") { | ||
newIndex-- | ||
} else if (movement === "down") { | ||
newIndex++ | ||
} else if (movement === "move-to-top") { | ||
newIndex = 0 | ||
} else if (movement === "move-to-bottom") { | ||
newIndex = this.suggestionsCount | ||
let index = getActiveIndex() | ||
|
||
if (props.movement === "up") { | ||
index-- | ||
} else if (props.movement === "down") { | ||
index++ | ||
} else if (props.movement === "move-to-top") { | ||
index = 0 | ||
} else if (props.movement === "move-to-bottom") { | ||
index = suggestionsCount | ||
} | ||
|
||
// TODO: Implement page up/down | ||
newIndex %= this.suggestionsCount | ||
index %= suggestionsCount | ||
|
||
if (newIndex < 0) { | ||
newIndex = this.suggestionsCount + newIndex | ||
if (index < 0) { | ||
index = suggestionsCount + index | ||
} | ||
|
||
this.selectIndex(newIndex) | ||
}, | ||
setActiveIndex(index) | ||
} | ||
|
||
selectIndex(index) { | ||
if (this.refs.active) { | ||
this.refs.active.classList.remove("selected") | ||
createComputed(() => { | ||
handleMove() | ||
}) | ||
createEffect(() => { | ||
if (props.select) { | ||
props.selectCallback(props.suggestions[getActiveIndex()]) | ||
} | ||
}) | ||
|
||
this.refs.active = this.refs.list.children[index] | ||
this.refs.active.classList.add("selected") | ||
this.refs.active.scrollIntoViewIfNeeded(false) | ||
this.suggestionsIndex = index | ||
}, | ||
let className = "select-list popover-list" | ||
if (props.suggestions.length > 7) { | ||
className += " intentions-scroll" | ||
} | ||
|
||
select() { | ||
this.selectCallback(this.suggestions[this.suggestionsIndex]) | ||
}, | ||
}) | ||
return ( | ||
<div class={className} id="intentions-list"> | ||
<ol className="list-group"> | ||
<For each={props.suggestions}> | ||
{(suggestion, getIndex) => { | ||
const index = getIndex() | ||
let liRef: HTMLLIElement | undefined | ||
return ( | ||
<li ref={liRef} class={isSelected(index) ? "selected" : ""}> | ||
<span | ||
className={suggestion[$class]} | ||
onClick={() => { | ||
handleSelection(liRef!, suggestion, index) | ||
}} | ||
> | ||
{suggestion.title} | ||
</span> | ||
</li> | ||
) | ||
}} | ||
</For> | ||
</ol> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.