Bläddra i källkod

Fixes #1142. Show confirmation dialog for editing messages

when the textarea contains an unsent message
JC Brand 6 år sedan
förälder
incheckning
9750dcf3a8
3 ändrade filer med 31 tillägg och 1 borttagningar
  1. 21 0
      spec/messages.js
  2. 9 0
      src/converse-chatview.js
  3. 1 1
      src/converse-modal.js

+ 21 - 0
spec/messages.js

@@ -114,7 +114,28 @@
                 }).c('body').t('Hello').up()
                 }).c('body').t('Hello').up()
                 .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree()
                 .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree()
             );
             );
+            await new Promise((resolve, reject) => view.once('messageInserted', resolve));
             expect(view.el.querySelectorAll('.chat-msg .chat-msg__action').length).toBe(1);
             expect(view.el.querySelectorAll('.chat-msg .chat-msg__action').length).toBe(1);
+
+            // Test confirmation dialog
+            spyOn(window, 'confirm').and.returnValue(true);
+            textarea.value = 'But soft, what light through yonder airlock breaks?';
+            action = view.el.querySelector('.chat-msg .chat-msg__action');
+            action.style.opacity = 1;
+            action.click();
+            expect(window.confirm).toHaveBeenCalledWith(
+                'You have an unsent message which will be lost if you continue. Are you sure?');
+            expect(view.model.messages.at(0).get('correcting')).toBe(true);
+            expect(textarea.value).toBe('But soft, what light through yonder window breaks?');
+
+            textarea.value = 'But soft, what light through yonder airlock breaks?'
+            action.click();
+            expect(view.model.messages.at(0).get('correcting')).toBe(false);
+            expect(window.confirm.calls.count()).toBe(2);
+            expect(window.confirm.calls.argsFor(0)).toEqual(
+                ['You have an unsent message which will be lost if you continue. Are you sure?']);
+            expect(window.confirm.calls.argsFor(1)).toEqual(
+                ['You have an unsent message which will be lost if you continue. Are you sure?']);
             done();
             done();
         }));
         }));
 
 

+ 9 - 0
src/converse-chatview.js

@@ -1022,11 +1022,20 @@ converse.plugins.add('converse-chatview', {
 
 
             onMessageEditButtonClicked (ev) {
             onMessageEditButtonClicked (ev) {
                 ev.preventDefault();
                 ev.preventDefault();
+
                 const idx = this.model.messages.findLastIndex('correcting'),
                 const idx = this.model.messages.findLastIndex('correcting'),
                       currently_correcting = idx >=0 ? this.model.messages.at(idx) : null,
                       currently_correcting = idx >=0 ? this.model.messages.at(idx) : null,
                       message_el = u.ancestor(ev.target, '.chat-msg'),
                       message_el = u.ancestor(ev.target, '.chat-msg'),
                       message = this.model.messages.findWhere({'msgid': message_el.getAttribute('data-msgid')});
                       message = this.model.messages.findWhere({'msgid': message_el.getAttribute('data-msgid')});
 
 
+                const textarea = this.el.querySelector('.chat-textarea');
+                if (textarea.value &&
+                        (currently_correcting === null || currently_correcting.get('message') !== textarea.value)) {
+                    if (! confirm(__("You have an unsent message which will be lost if you continue. Are you sure?"))) {
+                        return;
+                    }
+                }
+
                 if (currently_correcting !== message) {
                 if (currently_correcting !== message) {
                     if (!_.isNil(currently_correcting)) {
                     if (!_.isNil(currently_correcting)) {
                         currently_correcting.save('correcting', false);
                         currently_correcting.save('correcting', false);

+ 1 - 1
src/converse-modal.js

@@ -73,7 +73,7 @@ converse.plugins.add('converse-modal', {
 
 
         /************************ BEGIN API ************************/
         /************************ BEGIN API ************************/
         // We extend the default converse.js API to add methods specific to MUC chat rooms.
         // We extend the default converse.js API to add methods specific to MUC chat rooms.
-        let alert 
+        let alert;
 
 
         Object.assign(_converse.api, {
         Object.assign(_converse.api, {
             'alert': {
             'alert': {