mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
Typing a title starting with "[LabelName] " in the quick-add-card composer at the bottom of a list now applies that label to the new card - creating the label on the board first, with the same default-color pick "Add label" uses, when no label with that name exists yet (case-insensitive match). Only a single bracket prefix at the very start of the title is parsed; an empty bracket, nested brackets, or a bracket with nothing left after it is left as literal title text. The parsing and label-resolution logic lives in the new pure module models/lib/quickAddCardLabel.js (parseQuickAddCardLabel, findExistingLabelIdByName, pickDefaultLabelColor), wired into addCard in client/components/lists/listBody.js. tests/quickAddCardLabel.test.cjs covers the parser (including the empty/nested-bracket/no-rest negative cases), the case-insensitive label lookup, and the default-color fallback. Thanks to mattdm and xet7 ! Closes #3986,
75 lines
3.4 KiB
JavaScript
75 lines
3.4 KiB
JavaScript
'use strict';
|
|
|
|
// Pure helpers for GitHub issue #3986: quick-add-card bracket label syntax.
|
|
// Typing "[Fedora] Do a thing" in the quick-add-card textarea at the bottom of
|
|
// a list creates a card titled "Do a thing" with the "Fedora" label applied,
|
|
// creating the label on the board first if it does not already exist.
|
|
//
|
|
// Only a SINGLE bracket prefix at the very start of the title is parsed - this
|
|
// is deliberately not a general mid-title or multi-label syntax. Extracted so
|
|
// the parsing/resolution logic is unit-testable in plain Node, mirroring the
|
|
// other models/lib pure helpers (labelAutocomplete.js, memberAutocomplete.js).
|
|
|
|
// Parses a leading "[LabelName] " prefix off a quick-add-card title.
|
|
//
|
|
// Returns { title, labelName }:
|
|
// - When the title starts with a well-formed bracket prefix (non-empty
|
|
// content, no nested brackets, followed by at least one non-whitespace
|
|
// character), `labelName` is the trimmed bracket content and `title` is the
|
|
// remainder with the prefix stripped.
|
|
// - Otherwise (no brackets, empty brackets, nested brackets, or nothing left
|
|
// after the bracket) the title is returned unchanged and `labelName` is
|
|
// `null` - the bracket is treated as literal title text, not label syntax.
|
|
function parseQuickAddCardLabel(rawTitle) {
|
|
const title = rawTitle == null ? '' : String(rawTitle);
|
|
const trimmed = title.trim();
|
|
// [^[\]]* forbids a second '[' or ']' inside the brackets, so a title like
|
|
// "[a[b] rest" (nested/unbalanced brackets) never matches and is left as
|
|
// literal text, per the "defensive, not overly clever" scope.
|
|
const match = /^\[([^[\]]*)\](?:\s+(\S.*))?$/s.exec(trimmed);
|
|
if (!match) {
|
|
return { title: rawTitle, labelName: null };
|
|
}
|
|
const labelName = match[1].trim();
|
|
const rest = (match[2] || '').trim();
|
|
if (!labelName || !rest) {
|
|
// Empty bracket ("[] Do a thing") or nothing after the bracket
|
|
// ("[Fedora]" alone) - not the syntax, keep the literal title.
|
|
return { title: rawTitle, labelName: null };
|
|
}
|
|
return { title: rest, labelName };
|
|
}
|
|
|
|
// Finds an existing label on the board whose name matches `labelName`
|
|
// case-insensitively. Returns the label's `_id`, or `null` when no label
|
|
// matches (including when `labels` is empty/absent or `labelName` is falsy).
|
|
function findExistingLabelIdByName(labels, labelName) {
|
|
if (!Array.isArray(labels) || !labelName) {
|
|
return null;
|
|
}
|
|
const needle = String(labelName).trim().toLowerCase();
|
|
const found = labels.find(
|
|
label => String(label?.name == null ? '' : label.name).toLowerCase() === needle,
|
|
);
|
|
return found ? found._id : null;
|
|
}
|
|
|
|
// Picks a default color for a newly created label: the first palette color
|
|
// not already used by one of the board's labels, falling back to the first
|
|
// palette color when every color is already used. Mirrors
|
|
// Template.createLabelPopup.helpers.defaultColor() in
|
|
// client/components/cards/labels.js, so a label auto-created by the quick-add
|
|
// bracket syntax gets the same kind of color a manually created one would.
|
|
function pickDefaultLabelColor(labels, colors) {
|
|
const palette = Array.isArray(colors) && colors.length > 0 ? colors : ['green'];
|
|
const used = (Array.isArray(labels) ? labels : []).map(label => label?.color);
|
|
const available = palette.filter(color => !used.includes(color));
|
|
return available.length > 0 ? available[0] : palette[0];
|
|
}
|
|
|
|
export {
|
|
parseQuickAddCardLabel,
|
|
findExistingLabelIdByName,
|
|
pickDefaultLabelColor,
|
|
};
|