Forráskód Böngészése

Shortcut reduce/expands as user types/backspaces

Vijayakrishnan 4 éve
szülő
commit
46401a0f28

+ 1 - 1
app/Models/Pro.php

@@ -71,7 +71,7 @@ class Pro extends Model
     }
     }
 
 
     public function shortcuts() {
     public function shortcuts() {
-        return $this->hasMany(ProTextShortcut::class, 'pro_id');
+        return $this->hasMany(ProTextShortcut::class, 'pro_id')->where('is_removed', false);
     }
     }
 
 
     public function noteTemplates() {
     public function noteTemplates() {

+ 72 - 15
public/js/shortcut.js

@@ -3,6 +3,7 @@
 (function() {
 (function() {
     let input = null, menu = null, options = [], index = -1;
     let input = null, menu = null, options = [], index = -1;
     let backtrackAfterApplying = false;
     let backtrackAfterApplying = false;
+    let strPart = '';
     function getCaretPos(win) {
     function getCaretPos(win) {
         win = win || window;
         win = win || window;
         let doc = win.document;
         let doc = win.document;
@@ -38,11 +39,17 @@
         }
         }
         return { x: x, y: y };
         return { x: x, y: y };
     }
     }
-    function show(_input) {
+    function show(_initial = false) {
         let pos = getCaretPos();
         let pos = getCaretPos();
+        // strPart = '';
         if(pos) {
         if(pos) {
             index = -1;
             index = -1;
             options = window.userShortcuts;
             options = window.userShortcuts;
+            if(strPart.length > 1) {
+                options = window.userShortcuts.filter(function(_x) {
+                    return _x.name.toLowerCase().indexOf(strPart.substr(1).toLowerCase()) !== -1;
+                });
+            }
             menu.empty();
             menu.empty();
             for(let i = 0; i < options.length; i++) {
             for(let i = 0; i < options.length; i++) {
                 menu.append(
                 menu.append(
@@ -52,13 +59,17 @@
                         .attr('title', options[i].value)
                         .attr('title', options[i].value)
                 );
                 );
             }
             }
-            menu
-                .css({
-                    left: pos[0] + 'px',
-                    top: (pos[1] + $(window).scrollTop()) + 'px',
-                })
-                .show();
-
+            console.log(strPart)
+            if(_initial) {
+                menu
+                    .css({
+                        left: pos[0] + 'px',
+                        top: (pos[1] + $(window).scrollTop()) + 'px',
+                    })
+                    .show();
+                document.execCommand("insertText", true, '@');
+                document.execCommand("delete", true, null);
+            }
         }
         }
     }
     }
     function discard() {
     function discard() {
@@ -77,7 +88,9 @@
         if(input && options && options.length && index >= 0 && index < options.length) {
         if(input && options && options.length && index >= 0 && index < options.length) {
             // $(input).focus();
             // $(input).focus();
             if(backtrackAfterApplying) {
             if(backtrackAfterApplying) {
-                document.execCommand("delete", true, null);
+                for (let i = 0; i < strPart.length; i++) {
+                    document.execCommand("delete", true, null);
+                }
             }
             }
             document.execCommand("insertText", true, options[index].value);
             document.execCommand("insertText", true, options[index].value);
             discard();
             discard();
@@ -96,6 +109,8 @@
             .addClass('stag-shortcuts')
             .addClass('stag-shortcuts')
             .appendTo('body');
             .appendTo('body');
 
 
+        let refreshOptions = false;
+
         $(document)
         $(document)
             .off('mousedown.outside-shortcuts')
             .off('mousedown.outside-shortcuts')
             .on('mousedown.outside-shortcuts', function(_e) {
             .on('mousedown.outside-shortcuts', function(_e) {
@@ -105,24 +120,29 @@
         $(document)
         $(document)
             .off('keypress.shortcuts', '[with-shortcuts]')
             .off('keypress.shortcuts', '[with-shortcuts]')
             .on('keypress.shortcuts', '[with-shortcuts]', function(_e) {
             .on('keypress.shortcuts', '[with-shortcuts]', function(_e) {
-                // console.log('KP: ', _e.which);
                 input = this;
                 input = this;
                 switch(_e.which) {
                 switch(_e.which) {
                     case 64:
                     case 64:
                         backtrackAfterApplying = true;
                         backtrackAfterApplying = true;
-                        show(this);
+                        strPart = '@';
+                        show(true);
                         break;
                         break;
                     default:
                     default:
-                        if(isVisible()) return false;
+                        if(!isVisible()) return;
+                        let char = String.fromCharCode(_e.which);
+                        if(_e.ctrlKey || _e.metaKey || !char.match(/[a-z0-9 ]/i)) {
+                            return false;
+                        }
                         break;
                         break;
                 }
                 }
             })
             })
             .off('keydown.shortcuts', '[with-shortcuts]')
             .off('keydown.shortcuts', '[with-shortcuts]')
             .on('keydown.shortcuts', '[with-shortcuts]', function(_e) {
             .on('keydown.shortcuts', '[with-shortcuts]', function(_e) {
-                // console.log('KD: ', _e.which);
                 input = this;
                 input = this;
                 let consumed = false;
                 let consumed = false;
+                refreshOptions = false;
                 switch(_e.which) {
                 switch(_e.which) {
+                    /*
                     case 32:
                     case 32:
                         if(_e.ctrlKey && !isVisible()) {
                         if(_e.ctrlKey && !isVisible()) {
                             backtrackAfterApplying = false;
                             backtrackAfterApplying = false;
@@ -136,6 +156,7 @@
                             consumed = true;
                             consumed = true;
                         }
                         }
                         break;
                         break;
+                     */
                     case 27:
                     case 27:
                         if(!isVisible()) return;
                         if(!isVisible()) return;
                         consumed = !discard();
                         consumed = !discard();
@@ -157,11 +178,47 @@
                         apply();
                         apply();
                         consumed = true;
                         consumed = true;
                         break;
                         break;
+                    case 8:
+                        if(!isVisible()) break;
+                        if(strPart.length === 1) {
+                            strPart = '';
+                            discard();
+                        }
+                        else {
+                            strPart = strPart.substr(0, strPart.length - 1);
+                            refreshOptions = true;
+                        }
+                        consumed = false;
+                        break;
                     default:
                     default:
-                        consumed = isVisible();
+                        if(!isVisible()) break;
+                        let char = String.fromCharCode(_e.which);
+                        if(!_e.ctrlKey && !_e.metaKey && char.match(/[a-z0-9 ]/i)) {
+                            strPart += char;
+                            refreshOptions = true;
+                            consumed = false;
+                        }
+                        else {
+                            consumed = true;
+                        }
                         break;
                         break;
                 }
                 }
-                if(consumed) return false;
+                if(consumed) {
+                    _e.stopImmediatePropagation();
+                    _e.preventDefault();
+                    return false;
+                }
+            })
+            .off('keyup.shortcuts', '[with-shortcuts]')
+            .on('keyup.shortcuts', '[with-shortcuts]', function(_e) {
+                console.log('Reached keyup!', strPart);
+                if(isVisible() && refreshOptions) {
+                    show();
+                }
+            })
+            .off('paste.shortcuts', '[with-shortcuts]')
+            .on('paste.shortcuts', '[with-shortcuts]', function(_e) {
+                if(isVisible()) return false;
             });
             });
         $(document)
         $(document)
             .off('mousedown.apply-shortcuts', '.stag-shortcuts>.sc')
             .off('mousedown.apply-shortcuts', '.stag-shortcuts>.sc')

+ 1 - 1
resources/views/app/practice-management/my-text-shortcuts.blade.php

@@ -69,7 +69,7 @@
                                         </div>
                                         </div>
                                     </form>
                                     </form>
                                 </div>
                                 </div>
-                                <div moe wide class="mr-2">
+                                <div moe relative wide class="mr-2">
                                     <a start show class="text-danger">
                                     <a start show class="text-danger">
                                         <i class="fa fa-trash-alt"></i>
                                         <i class="fa fa-trash-alt"></i>
                                     </a>
                                     </a>