Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Extact quote comments into standalone controller

Kabiru Mwenjaf568373a31b4
diff --git a/app/components/work_packages/activities_tab/journals/item_component.rb b/app/components/work_packages/activities_tab/journals/item_component.rb
index f93b90da0f5..008613f9d4f 100644
--- a/app/components/work_packages/activities_tab/journals/item_component.rb
+++ b/app/components/work_packages/activities_tab/journals/item_component.rb
@@ -136,18 +136,29 @@ module WorkPackages
menu.with_item(label: t("js.label_quote_comment"),
tag: :button,
content_arguments: {
- data: {
- action: "click->work-packages--activities-tab--index#quote",
- "content-param": journal.notes,
- "user-id-param": journal.user_id,
- "user-name-param": journal.user.name,
- "text-wrote-param": t(:text_wrote),
- test_selector: "op-wp-journal-#{journal.id}-quote"
- }
+ data: quote_action_data_attributes
}) do |item|
item.with_leading_visual_icon(icon: :quote)
end
end
+
+ def quote_action_data_attributes # rubocop:disable Metrics/AbcSize
+ {
+ controller: quote_comments_stimulus_controller,
+ "application-target": "dynamic",
+ action: "click->#{quote_comments_stimulus_controller}#quote:prevent",
+ quote_comments_stimulus_controller("-content-param") => journal.notes,
+ quote_comments_stimulus_controller("-user-id-param") => journal.user_id,
+ quote_comments_stimulus_controller("-user-name-param") => journal.user.name,
+ quote_comments_stimulus_controller("-text-wrote-param") => I18n.t(:text_wrote),
+ quote_comments_stimulus_controller("-#{index_stimulus_controller}-outlet") => "##{index_stimulus_controller_wrapper_key}", # rubocop:disable Layout/LineLength
+ test_selector: "op-wp-journal-#{journal.id}-quote"
+ }
+ end
+
+ def quote_comments_stimulus_controller(suffix = nil) = "work-packages--activities-tab--quote-comment#{suffix}"
+ def index_stimulus_controller(suffix = nil) = "work-packages--activities-tab--index#{suffix}"
+ def index_stimulus_controller_wrapper_key = WorkPackages::ActivitiesTab::IndexComponent.wrapper_key
end
end
end
diff --git a/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/index.controller.ts b/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/index.controller.ts
index 84d02e9a084..b07a2134f00 100644
--- a/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/index.controller.ts
+++ b/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/index.controller.ts
@@ -495,7 +495,7 @@ export default class IndexController extends Controller {
return this.element.querySelector('opce-ckeditor-augmented-textarea');
}
- private getCkEditorInstance():ICKEditorInstance | null {
+ getCkEditorInstance():ICKEditorInstance | null {
const AngularCkEditorElement = this.getCkEditorElement();
return AngularCkEditorElement ? jQuery(AngularCkEditorElement).data('editor') as ICKEditorInstance : null;
}
@@ -650,44 +650,6 @@ export default class IndexController extends Controller {
}
}
- quote(event:Event) {
- event.preventDefault();
- const target = event.currentTarget as HTMLElement;
- const userId = target.dataset.userIdParam as string;
- const userName = target.dataset.userNameParam as string;
- const textWrote = target.dataset.textWroteParam as string;
- const content = target.dataset.contentParam as string;
-
- const quotedText = this.quotedText(content, userId, userName, textWrote);
- const formVisible = !this.formRowTarget.classList.contains('d-none');
- if (formVisible) {
- this.insertQuoteOnExistingEditor(quotedText);
- } else {
- this.openEditorWithInitialData(quotedText);
- }
- }
-
- private quotedText(rawComment:string, userId:string, userName:string, textWrote:string) {
- const quoted = rawComment.split('\n')
- .map((line:string) => `\n> ${line}`)
- .join('');
-
- // if we ever change CKEditor or how @mentions work this will break
- return `<mention class="mention" data-id="${userId}" data-type="user" data-text="@${userName}">@${userName}</mention> ${textWrote}:\n\n${quoted}`;
- }
-
- insertQuoteOnExistingEditor(quotedText:string) {
- const ckEditorInstance = this.getCkEditorInstance();
- if (ckEditorInstance) {
- const editorData = ckEditorInstance.getData({ trim: false });
- if (editorData.endsWith('<br>') || editorData.endsWith('\n')) {
- ckEditorInstance.setData(`${editorData}${quotedText}`);
- } else {
- ckEditorInstance.setData(`${editorData}\n\n${quotedText}`);
- }
- }
- }
-
openEditorWithInitialData(quotedText:string) {
this.showForm();
const ckEditorInstance = this.getCkEditorInstance();
diff --git a/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/quote-comment.controller.ts b/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/quote-comment.controller.ts
new file mode 100644
index 00000000000..5b977905f4d
--- /dev/null
+++ b/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/quote-comment.controller.ts
@@ -0,0 +1,88 @@
+/*
+ * -- copyright
+ * OpenProject is an open source project management software.
+ * Copyright (C) 2023 the OpenProject GmbH
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License version 3.
+ *
+ * OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+ * Copyright (C) 2006-2013 Jean-Philippe Lang
+ * Copyright (C) 2010-2013 the ChiliProject Team
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * See COPYRIGHT and LICENSE files for more details.
+ * ++
+ */
+
+import { Controller } from '@hotwired/stimulus';
+import type IndexController from './index.controller';
+
+type QuoteParams = {
+ userId:string;
+ userName:string;
+ textWrote:string;
+ content:string;
+};
+
+export default class QuoteCommentController extends Controller {
+ static outlets = ['work-packages--activities-tab--index'];
+
+ declare readonly workPackagesActivitiesTabIndexOutlet:IndexController;
+
+ quote({ params: { userId, userName, textWrote, content } }:{ params:QuoteParams }) {
+ const quotedText = this.quotedText(content, userId, userName, textWrote);
+
+ if (this.isFormVisible) {
+ this.insertQuoteOnExistingEditor(quotedText);
+ } else {
+ this.openEditorWithInitialData(quotedText);
+ }
+ }
+
+ private quotedText(rawComment:string, userId:string, userName:string, textWrote:string) {
+ const quoted = rawComment.split('\n')
+ .map((line:string) => `\n> ${line}`)
+ .join('');
+
+ // if we ever change CKEditor or how @mentions work this will break
+ return `<mention class="mention" data-id="${userId}" data-type="user" data-text="@${userName}">@${userName}</mention> ${textWrote}:\n\n${quoted}`;
+ }
+
+ private insertQuoteOnExistingEditor(quotedText:string) {
+ if (this.ckEditorInstance) {
+ const editorData = this.ckEditorInstance.getData({ trim: false });
+
+ if (editorData.endsWith('<br>') || editorData.endsWith('\n')) {
+ this.ckEditorInstance.setData(`${editorData}${quotedText}`);
+ } else {
+ this.ckEditorInstance.setData(`${editorData}\n\n${quotedText}`);
+ }
+ }
+ }
+
+ private openEditorWithInitialData(quotedText:string) {
+ this.workPackagesActivitiesTabIndexOutlet.openEditorWithInitialData(quotedText);
+ }
+
+ private get ckEditorInstance() {
+ return this.workPackagesActivitiesTabIndexOutlet.getCkEditorInstance();
+ }
+
+ private get isFormVisible():boolean {
+ return !this.workPackagesActivitiesTabIndexOutlet.formRowTarget.classList.contains('d-none');
+ }
+}