如何从textAngular工具栏上的自定义按钮插入文本/符号

als*_*o77 4 javascript jquery angularjs angular-ui

基本上我想在工具栏上添加一个按钮,允许用户将©插入文本框编辑器(http://textangular.com/),但是我无法理解如何在注册后为我的按钮添加功能...由于textangular网站上的自定义功能的所有示例都使用相同的语句"wrapSelection",其中包含非常少的文档,下面显示了一个示例,其中包含引号按钮.

    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'); }
});
Run Code Online (Sandbox Code Playgroud)

我很困惑"formatBlock"初始化的地方,并相信找到它的来源将帮助我解决这个问题.如你所见,任何帮助将不胜感激

    taRegisterTool('insertCopyright', {
        buttontext: '&copy;',
        tooltiptext: taTranslations.insertCopyright.tooltip,
        action: function () {
            //???
       },
    });
Run Code Online (Sandbox Code Playgroud)

als*_*o77 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);
            },
        });
Run Code Online (Sandbox Code Playgroud)