EditContext:updateControlBounds() 方法

可用性有限

此特性不是基線特性,因為它在一些最廣泛使用的瀏覽器中不起作用。

實驗性: 這是一項實驗性技術
在生產中使用此技術之前,請仔細檢查瀏覽器相容性表格

EditContext 介面的 EditContext.updateControlBounds() 方法用於將可編輯文字區域的位置和大小資訊通知作業系統。

呼叫此方法可告知作業系統當前可編輯區域的邊界。您應該在初始化 EditContext 時以及每當可編輯區域的邊界發生變化時(例如網頁大小調整時)呼叫此方法。這些邊界用於定位平臺特定的、與編輯相關的 UI 表面,例如 輸入法編輯器 (IME) 視窗。

語法

js
updateControlBounds(controlBounds)

引數

controlBounds

一個表示新控制元件邊界的 DOMRect 物件。

返回值

無 (undefined)。

異常

TypeError

如果方法在沒有引數的情況下呼叫,或者提供的引數不是 DOMRect 物件,則會丟擲錯誤。

示例

在編輯器初始化和視窗大小調整時更新控制元件邊界

此示例展示瞭如何使用 updateControlBounds() 方法隨時告知平臺可編輯區域的位置。

css
#editor {
  border: 1px solid black;
  height: 50vw;
  width: 50vh;
}
html
<div id="editor"></div>
js
const editorEl = document.getElementById("editor");
const editContext = new EditContext();
editorEl.editContext = editContext;

function updateControlBounds() {
  const editorBounds = editorEl.getBoundingClientRect();
  editContext.updateControlBounds(editorBounds);
  console.log(
    `Updated control bounds to ${editorBounds.x}, ${editorBounds.y}, ${editorBounds.width}, ${editorBounds.height}`,
  );
}

// Update the control bounds now.
updateControlBounds();
// And when the page is resized.
window.addEventListener("resize", updateControlBounds);

規範

規範
EditContext API
# dom-editcontext-updatecontrolbounds

瀏覽器相容性

另見