/**
* WYSIWYG - jQuery plugin 0.5
*
* Copyright (c) 2008-2009 Juan M Martinez
* http://plugins.jquery.com/project/jWYSIWYG
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
* $Id: $
*/
(function( $ )
{
$.fn.document = function()
{
var element = this[0];
if ( element.nodeName.toLowerCase() == 'iframe' )
return element.contentWindow.document;
/*
return ( $.browser.msie )
? document.frames[element.id].document
: element.contentWindow.document // contentDocument;
*/
else
return $(this);
};
$.fn.documentSelection = function()
{
var element = this[0];
if ( element.contentWindow.document.selection )
return element.contentWindow.document.selection.createRange().text;
else
return element.contentWindow.getSelection().toString();
};
$.fn.wysiwyg = function( options )
{
if ( arguments.length > 0 && arguments[0].constructor == String )
{
var action = arguments[0].toString();
var params = [];
for ( var i = 1; i < arguments.length; i++ )
params[i - 1] = arguments[i];
if ( action in Wysiwyg )
{
return this.each(function()
{
$.data(this, 'wysiwyg')
.designMode();
Wysiwyg[action].apply(this, params);
});
}
else return this;
}
var controls = {};
/**
* If the user set custom controls, we catch it, and merge with the
* defaults controls later.
*/
if ( options && options.controls )
{
var controls = options.controls;
delete options.controls;
}
var options = $.extend({
html : '<'+'?xml version="1.0" encoding="UTF-8"?'+'><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">STYLE_SHEET</head><body>INITIAL_CONTENT</body></html>',
css : {},
debug : false,
autoSave : true, // http://code.google.com/p/jwysiwyg/issues/detail?id=11
rmUnwantedBr : true, // http://code.google.com/p/jwysiwyg/issues/detail?id=15
brIE : true,
controls : {},
messages : {}
}, options);
options.messages = $.extend(true, options.messages, Wysiwyg.MSGS_EN);
options.controls = $.extend(true, options.controls, Wysiwyg.TOOLBAR);
for ( var control in controls )
{
if ( control in options.controls )
$.extend(options.controls[control], controls[control]);
else
options.controls[control] = controls[control];
}
// not break the chain
return this.each(function()
{
Wysiwyg(this, options);
});
};
function Wysiwyg( element, options )
{
return this instanceof Wysiwyg
? this.init(element, options)
: new Wysiwyg(element, options);
}
$.extend(Wysiwyg, {
insertImage : function( szURL, attributes )
{
var self = $.data(this, 'wysiwyg');
if ( self.constructor == Wysiwyg && szURL && szURL.length > 0 )
{
if ( attributes )
{
self.editorDoc.execCommand('insertImage', false, '#jwysiwyg#');
var img = self.getElementByAttributeValue('img', 'src', '#jwysiwyg#');
if ( img )
{
img.src = szURL;
for ( var attribute in attributes )
{
img.setAttribute(attribute, attributes[attribute]);
}
}
}
else
{
self.editorDoc.execCommand('insertImage', false, szURL);
}
}
},
createLink : function( szURL )
{
var self = $.data(this, 'wysiwyg');
if ( self.constructor == Wysiwyg && szURL && szURL.length > 0 )
{
var selection = $(self.editor).documentSelection();
if ( selection.length > 0 )
{
self.editorDoc.execCommand('unlink', false, []);
self.editorDoc.execCommand('createLink', false, szURL);
}
else if ( self.options.messages.nonSelection )
alert(self.options.messages.nonSelection);
}
},
setContent : function( newContent )
{
var self = $.data(this, 'wysiwyg');
self.setContent( newContent );
self.saveContent();
},
clear : function()
{
var self = $.data(this, 'wysiwyg');
self.setContent('');
self.saveContent();
},
MSGS_EN : {
nonSelection : 'select the text you wish to link'
},
TOOLBAR : {
bold : { visible : true, tags : ['b', 'strong'], css : { fontWeight : 'bold' } },
italic : { visible : true, tags : ['i', 'em'], css : { fontStyle : 'italic' } },
strikeThrough : { visible : false, tags : ['s', 'strike'], css : { textDecoration : 'line-through' } },
underline : { visible : false, tags : ['u'], css : { textDecoration : 'underline' } },
separator00 : { visible : false, separator : true },
justifyLeft : { visible : false, css : { textAlign : 'left' } },
justifyCenter : { visible : false, tags : ['center'], css : { textAlign : 'center' } },
justifyRight : { visible : false, css : { textAlign : 'right' } },
justifyFull : { visible : false, css : { textAlign : 'justify' } },
separator01 : { visible : false, separator : true },
indent : { visible : false },
outdent : { visible : false },
separator02 : { visible : false, separator : true },
subscript : { visible : false, tags : ['sub'] },
superscript : { visible : false, tags : ['sup'] },
separator03 : { visible : false, separator : true },
undo : { visible : false },
redo : { visible : false },
separator04 : { visible : false, separator : true },
insertOrderedList : { visible : false, tags : ['ol'] },
insertUnorderedList : { visible : false, tags : ['ul'] },
insertHorizontalRule : { visible : false, tags : ['hr'] },
separator05 : { separator : true },
createLink : {
visible : true,
exec : function()
{
var selection = $(this.editor).documentSelection();
if ( selection.length > 0 )
{
if ( $.browser.msie )
this.editorDoc.execCommand('createLink', true, null);
else
{
var szURL = prompt('URL', 'http://');
if ( szURL && szURL.length > 0 )
{
this.editorDoc.execCommand('unlink', false, []);
this.editorDoc.execCommand('createLink', false, szURL);
}
}
jquery 文本编辑
需积分: 10 143 浏览量
2009-06-12
12:23:32
上传
评论
收藏 10KB RAR 举报
whangliangwhang
- 粉丝: 1
- 资源: 2