跳到主要内容

fallback composition state

一、作用

二、初始化

export function initialize(nativeEventTarget) {
root = nativeEventTarget;
startText = getText();
return true;
}

三、重置

export function reset() {
root = null;
startText = null;
fallbackText = null;
}

四、获取数据

export function getData() {
if (fallbackText) {
return fallbackText;
}

let start;
const startValue = startText;
const startLength = startValue.length;
let end;
const endValue = getText();
const endLength = endValue.length;

for (start = 0; start < startLength; start++) {
if (startValue[start] !== endValue[start]) {
break;
}
}

const minEnd = startLength - start;
for (end = 1; end <= minEnd; end++) {
if (startValue[startLength - end] !== endValue[endLength - end]) {
break;
}
}

const sliceTail = end > 1 ? 1 - end : undefined;
fallbackText = endValue.slice(start, sliceTail);
return fallbackText;
}

五、获取文本

export function getText() {
if ('value' in root) {
return root.value;
}
return root.textContent;
}

六、变量

1. 根

/**
* These variables store information about text content of a target node,
* allowing comparison of content before and after a given event.
*
* 这些变量存储目标节点的文本内容信息,允许在特定事件前后比较内容。
*
* Identify the node where selection currently begins, then observe
* both its text content and its current position in the DOM. Since the
* browser may natively replace the target node during composition, we can
* use its position to find its replacement.
*
* 确定当前选择开始的节点,然后观察它的文本内容以及在 DOM 中的当前位置。由于浏览器在输入法编辑过程
* 中可能会原生替换目标节点,我们可以使用它的位置来找到其替代节点。
*
*
*/

// 根
let root = null;
// 开始文本
let startText = null;
// 备选文本
let fallbackText = null;