replaceText
2021-01-10 19:56:29 もうメンテしていません
/icons/hr.icon
oldTextの文字数だけDeleteを実行した後に、insertTextを挿入する関数
code:script.js
export function replaceText(cursor, oldText, newText) {
const isFirefox = () => {
const userAgent = window.navigator.userAgent.toLowerCase();
if (userAgent.indexOf('firefox') != -1) {
return true;
}
return false;
};
setTimeout(() => {
// Deleteを実行
for(const _ of oldText.match(/./ug)) {
cursor.dispatchEvent(new KeyboardEvent('keydown',
{bubbles: true, cancelable: true, keyCode: 46}));
}
// Insertを実行
if (isFirefox()) {
const start = cursor.selectionStart; // in this case maybe 0
cursor.setRangeText(newText);
cursor.selectionStart = cursor.selectionEnd = start + newText.length;
const uiEvent = document.createEvent('UIEvent');
uiEvent.initEvent('input', true, false);
cursor.dispatchEvent(uiEvent);
} else {
document.execCommand('insertText', false, newText);
}
}, 50);
}