Angularjs 无法在从引导所见即所得编辑器创建的新选项卡中打开超链接
我在应用程序中使用的是bootstrap wysiwyg富文本编辑器,无法打开在新窗口中创建的hyerlink 我正在使用下面的bootstrap-wysiwyg.js文件。我不知道如何创建超链接,如何在新选项卡中打开Angularjs 无法在从引导所见即所得编辑器创建的新选项卡中打开超链接,angularjs,twitter-bootstrap-3,angular-ui-bootstrap,Angularjs,Twitter Bootstrap 3,Angular Ui Bootstrap,我在应用程序中使用的是bootstrap wysiwyg富文本编辑器,无法打开在新窗口中创建的hyerlink 我正在使用下面的bootstrap-wysiwyg.js文件。我不知道如何创建超链接,如何在新选项卡中打开 (function ($) { 'use strict'; /** underscoreThrottle() * From underscore http://underscorejs.org/docs/underscore.html */
(function ($) {
'use strict';
/** underscoreThrottle()
* From underscore http://underscorejs.org/docs/underscore.html
*/
var underscoreThrottle = function(func, wait) {
var context, args, timeout, result;
var previous = 0;
var later = function() {
previous = new Date;
timeout = null;
result = func.apply(context, args);
};
return function() {
var now = new Date;
var remaining = wait - (now - previous);
context = this;
args = arguments;
if (remaining <= 0) {
clearTimeout(timeout);
timeout = null;
previous = now;
result = func.apply(context, args);
} else if (!timeout) {
timeout = setTimeout(later, remaining);
}
return result;
};
}
var readFileIntoDataUrl = function (fileInfo) {
var loader = $.Deferred(),
fReader = new FileReader();
fReader.onload = function (e) {
loader.resolve(e.target.result);
};
fReader.onerror = loader.reject;
fReader.onprogress = loader.notify;
fReader.readAsDataURL(fileInfo);
return loader.promise();
};
$.fn.cleanHtml = function (o) {
if ( $(this).data("wysiwyg-html-mode") === true ) {
$(this).html($(this).text());
$(this).attr('contenteditable',true);
$(this).data('wysiwyg-html-mode',false);
}
// Strip the images with src="data:image/.." out;
if ( o === true && $(this).parent().is("form") ) {
var gGal = $(this).html;
if ( $(gGal).has( "img" ).length ) {
var gImages = $( "img", $(gGal));
var gResults = [];
var gEditor = $(this).parent();
$.each(gImages, function(i,v) {
if ( $(v).attr('src').match(/^data:image\/.*$/) ) {
gResults.push(gImages[i]);
$(gEditor).prepend("<input value='"+$(v).attr('src')+"' type='hidden' name='postedimage/"+i+"' />");
$(v).attr('src', 'postedimage/'+i);
}});
}
}
var html = $(this).html();
return html && html.replace(/(<br>|\s|<div><br><\/div>| )*$/, '');
};
$.fn.wysiwyg = function (userOptions) {
var editor = this,
wrapper = $(editor).parent(),
selectedRange,
options,
toolbarBtnSelector,
updateToolbar = function () {
if (options.activeToolbarClass) {
$(options.toolbarSelector,wrapper).find(toolbarBtnSelector).each(underscoreThrottle(function () {
var commandArr = $(this).data(options.commandRole).split(' '),
command = commandArr[0];
// If the command has an argument and its value matches this button. == used for string/number comparison
if (commandArr.length > 1 && document.queryCommandEnabled(command) && document.queryCommandValue(command) == commandArr[1]) {
$(this).addClass(options.activeToolbarClass);
// Else if the command has no arguments and it is active
} else if (commandArr.length === 1 && document.queryCommandEnabled(command) && document.queryCommandState(command)) {
$(this).addClass(options.activeToolbarClass);
// Else the command is not active
} else {
$(this).removeClass(options.activeToolbarClass);
}
}, options.keypressTimeout));
}
},
execCommand = function (commandWithArgs, valueArg) {
var commandArr = commandWithArgs.split(' '),
command = commandArr.shift(),
args = commandArr.join(' ') + (valueArg || '');
var parts = commandWithArgs.split('-');
if ( parts.length == 1 ) {
document.execCommand(command, 0, args);
}
else if ( parts[0] == 'format' && parts.length == 2) {
document.execCommand('formatBlock', false, parts[1] );
}
editor.trigger('change');
updateToolbar();
},
bindHotkeys = function (hotKeys) {
$.each(hotKeys, function (hotkey, command) {
editor.keydown(hotkey, function (e) {
if (editor.attr('contenteditable') && editor.is(':visible')) {
e.preventDefault();
e.stopPropagation();
execCommand(command);
}
}).keyup(hotkey, function (e) {
if (editor.attr('contenteditable') && editor.is(':visible')) {
e.preventDefault();
e.stopPropagation();
}
});
});
editor.keyup(function(){ editor.trigger('change'); });
},
getCurrentRange = function () {
var sel, range;
if (window.getSelection) {
sel = window.getSelection();
if (sel.getRangeAt && sel.rangeCount) {
range = sel.getRangeAt(0);
}
} else if (document.selection) {
range = document.selection.createRange();
} return range;
},
saveSelection = function () {
selectedRange = getCurrentRange();
},
restoreSelection = function () {
var selection;
if (window.getSelection || document.createRange) {
selection = window.getSelection();
if (selectedRange) {
try {
selection.removeAllRanges();
} catch (ex) {
document.body.createTextRange().select();
document.selection.empty();
}
selection.addRange(selectedRange);
}
}
else if (document.selection && selectedRange) {
selectedRange.select()
}
},
// Adding Toggle HTML based on the work by @jd0000, but cleaned up a little to work in this context.
toggleHtmlEdit = function(a) {
if ( $(editor).data("wysiwyg-html-mode") !== true ) {
var oContent = $(editor).html();
var editorPre = $( "<pre />" )
$(editorPre).append( document.createTextNode( oContent ) );
$(editorPre).attr('contenteditable',true);
$(editor).html(' ');
$(editor).append($(editorPre));
$(editor).attr('contenteditable', false);
$(editor).data("wysiwyg-html-mode", true);
$(editorPre).focus();
}
else {
$(editor).html($(editor).text());
$(editor).attr('contenteditable',true);
$(editor).data('wysiwyg-html-mode',false);
$(editor).focus();
}
},
insertFiles = function (files) {
editor.focus();
$.each(files, function (idx, fileInfo) {
if (/^image\//.test(fileInfo.type)) {
$.when(readFileIntoDataUrl(fileInfo)).done(function (dataUrl) {
execCommand('insertimage', dataUrl);
editor.trigger('image-inserted');
}).fail(function (e) {
options.fileUploadError("file-reader", e);
});
} else {
options.fileUploadError("unsupported-file-type", fileInfo.type);
}
});
},
markSelection = function (input, color) {
restoreSelection();
if (document.queryCommandSupported('hiliteColor')) {
document.execCommand('hiliteColor', 0, color || 'transparent');
}
saveSelection();
input.data(options.selectionMarker, color);
},
bindToolbar = function (toolbar, options) {
toolbar.find(toolbarBtnSelector, wrapper).click(function () {
restoreSelection();
editor.focus();
if ($(this).data(options.commandRole) === 'html') {
toggleHtmlEdit();
}
else {
execCommand($(this).data(options.commandRole));
}
saveSelection();
});
toolbar.find('[data-toggle=dropdown]').click(restoreSelection);
toolbar.find('input[type=text][data-' + options.commandRole + ']').on('webkitspeechchange change', function () {
var newValue = this.value; /* ugly but prevents fake double-calls due to selection restoration */
this.value = '';
restoreSelection();
if (newValue) {
editor.focus();
execCommand($(this).data(options.commandRole), newValue);
}
saveSelection();
}).on('focus', function () {
var input = $(this);
if (!input.data(options.selectionMarker)) {
markSelection(input, options.selectionColor);
input.focus();
}
}).on('blur', function () {
var input = $(this);
if (input.data(options.selectionMarker)) {
markSelection(input, false);
}
});
toolbar.find('input[type=file][data-' + options.commandRole + ']').change(function () {
restoreSelection();
if (this.type === 'file' && this.files && this.files.length > 0) {
insertFiles(this.files);
}
saveSelection();
this.value = '';
});
},
initFileDrops = function () {
editor.on('dragenter dragover', false)
.on('drop', function (e) {
var dataTransfer = e.originalEvent.dataTransfer;
e.stopPropagation();
e.preventDefault();
if (dataTransfer && dataTransfer.files && dataTransfer.files.length > 0) {
insertFiles(dataTransfer.files);
}
});
};
options = $.extend(true, {}, $.fn.wysiwyg.defaults, $.fn.wysiwyg.defaults1, userOptions);
toolbarBtnSelector = 'a[data-' + options.commandRole + '],button[data-' + options.commandRole + '],input[type=button][data-' + options.commandRole + ']';
bindHotkeys(options.hotKeys);
// Support placeholder attribute on the DIV
if ($(this).attr('placeholder') != '') {
$(this).addClass('placeholderText');
$(this).html($(this).attr('placeholder'));
$(this).bind('focus',function(e) {
if ( $(this).attr('placeholder') != '' && $(this).text() == $(this).attr('placeholder') ) {
$(this).removeClass('placeholderText');
$(this).html('');
}
});
$(this).bind('blur',function(e) {
if ( $(this).attr('placeholder') != '' && $(this).text() == '' ) {
$(this).addClass('placeholderText');
$(this).html($(this).attr('placeholder'));
}
})
}
if (options.dragAndDropImages) {
initFileDrops();
}
bindToolbar($(options.toolbarSelector), options);
editor.attr('contenteditable', true)
.on('mouseup keyup mouseout', function () {
saveSelection();
updateToolbar();
});
$(window).bind('touchend', function (e) {
var isInside = (editor.is(e.target) || editor.has(e.target).length > 0),
currentRange = getCurrentRange(),
clear = currentRange && (currentRange.startContainer === currentRange.endContainer && currentRange.startOffset === currentRange.endOffset);
if (!clear || isInside) {
saveSelection();
updateToolbar();
}
});
return this;
};
$.fn.wysiwyg.defaults = {
hotKeys: {
'Ctrl+b meta+b': 'bold',
'Ctrl+i meta+i': 'italic',
'Ctrl+u meta+u': 'underline',
'Ctrl+z': 'undo',
'Ctrl+y meta+y meta+shift+z': 'redo',
'Ctrl+l meta+l': 'justifyleft',
'Ctrl+r meta+r': 'justifyright',
'Ctrl+e meta+e': 'justifycenter',
'Ctrl+j meta+j': 'justifyfull',
'Shift+tab': 'outdent',
'tab': 'indent'
},
toolbarSelector: '[data-role=editor-toolbar]',
commandRole: 'edit',
activeToolbarClass: 'btn-info',
selectionMarker: 'edit-focus-marker',
selectionColor: 'darkgrey',
dragAndDropImages: true,
keypressTimeout: 200,
fileUploadError: function (reason, detail) { console.log("File upload error", reason, detail); }
};
$.fn.wysiwyg.defaults1 = {
hotKeys: {
'Ctrl+b meta+b': 'bold',
'Ctrl+i meta+i': 'italic',
'Ctrl+u meta+u': 'underline',
'Ctrl+z': 'undo',
'Ctrl+y meta+y meta+shift+z': 'redo',
'Ctrl+l meta+l': 'justifyleft',
'Ctrl+r meta+r': 'justifyright',
'Ctrl+e meta+e': 'justifycenter',
'Ctrl+j meta+j': 'justifyfull',
'Shift+tab': 'outdent',
'tab': 'indent'
},
toolbarSelector: '[data-role=editor1-toolbar]',
commandRole: 'edit',
activeToolbarClass: 'btn-info',
selectionMarker: 'edit-focus-marker',
selectionColor: 'darkgrey',
dragAndDropImages: true,
keypressTimeout: 200,
fileUploadError: function (reason, detail) { console.log("File upload error", reason, detail); }
};
}(window.jQuery));
(函数($){
"严格使用",;
/**乙酰胆碱()
*从下划线开始http://underscorejs.org/docs/underscore.html
*/
var下划线throttle=函数(func,wait){
变量上下文、参数、超时、结果;
var-previous=0;
var later=function(){
以前=新日期;
超时=空;
结果=函数应用(上下文,参数);
};
返回函数(){
var now=新日期;
var剩余=等待-(现在-以前);
上下文=这个;
args=参数;
if(剩余1&&document.queryCommandEnabled(命令)&&document.queryCommandValue(命令)==commandArr[1]){
$(this.addClass(options.activeToolbarClass);
//如果命令没有参数且处于活动状态,则返回Else
}else if(commandArr.length==1&&document.queryCommandEnabled(命令)&&document.queryCommandState(命令)){
$(this.addClass(options.activeToolbarClass);
//否则该命令将不处于活动状态
}否则{
$(this.removeClass(options.activeToolbarClass);
}
},options.keypressTimeout));
}
},
execCommand=函数(commandWithArgs,valueArg){
var commandArr=commandWithArgs.split(“”),
command=commandArr.shift(),
args=commandArr.join(“”)+(valueArg | |“”);
var parts=commandWithArgs.split('-');
如果(parts.length==1){
document.execCommand(命令,0,参数);
}
else if(parts[0]='format'&&parts.length==2){
document.execCommand('formatBlock',false,parts[1]);
}
编辑器.trigger('change');
updateToolbar();
},
bindHotkeys=函数(热键){
$.each(热键、函数(热键、命令){
editor.keydown(热键,函数(e){
if(editor.attr('contenteditable')&&editor.is(':visible')){
e、 预防默认值();
e、 停止传播();
execCommand(命令);
}
}).keyup(热键,功能(e){
if(editor.attr('contenteditable')&&editor.is(':visible')){
e、 预防默认值();
e、 停止传播();
}
});
});
keyup(函数(){editor.trigger('change');});
},
getCurrentRange=函数(){
var-sel,范围;
if(window.getSelection){
sel=window.getSelection();
if(sel.getRangeAt&&sel.rangeCount){
范围=选择范围(0);
}
}else if(文档选择){
range=document.selection.createRange();
}返回范围;
},
saveSelection=函数(){
selectedRange=getCurrentRange();
},
restoreSelection=函数(){
var选择;
if(window.getSelection | | document.createRange){
selection=window.getSelection();
如果(已选择范围){
试一试{
selection.removeAllRanges();
}捕获(ex){
document.body.createTextRange().select();
document.selection.empty();
}
selection.addRange(selectedRange);
}
}
else if(document.selection&&selectedRange){
selectedRange.select()
}
},
//在@jd0000的工作基础上添加Toggle HTML,但是在这个上下文中进行了一些清理。
toggleHtmlEdit=函数(a){
if($(编辑器).data(“所见即所得html模式”)!==true){
var oContent=$(编辑器).html();
var editorPre=$(“”)
$(editorPre).append(document.createTextNode(oContent));
$(editorPre).attr('contenteditable',true);
$(编辑器).html(“”);
$(编辑器).append($(编辑器));
$(编辑器).attr('contenteditable',false);
$(编辑器).data(“所见即所得html模式”,true);
$(editorPre.focus();
}
否则{
$(编辑器).html($(编辑器).text());
$(editor.attr('contenteditable',true);
$(编辑器).data('wysiwyg-html-mode',false);
$(编辑器).focus();
}
},
insertFiles=函数(文件){
editor.focus();
$.each(文件、函数(idx、fileInfo){
if(/^image\/.test(fileInfo.type)){
$.when(readFileIntoDataUrl(fileInfo)).done(函数(dataUrl){
execCommand('insertimage',dataUrl);
editor.trigger('image-inserted');
<p class="textAlignLeft" ng-bind-html="editorContent | unsafe"></p>
<p class="textAlignLeft ng-binding" ng-bind-html="editorContent | unsafe">ajslkjsak <a href="http://www.google.com">sdsad</a></p>
$("a", "#editor").click(function(e) {
window.open($(this).attr('href'), '_blank')
});
<p id="myCustomContent" class="textAlignLeft" ng-bind-html="newsContent | unsafe"></p>
$("a", "#myCustomContent").each(function() {
$(this).attr('target', '_blank');
});