Midas 编辑

Introduction

Midas is the code name for Gecko's built-in rich text editor. Midas can be enabled via JavaScript on an HTML document. When Midas is enabled, the document becomes editable by the user. Scripting for Midas is based on the DHTML commands supported by Internet Explorer. Internet Explorer supports the ability to make an entire document editable by setting the designMode property of the document object; this is how Midas is invoked in Gecko. Internet Explorer also supports the ability to edit specific elements using the contentEditable attribute; Starting with Firefox 3, Gecko also supports contentEditable. Once Midas is invoked, a few more methods of the document object become available.

You can try demo of this here.

Properties

document.designMode
By setting this property to "on", the document becomes editable.

Notes

Since an entire document becomes editable, authors often load the editable document into an IFRAME and do the bulk of the scripting in the parent document. According to standards, The IFRAME element has the contentDocument property that refers to the document in the inline frame. It also has a property called contentWindow that refers to the window object inside the inline frame. This also avoids problems with bug 198155.

In addition to the built-in commands, advanced editing can be done by manipulating the Selection and range objects. It is beneficial to be familiar with these objects when working with an editable document.

Examples

This example shows the basic structure described in the Notes section :

<html>
	<head>
		<title>Simple Edit Box</title>
	</head>
	<body>
		<iframe
			id="MidasForm"
			src="about:blank"
			onload="this.contentDocument.designMode='on';"
		></iframe>
	</body>
</html>

Methods

Document.execCommand
Executes the given command.
Document.queryCommandEnabled
Determines whether the given command can be executed on the document in its current state.
Document.queryCommandIndeterm
Determines whether the current selection is in an indetermined state.
Document.queryCommandState
Determines whether the given command has been executed on the current selection.
Document.queryCommandValue
Determines the current value of the document, range, or current selection for the given command.

Supported Commands

CommandValueDescription
backcolorA color code.This command will set the background color of the document.
boldIf there is no selection, the insertion point will set bold for subsequently typed characters.

If there is a selection and all of the characters are already bold, the bold will be removed. Otherwise, all selected characters will become bold.

contentReadOnlyThis command will make the editor readonly(true)or editable(false). Anticipated usage is for temporarily disabling input while something else is occurring elsewhere in the web page.
copyIf there is a selection, this command will copy the selection to the clipboard. If there isn't a selection, nothing will happen.

note: this command won't work without setting a pref or using signed JS. See: more about security preferences

note: the shortcut key will automatically trigger this command (typically accel-C) with or without the signed JS or any code on the page to handle it.

createlinkA URI.This command will not do anything if no selection is made. If there is a selection, a link will be inserted around the selection with the url parameter as the href of the link.
cutIf there is a selection, this command will copy the selection to the clipboard and remove the selection from the edit control. If there isn't a selection, nothing will happen.

note: this command won't work without setting a pref or using signed JS. See: more about security preferences

note: the shortcut key will automatically trigger this command (typically accel-X) with or without the signed JS or any code on the page to handle it.

decreasefontsizeThis command will add a <small> tag around selection or at insertion point.
deleteThis command will delete all text and objects that are selected. If no text is selected it deletes one character to the right. This is similar to the Delete button on the keyboard.
fontnameA font nameThis command will set the font face for a selection or at the insertion point if there is no selection.

The given string is such as would be used in the "face" attribute of the font tag, i.e., a comma-separated list of font names.

fontsizeA numberThis command will set the fontsize for a selection or at the insertion point if there is no selection.

The given number is such as would be used in the "size" attribute of the font tag.

