2014-10-01 53 views
4

基本上我想要一個按鈕添加到工具欄,允許用戶插入©到textangular編輯器(http://textangular.com/),但我無法理解怎麼加功能註冊到我的按鈕之後...由於textangular站點上所有自定義功能的示例都使用相同的語句「wrapSelection」,它具有非常簡單的文檔,下面將使用quote按鈕顯示此示例。如何從textAngular工具欄上的自定義按鈕插入文本/符號

taRegisterTool('quote', { 
    iconclass: 'fa fa-quote-right', 
    tooltiptext: taTranslations.quote.tooltip, 
    action: function(){ 
     return this.$editor().wrapSelection("formatBlock", "<BLOCKQUOTE>"); 
    }, 
    activeState: function(){ return this.$editor().queryFormatBlockState('blockquote'); } 
}); 

我對「formatBlock」被初始化的地方感到困惑,並且認爲找到它的源代碼會幫助我解決這個問題。正如你所看到的任何幫助,將不勝感激

taRegisterTool('insertCopyright', { 
     buttontext: '&copy;', 
     tooltiptext: taTranslations.insertCopyright.tooltip, 
     action: function() { 
      //??? 
     }, 
    }); 
+0

我會記下wrapSelection窮docs - 它基本上是'execCommand'的包裝,但我們修復了一些不一致的調用。 – 2014-10-07 21:18:19

+1

@SimeonCheeseman很酷的感謝 – alsco77 2014-10-08 08:15:35

回答

7

只是想我會後我們的解決辦法答案爲希望插入自定義符號之類的東西的人,很明顯,我們可以移動「insertTextAtCursor」和「moveCaret」別處清理,但不管..

taRegisterTool('insertCopyright', { 
      buttontext: '&copy;', 
      tooltiptext: taTranslations.insertCopyright.tooltip, 
      action: function() { 
       function insertTextAtCursor(text) { 
        var sel, range; 
        if (window.getSelection) { 
         sel = window.getSelection(); 
         if (sel.getRangeAt && sel.rangeCount) { 
          range = sel.getRangeAt(0); 
          range.deleteContents(); 
          range.insertNode(document.createTextNode(text)); 
         } 
        } else if (document.selection && document.selection.createRange) { 
         document.selection.createRange().text = text; 
        } 
       } 

       function moveCaret(charCount) { 
        var sel, range; 
        if (window.getSelection) { 
         sel = window.getSelection(); 
         if (sel.rangeCount > 0) { 
          var textNode = sel.focusNode; 
          sel.collapse(textNode.nextSibling, charCount); 
         } 
        } else if ((sel = window.document.selection)) { 
         if (sel.type != "Control") { 
          range = sel.createRange(); 
          range.move("character", charCount); 
          range.select(); 
         } 
        } 
       } 

       insertTextAtCursor(String.fromCharCode(169)); 
       return moveCaret(1); 
      }, 
     }); 
相關問題