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

Update the clipboard copy logic

Dombi Attila49c722a008c3
diff --git a/config/locales/js-en.yml b/config/locales/js-en.yml
index 63c2cad25dd..ef1a4181b16 100644
--- a/config/locales/js-en.yml
+++ b/config/locales/js-en.yml
@@ -125,7 +125,7 @@ en:
caption_rate_history: "Rate history"
clipboard:
- browser_error: "Your browser doesn't support copying to clipboard. Please copy the selected text manually."
+ browser_error: "Your browser doesn't support copying to clipboard. Please copy it manually: %{content}"
copied_successful: "Successfully copied to clipboard!"
chart:
@@ -367,8 +367,6 @@ en:
token_name_description_text: "This name will be used to distinguish it form other tokens in the <a href=\"my/access_token\", target=\"_blank\">access tokens list</a>. It can only be used once per calendar."
copy_url_label: "Copy URL"
ical_generation_error_text: "An error occured while generating the iCal URL."
- copy_url_success_text: "URL copied to clipboard."
- copy_url_error_text: "Due to browser errors, the URL couldn't be copied to the clipboard. Please copy it manually: %{icalUrl}"
label_activate: "Activate"
label_assignee: 'Assignee'
diff --git a/frontend/src/app/shared/components/copy-to-clipboard/copy-to-clipboard.directive.ts b/frontend/src/app/shared/components/copy-to-clipboard/copy-to-clipboard.directive.ts
index 0d9c88cd84b..9cb6cec3141 100644
--- a/frontend/src/app/shared/components/copy-to-clipboard/copy-to-clipboard.directive.ts
+++ b/frontend/src/app/shared/components/copy-to-clipboard/copy-to-clipboard.directive.ts
@@ -73,6 +73,8 @@ export class CopyToClipboardComponent implements OnInit {
onClick($event:JQuery.TriggeredEvent) {
$event.preventDefault();
- this.copyToClipboardService.copy(this.target.val()!.toString());
+ // Select the text in case the clipboard is not supported by the browser
+ this.target.select().focus();
+ this.copyToClipboardService.copy(String(this.target.val()));
}
}
diff --git a/frontend/src/app/shared/components/copy-to-clipboard/copy-to-clipboard.service.ts b/frontend/src/app/shared/components/copy-to-clipboard/copy-to-clipboard.service.ts
index c8fcd3af37b..931a84c8c0a 100644
--- a/frontend/src/app/shared/components/copy-to-clipboard/copy-to-clipboard.service.ts
+++ b/frontend/src/app/shared/components/copy-to-clipboard/copy-to-clipboard.service.ts
@@ -26,7 +26,7 @@
// See COPYRIGHT and LICENSE files for more details.
//++
-import { Injectable, Renderer2, RendererFactory2 } from '@angular/core';
+import { Injectable } from '@angular/core';
import { ToastService } from 'core-app/shared/components/toaster/toast.service';
import { I18nService } from 'core-app/core/i18n/i18n.service';
@@ -35,48 +35,25 @@ import { I18nService } from 'core-app/core/i18n/i18n.service';
})
export class CopyToClipboardService {
-
- private renderer: Renderer2;
-
constructor(
readonly toastService:ToastService,
readonly I18n:I18nService,
- readonly rendererFactory:RendererFactory2,
- ) {
- this.renderer = rendererFactory.createRenderer(null, null);
- }
+ ) { }
copy(content:string) {
- const supported = (document.queryCommandSupported && document.queryCommandSupported('copy'));
- const contentEl = this.appendContentEl(content);
-
- // At least select the input for the user
- // even when clipboard API not supported
- contentEl.select();
- contentEl.focus();
- if (supported) {
- try {
- // Copy it to the clipboard and remove the hidden input
- if (document.execCommand('copy')) {
- this.renderer.removeChild(document.body, contentEl);
+ if (!navigator.clipboard) {
+ // fallback for browsers that don't support clipboard API at all
+ this.addNotification('addError', this.I18n.t('js.clipboard.browser_error', { content }));
+ } else {
+ void navigator.clipboard.writeText(content)
+ .then(() => {
this.addNotification('addSuccess', this.I18n.t('js.clipboard.copied_successful'));
- return;
- }
- } catch (e) {
- console.log(
- `Your browser seems to support the clipboard API, but copying failed: ${e}`,
- );
- }
+ })
+ .catch(() => {
+ // fallback when running into e.g. browser permission errors
+ this.addNotification('addError', this.I18n.t('js.clipboard.browser_error', { content }));
+ });
}
-
- this.addNotification('addError', this.I18n.t('js.clipboard.browser_error'));
- }
-
- appendContentEl(value:string):JQuery<HTMLElement> {
- const contentEl = this.renderer.createElement('input');
- this.renderer.setAttribute(contentEl, 'value', value);
- this.renderer.appendChild(document.body, contentEl);
- return contentEl;
}
addNotification(type:'addSuccess'|'addError', message:string) {
diff --git a/frontend/src/app/shared/components/modals/get-ical-url-modal/query-get-ical-url.modal.ts b/frontend/src/app/shared/components/modals/get-ical-url-modal/query-get-ical-url.modal.ts
index a344645008d..05ea1822d30 100644
--- a/frontend/src/app/shared/components/modals/get-ical-url-modal/query-get-ical-url.modal.ts
+++ b/frontend/src/app/shared/components/modals/get-ical-url-modal/query-get-ical-url.modal.ts
@@ -46,6 +46,7 @@ import { I18nService } from 'core-app/core/i18n/i18n.service';
import { IsolatedQuerySpace } from 'core-app/features/work-packages/directives/query-space/isolated-query-space';
import { ApiV3Service } from 'core-app/core/apiv3/api-v3.service';
import { QueryResource } from 'core-app/features/hal/resources/query-resource';
+import { CopyToClipboardService } from 'core-app/shared/components/copy-to-clipboard/copy-to-clipboard.service';
interface TokenNameFormValue {
name:string;
@@ -72,7 +73,6 @@ export class QueryGetIcalUrlModalComponent extends OpModalComponent implements O
token_name_description_text: this.I18n.t('js.ical_sharing_modal.token_name_description_text'),
token_name_already_in_use_error_text: this.I18n.t('js.ical_sharing_modal.token_name_already_in_use_error_text'),
button_copy: this.I18n.t('js.ical_sharing_modal.copy_url_label'),
- copy_success_text: this.I18n.t('js.ical_sharing_modal.copy_url_success_text'),
button_cancel: this.I18n.t('js.button_cancel'),
close_popup: this.I18n.t('js.close_popup_title'),
};
@@ -97,6 +97,7 @@ export class QueryGetIcalUrlModalComponent extends OpModalComponent implements O
readonly halNotification:HalResourceNotificationService,
readonly toastService:ToastService,
protected apiV3Service:ApiV3Service,
+ protected copyToClipboardService:CopyToClipboardService,
) {
super(locals, cdRef, elementRef);
}
@@ -117,28 +118,6 @@ export class QueryGetIcalUrlModalComponent extends OpModalComponent implements O
}
}
- public copyUrlAndCloseModal(url:string):void {
- if (!navigator.clipboard) {
- // fallback for browsers that don't support clipboard API at all
- this.toastService.addWarning(
- this.I18n.t('js.ical_sharing_modal.copy_url_error_text', { icalUrl: url }),
- );
- } else {
- void navigator.clipboard.writeText(url)
- .then(() => {
- this.toastService.addSuccess(this.text.copy_success_text);
- })
- .catch(() => {
- // fallback when running into e.g. browser permission errors
- this.toastService.addWarning(
- this.I18n.t('js.ical_sharing_modal.copy_url_error_text', { icalUrl: url }),
- );
- });
- }
-
- this.closeMe();
- }
-
public generateAndCopyUrl():void {
if (this.isBusy) {
return;
@@ -157,8 +136,8 @@ export class QueryGetIcalUrlModalComponent extends OpModalComponent implements O
void promise
.then((response:{ icalUrl:{ href:string } }) => {
- icalUrl = String(response.icalUrl.href);
- this.copyUrlAndCloseModal(icalUrl);
+ this.copyToClipboardService.copy(String(response.icalUrl.href))
+ this.closeMe();
})
.catch((error:{ message:string }) => {
this.nameControl?.markAsDirty();
diff --git a/frontend/src/app/shared/components/op-context-menu/wp-context-menu/wp-single-context-menu.ts b/frontend/src/app/shared/components/op-context-menu/wp-context-menu/wp-single-context-menu.ts
index 046757513d9..ec1be0e39d7 100644
--- a/frontend/src/app/shared/components/op-context-menu/wp-context-menu/wp-single-context-menu.ts
+++ b/frontend/src/app/shared/components/op-context-menu/wp-context-menu/wp-single-context-menu.ts
@@ -36,8 +36,8 @@ export class WorkPackageSingleContextMenuDirective extends OpContextMenuTrigger
readonly opModalService:OpModalService,
readonly opContextMenuService:OPContextMenuService,
readonly authorisationService:AuthorisationService,
- protected copyToClipboardService:CopyToClipboardService
- ) {
+ protected copyToClipboardService:CopyToClipboardService,
+ ) {
super(elementRef, opContextMenuService);
}
diff --git a/frontend/src/app/shared/components/op-context-menu/wp-context-menu/wp-static-context-menu-actions.ts b/frontend/src/app/shared/components/op-context-menu/wp-context-menu/wp-static-context-menu-actions.ts
index 34d9cdf62e5..d3c0ad51519 100644
--- a/frontend/src/app/shared/components/op-context-menu/wp-context-menu/wp-static-context-menu-actions.ts
+++ b/frontend/src/app/shared/components/op-context-menu/wp-context-menu/wp-static-context-menu-actions.ts
@@ -17,7 +17,7 @@ export const PERMITTED_CONTEXT_MENU_ACTIONS:WorkPackageAction[] = [
{
key: 'copy_to_clipboard',
icon: 'icon-clipboard',
- link: 'id'
+ link: 'id',
},
{
key: 'copy_to_other_project',
diff --git a/spec/features/work_packages/bulk/copy_work_package_spec.rb b/spec/features/work_packages/bulk/copy_work_package_spec.rb
index 967d43e2a78..466c5b10473 100644
--- a/spec/features/work_packages/bulk/copy_work_package_spec.rb
+++ b/spec/features/work_packages/bulk/copy_work_package_spec.rb
@@ -233,6 +233,24 @@ RSpec.describe 'Copy work packages through Rails view', js: true do
end
end
+ describe 'copying work package to clipboard' do
+ let(:current_user) { dev }
+ let(:wp_table_target) { Pages::WorkPackagesTable.new(project2) }
+
+ before do
+ wp_table.expect_work_package_count 2
+ context_menu.open_for work_package
+ context_menu.choose 'Copy to clipboard'
+ end
+
+ it 'successfully copies the short url of the work package' do
+ # We cannot access the navigator.clipboard from a headless browser.
+ # This test makes sure the copy to clipboard logic is working,
+ # regardless of the browser permissions.
+ expect(page).to have_content("/wp/#{work_package.id}")
+ end
+ end
+
describe 'accessing the bulk copy from the card view' do
before do
display_representation.switch_to_card_layout
diff --git a/spec/features/work_packages/details/context_menu_spec.rb b/spec/features/work_packages/details/context_menu_spec.rb
index 51be0ff6711..d872dc4137f 100644
--- a/spec/features/work_packages/details/context_menu_spec.rb
+++ b/spec/features/work_packages/details/context_menu_spec.rb
@@ -9,14 +9,22 @@ RSpec.describe 'Work package single context menu', js: true do
before do
login_as(user)
wp_view.visit!
+ find('#action-show-more-dropdown-menu .button').click
end
it 'sets the correct copy project link' do
- find('#action-show-more-dropdown-menu .button').click
find('.menu-item', text: 'Copy to other project', exact_text: true).click
-
expect(page).to have_selector('h2', text: I18n.t(:button_copy))
expect(page).to have_selector('a.work_package', text: "##{work_package.id}")
expect(page).to have_current_path /work_packages\/move\/new\?copy=true&ids\[\]=#{work_package.id}/
end
+
+ it 'successfully copies the short url of the work package' do
+ find('.menu-item', text: 'Copy to clipboard', exact_text: true).click
+
+ # We cannot access the navigator.clipboard from a headless browser.
+ # This test makes sure the copy to clipboard logic is working,
+ # regardless of the browser permissions.
+ expect(page).to have_content("/wp/#{work_package.id}")
+ end
end