-
-
Notifications
You must be signed in to change notification settings - Fork 231
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
"Global by-function code" #3024
Comments
Good news. This is possible already using the CustomJS plugin - and just needs documenting! Credit: @mathijsvdv in this comment: #2219 (comment) See some of my custom functions below, and the class Tasks {
/**
* group by recurring, but only if there is a happens date.
*
* @example
* group by function \
* const {Tasks} = customJS; \
* return Tasks.recurringIfHasHappensDate(task);
*/
recurringIfHasHappensDate(task) {
if (task.happens.moment === null) return '';
if (task.recurrence !== null) return ['Recurring'];
return ['Not Recurring'];
}
/**
* group by all IDs in a task - its id, and anything it depends on
*
* @example
* group by function \
* const {Tasks} = customJS; \
* return Tasks.allIds(task);
*/
allIds(task) {
const combinedIds = Array.from(task.dependsOn);
if (task.id !== '') combinedIds.push(task.id);
return combinedIds;
}
/**
* Group tasks by whether they are:
* - Blocked (and cannot be done).
* - Blocking (and can be done).
* - Or are neither, and can be done in any order.
*
* @example
* group by function \
* const {Tasks} = customJS; \
* return Tasks.blockingAndBlockedLabel(task, query);
*/
blockingAndBlockedLabel(task, query) {
const blocked = task.isBlocked(query.allTasks);
const blocking = task.isBlocking(query.allTasks);
if (blocked) {
return '%%3%% 🔒 Blocked';
}
if (blocking) {
return '%%1%% 🚧 Blocking';
}
// Maybe 🌀 or 🎲 ?
return '%%1%% 🌀 Do in any order';
}
/**
* Return true if no tasks have the given ID
* @param id
* @param allTasks
*/
idMissing(id, allTasks) {
return ! allTasks.some((task) => task.id === id);
}
/**
* Return true if the task depends on any IDs which do not match any tasks in the vault.
* @param task
* @param query
* @returns {boolean}
*/
dependsOnMissingID(task, query) {
return task.dependsOn.some((id) => this.idMissing(id, query.allTasks));
}
/**
* @example
* sort by function \
* const {Tasks} = customJS; \
* return Tasks.randomSortKey(task);
*
* Credit: @qelo in https://github.com/obsidian-tasks-group/obsidian-tasks/discussions/330#discussioncomment-8902878
*/
randomSortKey(task) {
const TSH = s => {
for (var i = 0, h = 9; i < s.length;) h = Math.imul(h ^ s.charCodeAt(i++), 9 ** 9);
return h ^ h >>> 9
};
return TSH(moment().format('Y-MM-DD') + ' ' + task.description)
}
} |
That's perfect, thank you. I just found a problem: if I use To avoid that problem, the CustomJS docs recommend to start with something like So I'm having to reload that first page. |
🔖 Feature description
I have various helper JS functions that I find myself copy-pasting into every custom filter/sorting query block. This turns into a maintenance problem whenever a change is required in those functions: I have to change them everywhere.
So it would be great to be able to define those functions globally so that they are available to every custom filter/sorting query block.
✔️ Solution
Just like there is a setting for a "global query" that gets inserted into every query, there could be a "global
by function
code" setting that gets inserted into everyby function
block.❓ Alternatives
Maybe it could be implemented with something like the macros mentioned in issue #2443. However I suspect that its scope doesn't really apply here.
📝 Additional Context
No response
The text was updated successfully, but these errors were encountered: