Files
wekan/models/lib/checklistItemsAsText.js
T
Lauri Ojansivu bed9bce312 Let a checklist's items be bulk-edited as plain text, one line per item.
A checklist could previously only be edited one item at a time through its
per-item HTML row - reordering, copying between checklists/cards, or a bulk
rewording meant a click-drag or a click-edit-save per item. WeKan already
had "one line = one new item" for ADDING items (the newline-becomes-item
toggle), but nothing let a user rewrite the items an existing checklist
already has as one text block.

Adds "Edit as text" to a checklist's actions menu (alongside Export/Import,
Delete, Move, Copy). It opens a textarea pre-filled with the checklist's
current items, one per line, using the Markdown-checklist convention
`[x] Done item` / `[ ] Todo item` for checked state (a plain line with no
marker defaults to unchecked, so text pasted from elsewhere still works).
Saving replaces the item list with what was typed, in that order.

The parsing and the replace-plan are pure functions
(models/lib/checklistItemsAsText.js): checklistItemsToText/
parseChecklistItemsText convert between the textarea's text and
{title, isFinished} objects, and planChecklistItemsTextUpdate matches each
parsed line against the checklist's current items by unchanged title text
(consumed top-to-bottom, so reordered duplicates still pair 1:1) - a match
KEEPS that item's existing document (only sort/isFinished change), so any
other metadata on it, notably the #4755 due date, survives an edit that
doesn't touch its text. Only a line whose title has no remaining match
becomes a new item, and only an existing item whose text is gone from the
new text is removed.

Thanks to gerroon and xet7 !

Closes #4218,
2026-09-10 19:52:40 +03:00

99 lines
4.1 KiB
JavaScript

'use strict';
// Pure helpers for #4218 ("bulk-edit a checklist's items as plain text").
//
// A checklist previously could only be edited one item at a time through its
// per-item HTML row - reordering, copying between checklists/cards, or a bulk
// rewording meant a click-drag or a click-edit-save per item. This lets a user
// open the whole checklist as ONE multi-line textarea (one line per item, a
// leading `[x]`/`[ ]` marking the checked state - the common Markdown
// checklist convention, so text pasted in from elsewhere already matches) and
// save it back as the new item list.
//
// Kept pure (no Meteor/DOM) so the parsing and the replace-plan can be
// unit-tested directly; the client event handler only calls these and then
// performs the ChecklistItems.insert/update/remove calls the plan describes.
// One item -> one line: "[x] title" (checked) or "[ ] title" (unchecked).
function checklistItemsToText(items) {
if (!Array.isArray(items)) return '';
return items
.map(item => `[${item.isFinished ? 'x' : ' '}] ${item.title}`)
.join('\n');
}
const MARKER_RE = /^\[([ xX])\]\s?(.*)$/;
// Parse the textarea's text back into { title, isFinished } objects, one per
// non-empty line, in the order typed. A line with no recognised `[ ]`/`[x]`
// marker keeps its whole trimmed text as the title and defaults to unchecked -
// so plain lines (no marker at all) still work, matching how a user would
// paste in a plain list rather than one already formatted as a checklist.
function parseChecklistItemsText(text) {
if (typeof text !== 'string') return [];
return text
.split('\n')
.map(line => line.trim())
.filter(line => line !== '')
.map(line => {
const match = line.match(MARKER_RE);
if (match) {
const isFinished = match[1].toLowerCase() === 'x';
const title = match[2].trim();
return { title, isFinished };
}
return { title: line, isFinished: false };
})
.filter(entry => entry.title !== '');
}
// Build the plan to turn `existingItems` (each { _id, title, isFinished, sort
// ... }, in current display order) into `parsedLines` (each { title,
// isFinished }, the new desired order), replacing the list while preserving
// as much of each existing item document as practical:
//
// - A parsed line whose title exactly matches an as-yet-unmatched existing
// item's title is a KEEP: that item's `_id` (and therefore every other
// field on it - dueAt included) is reused, only its `sort` (and `isFinished`
// if the marker changed) is updated. Matching is by title text and consumes
// the existing items top-to-bottom so that reordering identical-looking
// items still pairs them 1:1 rather than losing one.
// - A parsed line with no remaining match is an INSERT (a brand new item).
// - An existing item nothing matched is a REMOVE (its text is gone from the
// new text, so nothing else can be reasonably preserving it).
//
// Returns { keep: [{ _id, sort, isFinished }], insert: [{ title, isFinished,
// sort }], remove: [_id] }. `sort` values are plain increasing integers in
// the new order - callers only need them to sort correctly relative to each
// other, not to match any particular numbering scheme.
function planChecklistItemsTextUpdate(existingItems, parsedLines) {
const pool = Array.isArray(existingItems) ? existingItems.slice() : [];
const usedIndexes = new Set();
const keep = [];
const insert = [];
(Array.isArray(parsedLines) ? parsedLines : []).forEach((line, order) => {
const matchIndex = pool.findIndex(
(item, idx) => !usedIndexes.has(idx) && item.title === line.title,
);
if (matchIndex === -1) {
insert.push({ title: line.title, isFinished: !!line.isFinished, sort: order });
return;
}
usedIndexes.add(matchIndex);
keep.push({
_id: pool[matchIndex]._id,
sort: order,
isFinished: !!line.isFinished,
});
});
const remove = pool
.map((item, idx) => (usedIndexes.has(idx) ? null : item._id))
.filter(id => id !== null);
return { keep, insert, remove };
}
export { checklistItemsToText, parseChecklistItemsText, planChecklistItemsTextUpdate };