Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quick Tags: Add suggestion-from-reblog-source feature #1653

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions src/features/quick_tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const controlIconSelector = keyToCss('controlIcon');
let originalPostTag;
let answerTag;
let autoTagAsker;
let showReblogSuggestions;
let tagBundles;

let controlButtonTemplate;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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 })));
}
}
}
}
};

Expand All @@ -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);
}
};
Expand Down Expand Up @@ -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);
}
Expand Down
5 changes: 5 additions & 0 deletions src/features/quick_tags.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down