Fix broken wysiwyg fields in Afforms due to timing issue
authorColeman Watts <coleman@civicrm.org>
Sat, 8 Apr 2023 21:10:26 +0000 (17:10 -0400)
committerColeman Watts <coleman@civicrm.org>
Sat, 8 Apr 2023 21:10:26 +0000 (17:10 -0400)
ang/crmUi.js

index 309d512bdd690be8fe90fd66b167d44c24a629c6..bc0bb7bb458ea41a64119c7ddcd2f5260bb36629 100644 (file)
         require: '?ngModel',
         link: function (scope, elm, attr, ngModel) {
 
-          var editor = CRM.wysiwyg.create(elm);
-          if (!ngModel) {
-            return;
-          }
+          // Wait for #id to stabilize so the wysiwyg doesn't init with an id like `cke_{{:: fieldId }}`
+          $timeout(function() {
+            var editor = CRM.wysiwyg.create(elm);
 
-          if (attr.ngBlur) {
-            $(elm).on('blur', function() {
-              $timeout(function() {
-                scope.$eval(attr.ngBlur);
+            if (!ngModel) {
+              return;
+            }
+
+            if (attr.ngBlur) {
+              $(elm).on('blur', function() {
+                $timeout(function() {
+                  scope.$eval(attr.ngBlur);
+                });
               });
-            });
-          }
+            }
 
-          ngModel.$render = function(value) {
-            editor.done(function() {
-              CRM.wysiwyg.setVal(elm, ngModel.$viewValue || '');
-            });
-          };
+            ngModel.$render = function(value) {
+              editor.done(function() {
+                CRM.wysiwyg.setVal(elm, ngModel.$viewValue || '');
+              });
+            };
+          });
         }
       };
     })