forecolorA color codeThis command will set the text color of the selection or at the insertion point.
formatblockH1, H2, H3, H4, H5, H6, P, DIV, ADDRESS, BLOCKQUOTE (more?)The selection surrounded by the given block element.
headingH1, H2, H3, H4, H5, H6Selected block will be formatted as the given type of heading.
hilitecolorA color codeThis command will set the hilite color of the selection or at the insertion point. It only works with styleWithCSS enabled.
increasefontsizeThis command will add a <big> tag around selection or at insertion point.
indentIndent the block where the caret is located. If the caret is inside a list, that item becomes a sub-item one level deeper.
insertbronreturntrue/falseSelects whether pressing return inside a paragraph creates another paragraph or just inserts a <br> tag.
inserthorizontalrulenull/string (when string is the Line's id)This command will insert a horizontal rule (line) at the insertion point.

Does it delete the selection? Yes!

inserthtmlA string.This command will insert the given html into the <body> in place of the current selection or at the caret location.

The given string is the HTML to insert.

insertimageA URI.This command will insert an image (referenced by the given url) at the insertion point.
insertorderedlistDepends on the selection. If the caret is not inside a non-LI block, that block becomes the first LI and an OL. If the caret is inside a bulleted item, the bulleted item becomes a numbered item.
insertunorderedlistDepends on the selection. If the caret is not inside a non-LI block, that block becomes the first LI and UL. If the caret is inside a numbered item, the numbered item becomes a bulleted item.
insertparagraphInserts a new paragraph.
italicIf there is no selection, the insertion point will set italic for subsequently typed characters.

If there is a selection and all of the characters are already italic, the italic will be removed. Otherwise, all selected characters will become italic.

justifycenterCenter-aligns the current block.
justifyfullFully-justifies the current block.
justifyleftLeft-aligns the current block.
justifyrightRight aligns the current block.
outdentOutdent the block where the caret is located. If the block is not indented prior to calling outdent, nothing will happen.

If the caret is in a list item, the item will bump up a level in the list or break out of the list entirely.

pasteThis command will paste the contents of the clipboard at the location of the caret. If there is a selection, it will be deleted prior to the insertion of the clipboard's contents.

note: this command won't work without setting a pref or using signed JS. user_pref("capability.policy.policynames", "allowclipboard"); user_pref("capability.policy.allowclipboard.Clipboard.paste", "allAccess"); See: more about security preferences

note: the shortcut key will automatically trigger this command (typically accel-V) with or without the signed JS or any code on the page to handle it.

redoThis command will redo the previous undo action. If undo was not the most recent action, this command will have no effect.

note: the shortcut key will automatically trigger this command (typically accel-shift-Z)

removeformatRemoves inline formatting from the current selection.
selectallThis command will select all of the contents within the editable area.

note: the shortcut key will automatically trigger this command (typically accel-A)

strikethroughIf there is no selection, the insertion point will set strikethrough for subsequently typed characters.

If there is a selection and all of the characters are already striked, the strikethrough will be removed. Otherwise, all selected characters will have a line drawn through them.

styleWithCSSThis command is used for toggling the format of generated content. By default (at least today), this is true. An example of the differences is that the "bold" command will generate <b> if the styleWithCSS command is false and generate css style attribute if the styleWithCSS command is true.
subscriptIf there is no selection, the insertion point will set subscript for subsequently typed characters.

If there is a selection and all of the characters are already subscripted, the subscript will be removed. Otherwise, all selected characters will be drawn slightly lower than normal text.

superscriptIf there is no selection, the insertion point will set superscript for subsequently typed characters.

If there is a selection and all of the characters are already superscripted, the superscript will be removed. Otherwise, all selected characters will be drawn slightly higher than normal text.

underlineIf there is no selection, the insertion point will set underline for subsequently typed characters.

If there is a selection and all of the characters are already underlined, the underline will be removed. Otherwise, all selected characters will become underlined.

undoThis command will undo the previous action. If no action has occurred in the document, then this command will have no effect.

note: the shortcut key will automatically trigger this command (typically accel-Z)

unlinkIf the insertion point is within a link or if the current selection contains a link, the link will be removed and the text will remain.

Deprecated
This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

readonlyThis command has been replaced with contentReadOnly. It takes the same values as contentReadOnly, but the meaning of true and false are inversed.
useCSSThis command has been replaced with styleWithCSS. It takes the same values as styleWithCSS, but the meaning of true and false are inversed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:121 次

字数:16779

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文