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

浏览器兼容性

另见