且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

FreeTextBox实现机制

更新时间:2022-09-11 12:32:00

  刚开始试用FTB2.0的时候,感觉FTB真的很神,居然可以在网页状态实现编辑过程的What you see is what you get。看完FTB的文档(其实也不是很多的东西,估计就是用个NDOC或者什么类似的工具生成的SDK文档)又仔细试做了几个程序,觉得FTB的实现思路不复杂,但十分巧妙。它通过FTB这个中间部件将客户端(浏览器)的程序(javascript代码)和后台程序(C#写的aspx等ASP.NET代码)紧密结合,从而实现了这种所见即所得的效果。

FTB的结构主要有三个命名空间组成:
FreeTextBoxControls,FreeTextBoxControls.Design和FreeTextBoxControls.Support。

       使用得最多的是FreeTextBoxControls,基本上用到的界面部件都来自于这里,例如ToolBar上每个功能Button可以在这里找到映射,而每个Button又对应着javascrip中的一个函数功能。

例如:下划线这个功能,有个Underline的类(继承于ToolbarButton)实现,而这个类实际调用客户端的一段javascript代码FTB_Underline(在FreeTextBox-ToolbarItemsSrcipt.js中)。

FreeTextBox实现机制function FTB_Underline(ftbName) { 
FreeTextBox实现机制    FTB_Format(ftbName,'underline'); 
FreeTextBox实现机制

        如果再深究下去,仔细跟踪下这段js的代码又可以它调用FTB_Format这段代码(在FreeTextBox-MainScript.js中)。

FreeTextBox实现机制function FTB_Format(ftbName,commandName) {
FreeTextBox实现机制    editor = FTB_GetIFrame(ftbName);
FreeTextBox实现机制
FreeTextBox实现机制    if (FTB_IsHtmlMode(ftbName)) return;
FreeTextBox实现机制    editor.focus();
FreeTextBox实现机制    editor.document.execCommand(commandName,'',null);
FreeTextBox实现机制
FreeTextBox实现机制}
FreeTextBox实现机制

它正是通过document的execCommand方法来实现效果的,查MSDN文挡可以发现它对应执行的正是Underline的命令参数。

execCommand可以执行的命令参数:

FreeTextBox实现机制Command Identifiers
FreeTextBox实现机制2D-Position
FreeTextBox实现机制Allows absolutely positioned elements to be moved by dragging.
FreeTextBox实现机制
FreeTextBox实现机制AbsolutePosition
FreeTextBox实现机制Sets an element's position property to "absolute."
FreeTextBox实现机制
FreeTextBox实现机制BackColor
FreeTextBox实现机制Sets or retrieves the background color of the current selection.
FreeTextBox实现机制
FreeTextBox实现机制BlockDirLTR
FreeTextBox实现机制Not currently supported.
FreeTextBox实现机制
FreeTextBox实现机制BlockDirRTL
FreeTextBox实现机制Not currently supported.
FreeTextBox实现机制
FreeTextBox实现机制Bold
FreeTextBox实现机制Toggles the current selection between bold and nonbold.
FreeTextBox实现机制
FreeTextBox实现机制BrowseMode
FreeTextBox实现机制Not currently supported.
FreeTextBox实现机制
FreeTextBox实现机制ClearAuthenticationCache
FreeTextBox实现机制Clears all authentication credentials from the cache. Applies only to execCommand.
FreeTextBox实现机制
FreeTextBox实现机制Copy
FreeTextBox实现机制Copies the current selection to the clipboard.
FreeTextBox实现机制
FreeTextBox实现机制CreateBookmark
FreeTextBox实现机制Creates a bookmark anchor or retrieves the name of a bookmark anchor for the current selection or insertion point.
FreeTextBox实现机制
FreeTextBox实现机制CreateLink
FreeTextBox实现机制Inserts a hyperlink on the current selection, or displays a dialog box enabling the user to specify a URL to insert as a hyperlink on the current selection.
FreeTextBox实现机制
FreeTextBox实现机制Cut
FreeTextBox实现机制Copies the current selection to the clipboard and then deletes it.
FreeTextBox实现机制
FreeTextBox实现机制Delete
FreeTextBox实现机制Deletes the current selection.
FreeTextBox实现机制
FreeTextBox实现机制DirLTR
FreeTextBox实现机制Not currently supported.
FreeTextBox实现机制
FreeTextBox实现机制DirRTL
FreeTextBox实现机制Not currently supported.
FreeTextBox实现机制
FreeTextBox实现机制EditMode
FreeTextBox实现机制Not currently supported.
FreeTextBox实现机制
FreeTextBox实现机制FontName
FreeTextBox实现机制Sets or retrieves the font for the current selection.
FreeTextBox实现机制
FreeTextBox实现机制FontSize
FreeTextBox实现机制Sets or retrieves the font size for the current selection.
FreeTextBox实现机制
FreeTextBox实现机制ForeColor
FreeTextBox实现机制Sets or retrieves the foreground (text) color of the current selection.
FreeTextBox实现机制
FreeTextBox实现机制FormatBlock
FreeTextBox实现机制Sets the current block format tag.
FreeTextBox实现机制
FreeTextBox实现机制Indent
FreeTextBox实现机制Increases the indent of the selected text by one indentation increment.
FreeTextBox实现机制
FreeTextBox实现机制InlineDirLTR
FreeTextBox实现机制Not currently supported.
FreeTextBox实现机制
FreeTextBox实现机制InlineDirRTL
FreeTextBox实现机制Not currently supported.
FreeTextBox实现机制
FreeTextBox实现机制InsertButton
FreeTextBox实现机制Overwrites a button control on the text selection.
FreeTextBox实现机制
FreeTextBox实现机制InsertFieldset
FreeTextBox实现机制Overwrites a box on the text selection.
FreeTextBox实现机制
FreeTextBox实现机制InsertHorizontalRule
FreeTextBox实现机制Overwrites a horizontal line on the text selection.
FreeTextBox实现机制
FreeTextBox实现机制InsertIFrame
FreeTextBox实现机制Overwrites an inline frame on the text selection.
FreeTextBox实现机制
FreeTextBox实现机制InsertImage
FreeTextBox实现机制Overwrites an image on the text selection.
FreeTextBox实现机制
FreeTextBox实现机制InsertInputButton
FreeTextBox实现机制Overwrites a button control on the text selection.
FreeTextBox实现机制
FreeTextBox实现机制InsertInputCheckbox
FreeTextBox实现机制Overwrites a check box control on the text selection.
FreeTextBox实现机制
FreeTextBox实现机制InsertInputFileUpload
FreeTextBox实现机制Overwrites a file upload control on the text selection.
FreeTextBox实现机制
FreeTextBox实现机制InsertInputHidden
FreeTextBox实现机制Inserts a hidden control on the text selection.
FreeTextBox实现机制
FreeTextBox实现机制InsertInputImage
FreeTextBox实现机制Overwrites an image control on the text selection.
FreeTextBox实现机制
FreeTextBox实现机制InsertInputPassword
FreeTextBox实现机制Overwrites a password control on the text selection.
FreeTextBox实现机制
FreeTextBox实现机制InsertInputRadio
FreeTextBox实现机制Overwrites a radio control on the text selection.
FreeTextBox实现机制
FreeTextBox实现机制InsertInputReset
FreeTextBox实现机制Overwrites a reset control on the text selection.
FreeTextBox实现机制
FreeTextBox实现机制InsertInputSubmit
FreeTextBox实现机制Overwrites a submit control on the text selection.
FreeTextBox实现机制
FreeTextBox实现机制InsertInputText
FreeTextBox实现机制Overwrites a text control on the text selection.
FreeTextBox实现机制
FreeTextBox实现机制InsertMarquee
FreeTextBox实现机制Overwrites an empty marquee on the text selection.
FreeTextBox实现机制
FreeTextBox实现机制InsertOrderedList
FreeTextBox实现机制Toggles the text selection between an ordered list and a normal format block.
FreeTextBox实现机制
FreeTextBox实现机制InsertParagraph
FreeTextBox实现机制Overwrites a line break on the text selection.
FreeTextBox实现机制
FreeTextBox实现机制InsertSelectDropdown
FreeTextBox实现机制Overwrites a drop-down selection control on the text selection.
FreeTextBox实现机制
FreeTextBox实现机制InsertSelectListbox
FreeTextBox实现机制Overwrites a list box selection control on the text selection.
FreeTextBox实现机制
FreeTextBox实现机制InsertTextArea
FreeTextBox实现机制Overwrites a multiline text input control on the text selection.
FreeTextBox实现机制
FreeTextBox实现机制InsertUnorderedList
FreeTextBox实现机制Toggles the text selection between an ordered list and a normal format block.
FreeTextBox实现机制
FreeTextBox实现机制Italic
FreeTextBox实现机制Toggles the current selection between italic and nonitalic.
FreeTextBox实现机制
FreeTextBox实现机制JustifyCenter
FreeTextBox实现机制Centers the format block in which the current selection is located.
FreeTextBox实现机制
FreeTextBox实现机制JustifyFull
FreeTextBox实现机制Not currently supported.
FreeTextBox实现机制
FreeTextBox实现机制JustifyLeft
FreeTextBox实现机制Left-justifies the format block in which the current selection is located.
FreeTextBox实现机制
FreeTextBox实现机制JustifyNone
FreeTextBox实现机制Not currently supported.
FreeTextBox实现机制
FreeTextBox实现机制JustifyRight
FreeTextBox实现机制Right-justifies the format block in which the current selection is located.
FreeTextBox实现机制
FreeTextBox实现机制LiveResize
FreeTextBox实现机制Causes the MSHTML Editor to update an element's appearance continuously during a resizing or moving operation, rather than updating only at the completion of the move or resize.
FreeTextBox实现机制
FreeTextBox实现机制MultipleSelection
FreeTextBox实现机制Allows for the selection of more than one site selectable element at a time when the user holds down the SHIFT or CTRL keys.
FreeTextBox实现机制
FreeTextBox实现机制Open
FreeTextBox实现机制Not currently supported.
FreeTextBox实现机制
FreeTextBox实现机制Outdent
FreeTextBox实现机制Decreases by one increment the indentation of the format block in which the current selection is located.
FreeTextBox实现机制
FreeTextBox实现机制OverWrite
FreeTextBox实现机制Toggles the text-entry mode between insert and overwrite.
FreeTextBox实现机制
FreeTextBox实现机制Paste
FreeTextBox实现机制Overwrites the contents of the clipboard on the current selection.
FreeTextBox实现机制
FreeTextBox实现机制PlayImage
FreeTextBox实现机制Not currently supported.
FreeTextBox实现机制
FreeTextBox实现机制Print
FreeTextBox实现机制Opens the print dialog box so the user can print the current page.
FreeTextBox实现机制
FreeTextBox实现机制Redo
FreeTextBox实现机制Not currently supported.
FreeTextBox实现机制
FreeTextBox实现机制Refresh
FreeTextBox实现机制Refreshes the current document.
FreeTextBox实现机制
FreeTextBox实现机制RemoveFormat
FreeTextBox实现机制Removes the formatting tags from the current selection.
FreeTextBox实现机制
FreeTextBox实现机制RemoveParaFormat
FreeTextBox实现机制Not currently supported.
FreeTextBox实现机制
FreeTextBox实现机制SaveAs
FreeTextBox实现机制Saves the current Web page to a file.
FreeTextBox实现机制
FreeTextBox实现机制SelectAll
FreeTextBox实现机制Selects the entire document.
FreeTextBox实现机制
FreeTextBox实现机制SizeToControl
FreeTextBox实现机制Not currently supported.
FreeTextBox实现机制
FreeTextBox实现机制SizeToControlHeight
FreeTextBox实现机制Not currently supported.
FreeTextBox实现机制
FreeTextBox实现机制SizeToControlWidth
FreeTextBox实现机制Not currently supported.
FreeTextBox实现机制
FreeTextBox实现机制Stop
FreeTextBox实现机制Not currently supported.
FreeTextBox实现机制
FreeTextBox实现机制StopImage
FreeTextBox实现机制Not currently supported.
FreeTextBox实现机制
FreeTextBox实现机制StrikeThrough
FreeTextBox实现机制Not currently supported.
FreeTextBox实现机制
FreeTextBox实现机制Subscript
FreeTextBox实现机制Not currently supported.
FreeTextBox实现机制
FreeTextBox实现机制Superscript
FreeTextBox实现机制Not currently supported.
FreeTextBox实现机制
FreeTextBox实现机制UnBookmark
FreeTextBox实现机制Removes any bookmark from the current selection.
FreeTextBox实现机制
FreeTextBox实现机制Underline
FreeTextBox实现机制Toggles the current selection between underlined and not underlined.
FreeTextBox实现机制
FreeTextBox实现机制Undo
FreeTextBox实现机制Not currently supported.
FreeTextBox实现机制
FreeTextBox实现机制Unlink
FreeTextBox实现机制Removes any hyperlink from the current selection.
FreeTextBox实现机制
FreeTextBox实现机制Unselect
FreeTextBox实现机制Clears the current selection.
FreeTextBox实现机制
FreeTextBox实现机制

       基本上每个命令参数都可以在FTB的FreeTextBoxControls中找到对应的实现类,如果觉得有些没有实现,自己参照已经实现的功能来增加也十分简单和方便。

        FTB还提供了公开的接口,例如继承于ToolbarButton可以实现对应的工具按钮,继承于ToolbarDropDownList则实现下拉式选择(如选择字体那种),对应javascript的方法只须传递对应的方法名字符串给类即可,自己写的javascript可以放在js中,也可以放在ScriptBlock的字符串参数里面,前者前端查看源码看不到,后者则将整个函数源码传回,一切都十分公开和方便。
        这种思路是否也和ASP.NET的思路类似?
        由于javascript可以被多种浏览器支持(估计有些小兼容问题,可以通过javascript来兼容),因此FTB可以在多种环境下正常工作。现在用的这个blog系统(.Text)也用了FTB,但版本是1.6.3.26037(汉化版),有兴趣可以在发表文章的地方查看网页源代码看看,就会发现好多的FTB_XXX的javascript函数。这些在2.0已经全部集中放到FreeTextBox-ToolbarItemsSrcipt.js和FreeTextBox-MainScript.js中了,应该说这样比较归一些。

        如果担心FTB的免费协议对商业用途有些影响的话,自己根据这个思路来开发一个适合自己产品用的所见即所得编辑器控件应该也不是难事。


本文转自风前絮~~博客园博客,原文链接:http://www.cnblogs.com/windsails/archive/2004/09/27/47143.html,如需转载请自行联系原作者