当包含在.then()方法中时,JavaScript函数返回false,当在外部时返回true

时间:2014-01-17 12:25:02

标签: javascript angularjs

我目前正在尝试扩展Sir Trevor内容编辑器,以防止它使用JavaScript提示获取URL /超链接突出显示的文本并使用我创建的自定义对话框。我创建的模态窗口有一个文本输入,它返回一个有效的href(这是一个字符串),模态窗口关闭并根据需要传回值...一切似乎都返回并且运行良好但是我的修改后的代码没有工作,这是我的代码

// overwrite the prompt provided by Sir Trev Link.onClick
SirTrevor.Formatters.Link.onClick = function() {

    // this is my code
    var thisCMD = this.cmd;

    modalService.prompt(Label.for.textinput.from.jsonfile).result.then(function(href) {
        document.execCommand(thisCMD, false, href);
    });

    /*
    // This is the original code!
    var link = prompt(i18n.t("general:link")),
        link_regex = /((ftp|http|https):\/\/.)|mailto(?=\:[-\.\w]+@)/;

    if(link && link.length > 0) {

        if(!link_regex.test(link)) {
            link = "http://" + link;
        }

        document.execCommand(this.cmd, false, link);
    } 
    */
}

现在我返回的href有效且不为空,我创建并传递的thisCMD变量具有正确的值和正确的类型(与原始的this.cmd相同)。一切都应该有效但是在测试原始document.execCommand()

时我注意到以下内容
// original
console.log(document.execCommand(this.cmd, false, link)); // returns true

// modified / extended code
console.log(document.execCommand(thisCMD, false, href)); // returns false

有没有人对我如何解决这个问题或者告诉我哪里出错了?我的应用程序是使用AngularJS

编写的

更新

只是为了协助这是我试图部分修改的整个Sir Trevor功能,请注意OnClick方法

var Link = SirTrevor.Formatter.extend({


  title: "link",
  iconName: "link",
  cmd: "CreateLink",
  text : "link",

  onClick: function() {

    var link = prompt(i18n.t("general:link")),
        link_regex = /((ftp|http|https):\/\/.)|mailto(?=\:[-\.\w]+@)/;

    if(link && link.length > 0) {

     if (!link_regex.test(link)) {
       link = "http://" + link;
     }

     document.execCommand(this.cmd, false, link);
    }
  },

  isActive: function() {
    var selection = window.getSelection(),
        node;

    if (selection.rangeCount > 0) {
      node = selection.getRangeAt(0)
                      .startContainer
                      .parentNode;
    }

    return (node && node.nodeName == "A");
  }
});

0 个答案:

没有答案