Implement completion scroll

jh-changes
Shin'ya Ueoka 6 years ago
parent 0c2fcf74bb
commit 014963b327
  1. 1
      .eslintrc
  2. 51
      src/console/components/console/completion.jsx
  3. 1
      src/console/reducers/index.js

@ -40,6 +40,7 @@
"no-alert": "off", "no-alert": "off",
"no-bitwise": "off", "no-bitwise": "off",
"no-console": ["error", { "allow": ["warn", "error"] }], "no-console": ["error", { "allow": ["warn", "error"] }],
"no-continue": "off",
"no-empty-function": "off", "no-empty-function": "off",
"no-magic-numbers": "off", "no-magic-numbers": "off",
"no-mixed-operators": "off", "no-mixed-operators": "off",

@ -1,6 +1,8 @@
import { Component, h } from 'preact'; import { Component, h } from 'preact';
import { connect } from 'preact-redux'; import { connect } from 'preact-redux';
const COMPLETION_MAX_ITEMS = 33;
const CompletionTitle = (props) => { const CompletionTitle = (props) => {
return <li className='vimvixen-console-completion-title' >{props.title}</li>; return <li className='vimvixen-console-completion-title' >{props.title}</li>;
}; };
@ -25,23 +27,64 @@ const CompletionItem = (props) => {
class CompletionComponent extends Component { class CompletionComponent extends Component {
constructor() {
super();
this.state = { viewOffset: 0, select: -1 };
}
static getDerivedStateFromProps(nextProps, prevState) {
if (prevState.select === nextProps.select) {
return null;
}
let viewSelect = (() => {
let view = 0;
let index = 0;
for (let group of nextProps.completions) {
++view;
// TODO refactor
for (let _ of group.items) {
if (index === nextProps.select) {
return view;
}
++view;
++index;
}
}
})();
let viewOffset = 0;
if (nextProps.select < 0) {
viewOffset = 0;
} else if (prevState.select < nextProps.select) {
viewOffset = Math.max(prevState.viewOffset,
viewSelect - COMPLETION_MAX_ITEMS + 1);
} else if (prevState.select > nextProps.select) {
viewOffset = Math.min(prevState.viewOffset, viewSelect);
}
return { viewOffset, select: nextProps.select };
}
render() { render() {
let eles = []; let eles = [];
let index = 0; let index = 0;
for (let i = 0; i < this.props.completions.length; ++i) {
let group = this.props.completions[i]; for (let group of this.props.completions) {
eles.push(<CompletionTitle title={ group.name }/>); eles.push(<CompletionTitle title={ group.name }/>);
for (let j = 0; j < group.items.length; ++j, ++index) { for (let item of group.items) {
let item = group.items[j];
eles.push(<CompletionItem eles.push(<CompletionItem
icon={item.icon} icon={item.icon}
caption={item.caption} caption={item.caption}
url={item.url} url={item.url}
highlight={index === this.props.select} highlight={index === this.props.select}
/ >); / >);
++index;
} }
} }
let viewOffset = this.state.viewOffset;
eles = eles.slice(viewOffset, viewOffset + COMPLETION_MAX_ITEMS);
return ( return (
<ul className='vimvixen-console-completion'> <ul className='vimvixen-console-completion'>
{ eles } { eles }

@ -7,6 +7,7 @@ const defaultState = {
completionSource: '', completionSource: '',
completions: [], completions: [],
select: -1, select: -1,
viewIndex: 0,
}; };
const nextSelection = (state) => { const nextSelection = (state) => {