-
Notifications
You must be signed in to change notification settings - Fork 22.7k
/
Copy pathindex.md
76 lines (53 loc) · 3.63 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
---
title: TextFormat
slug: Web/API/TextFormat
page-type: web-api-interface
status:
- experimental
browser-compat: api.TextFormat
---
{{APIRef("EditContext API")}}{{SeeCompatTable}}
The **`TextFormat`** interface represents specific formatting that should be applied to a range of text in an editable text region that's attached to an {{domxref("EditContext")}} instance. The text formatting is requested by the {{glossary("Input Method Editor")}} (IME) window that the user is composing text with.
When using one of the default editable regions of the web, such as a [`<textarea>`](/en-US/docs/Web/HTML/Reference/Elements/textarea) element, IME composition is handled by the browser and operating system for you. For example, when using Japanese IME in a textarea, on Windows, the following text formats can be applied:
- When text is being composed with the keyboard, the typed characters have a thin wavy underline:

- When the user selects a suggestion from the list of candidates in the IME window, the text is replaced and is underlined with a thick solid line:

When creating your own custom editable region by using the {{domxref("EditContext API", "", "", "nocode")}}, you need to handle IME composition yourself. You should listen for the {{domxref("EditContext/textformatupdate_event", "textformatupdate")}} event, which gives you the list of text formats that the IME window wants to apply to the text being composed. You should then update the formatting of the text displayed in your editable region accordingly.
## Constructor
- {{DOMxRef("TextFormat.TextFormat", "TextFormat()")}} {{experimental_inline}}
- : Returns a new `TextFormat` instance.
## Instance properties
- {{domxref("TextFormat.rangeStart")}} {{readonlyinline}} {{experimental_inline}}
- : The start position of the text range that needs to be formatted with the given text format.
- {{domxref("TextFormat.rangeEnd")}} {{readonlyinline}} {{experimental_inline}}
- : The end position of the text range that needs to be formatted with the given text format.
- {{domxref("TextFormat.underlineStyle")}} {{readonlyinline}} {{experimental_inline}}
- : The style of the underline that needs to be applied to the text range that is being formatted.
- {{domxref("TextFormat.underlineThickness")}} {{readonlyinline}} {{experimental_inline}}
- : The thickness of the underline that needs to be applied to the text range that is being formatted.
## Examples
### Using the `textformatupdate` event
In the following example, the `textformatupdate` event is used to log the various formats that the IME composition window wants to apply to text ranges in the editable element. Note that the event listener callback in this example is only called when using an IME window to compose text.
```html
<div id="editor" style="height:200px;background:#eee;"></div>
```
```js
const editorEl = document.getElementById("editor");
const editContext = new EditContext(editorEl);
editorEl.editContext = editContext;
editContext.addEventListener("textformatupdate", (e) => {
// Get the TextFormat instances.
const formats = e.getTextFormats();
// Iterate over the TextFormat instances.
for (const format of formats) {
console.log(
`Applying a ${format.underlineThickness} ${format.underlineStyle} underline between ${format.rangeStart} and ${format.rangeEnd}.`,
);
}
});
```
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}