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

Listen to attachment added events for form submission

Kabiru Mwenja646c897b93c1
diff --git a/frontend/src/app/core/state/attachments/attachments.service.ts b/frontend/src/app/core/state/attachments/attachments.service.ts
index 652dcb334e8..fa845335353 100644
--- a/frontend/src/app/core/state/attachments/attachments.service.ts
+++ b/frontend/src/app/core/state/attachments/attachments.service.ts
@@ -144,6 +144,15 @@ export class AttachmentsResourceService extends ResourceStoreService<IAttachment
);
}
+ public static getAttachmentsSelfLink(resource: HalResource): string | null {
+ if (isNewResource(resource)) {
+ return null;
+ }
+
+ const attachments = resource.attachments as { href?: string };
+ return attachments?.href || null;
+ }
+
private uploadAttachments(href:string, files:IUploadFile[]):Observable<IAttachment[]> {
const observables = this.uploadService.upload<IAttachment>(href, files);
const uploads = files.map((f, i):[File, Observable<HttpEvent<unknown>>] => [f.file, observables[i]]);
@@ -193,15 +202,6 @@ export class AttachmentsResourceService extends ResourceStoreService<IAttachment
return null;
}
- private static getAttachmentsSelfLink(resource:HalResource):string|null {
- if (isNewResource(resource)) {
- return null;
- }
-
- const attachments = resource.attachments as { href?:string };
- return attachments?.href || null;
- }
-
private static getAddAttachmentsLink(resource:HalResource):string|null {
if (isNewResource(resource)) {
return null;
diff --git a/frontend/src/app/features/hal/resources/activity-comment-resource.ts b/frontend/src/app/features/hal/resources/activity-comment-resource.ts
new file mode 100644
index 00000000000..fa53e72842a
--- /dev/null
+++ b/frontend/src/app/features/hal/resources/activity-comment-resource.ts
@@ -0,0 +1,48 @@
+//-- copyright
+// OpenProject is an open source project management software.
+// Copyright (C) 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 { HalResource } from 'core-app/features/hal/resources/hal-resource';
+import { Attachable } from 'core-app/features/hal/resources/mixins/attachable-mixin';
+import { AttachmentCollectionResource } from './attachment-collection-resource';
+
+interface ActivityCommentResourceEmbedded {
+ attachments:AttachmentCollectionResource;
+}
+
+interface ActivityCommentResourceLinks extends ActivityCommentResourceEmbedded {
+ addAttachment(attachment:HalResource):Promise<unknown>;
+}
+
+class ActivityCommentBaseResource extends HalResource {
+ public $embedded:ActivityCommentResourceEmbedded;
+ public $links:ActivityCommentResourceLinks;
+}
+
+export const ActivityCommentResource = Attachable(ActivityCommentBaseResource);
+
+export interface ActivityCommentResource extends ActivityCommentBaseResource, ActivityCommentResourceLinks {}
diff --git a/frontend/src/app/features/hal/services/hal-resource.config.ts b/frontend/src/app/features/hal/services/hal-resource.config.ts
index 9df85e17d68..83233809f1f 100644
--- a/frontend/src/app/features/hal/services/hal-resource.config.ts
+++ b/frontend/src/app/features/hal/services/hal-resource.config.ts
@@ -66,6 +66,7 @@ import { RelationResource } from 'core-app/features/hal/resources/relation-resou
import { QueryFilterResource } from 'core-app/features/hal/resources/query-filter-resource';
import { SchemaResource } from 'core-app/features/hal/resources/schema-resource';
import { MeetingResource } from 'core-app/features/hal/resources/meeting-resource';
+import { ActivityCommentResource } from '../resources/activity-comment-resource';
const halResourceDefaultConfig:{ [typeName:string]:HalResourceFactoryConfigInterface } = {
WorkPackage: {
@@ -87,7 +88,7 @@ const halResourceDefaultConfig:{ [typeName:string]:HalResourceFactoryConfigInter
},
},
'Activity::Comment': {
- cls: HalResource,
+ cls: ActivityCommentResource,
attrTypes: {
user: 'User',
},
diff --git a/frontend/src/app/shared/components/editor/components/ckeditor-augmented-textarea/ckeditor-augmented-textarea.component.ts b/frontend/src/app/shared/components/editor/components/ckeditor-augmented-textarea/ckeditor-augmented-textarea.component.ts
index 0d0fc718a38..6fed27393bf 100644
--- a/frontend/src/app/shared/components/editor/components/ckeditor-augmented-textarea/ckeditor-augmented-textarea.component.ts
+++ b/frontend/src/app/shared/components/editor/components/ckeditor-augmented-textarea/ckeditor-augmented-textarea.component.ts
@@ -36,28 +36,31 @@ import {
Output,
ViewChild,
} from '@angular/core';
+import { navigator } from '@hotwired/turbo';
+import { componentDestroyed } from '@w11k/ngx-componentdestroyed';
+import { I18nService } from 'core-app/core/i18n/i18n.service';
import { PathHelperService } from 'core-app/core/path-helper/path-helper.service';
+import { IAttachment } from 'core-app/core/state/attachments/attachment.model';
+import { AttachmentsResourceService } from 'core-app/core/state/attachments/attachments.service';
+import { States } from 'core-app/core/states/states.service';
+import { HAL_NEW_RESOURCE_ID } from 'core-app/features/hal/helpers/is-new-resource';
+import { AttachmentCollectionResource } from 'core-app/features/hal/resources/attachment-collection-resource';
import { HalResource } from 'core-app/features/hal/resources/hal-resource';
import { HalResourceService } from 'core-app/features/hal/services/hal-resource.service';
-import { States } from 'core-app/core/states/states.service';
-import { filter, takeUntil } from 'rxjs/operators';
-import { ToastService } from 'core-app/shared/components/toaster/toast.service';
-import { I18nService } from 'core-app/core/i18n/i18n.service';
+import { populateInputsFromDataset } from 'core-app/shared/components/dataset-inputs';
import {
ICKEditorMacroType,
ICKEditorType,
} from 'core-app/shared/components/editor/components/ckeditor/ckeditor-setup.service';
-import { OpCkeditorComponent } from 'core-app/shared/components/editor/components/ckeditor/op-ckeditor.component';
-import { componentDestroyed } from '@w11k/ngx-componentdestroyed';
-import { UntilDestroyedMixin } from 'core-app/shared/helpers/angular/until-destroyed.mixin';
import {
ICKEditorContext,
ICKEditorInstance,
} from 'core-app/shared/components/editor/components/ckeditor/ckeditor.types';
+import { OpCkeditorComponent } from 'core-app/shared/components/editor/components/ckeditor/op-ckeditor.component';
+import { ToastService } from 'core-app/shared/components/toaster/toast.service';
+import { UntilDestroyedMixin } from 'core-app/shared/helpers/angular/until-destroyed.mixin';
import { fromEvent } from 'rxjs';
-import { AttachmentCollectionResource } from 'core-app/features/hal/resources/attachment-collection-resource';
-import { populateInputsFromDataset } from 'core-app/shared/components/dataset-inputs';
-import { navigator } from '@hotwired/turbo';
+import { filter, take, takeUntil } from 'rxjs/operators';
@Component({
@@ -95,6 +98,9 @@ export class CkeditorAugmentedTextareaComponent extends UntilDestroyedMixin impl
// Output focus events
@Output() editorFocus = new EventEmitter<void>();
+ // Output attachment added events
+ @Output() attachmentAdded = new EventEmitter<IAttachment>();
+
// Which template to include
public element:HTMLElement;
@@ -131,6 +137,7 @@ export class CkeditorAugmentedTextareaComponent extends UntilDestroyedMixin impl
protected Notifications:ToastService,
protected I18n:I18nService,
protected states:States,
+ protected attachmentsResourceService:AttachmentsResourceService,
) {
super();
populateInputsFromDataset(this);
@@ -232,6 +239,19 @@ export class CkeditorAugmentedTextareaComponent extends UntilDestroyedMixin impl
private setupAttachmentAddedCallback(editor:ICKEditorInstance) {
editor.model.on('op:attachment-added', () => {
+ const collectionKey = AttachmentsResourceService.getAttachmentsSelfLink(this.halResource as HalResource) || HAL_NEW_RESOURCE_ID;
+
+ this.attachmentsResourceService
+ .collection(collectionKey)
+ .pipe(
+ take(1),
+ filter((attachments) => attachments.length > 0),
+ )
+ .subscribe((attachments) => {
+ const latestAttachment = attachments[attachments.length - 1];
+ this.attachmentAdded.emit(latestAttachment);
+ });
+
this.states.forResource(this.halResource as HalResource)?.putValue(this.halResource);
});
}
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 b07a2134f00..0a82336698b 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
@@ -28,12 +28,14 @@
* ++
*/
+import { ID } from '@datorama/akita';
import { Controller } from '@hotwired/stimulus';
+import { ApiV3Service } from 'core-app/core/apiv3/api-v3.service';
+import { IAttachment } from 'core-app/core/state/attachments/attachment.model';
+import { TurboRequestsService } from 'core-app/core/turbo/turbo-requests.service';
import {
ICKEditorInstance,
} from 'core-app/shared/components/editor/components/ckeditor/ckeditor.types';
-import { TurboRequestsService } from 'core-app/core/turbo/turbo-requests.service';
-import { ApiV3Service } from 'core-app/core/apiv3/api-v3.service';
enum AnchorType {
Comment = 'comment',
@@ -47,6 +49,10 @@ interface CustomEventWithIdParam extends Event {
};
}
+interface AttachmentEvent extends CustomEvent {
+ detail:IAttachment;
+}
+
export default class IndexController extends Controller {
static values = {
updateStreamsPath: String,
@@ -95,11 +101,13 @@ export default class IndexController extends Controller {
private adjustMarginBound:EventListener;
private onBlurEditorBound:EventListener;
private onFocusEditorBound:EventListener;
+ private onAttachmentAddedBound:EventListener;
private saveInProgress:boolean;
private updateInProgress:boolean;
- private turboRequests:TurboRequestsService;
+ private attachmentIds:Set<ID> = new Set();
+ private turboRequests:TurboRequestsService;
private apiV3Service:ApiV3Service;
async connect() {
@@ -558,6 +566,11 @@ export default class IndexController extends Controller {
}
};
+ this.onAttachmentAddedBound = (event:AttachmentEvent) => {
+ const attachment = event.detail;
+ this.attachmentIds.add(attachment.id);
+ };
+
const editorElement = this.getCkEditorElement();
if (editorElement) {
editorElement.addEventListener('saveRequested', this.onSubmitBound);
@@ -565,6 +578,7 @@ export default class IndexController extends Controller {
editorElement.addEventListener('editorKeyup', this.adjustMarginBound);
editorElement.addEventListener('editorBlur', this.onBlurEditorBound);
editorElement.addEventListener('editorFocus', this.onFocusEditorBound);
+ editorElement.addEventListener('attachmentAdded', this.onAttachmentAddedBound);
}
}
@@ -576,6 +590,7 @@ export default class IndexController extends Controller {
editorElement.removeEventListener('editorKeyup', this.adjustMarginBound);
editorElement.removeEventListener('editorBlur', this.onBlurEditorBound);
editorElement.removeEventListener('editorFocus', this.onFocusEditorBound);
+ editorElement.removeEventListener('attachmentAdded', this.onAttachmentAddedBound);
}
}
@@ -774,6 +789,12 @@ export default class IndexController extends Controller {
formData.append('filter', this.filterValue);
formData.append('journal[notes]', data);
+ if (this.attachmentIds.size > 0) {
+ this.attachmentIds.forEach((attachmentId) => {
+ formData.append('journal[attachment_ids][]', attachmentId.toString());
+ });
+ }
+
return formData;
}