From 603f0d6d59031e24ca4b5ce2a226a627811c43d1 Mon Sep 17 00:00:00 2001 From: Patrick Brosset Date: Wed, 31 Jan 2024 11:03:08 +0100 Subject: [PATCH 1/9] Start of an EditContext tutorial --- .../web/api/editcontext_api/guide/index.md | 187 ++++++++++++++++++ files/jsondata/GroupData.json | 1 + 2 files changed, 188 insertions(+) create mode 100644 files/en-us/web/api/editcontext_api/guide/index.md diff --git a/files/en-us/web/api/editcontext_api/guide/index.md b/files/en-us/web/api/editcontext_api/guide/index.md new file mode 100644 index 000000000000000..d6410efeabeca66 --- /dev/null +++ b/files/en-us/web/api/editcontext_api/guide/index.md @@ -0,0 +1,187 @@ +--- +title: Using the EditContext API +slug: Web/API/EditContext_API/Guide +page-type: guide +--- + +{{DefaultAPISidebar("EditContext API")}} + +The **[EditContext API](/en-US/docs/Web/API/EditContext_API)** can be used to build rich text editors on the web that support advanced text input experiences, such as {{glossary("Input Method Editor")}} (IME) composition, emoji picker, or any other platform-specific editing-related UI surfaces. + +This article goes through all the steps that are necessary to build a text editor using the EditContext API. In this guide, you will build a simple HTML code editor that highlights the syntax of the code as you type, and that supports IME composition. + +## Final code and live demo + +If you want to see the final code, you can check out the [live demo](https://mdn.github.io/dom-examples/editcontext-api/html-editor/) and [source code](https://github.com/mdn/dom-examples/tree/master/editcontext-api/html-editor) on GitHub. + +## Create the editor UI + +The first step is to create the UI for the editor. The editor will be a simple [`
`](/en-US/docs/Web/HTML/Element/div) element with he [`spellcheck`](/en-US/docs/Web/HTML/Global_attributes/spellcheck) attribute set to `false` to disable spell checking: + +```html +
+``` + +The following CSS code is used to make the editor fill the entire viewport and scroll when there's too much content to fit. The [`white-space`](/en-US/docs/Web/CSS/white-space) property is also used to preserve whitespace, and the [`tab-size`](/en-US/docs/Web/CSS/tab-size) property is used to make tab characters render as two spaces. Finally, some default background and text colors are set. + +```css +#html-editor { + width: 100%; + height: 100%; + box-sizing: border-box; + padding: 1rem; + overflow: auto; + white-space: pre; + tab-size: 2; + background: #000; + color: red; +} +``` + +## Make the editor editable + +To make an element editable on the web, most of the time, you use an [``](/en-US/docs/Web/HTML/Element/input) element, a [`