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

fix(Todoist): Project with sections to get project name #2151

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
29 changes: 29 additions & 0 deletions src/content/todoist.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ togglbutton.render(
const rootEl = elem.closest('.task_list_item');
const content = rootEl.querySelector('.task_list_item__content');

const projects = [];
for (const [key, value] of Object.entries(togglbutton.projects)) {
projects.push(value.name);
}

const descriptionSelector = () => {
const text = content.querySelector('.task_content');
return text ? text.textContent.trim() : '';
Expand All @@ -98,13 +103,18 @@ togglbutton.render(

if (document.querySelector('.project_view h1 span.simple_content')) {
project = document.querySelector('.project_view h1 span.simple_content').textContent.trim();
const section = $('.section_head__title .simple_content', elem.closest('section:not(.section__default)')).textContent.trim();
if (! isExist(project, projects) && isExist(section, projects)) {
project = section;
}
} else if (document.getElementById(`item_${projectId}`)) {
// (legacy?) project ID element
const projectContent = document.getElementById(`item_${projectId}`).querySelector('.content');
project = getProjectNames(projectContent);
} else if (rootEl.querySelector('.task_list_item__project')) {
// Project name shown alongside the task in UI
project = rootEl.querySelector('.task_list_item__project').textContent.trim();
project = separateAndCheck(project, projects);
} else if (document.querySelector('[data-project-id] .simple_content')) {
project = document.querySelector('[data-project-id] .simple_content').textContent;
} else {
Expand Down Expand Up @@ -255,3 +265,22 @@ function getParentIfProject (elem) {

return project;
}

function separateAndCheck (str, projects) {
const words = str.split('/');
if (words.length < 2) {
return str;
}
const project = words[0].trim();
const section = words[1].trim();
if (isExist(project, projects)) {
return project;
} else if (isExist(section, projects)) {
return section;
}
return str;
}

function isExist(str, arr) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function isExist(str, arr) {
function contains(str, arr) {

Better naming. and should swap arguments too: contains(arr, str)

or use native Array.includes:

return arr.some((p) => p === str);
}