分类: 系统运维
2009-12-18 21:24:50
FCKeditor offers a complete JavaScript API so you can interact with it once the editor is loaded and running.
FCKeditor提供了一个完整的JavaScript API(Application Public Interface),你可以利用这些API来处理FCK编辑器,只要它被加载完成或在正在运行中.
Retrieving an editor instanceOnce loaded, the editor registers a global object called FCKeditorAPI. This object offers the entry point to interact with any editor instance placed in a page (you can have more than one). When placing the editor in the page, you give it an "instance name". So, to retrieve it, you must simply call the FCKeditorAPI.GetInstance method.
一旦编辑器实例化完成后,它都会注册一个全局的对象,叫作FCKeditorAPI. 这个对象提供一个接入点去获取在本页面内的任何已实例化的对象(不止一个).当你在页面生成一个编辑器时,你给了它一个实例名.所以,你只需简单地调用FCKeditorAPI.GetInstance方法就可以获取到它的对象引用.
For example: var oEditor = FCKeditorAPI.GetInstance('InstanceName') ;
例如,var oEditor = FCKeditorAPI.GetInstance('InstanceName') ; 这里就是获取实例名为"InstanceName"的FCKeditor对象.
The GetInstance method returns the main FCKeditor object that gives the necessary bridge to interact with it.
This is a list of properties and methods of this object:
这是一个FCKeditor的属性和方法的列表:
* Description = string 描述
* EditMode = Integer 编辑状态
* Name = string 名字
* Status = Integer 状态
* function AttachToOnSelectionChange(functionPointer)
* function CleanAndPaste(html)
* function CreateElement(tag)
* function CreateLink(url)
* function ExecOnSelectionChange() //Fires OnSelectionChange event in event manager
* function ExecOnSelectionChangeTimer()
* function ExecuteNamedCommand(commandName, commandParameter)
* function ExecuteRedirectedNamedCommand(commandName, commandParameter)
* function Focus()
* function GetHTML(format) // doesnt work. Use GetXHTML instead.
* function GetNamedCommandState(commandName)
* function GetNamedCommandValue(commandName)
* function GetXHTML(format)
* function InitializeBehaviors()
* function InsertElement(element)
* function InsertElementAndGetIt(e)
* function InsertHtml(html)
* function IsDirty();
* function MakeEditable()
* function OnDoubleClick(element)
* function Paste()
* function PasteAsPlainText()
* function PasteFromWord()
* function Preview()
* function RegisterDoubleClickHandler(handlerFunction, tag)
* function ResetIsDirty();
* function SetHTML(html, forceWYSIWYG)
* function SetStatus()
* function ShowContextMenu(x, y)
* function SwitchEditMode()
* function UpdateLinkedField()
EventsOnce the editor loading is complete and it is ready to use (and interact with JavaScript), a standard function is called in the page that contains the editor, if the function is defined.
This function must be named "FCKeditor_OnComplete" and receives the related editor instance as the parameter. Using it, you can execute any initial code that makes the initial interaction with the editor.
This is a declaration example:
function FCKeditor_OnComplete( editorInstance ) {
alert( editorInstance.Name ) ; }
Apart the above standard event, every FCKeditor instance has a "Event" object that can be used to listen for events to be fired.
For example, the following code listens for the "OnSelectionChange" to execute custom code:
var counter = 0 ;
function DoSomething( editorInstance ) {
window.document.title = editorInstance.Name + ' : ' + ( ++counter ) ; }
function FCKeditor_OnComplete( editorInstance ) {
editorInstance.Events.AttachEvent( 'OnSelectionChange', DoSomething ) ; }
Note that every callback function receives the editor instance as a parameter.
The following is the list of events available:
OnSelectionChange: fired when the actual selection in the editor area changes (by selection I mean the cursor position too... it changes on key strokes). Note: In IE6, this event does not fire on every keystroke, but only on some random keystrokes. Handy!
OnAfterSetHTML: fired once the HTML is loaded in the editor (including when changing views).
OnStatusChange: fired when the editor status changes. The following constants are also available globally in the page: FCK_STATUS_NOTLOADED, FCK_STATUS_ACTIVE and FCK_STATUS_COMPLETE.
OnPaste: fired when something is pasted in the editor