From 9208bbe05ee83b086e9fec47204de2a7f2322bcc Mon Sep 17 00:00:00 2001
From: Snater <git@snater.com>
Date: Fri, 30 Jun 2023 14:56:05 +0200
Subject: [PATCH] Issue #3348398 by Snater: Set default image alignment

---
 js/EditorInterface.js  |  3 ++-
 js/ImageHandler.js     | 20 ++++++++++++++++----
 js/editors/CKEditor.js |  6 ++++--
 3 files changed, 22 insertions(+), 7 deletions(-)

diff --git a/js/EditorInterface.js b/js/EditorInterface.js
index e463f30..ba20221 100644
--- a/js/EditorInterface.js
+++ b/js/EditorInterface.js
@@ -98,7 +98,8 @@
       /**
        * @param {*} editor
        * @param {string} uuid
-       * @return {string|undefined}
+       * @return {string|null|undefined} Either an alignment value, null if
+       *   alignment is not set, or undefined if there is no inserted image.
        */
       getAlign: function(editor, uuid) {
         throw new Error('Method not overridden.');
diff --git a/js/ImageHandler.js b/js/ImageHandler.js
index 40be94e..2948fb0 100644
--- a/js/ImageHandler.js
+++ b/js/ImageHandler.js
@@ -254,7 +254,10 @@
           });
 
         var value = this._getAlign();
-        this._$align.find('[value="' + value + '"]').prop('checked', true);
+
+        if (value) {
+          this._$align.find('[value="' + value + '"]').prop('checked', true);
+        }
       },
 
       /**
@@ -283,18 +286,26 @@
        * (If there are different alignments for an image's instances, alignment
        * messed up somehow anyway.)
        *
-       * @return {string}
+       * @return {string|null}
        */
       _getAlign: function() {
         var self = this;
         var value;
+        var hasInsertedImages = false;
 
         this._inserter.getFocusManager().getTextareas().each(function() {
           var $dom = $('<div>').html($(this).val());
-          self._findByUUID($dom, self._uuid).each(function() {
+          var $nodes = self._findByUUID($dom, self._uuid);
+
+          if ($nodes.length) {
+            hasInsertedImages = true;
+          }
+
+          $nodes.each(function() {
             value = $(this).attr('data-align');
             return false;
           });
+
           return !value;
         });
 
@@ -306,10 +317,11 @@
 
         $.each(this._inserter.getFocusManager().getEditors(), function() {
           value = editorInterface.getAlign(this, self._uuid);
+          hasInsertedImages = hasInsertedImages || value !== undefined;
           return false;
         });
 
-        return value ? value : 'none';
+        return !hasInsertedImages ? null : value ? value : 'none';
       },
 
       /**
diff --git a/js/editors/CKEditor.js b/js/editors/CKEditor.js
index c4ee6c3..acd14bc 100644
--- a/js/editors/CKEditor.js
+++ b/js/editors/CKEditor.js
@@ -133,14 +133,16 @@
        * @inheritDoc
        */
       getAlign: function(editor, uuid) {
-        var align = undefined;
+        var align = null;
+        var hasInstances = false;
 
         $.each(this._filterInstances(editor, uuid), function() {
           align = this.data.align;
+          hasInstances = true;
           return false;
         });
 
-        return align;
+        return !hasInstances ? undefined : align;
       },
 
       /**
-- 
GitLab