Quick toggle between different custom checkboxes #1236
celnda
started this conversation in
Templates Showcase
Replies: 1 comment 7 replies
-
Nice template! Here's a modified version, with a few changes:
<%*
// List of all defined checkbox states. Adjust to match your setup.
const states = {
' ': 'Unchecked | Pending',
'x': 'Checked | Done',
'>': 'Rescheduled',
'<': 'Scheduled',
'!': 'Important',
'-': 'Cancelled',
'/': 'In Progress',
'?': 'Question',
'*': 'Star',
'n': 'Note',
'l': 'Location',
'i': 'Information',
'I': 'Idea',
'S': 'Amount | Price | Money',
'p': 'Pro | Good',
'c': 'Con | Bad',
'b': 'Bookmark',
'"': 'Quote',
'u': 'Up | Like',
'd': 'Down | Dislike',
'w': 'Win',
'k': 'Key',
'f': 'Fire | Hot',
'': 'Clear Checkbox | Reset'
};
// Whether to show the new-state input dialog.
// When set to false, the checkbox loops though all defined states.
let promptUser = true;
// Default state if this line: Undone checkbox.
// This state is used, if the current line is no checkbox yet, or if
// it's current state is unknown.
const defaultState = ' ';
// -- end of configuration.
// Helper functions.
const getLabel = key => (key ? `[${key}] ` : '') + `${states[key]}`;
const stateByListType = (listNumber, key) => key
? (listNumber ? `${listNumber} [${key}] ` : `- [${key}] `)
: '';
const setState = (key, line) => line.replace(
/^(\s*)(-|\*|(\d\.))?(\s+\[.\])?\s*/,
(...match) => match[1] + stateByListType(match[3], key)
);
// Prepare vars, inspect current editor line.
const stateKeys = Object.keys(states);
const editor = app.workspace.activeLeaf.view.editor;
const cursor = editor.getCursor('from');
const currentLine = editor.getLine(cursor.line);
const stateMatch = currentLine.match(/^[\s-\*]+\s\[(.)\]\s/);
const currentState = stateMatch?.[1] || '';
let newState = defaultState;
// We found a valid checkbox state. Let's find the next state in the queue.
if (currentState) {
let nextIndex = stateKeys.indexOf(currentState) + 1
if (nextIndex >= stateKeys.length) {
nextIndex = 0;
}
newState = stateKeys[nextIndex];
}
// Optional branch: Let the user choose the new state.
if (promptUser) {
// Display the "next state" as first option, so it's auto-selected.
const suggestLabels = [getLabel(newState)];
const suggestKeys = [newState];
stateKeys.map(key => {
if (key === newState) {
return;
}
suggestLabels.push(getLabel(key))
suggestKeys.push(key)
});
newState = await tp.system.suggester(
suggestLabels,
suggestKeys,
false,
"Select new checkbox state"
);
}
if ('string' === typeof newState) {
editor.setLine(cursor.line, setState(newState, currentLine));
}
%> |
Beta Was this translation helpful? Give feedback.
7 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
What it is about
If you use our own custom set of checkboxes and find manual typing tedious you are at the right place. Instead of the selection from dialog you can bind the following script to hotkey and get the desired checkbox case with few key presses.
The code
Origins
I've taken inspiration from this example. If you need dialog the go there.
Discussed in #991
Originally posted by dmych January 12, 2023
Minimal theme allows us to use checkboxes with more than two states. It's good for Bullet Journal for instance.
Here is a quite simple Templater script which allows you to select a checkbox value from a menu. It is recommended to assign a template to a hotkey or a button in the mobile toolbar. Save the template as .md file to your Templater scripts folder. You can customize the list of available checkboxes in the suggester.
Beta Was this translation helpful? Give feedback.
All reactions