Skip to content

Commit

Permalink
fix: not create tag if searchtags has this one
Browse files Browse the repository at this point in the history
  • Loading branch information
betterRunner committed Sep 12, 2021
1 parent c945ccb commit 020dae4
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions src/content-scripts/renderer/popup/tag-book/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TagSearcher
:tags="tagsWithSelect"
@onSearch="handleSearchTags"
@onEnter="handleCreateTag"
@onEnter="handleEnterSearch"
></TagSearcher>
<TagSelector
:tags="tagsWithSelect"
Expand All @@ -23,7 +23,12 @@ import { Coor } from "@/types/common";
import { Tag } from "@/types/tag";
import { Storage } from "@/types/storage";
import mitt from "@/utils/mitt";
import { addItemToArr, addItemToArrProperty, delItemFromArr, delItemFromArrProperty } from "@/utils/storage";
import {
addItemToArr,
addItemToArrProperty,
delItemFromArr,
delItemFromArrProperty,
} from "@/utils/storage";
import { StorageKeys } from "@/utils/constant";
import TagSelector from "./tag-selector/index.vue";
import TagSearcher from "./tag-searcher.vue";
Expand Down Expand Up @@ -85,15 +90,15 @@ export default {
const storage: Storage = inject("storage", {
notes: [],
tags: []
tags: [],
});
/// mark tags with this noteId to `isSelect`
const tagsWithSelect = computed(() => {
return storage.tags.map((tag) => ({
...tag,
isSelect: tag.noteIds.includes(props.noteId),
}));
})
});
/// search tag
const searchText = ref("");
Expand All @@ -102,6 +107,16 @@ export default {
searchText.value = text;
searchTags.value = result;
};
const handleEnterSearch = (tagName: string) => {
// when pressing enter, if there is a tag in `searchTags` whose tagName is the same as the search text and is not selected,
// select it instead of create a new one.
const sameTag = searchTags.value.find(st => st.name === tagName);
if (sameTag) {
sameTag.isSelect || handleSelectTagItem(sameTag);
} else {
handleCreateTag(tagName);
}
};
/// create tag
const handleCreateTag = async (tagName: string) => {
Expand Down Expand Up @@ -134,16 +149,28 @@ export default {
tag: tag.name,
isAddOrDelete: true,
});
storage.tags = await addItemToArrProperty(StorageKeys.tags, "id", tag.id, "noteIds", props.noteId);
storage.tags = await addItemToArrProperty(
StorageKeys.tags,
"id",
tag.id,
"noteIds",
props.noteId
);
} else {
mitt.emit("update-note-tag", {
noteId: props.noteId,
tag: tag.name,
isAddOrDelete: false,
});
storage.tags = await delItemFromArrProperty(StorageKeys.tags, "id", tag.id, "noteIds", props.noteId);
storage.tags = await delItemFromArrProperty(
StorageKeys.tags,
"id",
tag.id,
"noteIds",
props.noteId
);
// if tag's noteIds is empty, delete this tag
const newTag = storage.tags.find(t => t.id === tag?.id);
const newTag = storage.tags.find((t) => t.id === tag?.id);
if (newTag?.noteIds.length === 0) {
storage.tags = await delItemFromArr(StorageKeys.tags, tag.id, "id");
}
Expand All @@ -159,6 +186,7 @@ export default {
searchText,
searchTags,
handleSearchTags,
handleEnterSearch,
handleCreateTag,
Expand Down

0 comments on commit 020dae4

Please sign in to comment.