Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recently used scripts #55

Merged
merged 38 commits into from
May 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
1b68f0a
Add recent bookmarklets to reducer
anthonyec May 19, 2021
4064153
Add logic to trim and push recents to front
anthonyec May 20, 2021
f5d05cd
Format actions and reducers
anthonyec May 20, 2021
12d8d4f
Add new list component and move fuzzy filter into hook
anthonyec May 21, 2021
f91f97a
Update comments in actions
anthonyec May 21, 2021
b85ea01
Add option for placeholder to use with items with no titles
anthonyec May 21, 2021
985f158
Add item ref option to list
anthonyec May 21, 2021
77ab51a
Refactor scroll view to scroll to element target
anthonyec May 21, 2021
e675009
Refactor homescreen to use new list component and scroll view
anthonyec May 21, 2021
c5ec11f
Remove legacy search list component
anthonyec May 21, 2021
f51e330
Remove unused use-callback dependency
anthonyec May 21, 2021
bac8aa6
Remove debug CSS
anthonyec May 21, 2021
1fc640b
Disable keyboard navigation on input blur
anthonyec May 21, 2021
fd6e0ec
Merge branch 'master' into recently-run-scripts
anthonyec May 22, 2021
16509ff
Format readme
anthonyec May 22, 2021
9f66f07
Sort by recent bookmarklets
anthonyec May 22, 2021
b06b5e2
Add ablity to show group headings
anthonyec May 22, 2021
a4a4afb
List style with line inbetween
anthonyec May 22, 2021
bdd001a
List style with coloured background
anthonyec May 22, 2021
f5eb873
List style that matches chrome menu
anthonyec May 22, 2021
8da5e70
Install reselect and move selector into seperate file
anthonyec May 22, 2021
9f426c7
Improve keydown performance
anthonyec May 22, 2021
8fb06ba
Check if ref exist and add TODO comment
anthonyec May 22, 2021
07c903d
Add groups
anthonyec May 22, 2021
3daf87e
Remove TODO
anthonyec May 22, 2021
63b84b8
Convert scrollview to render function to improve performance
anthonyec May 23, 2021
ace4e45
Adjust CSS
anthonyec May 23, 2021
5ee4a52
Remove editor toolbar
anthonyec May 23, 2021
0d7391d
Switch to built-in implementation of scroll into view
anthonyec May 23, 2021
117d3d2
Fix enter key index with hacky fic
anthonyec May 23, 2021
a7f8709
Add back close window function
anthonyec May 23, 2021
a789897
Rename recent to recents
anthonyec May 23, 2021
80f0bf5
Add search border
anthonyec May 23, 2021
a1a635e
Remove fuse.js and use fuzzyMatch which is similar to sublime text
anthonyec May 29, 2021
20a04c4
Revert "Remove fuse.js and use fuzzyMatch which is similar to sublime…
anthonyec May 29, 2021
10ea955
Avoid mutation when sorting
anthonyec May 29, 2021
5205550
Rename list api to action
anthonyec May 29, 2021
30d8a36
Disable recent group until ready
anthonyec May 29, 2021
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: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ $ npm run build

