Files
wekan/client/components/activities/comments.js
T
Lauri Ojansivu 3cb089b8ca Checklist from description, action items, and search phrases.
The last three of the features other kanban apps call AI, done with plain
code (docs/Features/General/Assistants/Assistants.md):

- Checklists -> From description: the list lines of a card's description
  ("- ", "* ", "1. ", task boxes; never inside code blocks) previewed, then
  made into a checklist - ticked where the line was [x] - or into subtasks
  through addSubtaskCard.
- Create cards from action items, on a comment and in the card menu for its
  description: "TODO: ...", "- [ ] ..." and "@user ..." lines become cards
  at the top of the card's list, linking back to it, with the mentioned
  user as member when they are an active board member.
- Global search understands plain phrases in the reader's language - "my
  overdue cards", "due this week", "unassigned cards" and seven more - and
  runs them as its operators, saying which; a query with an operator in it
  is never rewritten.

Checklist from description and Action items are Show on Card rows of Board
Settings / Card and of the Admin Panel's Card fields; Search phrases is an
Admin Panel / Settings / Visibility / Features tick. Translated in every
language.

Thanks to xet7 !
2026-10-17 16:25:51 +03:00

314 lines
12 KiB
JavaScript

import { isSubmitKey } from '/models/lib/editorSubmitKey';
import { ReactiveCache } from '/imports/reactiveCache';
import CardComments, { canEditComment, resolveParentId } from '/models/cardComments';
import { groupCommentsByThread } from '/imports/lib/commentThreading';
import { UnsavedEdits } from '/client/lib/unsavedEdits';
import { EscapeActions } from '/client/lib/escapeActions';
import { Utils } from '/client/lib/utils';
import autosize from 'autosize';
const commentFormIsOpen = new ReactiveVar(false);
// _id of the comment currently being replied to (threaded replies, issue #5907).
// Empty string means a new top-level comment.
const replyToCommentId = new ReactiveVar('');
// #5547: persist the comment draft (debounced) into UnsavedEdits while the user
// types, so a comment in progress survives closing the card, clicking outside
// it, or navigating away — the form prefills from {{getUnsavedValue
// 'cardComment' ...}} on the next open. Historical note: the click-open flag
// that armed the old escape-time draft save was removed in 2019 (3b3950369)
// because the escape handler CLEARED the visible text; since then nothing saved
// the draft at all, so any close of the card discarded the comment.
let commentDraftTimer = null;
let commentDraftPending = null; // { cardId, value }
function commentDraftWrite({ cardId, value }) {
if (!cardId) return;
const draftKey = { fieldName: 'cardComment', docId: cardId };
if ((value || '').trim()) {
UnsavedEdits.set(draftKey, value);
} else {
UnsavedEdits.reset(draftKey);
}
}
function scheduleCommentDraftSave(cardId, value) {
commentDraftPending = { cardId, value };
if (commentDraftTimer) clearTimeout(commentDraftTimer);
commentDraftTimer = setTimeout(() => {
commentDraftTimer = null;
const pending = commentDraftPending;
commentDraftPending = null;
if (pending) commentDraftWrite(pending);
}, 300);
}
// Run a pending (debounced) save immediately — e.g. the card is closing.
function flushCommentDraftSave() {
if (commentDraftTimer) {
clearTimeout(commentDraftTimer);
commentDraftTimer = null;
}
const pending = commentDraftPending;
commentDraftPending = null;
if (pending) commentDraftWrite(pending);
}
// Drop any pending save — the comment was submitted, so there is no draft.
function cancelCommentDraftSave() {
if (commentDraftTimer) {
clearTimeout(commentDraftTimer);
commentDraftTimer = null;
}
commentDraftPending = null;
}
Template.commentForm.onDestroyed(function () {
// #5547: the card can close without the escape handler ever running (the X
// button, a route change); make sure the last keystrokes reach the draft.
flushCommentDraftSave();
commentFormIsOpen.set(false);
});
Template.commentForm.helpers({
commentFormIsOpen() {
return commentFormIsOpen.get();
},
// The comment being replied to, if any, so the form can show an
// "In reply to ..." banner with a way to cancel.
replyToComment() {
const id = replyToCommentId.get();
return id ? ReactiveCache.getCardComment(id) : undefined;
},
// The name shown by the "Replying to [author]" banner while composing a
// reply, or undefined when the parent comment/its author can no longer be
// found (the banner then falls back to the plain "In reply to" wording).
replyToCommentAuthorName() {
const id = replyToCommentId.get();
const comment = id ? ReactiveCache.getCardComment(id) : undefined;
const user = comment && ReactiveCache.getUser(comment.userId);
return user && user.getName ? user.getName() : undefined;
},
});
Template.commentForm.events({
// #5547: arm the escape handler below (it saves the draft and closes the
// form); this flag had no setter since 2019, so the handler never ran.
'focus .js-new-comment-input'() {
commentFormIsOpen.set(true);
},
// #5547: keep the draft continuously saved (debounced) while typing.
'input .js-new-comment-input'(evt) {
scheduleCommentDraftSave(Utils.getCurrentCardId(), evt.currentTarget.value);
},
'submit .js-new-comment-form'(evt, tpl) {
const input = tpl.$('.js-new-comment-input');
const text = input.val().trim();
const card = Template.currentData();
let boardId = card.boardId;
let cardId = card._id;
if (card.isLinkedCard()) {
boardId = ReactiveCache.getCard(card.linkedId).boardId;
cardId = card.linkedId;
} else if (card.isLinkedBoard()) {
boardId = card.linkedId;
}
if (text) {
const parentId = replyToCommentId.get();
CardComments.insert({
text,
boardId,
cardId,
parentId: parentId || '',
});
// #5547: the comment is saved — cancel any debounced draft save still in
// flight and remove the stored draft so it does not resurface next open.
cancelCommentDraftSave();
UnsavedEdits.reset({
fieldName: 'cardComment',
docId: Utils.getCurrentCardId(),
});
replyToCommentId.set('');
resetCommentInput(input);
Tracker.flush();
autosize.update(input);
input.trigger('submitted');
}
evt.preventDefault();
},
'click .js-cancel-reply'(evt) {
evt.preventDefault();
replyToCommentId.set('');
},
// Follow the same submit-key preference as other multiline editors.
'keydown form textarea'(evt, tpl) {
if (isSubmitKey(evt, { submitOnEnter: !!ReactiveCache.getCurrentUser()?.hasSubmitOnEnter?.() })) {
evt.preventDefault();
evt.stopPropagation();
tpl.find('button[type=submit]').click();
}
},
});
Template.comments.helpers({
getComments() {
const data = Template.currentData();
if (!data || typeof data.comments !== 'function') return [];
// #3011: replies render directly under their (one-level) parent rather
// than interleaved by date with unrelated top-level comments.
return groupCommentsByThread(data.comments());
},
});
// Issue #4757: permalink to a comment. The card's own URL
// (models/lib/cardUrl.js) plus a `#comment-<id>` fragment - see
// client/lib/revealBoardItem.js for how the fragment turns back into a
// scroll+highlight on the other end. Shared by the helper below and the
// click handler, which needs the same URL to build the copy-to-clipboard
// text.
function commentPermalinkFor(comment) {
if (!comment || !comment._id || !comment.cardId) return '';
const card = ReactiveCache.getCard(comment.cardId);
const url = card && card.absoluteUrl();
return url ? `${url}#comment-${comment._id}` : '';
}
Template.comment.helpers({
// The element id `comments.jade` anchors this comment to, so a permalink's
// `#comment-<id>` fragment has something to scroll to.
commentDomId() {
return this._id ? `comment-${this._id}` : undefined;
},
commentPermalink() {
return commentPermalinkFor(this);
},
// Whether the current user may edit/delete this comment, honouring the
// board's restrictCommentEditing setting (issue #5906). Mirrors the
// server-side enforcement so the UI hides buttons the server would reject.
canEditThisComment() {
const user = ReactiveCache.getCurrentUser();
if (!user) return false;
const isAuthor = user._id === this.userId;
const board = ReactiveCache.getBoard(this.boardId);
const isBoardAdmin = !!board && board.hasAdmin(user._id);
const restrictCommentEditing = !!board && !!board.restrictCommentEditing;
return canEditComment({ isAuthor, isBoardAdmin, restrictCommentEditing });
},
// The parent comment this one replies to, or undefined.
parentComment() {
return this.parentId ? ReactiveCache.getCardComment(this.parentId) : undefined;
},
});
Template.comment.events({
// Issue #4757: the timestamp is a real `<a href>` to the permalink, so a
// normal click already navigates there (and back/forward and a fresh load
// of that URL are handled by client/lib/revealBoardItem.js reading the
// hash). This also sets the reveal Session value directly, rather than
// relying only on the browser's `hashchange` event, because neither a
// same-page anchor click nor FlowRouter's `pushState` navigation is
// guaranteed to fire one.
'click .js-comment-permalink'() {
if (this._id) Session.set('revealCommentId', this._id);
},
// docs/Features/Cards/Action-Items.md
'click .js-comment-action-items': Popup.open('actionItems', { titleKey: 'action-items' }),
'click .js-copy-comment-link'(evt, tpl) {
evt.preventDefault();
const url = commentPermalinkFor(this);
if (!url) return;
Utils.showCopied(Utils.copyTextToClipboard(url), tpl.$('.copied-tooltip'));
},
'click .js-reply-comment'(evt) {
evt.preventDefault();
// #3011: cap threading at one level - replying to a reply attaches the
// new comment to that reply's own parent (the original top-level
// comment) instead of nesting further, so the banner and the eventual
// insert both point at the same, already-flattened target.
const targetId = resolveParentId(this._id, id =>
ReactiveCache.getCardComment(id),
);
replyToCommentId.set(targetId);
// Open and focus the new-comment form.
const input = $('.js-new-comment-input');
input.click();
Tracker.afterFlush(() => {
$('.js-new-comment-input').focus();
});
},
'click .js-delete-comment': Popup.afterConfirm('deleteComment', function () {
const commentId = this._id;
// #3252: only remove if the doc is still in the local cache. Under heavy
// archive/delete churn the comment can already be evicted from Minimongo,
// and CardComments.remove() of a missing _id throws "Removed nonexistent
// document" on the client.
if (commentId && CardComments.findOne(commentId)) {
CardComments.remove(commentId);
}
Popup.back();
}),
'submit .js-edit-comment'(evt, tpl) {
evt.preventDefault();
const textarea = tpl.find('.js-edit-comment textarea,input[type=text]');
const commentText = textarea && textarea.value ? textarea.value.trim() : '';
const commentId = tpl.data._id;
if (commentText) {
CardComments.update(commentId, {
$set: {
text: commentText,
},
});
}
},
});
// XXX This should be a static method of the `commentForm` component
function resetCommentInput(input) {
input.val(''); // without manually trigger, input event won't be fired
input.blur();
commentFormIsOpen.set(false);
}
// XXX This should handled a `onUpdated` callback of the `commentForm` component
// but since this callback doesn't exists, and `onRendered` is not called if the
// data is not destroyed and recreated, we simulate the desired callback using
// Tracker.autorun to register the component dependencies, and re-run when these
// dependencies are invalidated. A better component API would remove this hack.
Tracker.autorun(() => {
Utils.getCurrentCardId();
Tracker.afterFlush(() => {
autosize.update($('.js-new-comment-input'));
});
});
EscapeActions.register(
'inlinedForm',
() => {
const draftKey = {
fieldName: 'cardComment',
docId: Utils.getCurrentCardId(),
};
const commentInput = $('.js-new-comment-input');
const draft = (commentInput.val() || '').trim();
cancelCommentDraftSave();
if (draft) {
UnsavedEdits.set(draftKey, draft);
} else {
UnsavedEdits.reset(draftKey);
}
// #5547: do NOT clear the input — clearing it on every click outside the
// comment box is exactly the "comment text disappearing" bug that got this
// handler disarmed in 2019 (3b3950369). The draft is saved above; if the
// card stays open, the text stays visible; if the card closes, the next
// open prefills the form from the draft.
commentInput.blur();
commentFormIsOpen.set(false);
},
() => {
return commentFormIsOpen.get();
},
{
noClickEscapeOn: '.js-new-comment',
},
);