From 020dae4e8577b77a4ad82fc5b515cf947ef042af Mon Sep 17 00:00:00 2001 From: kjimlau Date: Sun, 12 Sep 2021 21:10:46 +0800 Subject: [PATCH] fix: not create tag if searchtags has this one --- .../renderer/popup/tag-book/index.vue | 42 +++++++++++++++---- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/src/content-scripts/renderer/popup/tag-book/index.vue b/src/content-scripts/renderer/popup/tag-book/index.vue index 00ba0a3..cda90f3 100644 --- a/src/content-scripts/renderer/popup/tag-book/index.vue +++ b/src/content-scripts/renderer/popup/tag-book/index.vue @@ -3,7 +3,7 @@ { @@ -93,7 +98,7 @@ export default { ...tag, isSelect: tag.noteIds.includes(props.noteId), })); - }) + }); /// search tag const searchText = ref(""); @@ -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) => { @@ -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"); } @@ -159,6 +186,7 @@ export default { searchText, searchTags, handleSearchTags, + handleEnterSearch, handleCreateTag,