After the extension has been built, [load it in a browser.](#loading-extension-in-web-browser-locally)


## Loading extension in web browser locally

❗️Run the build script at least once before loading extensions into the browser.
Expand Down
1 change: 1 addition & 0 deletions docs/development.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Development setup

## Setup

When actively developing the extension you can use `start` to have Webpack watch for changes and automatically recompile the extension.

```bash
Expand Down
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"redux": "^4.0.5",
"redux-persist": "^6.0.0",
"redux-thunk": "^2.3.0",
"simple-plausible-tracker": "git+https://github.com/anthonyec/simple_plausible_tracker.git#0584220c975631d78fb2998f6ba538fe58aaa946",
"use-callback-ref": "^1.2.1"
"reselect": "^4.0.0",
"simple-plausible-tracker": "git+https://github.com/anthonyec/simple_plausible_tracker.git#0584220c975631d78fb2998f6ba538fe58aaa946"
},
"devDependencies": {
"@babel/core": "^7.9.0",
Expand Down
8 changes: 8 additions & 0 deletions src/popup/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
--icon-normal-fill: #5F6369;

--toolbar-border-color: #E1E3E6;

--list-heading-background: var(--global-background);
--list-heading-color: #5F6368;
--list-heading-border-color: #DADCE0;
}

@media(prefers-color-scheme: dark) {
Expand Down Expand Up @@ -65,6 +69,10 @@
--icon-normal-fill: #9AA0A7;

--toolbar-border-color: #393D40;

--list-heading-background: var(--global-background);
--list-heading-color: #9AA0A6;
--list-heading-border-color: #3C4043;
}
}

Expand Down
153 changes: 153 additions & 0 deletions src/popup/components/list/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import React, { useEffect, useState, useRef } from 'react';

import './list.css';

const KEYS = {
ENTER: 13,
UP: 38,
DOWN: 40,
RIGHT: 39,
LEFT: 37
};

// Return an array of concatenated ids. This is used for useEffect which only
// shallow compares. Thus would always render when an array is used instead
// of a string or number.
// E.g [{ id: 3 }, { id: 2 }] becomes '32'.
function getArrayAsStringOfIds(array) {
return array.map((item) => item.id).join('');
}

function shouldShowGroupHeading(previousItem, currentItem) {
if (!previousItem && currentItem) {
return true;
}

if (previousItem.group !== currentItem.group) {
return true;
}

return false;
}

function getGroupHeadingFromItem(groups, item) {
const foundGroup = groups.find((group) => {
return group.id === item.group;
});

return foundGroup && foundGroup.title;
}

const List = React.forwardRef(
(
{
items = [],
groups = [],
placeholder = '',
disableKeyboardNavigation,

/** Callback when item is clicked or enter key is pressed. */
onItemAction = () => {}
},
ref
) => {
const [selectedItemIndex, setSelectedItemIndex] = useState(0);
const selectItemIndexRef = useRef(selectedItemIndex);

// TODO: Hack fix for enter key to work correctly with up-to-date index
selectItemIndexRef.current = selectedItemIndex;

const handleItemClick = (item) => {
onItemAction(item);
};

const handleItemEnter = () => {
const item = items[selectItemIndexRef.current];
onItemAction(item);
};

const getPrevIndex = (index) => {
const calculatedIndex = index - 1;
return calculatedIndex < 0 ? items.length - 1 : calculatedIndex;
};

const getNextIndex = (index) => {
const calculatedIndex = index + 1;
return calculatedIndex > items.length - 1 ? 0 : calculatedIndex;
};

const handleKeyDown = (evt) => {
if (disableKeyboardNavigation) {
return;
}

switch (evt.keyCode) {
case KEYS.ENTER:
evt.preventDefault();
handleItemEnter();
break;

case KEYS.UP:
evt.preventDefault();
// Use state function for performance reasons. This allows us to
// access previous state without rebinding the key listeners.
// https://stackoverflow.com/a/62005831/4703489
setSelectedItemIndex((prevIndex) => getPrevIndex(prevIndex));
break;

case KEYS.DOWN:
evt.preventDefault();
setSelectedItemIndex((nextIndex) => getNextIndex(nextIndex));
break;

default:
}
};

useEffect(() => {
window.document.addEventListener('keydown', handleKeyDown);

return () => {
window.document.removeEventListener('keydown', handleKeyDown);
};
}, [disableKeyboardNavigation, getArrayAsStringOfIds(items)]);

// Reset selected to first item when `items` change to avoid selected being
// out of bounds when the array length changes.
useEffect(() => {
setSelectedItemIndex(0);
}, [getArrayAsStringOfIds(items)]);

const renderedItems = items.map((item, index) => {
const previousItem = index === 0 ? null : items[index - 1];
const groupHeading = getGroupHeadingFromItem(groups, item);
const showGroupHeading =
groupHeading && shouldShowGroupHeading(previousItem, item);
const isSelected = index === selectedItemIndex;
const listClassNameWithGroup =
groups.length !== 0 ? 'list__item--group' : '';
const className = isSelected
? `list__item list__item--selected ${listClassNameWithGroup}`
: `list__item ${listClassNameWithGroup}`;

return (
<React.Fragment key={item.id}>
{showGroupHeading && (
<div className="list__heading">{groupHeading}</div>
)}
<li
ref={isSelected ? ref && ref.selectedItem : null}
className={className}
onClick={handleItemClick.bind(null, item)}
>
<div className="list__text">{item.title || placeholder}</div>
</li>
</React.Fragment>
);
});

return <div className="list">{renderedItems}</div>;
}
);

export default List;
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
.search-list {}
.list {}

.search-list__item {
.list__heading {
background: var(--list-heading-background);
color: var(--list-heading-color);
border-top: 1px solid var(--list-heading-border-color);
padding: 20px var(--item-padding-left-right) 12px;
margin-top: 8px;
font-size: 12px;
position: relative;
}

.list__heading:first-child {
margin-top: 0;
}

.list__item {
padding-left: var(--item-padding-left-right);
padding-right: var(--item-padding-left-right);
height: 40px;
height: 34px;
font-size: 12px;
line-height: 20px;
user-select: none;
Expand All @@ -15,23 +29,27 @@
align-items: center;
}

.search-list__item:hover:not(.search-list__item--selected) {
.list__item:hover:not(.list__item--selected) {
background: var(--item-hover-background);
border-color: var(--item-hover-background);
}

.search-list__item--selected {
.list__item--selected {
background: var(--item-selected-background);
border-color: var(--item-selected-background);
}

.search-list__text {
.list__item--group {
padding-left: calc(var(--item-padding-left-right) + 8px);
}

.list__text {
width: 100%;
overflow: hidden;
margin-bottom: -1px;
text-overflow: ellipsis;
}

.search-list__actions {
.list__actions {
margin-right: -6px;
}
31 changes: 6 additions & 25 deletions src/popup/components/scroll_view/index.jsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,15 @@
import React, { useEffect, useRef } from 'react';
import React from 'react';

import './scroll_view.css';

export default function ScrollView({
children,
x = 0,
y = 0,
onScroll = () => {}
}) {
const scrollViewRef = useRef();

const handleOnScroll = () => {
onScroll(scrollViewRef.current.scrollLeft, scrollViewRef.current.scrollTop);
export default function ScrollView({ children }) {
const scrollToElement = (targetElement) => {
targetElement && targetElement.scrollIntoView({ block: 'nearest' });
};

useEffect(() => {
scrollViewRef.current.scrollTo(x, y);
}, [x, y]);

useEffect(() => {
scrollViewRef.current.addEventListener('scroll', handleOnScroll);

return () => {
scrollViewRef.current.removeEventListener('scroll', handleOnScroll);
};
}, []);

return (
<div className="scroll-view" ref={scrollViewRef}>
{children}
<div className="scroll-view">
{children && typeof children === 'function' && children(scrollToElement)}
</div>
);
}
6 changes: 4 additions & 2 deletions src/popup/components/search_field/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import './search_field.css';

function SearchField(
{
onKeyDown = () => {},
onChange = () => {},
onFocus = () => {},
onBlur = () => {},
placeholder = '',
defaultValue = ''
},
Expand All @@ -17,8 +18,9 @@ function SearchField(
className="search-field__input"
type="text"
ref={ref}
onKeyDown={onKeyDown}
onChange={onChange}
onFocus={onFocus}
onBlur={onBlur}
placeholder={placeholder}
defaultValue={defaultValue}
autoComplete="off"
Expand Down
3 changes: 3 additions & 0 deletions src/popup/components/search_field/search_field.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
.search-field {
box-shadow: 0 1px 0 0 var(--list-heading-border-color);
padding: 8px;
position: relative;
z-index: 2;
}

.search-field__input {
Expand Down
Loading