Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

feat: selection with ctrl+click/ctrl+A/escape

thecodrr37771bf93fb0
diff --git a/apps/web/src/components/list-container/index.js b/apps/web/src/components/list-container/index.js
index cb540542b..74de8fe14 100644
--- a/apps/web/src/components/list-container/index.js
+++ b/apps/web/src/components/list-container/index.js
@@ -1,9 +1,12 @@
-import React, { useEffect, useMemo, useRef } from "react";
+import { useMemo, useRef } from "react";
import { Flex } from "rebass";
import Button from "../button";
import * as Icon from "../icons";
import { Virtuoso } from "react-virtuoso";
-import { useStore as useSelectionStore } from "../../stores/selection-store";
+import {
+ useStore as useSelectionStore,
+ store as selectionStore,
+} from "../../stores/selection-store";
import GroupHeader from "../group-header";
import ListProfiles from "../../common/list-profiles";
import { CustomScrollbarsVirtualList } from "../scroll-container";
@@ -15,17 +18,11 @@ function ListContainer(props) {
const { type, groupType, items, context, refresh, header } = props;
const [announcements, removeAnnouncement] = useAnnouncements();
const profile = useMemo(() => ListProfiles[type], [type]);
- const shouldSelectAll = useSelectionStore((store) => store.shouldSelectAll);
const setSelectedItems = useSelectionStore((store) => store.setSelectedItems);
const listRef = useRef();
const focusedItemIndex = useRef(-1);
const listContainerRef = useRef();
- useEffect(() => {
- if (shouldSelectAll && window.currentViewKey === type)
- setSelectedItems(items.filter((item) => item.type !== "header"));
- }, [shouldSelectAll, type, setSelectedItems, items]);
-
return (
<Flex variant="columnFill">
{!props.items.length && props.placeholder ? (
@@ -55,7 +52,21 @@ function ListContainer(props) {
computeItemKey={(index) => items[index].id || items[index].title}
defaultItemHeight={profile.estimatedItemHeight}
totalCount={items.length}
+ onKeyUp={(e) => {
+ if (e.code === "Escape") {
+ selectionStore.toggleSelectionMode(false);
+ }
+ }}
onKeyDown={(e) => {
+ if (e.code === "KeyA" && e.ctrlKey) {
+ e.preventDefault();
+ e.stopPropagation();
+ setSelectedItems(
+ items.filter((item) => item.type !== "header")
+ );
+ return;
+ }
+
const isUp = e.code === "ArrowUp";
const isDown = e.code === "ArrowDown";
const isHeader = (i) => items && items[i]?.type === "header";
diff --git a/apps/web/src/components/list-item/index.js b/apps/web/src/components/list-item/index.js
index f21538011..3206d2ff2 100644
--- a/apps/web/src/components/list-item/index.js
+++ b/apps/web/src/components/list-item/index.js
@@ -1,4 +1,4 @@
-import React, { useEffect, useCallback, useMemo } from "react";
+import { useMemo } from "react";
import { Box, Flex, Text } from "rebass";
import * as Icon from "../icons";
import {
@@ -11,23 +11,6 @@ import Config from "../../utils/config";
import { db } from "../../common/db";
import * as clipboard from "clipboard-polyfill/text";
-function selectMenuItem(isSelected, toggleSelection) {
- return {
- key: "select",
- title: () => (isSelected ? "Unselect" : "Select"),
- icon: Icon.Select,
- onClick: () => {
- const selectionState = selectionStore.get();
- if (!selectionState.isSelectionMode) {
- selectionState.toggleSelectionMode();
- toggleSelection();
- } else {
- toggleSelection();
- }
- },
- };
-}
-
function debugMenuItems(type) {
if (!type) return [];
return [
@@ -51,41 +34,6 @@ function debugMenuItems(type) {
];
}
-const ItemSelector = ({ isSelected, toggleSelection }) => {
- return isSelected ? (
- <Icon.CheckCircle
- color="primary"
- size={16}
- sx={{
- flexShrink: 0,
- marginLeft: 0,
- marginRight: 1,
- color: "primary",
- cursor: "pointer",
- }}
- onClick={(e) => {
- e.stopPropagation();
- toggleSelection();
- }}
- />
- ) : (
- <Icon.CircleEmpty
- size={16}
- sx={{
- flexShrink: 0,
- marginLeft: 0,
- marginRight: 1,
- bg: "transparent",
- cursor: "pointer",
- }}
- onClick={(e) => {
- e.stopPropagation();
- toggleSelection();
- }}
- />
- );
-};
-
function ListItem(props) {
const {
colors: { text, background, primary } = {
@@ -97,25 +45,17 @@ function ListItem(props) {
isCompact,
} = props;
- const isSelectionMode = useSelectionStore((store) => store.isSelectionMode);
const selectedItems = useSelectionStore((store) => store.selectedItems);
const isSelected =
selectedItems.findIndex((item) => props.item.id === item.id) > -1;
const selectItem = useSelectionStore((store) => store.selectItem);
const { openMenu } = useMenuTrigger();
- const toggleSelection = useCallback(
- function toggleSelection() {
- selectItem(props.item);
- },
- [selectItem, props.item]
- );
-
const menuItems = useMemo(() => {
let items = props.menu?.items;
if (!items) return [];
- if (isSelectionMode) {
+ if (selectedItems.length > 0) {
const options = SELECTION_OPTIONS_MAP[window.currentViewType];
items = options.map((option) => {
return {
@@ -126,27 +66,15 @@ function ListItem(props) {
};
});
}
- if (props.selectable)
- items = [selectMenuItem(isSelected, toggleSelection), ...items];
+
if (Config.get("debugMode", false))
items = [...items, ...debugMenuItems(props.item.type)];
return items;
- }, [
- props.menu?.items,
- props.item.type,
- isSelected,
- isSelectionMode,
- toggleSelection,
- props.selectable,
- ]);
-
- useEffect(() => {
- if (!isSelectionMode && isSelected) toggleSelection();
- }, [isSelectionMode, toggleSelection, isSelected]);
+ }, [props.menu?.items, props.item.type, selectedItems.length]);
return (
<Flex
- bg={isSelected ? "bgSecondary" : background}
+ bg={isSelected ? "shade" : background}
onContextMenu={(e) => {
e.preventDefault();
openMenu(menuItems, {
@@ -166,7 +94,7 @@ function ListItem(props) {
: "none",
transition: "box-shadow 200ms ease-in",
":hover": {
- backgroundColor: "hover",
+ backgroundColor: isSelected ? "shade" : "hover",
},
":focus,:focus-visible": {
outline: "none",
@@ -182,11 +110,12 @@ function ListItem(props) {
flexDirection={isCompact ? "row" : "column"}
justifyContent={isCompact ? "space-between" : "center"}
alignItems={isCompact ? "center" : undefined}
- onClick={() => {
+ onClick={(e) => {
//e.stopPropagation();
- if (isSelectionMode) {
- toggleSelection();
- } else if (props.onClick) {
+ if (e.ctrlKey) {
+ selectItem(props.item);
+ } else {
+ selectionStore.toggleSelectionMode(false);
props.onClick();
}
}}
@@ -199,19 +128,13 @@ function ListItem(props) {
variant={isCompact ? "subtitle" : "title"}
fontWeight={isCompact ? "body" : "bold"}
color={text}
- display={isSelectionMode ? "flex" : "block"}
+ display={"block"}
sx={{
whiteSpace: "nowrap",
overflow: "hidden",
textOverflow: "ellipsis",
}}
>
- {isSelectionMode && (
- <ItemSelector
- isSelected={isSelected}
- toggleSelection={toggleSelection}
- />
- )}
{props.title}
</Text>