From 3dedc6e4d24e6cdf8c4c97d5d6af33346e44608d Mon Sep 17 00:00:00 2001
From: Dan Clark The {{EditContext}} is a new API that allows authors to more directly participate in the text input process.
- {{EditContext}} is an API that allows authors to more directly participate in the text input process.Introduction
EditContext state
If an {{EditContext}}'s associated element's parent is not @@ -228,7 +224,7 @@
+
In many ways, an EditContext editing host behaves in the same way as other types of [=editing host=], e.g. for a [^html-global/contenteditable^] element. Notable similarities include: @@ -262,7 +258,7 @@
@@ -321,18 +317,18 @@
Using an {{EditContext}}, an author can mark a region of the document editable by associating an {{EditContext}} object with an element as shown in the example below:
+<script type="module"> + let canvas = document.querySelector("canvas") + canvas.editContext = new EditContext() + // When the associated element is focused, the EditContext is automatically activated. + canvas.focus(); +</script> +<canvas></canvas>
In the example below, the author is using a canvas to draw an editable region that allows the user to input a single line of text rendered with a monospace font. The text for the editable region is maintained by the author as a String. The text offsets for the selection in the editable region are maintained by the author as a pair of Numbers: selectionStart and selectionEnd. The Numbers refer to the count of the number of UTF-16 codepoints to the left of the start and end of the selection respectively. For the sake of communicating the bounding boxes for the current selection and the editable region of the document to Text Input Services, the author also computes the bounding rectangle in CSS pixels for the selection and the editable region of the document. The offset of the rectangle is expressed relative to the origin of the canvas element since that is the element to which the author has associated an EditContext. Since the model for the author’s representation of text and selection location matches the form expected by the EditContext API, the author can simply assign those properties to the EditContext associated with the canvas whenever those values change.
Building on the previous example, in response to user input, authors should handle the events of both the editable element (in this case a canvas) and the EditContext.
@@ -410,7 +406,7 @@The below example shows how to handle {{TextUpdateEvent}}, {{TextFormatUpdateEvent}}, and {{CharacterBoundsUpdateEvent}} to update the model and render the result to the canvas.
@@ -504,8 +500,8 @@An author doesn’t have to use a canvas element with an EditContext. In the example below the author uses a div to establish an editable region of the document and renders the contents into that editable region using various other styled elements, images and text. This allows the author to leverage other built-in editing primitives from the user agent such as selection and spellcheck.
@@ -999,7 +995,7 @@+};dictionary EditContextInit { + dictionary EditContextInit { DOMString text; unsigned long selectionStart; unsigned long selectionEnd; @@ -1014,22 +1010,22 @@EditContext Interface
undefined updateSelection(unsigned long start, unsigned long end); undefined updateControlBounds(DOMRect controlBounds); undefined updateSelectionBounds(DOMRect selectionBounds); - undefined updateCharacterBounds(unsigned long rangeStart, sequencecharacterBounds); + undefined updateCharacterBounds(unsigned long rangeStart, sequence<DOMRect> characterBounds); - sequence attachedElements(); + sequence<HTMLElement> attachedElements(); readonly attribute DOMString text; readonly attribute unsigned long selectionStart; readonly attribute unsigned long selectionEnd; readonly attribute unsigned long characterBoundsRangeStart; - sequence characterBounds(); + sequence<DOMRect> characterBounds(); attribute EventHandler ontextupdate; attribute EventHandler ontextformatupdate; attribute EventHandler oncharacterboundsupdate; attribute EventHandler oncompositionstart; attribute EventHandler oncompositionend; -};
+};dictionary TextUpdateEventInit : EventInit { + dictionary TextUpdateEventInit : EventInit { unsigned long updateRangeStart; unsigned long updateRangeEnd; DOMString text; @@ -1197,7 +1193,7 @@TextUpdateEvent
readonly attribute DOMString text; readonly attribute unsigned long selectionStart; readonly attribute unsigned long selectionEnd; -};
+ sequence<TextFormat> getTextFormats(); +};enum UnderlineStyle { "none", "solid", "dotted", "dashed", "wavy" }; + enum UnderlineStyle { "none", "solid", "dotted", "dashed", "wavy" }; enum UnderlineThickness { "none", "thin", "thick" }; dictionary TextFormatInit { @@ -1237,14 +1233,14 @@TextFormatUpdateEvent
}; dictionary TextFormatUpdateEventInit : EventInit { - sequencetextFormats; + sequence<TextFormat> textFormats; }; [Exposed=Window] interface TextFormatUpdateEvent : Event { constructor(DOMString type, optional TextFormatUpdateEventInit options = {}); - sequence getTextFormats(); -};
+};dictionary CharacterBoundsUpdateEventInit : EventInit { + dictionary CharacterBoundsUpdateEventInit : EventInit { unsigned long rangeStart; unsigned long rangeEnd; }; @@ -1272,7 +1268,7 @@CharacterBoundsUpdateEvent
constructor(DOMString type, optional CharacterBoundsUpdateEventInit options = {}); readonly attribute unsigned long rangeStart; readonly attribute unsigned long rangeEnd; -};