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

avoid editor data loss on tab change or other browser events unmounting the stimulus component

Jonas Jabari8b4b138bc09a
diff --git a/app/components/work_packages/activities_tab/index_component.rb b/app/components/work_packages/activities_tab/index_component.rb
index 9abed8e499e..00295568de8 100644
--- a/app/components/work_packages/activities_tab/index_component.rb
+++ b/app/components/work_packages/activities_tab/index_component.rb
@@ -53,6 +53,8 @@ module WorkPackages
"work-packages--activities-tab--index-update-streams-url-value": update_streams_work_package_activities_url(work_package),
"work-packages--activities-tab--index-sorting-value": journal_sorting,
"work-packages--activities-tab--index-filter-value": filter,
+ "work-packages--activities-tab--index-user-id-value": User.current.id,
+ "work-packages--activities-tab--index-work-package-id-value": work_package.id,
"work-packages--activities-tab--index-polling-interval-in-ms-value": 10000 # protoypical implementation
}
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 5cd753d2aee..014769294a0 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
@@ -4,7 +4,7 @@ import {
ICKEditorInstance,
} from 'core-app/shared/components/editor/components/ckeditor/ckeditor.types';
import { KeyCodes } from 'core-app/shared/helpers/keyCodes.enum';
-import { set } from 'lodash';
+import { workPackageFilesCount } from 'core-app/features/work-packages/components/wp-tabs/services/wp-tabs/wp-files-count.function';
export default class IndexController extends Controller {
static values = {
@@ -12,6 +12,8 @@ export default class IndexController extends Controller {
sorting: String,
pollingIntervalInMs: Number,
filter: String,
+ userId: Number,
+ workPackageId: Number,
};
static targets = ['journalsContainer', 'buttonRow', 'formRow', 'form'];
@@ -27,31 +29,47 @@ export default class IndexController extends Controller {
declare intervallId:number;
declare pollingIntervalInMsValue:number;
declare filterValue:string;
+ declare userIdValue:number;
+ declare workPackageIdValue:number;
+ declare localStorageKey:string;
connect() {
+ this.setLocalStorageKey();
this.setLastUpdateTimestamp();
this.setupEventListeners();
this.handleInitialScroll();
this.startPolling();
+ this.populateRescuedEditorContent();
}
disconnect() {
+ this.rescueEditorContent();
this.removeEventListeners();
this.stopPolling();
}
+ private setLocalStorageKey() {
+ // scoped by user id in order to avoid data leakage when a user logs out and another user logs in on the same browser
+ // TODO: when a user logs out, the data should be removed anyways in order to avoid data leakage
+ this.localStorageKey = `work-package-${this.workPackageIdValue}-rescued-editor-data-${this.userIdValue}`;
+ }
+
private setupEventListeners() {
this.handleWorkPackageUpdate = this.handleWorkPackageUpdate.bind(this);
this.handleVisibilityChange = this.handleVisibilityChange.bind(this);
+ this.rescueEditorContent = this.rescueEditorContent.bind(this);
document.addEventListener('work-package-updated', this.handleWorkPackageUpdate);
document.addEventListener('visibilitychange', this.handleVisibilityChange);
+ document.addEventListener('beforeunload', this.rescueEditorContent);
}
private removeEventListeners() {
this.handleWorkPackageUpdate = this.handleWorkPackageUpdate.bind(this);
this.handleVisibilityChange = this.handleVisibilityChange.bind(this);
+ this.rescueEditorContent = this.rescueEditorContent.bind(this);
document.removeEventListener('work-package-updated', this.handleWorkPackageUpdate);
document.removeEventListener('visibilitychange', this.handleVisibilityChange);
+ document.removeEventListener('beforeunload', this.rescueEditorContent);
}
private handleVisibilityChange() {
@@ -94,6 +112,24 @@ export default class IndexController extends Controller {
}
}
+ private rescueEditorContent() {
+ const ckEditorInstance = this.getCkEditorInstance();
+ if (ckEditorInstance) {
+ const data = ckEditorInstance.getData({ trim: false });
+ if (data.length > 0) {
+ localStorage.setItem(this.localStorageKey, data);
+ }
+ }
+ }
+
+ private populateRescuedEditorContent() {
+ const rescuedEditorContent = localStorage.getItem(this.localStorageKey);
+ if (rescuedEditorContent) {
+ this.openEditorWithInitialData(rescuedEditorContent);
+ localStorage.removeItem(this.localStorageKey);
+ }
+ }
+
private handleInitialScroll() {
if (window.location.hash.includes('#activity-')) {
this.scrollToActivity();
@@ -217,7 +253,7 @@ export default class IndexController extends Controller {
const userName = target.dataset.userNameParam as string;
const content = target.dataset.contentParam as string;
- this.openEditorWithQuotedText(this.quotedText(content, userName));
+ this.openEditorWithInitialData(this.quotedText(content, userName));
}
private quotedText(rawComment:string, userName:string) {
@@ -228,7 +264,7 @@ export default class IndexController extends Controller {
return `${userName}\n${quoted}`;
}
- openEditorWithQuotedText(quotedText:string) {
+ openEditorWithInitialData(quotedText:string) {
this.showForm();
const ckEditorInstance = this.getCkEditorInstance();
if (ckEditorInstance && ckEditorInstance.getData({ trim: false }).length === 0) {