diff --git a/src/features/quick_tags.js b/src/features/quick_tags.js index 689c95ae17..e8d29d7e0a 100644 --- a/src/features/quick_tags.js +++ b/src/features/quick_tags.js @@ -20,6 +20,8 @@ const controlIconSelector = keyToCss('controlIcon'); let originalPostTag; let answerTag; let autoTagAsker; +let showReblogSuggestions; +let tagBundles; let controlButtonTemplate; @@ -65,13 +67,6 @@ const createBundleButton = tagBundle => { return bundleButton; }; -const populatePopups = async function () { - const { [storageKey]: tagBundles = [] } = await browser.storage.local.get(storageKey); - - popupElement.replaceChildren(popupForm, ...tagBundles.map(createBundleButton)); - postOptionPopupElement.replaceChildren(...tagBundles.map(createBundleButton)); -}; - const processPostForm = async function ([selectedTagsElement]) { if (selectedTagsElement.classList.contains(excludeClass)) { return; @@ -104,9 +99,11 @@ const processPostForm = async function ([selectedTagsElement]) { export const onStorageChanged = async function (changes, areaName) { if (Object.keys(changes).some(key => key.startsWith('quick_tags'))) { - if (Object.keys(changes).includes(storageKey)) populatePopups(); + if (Object.keys(changes).includes(storageKey)) { + ({ newValue: tagBundles = [] } = changes[storageKey]); + } - ({ originalPostTag, answerTag, autoTagAsker } = await getPreferences('quick_tags')); + ({ originalPostTag, answerTag, autoTagAsker, showReblogSuggestions } = await getPreferences('quick_tags')); if (originalPostTag || answerTag || autoTagAsker) { pageModifications.register('#selected-tags', processPostForm); } else { @@ -123,7 +120,26 @@ const togglePopupDisplay = async function ({ target, currentTarget: controlButto if (buttonContainer.contains(popupElement)) { buttonContainer.removeChild(popupElement); } else { + popupElement.replaceChildren(popupForm, ...tagBundles.map(createBundleButton)); appendWithoutOverflow(popupElement, buttonContainer); + + if (showReblogSuggestions) { + const { rebloggedFromUuid, rebloggedFromId } = await timelineObject(controlButton.closest(postSelector)); + if (rebloggedFromUuid && rebloggedFromId) { + const { response: { tags, blogName, postAuthor, rebloggedRootName } } = await apiFetch(`/v2/blog/${rebloggedFromUuid}/posts/${rebloggedFromId}`); + + const suggestableTags = tags; + if (blogName) suggestableTags.push(blogName); + if (postAuthor) suggestableTags.push(postAuthor); + if (rebloggedRootName) suggestableTags.push(rebloggedRootName); + const tagsToSuggest = suggestableTags.filter((tag, index, array) => array.indexOf(tag) === index); + + if (tagsToSuggest.length) { + popupElement.lastElementChild?.style?.setProperty('margin-bottom', '6px'); + popupElement.append(...tagsToSuggest.map(tag => createBundleButton({ title: tag, tags: tag }))); + } + } + } } }; @@ -133,6 +149,7 @@ const togglePostOptionPopupDisplay = async function ({ target, currentTarget }) if (currentTarget.contains(postOptionPopupElement)) { currentTarget.removeChild(postOptionPopupElement); } else { + postOptionPopupElement.replaceChildren(...tagBundles.map(createBundleButton)); appendWithoutOverflow(postOptionPopupElement, currentTarget); } }; @@ -282,9 +299,9 @@ export const main = async function () { onNewPosts.addListener(processPosts); registerPostOption('quick-tags', { symbolId, onclick: togglePostOptionPopupDisplay }); - populatePopups(); + ({ [storageKey]: tagBundles = [] } = await browser.storage.local.get(storageKey)); - ({ originalPostTag, answerTag, autoTagAsker } = await getPreferences('quick_tags')); + ({ originalPostTag, answerTag, autoTagAsker, showReblogSuggestions } = await getPreferences('quick_tags')); if (originalPostTag || answerTag || autoTagAsker) { pageModifications.register('#selected-tags', processPostForm); } diff --git a/src/features/quick_tags.json b/src/features/quick_tags.json index 391ed45159..c6ea1f2883 100644 --- a/src/features/quick_tags.json +++ b/src/features/quick_tags.json @@ -24,6 +24,11 @@ "label": "Automatically tag asker when answering", "default": false }, + "showReblogSuggestions": { + "type": "checkbox", + "label": "Suggest tags from the source post on reblogs", + "default": false + }, "tagBundles": { "type": "iframe", "label": "Manage tag bundles",