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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Allow saving CKEditor with ctrl+enter or cmd+enter (#13127)
Oliver Güntherae3da7e74211
diff --git a/frontend/src/app/shared/components/dynamic-forms/components/dynamic-inputs/formattable-textarea-input/components/formattable-control/formattable-control.component.html b/frontend/src/app/shared/components/dynamic-forms/components/dynamic-inputs/formattable-textarea-input/components/formattable-control/formattable-control.component.htmlindex b8b27311404..25bde37b162 100644--- a/frontend/src/app/shared/components/dynamic-forms/components/dynamic-inputs/formattable-textarea-input/components/formattable-control/formattable-control.component.html+++ b/frontend/src/app/shared/components/dynamic-forms/components/dynamic-inputs/formattable-textarea-input/components/formattable-control/formattable-control.component.html@@ -2,9 +2,9 @@<op-ckeditor[context]="ckEditorContext"[content]="value?.raw"- (onContentChange)="onContentChange($event)"- (onInitializationFailed)="initializationError = true"- (onInitialized)="onCkeditorSetup($event)"+ (contentChanged)="onContentChange($event)"+ (initializationFailed)="initializationError = true"+ (initializeDone)="onCkeditorSetup($event)"></op-ckeditor>-</div>\ No newline at end of file+</div>diff --git a/frontend/src/app/shared/components/editor/components/ckeditor-augmented-textarea/ckeditor-augmented-textarea.html b/frontend/src/app/shared/components/editor/components/ckeditor-augmented-textarea/ckeditor-augmented-textarea.htmlindex 2c7273d92b8..91ecc9b8e64 100644--- a/frontend/src/app/shared/components/editor/components/ckeditor-augmented-textarea/ckeditor-augmented-textarea.html+++ b/frontend/src/app/shared/components/editor/components/ckeditor-augmented-textarea/ckeditor-augmented-textarea.html@@ -2,8 +2,8 @@<op-ckeditor[context]="context"[content]="initialContent"- (onInitialized)="setup($event)"- (onContentChange)="markEdited()"+ (initializeDone)="setup($event)"+ (contentChanged)="markEdited()"></op-ckeditor></div>diff --git a/frontend/src/app/shared/components/editor/components/ckeditor/ckeditor.types.ts b/frontend/src/app/shared/components/editor/components/ckeditor/ckeditor.types.tsindex 9859a7f2b11..f60aa15bae9 100644--- a/frontend/src/app/shared/components/editor/components/ckeditor/ckeditor.types.ts+++ b/frontend/src/app/shared/components/editor/components/ckeditor/ckeditor.types.ts@@ -4,6 +4,22 @@ import {ICKEditorType,} from 'core-app/shared/components/editor/components/ckeditor/ckeditor-setup.service';+export interface CKEditorEvent {+ stop:() => void;+}++export interface CKEditorListenOptions {+ priority:string;+}++export interface CKEditorDomEventData {+ altKey:boolean;+ shiftKey:boolean;+ ctrlKey:boolean;+ metaKey:boolean;+ keyCode:number;+}+export interface ICKEditorInstance {getData(options:{ trim:boolean }):string;@@ -12,12 +28,26 @@ export interface ICKEditorInstance {destroy():void;enableReadOnlyMode(lockId:string):void;+disableReadOnlyMode(lockId:string):void;on(event:string, callback:() => unknown):void;- model:any;- editing:any;+ listenTo(node:unknown, key:string, callback:(evt:CKEditorEvent, data:CKEditorDomEventData) => unknown, options:CKEditorListenOptions):void;++ model:{+ on(ev:string, callback:() => unknown):void+ fire(ev:string, data:unknown):void+ document:{+ on(ev:string, callback:() => unknown):void+ };+ };+ editing:{+ view:{+ focus():void;+ document:Document+ }+ };config:any;ui:any;element:HTMLElement;diff --git a/frontend/src/app/shared/components/editor/components/ckeditor/op-ckeditor.component.ts b/frontend/src/app/shared/components/editor/components/ckeditor/op-ckeditor.component.tsindex 126a90d241d..02fa45e1c8f 100644--- a/frontend/src/app/shared/components/editor/components/ckeditor/op-ckeditor.component.ts+++ b/frontend/src/app/shared/components/editor/components/ckeditor/op-ckeditor.component.ts@@ -45,11 +45,11 @@ import {ICKEditorWatchdog,} from 'core-app/shared/components/editor/components/ckeditor/ckeditor.types';import { CKEditorSetupService } from 'core-app/shared/components/editor/components/ckeditor/ckeditor-setup.service';+import { KeyCodes } from 'core-app/shared/helpers/keyCodes.enum';+import { debugLog } from 'core-app/shared/helpers/debug_output';declare module 'codemirror';-const manualModeLocalStorageKey = 'op-ckeditor-uses-manual-mode';-@Component({selector: 'op-ckeditor',templateUrl: './op-ckeditor.html',@@ -68,13 +68,16 @@ export class OpCkeditorComponent implements OnInit, OnDestroy {}// Output notification once ready- @Output() onInitialized = new EventEmitter<ICKEditorInstance>();+ @Output() initializeDone = new EventEmitter<ICKEditorInstance>();// Output notification at max once/s for data changes- @Output() onContentChange = new EventEmitter<string>();+ @Output() contentChanged = new EventEmitter<string>();// Output notification when editor cannot be initialized- @Output() onInitializationFailed = new EventEmitter<string>();+ @Output() initializationFailed = new EventEmitter<string>();++ // Output save requests (ctrl+enter and cmd+enter)+ @Output() saveRequested = new EventEmitter<string>();// View container of the replacement used to initialize CKEditor5@ViewChild('opCkeditorReplacementContainer', { static: true }) opCkeditorReplacementContainer:ElementRef;@@ -105,9 +108,9 @@ export class OpCkeditorComponent implements OnInit, OnDestroy {// to read back changes as they happenprivate debouncedEmitter = _.debounce(() => {- this.getTransformedContent(false)+ void this.getTransformedContent(false).then((val) => {- this.onContentChange.emit(val);+ this.contentChanged.emit(val);});},1000,@@ -116,11 +119,13 @@ export class OpCkeditorComponent implements OnInit, OnDestroy {private $element:JQuery;- constructor(private readonly elementRef:ElementRef,+ constructor(+ private readonly elementRef:ElementRef,private readonly Notifications:ToastService,private readonly I18n:I18nService,private readonly configurationService:ConfigurationService,- private readonly ckEditorSetup:CKEditorSetupService) {+ private readonly ckEditorSetup:CKEditorSetupService,+ ) {}/**@@ -176,13 +181,14 @@ export class OpCkeditorComponent implements OnInit, OnDestroy {ngOnInit() {try {this.initializeEditor();- } catch (error) {+ } catch (error:unknown) {// We will run into this error if, among others, the browser does not fully support// CKEditor's requirements on ES6.- console.error(`Failed to setup CKEditor instance: ${error}`);- this.error = error;- this.onInitializationFailed.emit(error);+ const message = (error as Error).toString();+ console.error('Failed to setup CKEditor instance: %O', error);+ this.error = message;+ this.initializationFailed.emit(message);}}@@ -208,18 +214,21 @@ export class OpCkeditorComponent implements OnInit, OnDestroy {}).then((watchdog:ICKEditorWatchdog) => {this.setupWatchdog(watchdog);- this.ckEditorInstance = watchdog.editor;+ const editor = watchdog.editor;+ this.ckEditorInstance = editor;- // Save changes while in wysiwyg mode- watchdog.editor.model.document.on('change', this.debouncedEmitter);// Switch mode- watchdog.editor.on('op:source-code-enabled', () => this.enableManualMode());- watchdog.editor.on('op:source-code-disabled', () => this.disableManualMode());+ editor.on('op:source-code-enabled', () => this.enableManualMode());+ editor.on('op:source-code-disabled', () => this.disableManualMode());++ // Capture CTRL+ENTER commands+ this.interceptModifiedEnterKeystrokes(editor);// Emit global dragend events for other drop zones to react.// This is needed, as CKEditor does not bubble any drag events- const model = watchdog.editor.model as unknown&{ on:(ev:string, callback:() => unknown) => void };+ const model = watchdog.editor.model;+ model.document.on('change', this.debouncedEmitter);model.on('op:attachment-added', () => document.body.dispatchEvent(new DragEvent('dragend')));model.on('op:attachment-removed', () => document.body.dispatchEvent(new DragEvent('dragend')));@@ -230,13 +239,28 @@ export class OpCkeditorComponent implements OnInit, OnDestroy {// not vanishing after ending the drag.this.$element.on('dragleave', () => document.body.dispatchEvent(new DragEvent('dragleave')));- this.onInitialized.emit(watchdog.editor);+ this.initializeDone.emit(watchdog.editor);return watchdog.editor;});this.$element.data('editor', editorPromise);}+ private interceptModifiedEnterKeystrokes(editor:ICKEditorInstance) {+ editor.listenTo(+ editor.editing.view.document,+ 'keydown',+ (evt, data) => {+ if ((data.ctrlKey || data.metaKey) && data.keyCode === KeyCodes.ENTER) {+ debugLog('Sending save request from CKEditor.');+ this.saveRequested.emit();+ evt.stop();+ }+ },+ { priority: 'highest' },+ );+ }+/*** Disable the manual mode, kill the codeMirror instance and switch back to CKEditor*/diff --git a/frontend/src/app/shared/components/fields/edit/field-types/formattable-edit-field/formattable-edit-field.component.html b/frontend/src/app/shared/components/fields/edit/field-types/formattable-edit-field/formattable-edit-field.component.htmlindex c34aefb7621..b9cd1473fe1 100644--- a/frontend/src/app/shared/components/fields/edit/field-types/formattable-edit-field/formattable-edit-field.component.html+++ b/frontend/src/app/shared/components/fields/edit/field-types/formattable-edit-field/formattable-edit-field.component.html@@ -2,9 +2,10 @@<div class="op-ckeditor--wrapper op-ckeditor-element"><op-ckeditor [context]="ckEditorContext"[content]="initialContent"- (onContentChange)="onContentChange($event)"- (onInitializationFailed)="initializationError = true"- (onInitialized)="onCkeditorSetup($event)"+ (contentChanged)="onContentChange($event)"+ (initializationFailed)="initializationError = true"+ (initializeDone)="onCkeditorSetup($event)"+ (saveRequested)="handleUserSubmit()"></op-ckeditor></div>diff --git a/spec/features/work_packages/details/markdown/description_editor_spec.rb b/spec/features/work_packages/details/markdown/description_editor_spec.rbindex 27c78f20d37..e5c077b8f5b 100644--- a/spec/features/work_packages/details/markdown/description_editor_spec.rb+++ b/spec/features/work_packages/details/markdown/description_editor_spec.rb@@ -84,6 +84,14 @@ RSpec.describe 'description inplace editor', js: true, selenium: true do# Cancelling through the action panelfield.cancel_by_clickfield.expect_inactive!++ # Saving the field with ctrl+enter+ field.activate!+ field.set_value "Edit to be saved by keyboard"+ field.submit_by_enter++ wp_page.expect_toast message: I18n.t('js.notice_successful_update')+ field.expect_state_text 'Edit to be saved by keyboard'endenddiff --git a/spec/support/edit_fields/text_editor_field.rb b/spec/support/edit_fields/text_editor_field.rbindex 18992649002..14572e03870 100644--- a/spec/support/edit_fields/text_editor_field.rb+++ b/spec/support/edit_fields/text_editor_field.rb@@ -49,6 +49,10 @@ class TextEditorField < EditFieldinput_element.native.send_keys :tabend+ def submit_by_enter+ input_element.native.send_keys %i[control enter]+ end+def cancel_by_clicktarget = field_container.find(control_link(:cancel), wait: 10)scroll_to_element(target)