Skip to content

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

feat: tags dialog

ammarahm-ed9b95ea7d22c3
diff --git a/apps/mobile/src/components/ActionSheetComponent/ActionSheetTagsSection.js b/apps/mobile/src/components/ActionSheetComponent/ActionSheetTagsSection.js
index ea159285f..b09567865 100644
--- a/apps/mobile/src/components/ActionSheetComponent/ActionSheetTagsSection.js
+++ b/apps/mobile/src/components/ActionSheetComponent/ActionSheetTagsSection.js
@@ -1,12 +1,15 @@
import React, {createRef, useCallback, useEffect, useState} from 'react';
+import {Text} from 'react-native';
import {TextInput, TouchableOpacity, View} from 'react-native';
import {ScrollView} from 'react-native-gesture-handler';
import {notesnook} from '../../../e2e/test.ids';
import {useTracked} from '../../provider';
-import {ToastEvent} from '../../services/EventManager';
+import {eSendEvent, ToastEvent} from '../../services/EventManager';
import Navigation from '../../services/Navigation';
import {db} from '../../utils/DB';
+import {eOpenTagsDialog, refreshNotesPage} from '../../utils/Events';
import {SIZE} from '../../utils/SizeUtils';
+import {sleep} from '../../utils/TimeUtils';
import {Button} from '../Button';
const tagsInputRef = createRef();
@@ -20,278 +23,80 @@ export const ActionSheetTagsSection = ({item, close}) => {
const [suggestions, setSuggestions] = useState([]);
const [focused, setFocused] = useState(false);
const [note, setNote] = useState(item);
- console.log(item.tags);
-
- const localRefresh = () => {
- toAdd = db.notes.note(note.id).data;
- Navigation.setRoutesToUpdate([
- Navigation.routeNames.Tags,
- Navigation.routeNames.Notes,
- Navigation.routeNames.NotesPage,
- ]);
- setNote({...toAdd});
- if (prevQuery) {
- getSuggestions(prevQuery, toAdd);
- } else {
- getSuggestions(null, toAdd);
- }
- };
-
- const _onSubmit = async () => {
- if (!tagToAdd || tagToAdd === '' || tagToAdd.trimStart().length == 0) {
- ToastEvent.show({
- heading: 'Tag field is empty',
- type: 'error',
- context: 'local',
- });
- return;
- }
-
- if (tagToAdd.startsWith('#')) {
- tagToAdd = tagToAdd.slice(1);
- }
-
- let tag = tagToAdd;
- tag = tag.trim();
- if (tag.includes(' ')) {
- tag = tag.replace(' ', '_');
- }
- if (tag.includes(',')) {
- tag = tag.replace(',', '');
- }
- setNote({...note, tags: note.tags ? [...note.tags, tag] : [tag]});
- try {
- await db.notes.note(note.id).tag(tag);
- Navigation.setRoutesToUpdate([Navigation.routeNames.Tags]);
- localRefresh(note.type);
- prevQuery = null;
- tagsInputRef.current?.setNativeProps({
- text: '',
- });
- tagToAdd = '';
- } catch (e) {
- ToastEvent.show({
- heading: 'Cannot add tag',
- type: 'error',
- message: e.message,
- context: 'local',
- });
- }
- };
-
- useEffect(() => {
- if (prevQuery) {
- console.log(note.tags);
- getSuggestions(prevQuery, note);
- } else {
- getSuggestions(null, note);
- }
-
- return () => {
- prevQuery = null;
- };
- }, []);
-
- const _onKeyPress = useCallback(async event => {
- console.log('keypress', event.nativeEvent.key);
- if (event.nativeEvent.key === 'Backspace') {
- if (backPressCount === 0 && !tagToAdd) {
- backPressCount = 1;
- return;
- }
- if (backPressCount === 1 && !tagToAdd) {
- backPressCount = 0;
-
- let tagInputValue = note.tags[note.tags.length - 1];
- let oldProps = {...note};
- if (oldProps.tags.length === 0) return;
-
- await db.notes
- .note(note.id)
- .untag(oldProps.tags[oldProps.tags.length - 1]);
-
- localRefresh(note.type);
-
- tagsInputRef.current?.setNativeProps({
- text: tagInputValue,
- });
- }
- } else if (event.nativeEvent.key === ',') {
- await _onSubmit();
- tagsInputRef.current?.setNativeProps({
- text: '',
- });
- }
- });
-
- const getSuggestions = (query, note) => {
- if (!note || !note?.id) return;
-
- let _tags = db.tags.all;
-
- prevQuery = query;
- let _suggestions;
- if (query) {
- _suggestions = _tags.filter(
- t => t.title.startsWith(query) && !note.tags.find(n => n === t.title),
- );
- } else {
- _suggestions = _tags
- .slice()
- .sort(function (x, y) {
- return x.dateEdited - y.dateEdited;
- })
- .filter(
- o => o.noteIds.length >= 1 && !note.tags.find(t => t === o.title),
- )
- .slice(0, 10);
- }
- console.log(_suggestions);
- setSuggestions(_suggestions);
- };
-
- const _onChange = value => {
- tagToAdd = value;
- getSuggestions(value);
- if (value.endsWith(' ')) {
- _onSubmit();
- tagsInputRef.current?.setNativeProps({
- text: '',
- });
- }
- if (tagToAdd.length > 0) backPressCount = 0;
- };
return note.id || note.dateCreated ? (
<View
style={{
marginHorizontal: 12,
}}>
- <ScrollView
- horizontal
- keyboardShouldPersistTaps="always"
- keyboardDismissMode="none"
- contentContainerStyle={{
- flexDirection: 'row',
- alignItems: 'center',
- paddingVertical: 5,
- paddingBottom: 10,
- }}>
- {suggestions.map(tag => (
- <Button
- key={tag.title}
- onPress={() => {
- tagToAdd = tag.title;
- _onSubmit();
- }}
- title={'#' + tag.title}
- textStyle={{
- fontWeight: 'normal',
- }}
- type="shade"
- height={25}
- style={{
- margin: 1,
- marginRight: 5,
- paddingHorizontal: 0,
- paddingVertical: 2.5,
- borderRadius: 100,
- paddingHorizontal: 12,
- }}
- />
- ))}
- </ScrollView>
- <TouchableOpacity
- activeOpacity={1}
- onPress={() => {
- tagsInputRef.current?.focus();
- }}
+ <View
style={{
flexDirection: 'row',
flexWrap: 'wrap',
- borderBottomWidth: 1.5,
- borderColor: focused ? colors.accent : colors.nav,
alignItems: 'center',
}}>
{note.tags.map(
- (item, index) =>
- item && (
- <TagItem
- key={item}
- tag={item}
- note={note}
- localRefresh={localRefresh}
- />
- ),
+ (item, index) => item && <TagItem key={item} tag={item} />,
)}
- <TextInput
- key="inputItem"
- style={{
- minWidth: 100,
- zIndex: 10,
- color: colors.pri,
- paddingHorizontal: 5,
- paddingVertical: 0,
- height: 40,
- fontSize: SIZE.sm,
- textAlignVertical: 'center',
- }}
- testID={notesnook.ids.dialogs.actionsheet.hashtagInput}
- autoCapitalize="none"
- textAlignVertical="center"
- blurOnSubmit={false}
- ref={tagsInputRef}
- placeholderTextColor={colors.icon}
- onFocus={() => {
- setFocused(true);
+
+ <Button
+ onPress={async () => {
+ close();
+ await sleep(300);
+ eSendEvent(eOpenTagsDialog, note);
}}
- selectionColor={colors.accent}
- onBlur={() => {
- setFocused(false);
+ title="Add new tag"
+ type="accent"
+ icon="plus"
+ iconPosition="right"
+ height={30}
+ fontSize={SIZE.sm}
+ style={{
+ margin: 1,
+ marginRight: 5,
+ paddingHorizontal: 0,
+ borderRadius: 100,
+ paddingHorizontal: 12,
}}
- placeholder="Add a tag"
- onChangeText={_onChange}
- onSubmitEditing={_onSubmit}
- onKeyPress={_onKeyPress}
/>
- </TouchableOpacity>
+ </View>
</View>
) : null;
};
-const TagItem = ({tag, note, localRefresh}) => {
+const TagItem = ({tag}) => {
const onPress = async () => {
- let prevNote = {...note};
+ let params = (params = {
+ ...item,
+ type: 'tag',
+ menu: false,
+ get: 'tagged',
+ });
+ Navigation.navigate('NotesPage', params, {
+ heading: '#' + tag.title,
+ id: tag.id,
+ type: tag.type,
+ });
+ eSendEvent(refreshNotesPage, params);
+ close();
+ };
- try {
- await db.notes
- .note(note.id)
- .untag(prevNote.tags[prevNote.tags.indexOf(tag)]);
- localRefresh(note.type);
- } catch (e) {
- } finally {
- Navigation.setRoutesToUpdate([
- Navigation.routeNames.NotesPage,
- Navigation.routeNames.Favorites,
- Navigation.routeNames.Notes,
- ]);
- }
+ const style = {
+ margin: 1,
+ marginRight: 5,
+ paddingHorizontal: 0,
+ borderRadius: 100,
+ paddingHorizontal: 12,
};
return (
<Button
onPress={onPress}
title={'#' + tag}
- type="accent"
- height={25}
+ type="grayBg"
+ height={30}
fontSize={SIZE.sm}
- style={{
- margin: 1,
- marginRight: 5,
- paddingHorizontal: 0,
- borderRadius: 100,
- paddingHorizontal: 12,
- }}
+ style={style}
/>
);
};
diff --git a/apps/mobile/src/components/Button/index.js b/apps/mobile/src/components/Button/index.js
index 95001c9bf..08d3064d4 100644
--- a/apps/mobile/src/components/Button/index.js
+++ b/apps/mobile/src/components/Button/index.js
@@ -28,6 +28,7 @@ export const Button = ({
onLongPress,
tooltipText,
textStyle,
+ iconPosition = 'left',
}) => {
const [state] = useTracked();
const {colors} = state;
@@ -67,7 +68,7 @@ export const Button = ({
...style,
}}>
{loading && <ActivityIndicator color={textColor} />}
- {icon && !loading && (
+ {icon && !loading && iconPosition === 'left' && (
<Icon
name={icon}
style={{
@@ -77,19 +78,32 @@ export const Button = ({
size={iconSize}
/>
)}
+
{!title ? null : (
<Heading
color={textColor}
size={fontSize}
style={[
{
- marginLeft: icon || loading ? 5 : 0,
+ marginLeft: icon || loading && iconPosition === "left" ? 5 : 0,
+ marginRight: icon || loading && iconPosition === "right" ? 5 : 0,
},
textStyle,
]}>
{title}
</Heading>
)}
+
+ {icon && !loading && iconPosition === 'right' && (
+ <Icon
+ name={icon}
+ style={{
+ marginLeft: 0,
+ }}
+ color={textColor}
+ size={iconSize}
+ />
+ )}
</PressableButton>
);
};
diff --git a/apps/mobile/src/components/Input/index.js b/apps/mobile/src/components/Input/index.js
index 7aa959eff..685607006 100644
--- a/apps/mobile/src/components/Input/index.js
+++ b/apps/mobile/src/components/Input/index.js
@@ -218,7 +218,7 @@ const Input = ({
{button && (
<ActionIcon
name={button.icon}
- size={20}
+ size={SIZE.lg}
top={10}
bottom={10}
onPress={button.onPress}
diff --git a/apps/mobile/src/components/TagsDialog/index.js b/apps/mobile/src/components/TagsDialog/index.js
new file mode 100644
index 000000000..f412eaa23
--- /dev/null
+++ b/apps/mobile/src/components/TagsDialog/index.js
@@ -0,0 +1,244 @@
+import React, {useEffect, useRef, useState} from 'react';
+import {ScrollView, View} from 'react-native';
+import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
+import {useTracked} from '../../provider';
+import {useTagStore} from '../../provider/stores';
+import {eSubscribeEvent, eUnSubscribeEvent} from '../../services/EventManager';
+import Navigation from '../../services/Navigation';
+import {db} from '../../utils/DB';
+import {eCloseTagsDialog, eOpenTagsDialog} from '../../utils/Events';
+import {SIZE} from '../../utils/SizeUtils';
+import ActionSheetWrapper from '../ActionSheetComponent/ActionSheetWrapper';
+import Input from '../Input';
+import {PressableButton} from '../PressableButton';
+import Paragraph from '../Typography/Paragraph';
+
+const TagsDialog = () => {
+ const [state] = useTracked();
+ const colors = state.colors;
+ const [visible, setVisible] = useState(false);
+ const [note, setNote] = useState(null);
+ const allTags = useTagStore(state => state.tags);
+ const [tags, setTags] = useState(allTags);
+ const [query, setQuery] = useState(null);
+ const inputRef = useRef();
+ const actionSheetRef = useRef();
+
+ useEffect(() => {
+ eSubscribeEvent(eOpenTagsDialog, open);
+ eSubscribeEvent(eCloseTagsDialog, close);
+ return () => {
+ eUnSubscribeEvent(eOpenTagsDialog, open);
+ eUnSubscribeEvent(eCloseTagsDialog, close);
+ };
+ }, []);
+
+ useEffect(() => {
+ sortTags();
+ }, [allTags, note, query]);
+
+ const sortTags = () => {
+ let _tags = [...allTags];
+ _tags = _tags.sort((a, b) => a.title.localeCompare(b.title));
+ if (query) {
+ _tags = _tags.filter(t => t.title.startsWith(query));
+ }
+
+ if (!note || !note.tags) {
+ setTags(_tags);
+ return;
+ }
+ let noteTags = [];
+ for (tag of note.tags) {
+ let index = _tags.findIndex(t => t.title === tag);
+ if (index !== -1) {
+ noteTags.push(_tags[index]);
+ _tags.splice(index, 1);
+ }
+ }
+ noteTags = noteTags.sort((a, b) => a.title.localeCompare(b.title));
+
+ setTags([...noteTags, ..._tags]);
+ };
+
+ const open = item => {
+ setNote(item);
+ useTagStore.getState().setTags();
+ setVisible(true);
+ };
+
+ useEffect(() => {
+ if (visible) {
+ actionSheetRef.current?.show();
+ }
+ }, [visible]);
+
+ const close = () => {
+ setQuery(null);
+ actionSheetRef.current?.hide();
+ };
+
+ const onSubmit = async () => {
+ let _query = query;
+ if (!_query || _query === '' || _query.trimStart().length == 0) {
+ ToastEvent.show({
+ heading: 'Tag field is empty',
+ type: 'error',
+ context: 'local',
+ });
+ return;
+ }
+
+ if (_query.startsWith('#')) {
+ _query = _query.slice(1);
+ }
+
+ let tag = _query;
+ tag = tag.trim();
+ if (tag.includes(' ')) {
+ tag = tag.replace(' ', '_');
+ }
+ if (tag.includes(',')) {
+ tag = tag.replace(',', '');
+ }
+ setNote({...note, tags: note.tags ? [...note.tags, tag] : [tag]});
+ inputRef.current?.setNativeProps({
+ text:""
+ })
+ try {
+ await db.notes.note(note.id).tag(tag);
+ useTagStore.getState().setTags();
+ setNote(db.notes.note(note.id).data);
+ setQuery(null);
+ } catch (e) {
+ ToastEvent.show({
+ heading: 'Cannot add tag',
+ type: 'error',
+ message: e.message,
+ context: 'local',
+ });
+ }
+ };
+
+ return !visible ? null : (
+ <ActionSheetWrapper
+ centered={false}
+ fwdRef={actionSheetRef}
+ onClose={async () => {
+ setQuery(null);
+ setVisible(false);
+ }}>
+ <View
+ style={{
+ width: '100%',
+ alignSelf: 'center',
+ paddingHorizontal: 12,
+ minHeight: '80%',
+ }}>
+ <Input
+ button={{
+ icon: 'magnify',
+ color: colors.accent,
+ onPress: () => {},
+ size: SIZE.lg,
+ }}
+ fwdRef={inputRef}
+ autoCapitalize="none"
+ onChangeText={v => {
+ setQuery(v);
+ }}
+ height={50}
+ placeholder="Search or add tag"
+ />
+
+ <ScrollView nestedScrollEnabled>
+ {query ? (
+ <PressableButton
+ key={"query_item"}
+ customStyle={{
+ flexDirection: 'row',
+ marginVertical: 5,
+ justifyContent: 'space-between',
+ padding: 12,
+ }}
+ onPress={onSubmit}
+ type="shade">
+ <Paragraph color={colors.accent}>Add {'#' + query}</Paragraph>
+ <Icon name="plus" color={colors.accent} size={SIZE.lg} />
+ </PressableButton>
+ ) : null}
+ {tags.map(item => (
+ <TagItem
+ key={item.title} tag={item} note={note} setNote={setNote} />
+ ))}
+ </ScrollView>
+ </View>
+ </ActionSheetWrapper>
+ );
+};
+
+export default TagsDialog;
+
+const TagItem = ({tag, note, setNote}) => {
+ const [state] = useTracked();
+ const colors = state.colors;
+
+ const onPress = async () => {
+ let prevNote = {...note};
+ try {
+ if (prevNote.tags.indexOf(tag.title) !== -1) {
+ await db.notes
+ .note(note.id)
+ .untag(prevNote.tags[prevNote.tags.indexOf(tag.title)]);
+ } else {
+ await db.notes.note(note.id).tag(tag.title);
+ }
+ setNote(db.notes.note(note.id).data);
+ } catch (e) {
+ } finally {
+ Navigation.setRoutesToUpdate([
+ Navigation.routeNames.NotesPage,
+ Navigation.routeNames.Favorites,
+ Navigation.routeNames.Notes,
+ ]);
+ }
+ };
+
+ return (
+ <PressableButton
+ customStyle={{
+ flexDirection: 'row',
+ marginVertical: 5,
+ justifyContent: 'space-between',
+ padding: 12,
+ }}
+ onPress={onPress}
+ type={
+ note && note.tags.findIndex(t => t === tag.title) !== -1
+ ? 'accent'
+ : 'grayBg'
+ }>
+ <Paragraph
+ color={
+ note && note?.tags.findIndex(t => t === tag.title) !== -1
+ ? colors.light
+ : colors.pri
+ }>
+ {'#' + tag.title}
+ </Paragraph>
+ <Icon
+ name={
+ note && note?.tags.findIndex(t => t === tag.title) !== -1
+ ? 'minus'
+ : 'plus'
+ }
+ color={
+ note && note?.tags.findIndex(t => t === tag.title) !== -1
+ ? colors.light
+ : colors.accent
+ }
+ size={SIZE.lg}
+ />
+ </PressableButton>
+ );
+};