From 8bf67f8debd72d7f8699aff03e36c620d32efc59 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Sat, 2 May 2015 20:39:20 -0500
Subject: [PATCH] variable: Auto add trailing `}` on tab expansion

---
 js/redactor-plugins.js | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/js/redactor-plugins.js b/js/redactor-plugins.js
index 8a658ab34..772573bab 100644
--- a/js/redactor-plugins.js
+++ b/js/redactor-plugins.js
@@ -1768,7 +1768,10 @@ RedactorPlugins.contexttypeahead = function() {
 
     select: function(item) {
       var current = this.selection.getCurrent(),
-          search = new RegExp(/%\{([^}]*)$/);
+          sel     = this.selection.get(),
+          range   = this.sel.getRangeAt(0),
+          cursorAt = range.endOffset,
+          search  = new RegExp(/%\{([^}]*)(\}?)$/);
 
       // FIXME: ENTER will end up here, but current will be empty
 
@@ -1776,10 +1779,16 @@ RedactorPlugins.contexttypeahead = function() {
         return;
 
       // Set cursor at the end of the expanded text
-      var q = current.textContent
-            = current.textContent.replace(search, '%{' + item.variable);
-      this.range.setStart(current, current.length);
-      this.range.setEnd(current, current.length);
+      var left = current.textContent.substring(0, cursorAt),
+          right = current.textContent.substring(cursorAt),
+          newLeft = left.replace(search, '%{' + item.variable + '}');
+
+      current.textContent = newLeft
+        // Drop the remaining part of a variable block, if any
+        + right.replace(/[^%}]*?[%}]/, '');
+
+      this.range.setStart(current, newLeft.length - 1);
+      this.range.setEnd(current, newLeft.length - 1);
       this.selection.addRange();
       return this.contexttypeahead.destroy();
     }
-- 
GitLab