EditContext API
The EditContext() constructor returns a new EditContext object.
Syntax
new EditContext()
new EditContext(options)
Parameters
options- : An optional object with the following properties:
text- : A string to set the initial text of the
EditContext.
- : A string to set the initial text of the
selectionStart- : A number to set the initial selection start of the
EditContext.
- : A number to set the initial selection start of the
selectionEnd- : A number to set the initial selection end of the
EditContext.
- : A number to set the initial selection end of the
- : An optional object with the following properties:
Examples
Instantiating an EditContext object
The following example creates a new EditContext object with the initial text "Hello world!" and the initial selection covering the entire text.
<div id="editor"></div>
const initialText = "Hello world!";
const editContext = new EditContext({
text: initialText,
selectionStart: 0,
selectionEnd: initialText.length,
});
const editorElement = document.getElementById("editor");
editorElement.editContext = editContext;
console.log(
`EditContext object ready. Text: ${editContext.text}. Selection: ${editContext.selectionStart} - ${editContext.selectionEnd}.`,
);
Specifications
Browser compatibility
See also
- The EditContext interface it belongs to.
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
