LGX.NET
漂泊...

FCKeditor JavaScript API

FCKeditor offers a complete JavaScript API so you can interact with it once the editor is loaded and running.

Once 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).

/!\ The FCKeditorAPI object will not be available during the page load. You need to wait for the editor to be loaded to use it. If you need to interact with the editor right after is has been loaded, use the "FCKeditor_OnComplete" function (see "Events").

Retrieving an Editor Instance

From out of an external script:

When placing the editor in the page, you give it an "instance name". To retrieve it, you must simply call the FCKeditorAPI.GetInstance method. For example:

var oEditor = FCKeditorAPI.GetInstance('InstanceName') ;

From out of a dialog of the editor:

Call the InnerDialogLoaded to get the FCK Object. Read more in this HOWTO

在弹出窗口获取父窗口编辑器的引用.(模式窗)
var oEditor = window.parent.InnerDialogLoaded().FCK ;

Both methods return the main FCKeditor object that gives the necessary bridge to interact with it. These are the most useful properties and methods of this object:

Properties:
 * Name = (string) The instance name.
 * Status = (integer) The editor status (loading status).
 * EditorDocument = (object) The DOM Document object for the editing area.
 * EditorWindow = (object) The DOM Window object for the editing area.
Methods:
 * AttachToOnSelectionChange( functionPointer )
 * Focus()
 * GetXHTML( formatted )
 * InsertElement(element)
 * InsertElementAndGetIt(e)
 * InsertHtml(html)  // Inserts HTML in the current cursor position
 * IsDirty()  // Checks if the content in the editor has been changed
 * MakeEditable()
 * ResetIsDirty()  // Resets the dirty state
 * SetHTML( html, forceWYSIWYG )  // Sets the contents of the editor
     // Note that when using this method, you will loose any listener that you
     // may have previously registered on the editor.EditorDocument. A workaround is
     // to use instead: editor.EditorDocument.body.innerHTML.
 * SwitchEditMode()
 * UpdateLinkedField()
 
//-----一些代码
var oEditor = window.parent.InnerDialogLoaded().FCK;
oEditor.EditorDocument.body.innerHTML = thisFCK.GetXHTML();

Events

Once the editor loading phase 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 may interact with the editor. This is a declaration example:

function FCKeditor_OnComplete( editorInstance )
{
    alert( editorInstance.Name ) ;
}

Other than the above standard function, every FCKeditor instance has an "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.

  • OnAfterLinkedFieldUpdate: fired right after the hidden linked field attached to the editor has its contents updated. It happens usually when the form is being posted.

Misc commands

Through this API, many things can be controlled:

Developer's Guide/Javascript API (2006-11-03 01:08:07由Alessandro Vernet编辑)

posted on 2006-11-06 17:33  LGX.NET  阅读(851)  评论(0)    收藏  举报