Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

insertValue 后光标移动到插入的字符串之后 #1464

Closed
thinkido opened this issue Sep 5, 2023 · 0 comments
Closed

insertValue 后光标移动到插入的字符串之后 #1464

thinkido opened this issue Sep 5, 2023 · 0 comments
Assignees
Milestone

Comments

@thinkido
Copy link

thinkido commented Sep 5, 2023

你在什么场景下需要该功能?

编辑器里面添加快捷键插入字符。如F2快捷键插入当前时间。怎么解决,模拟事件不行

const onF2KeyPressed = (event) => {

const cursorPosition = vditor.value?.getCursorPosition();
let text = dayjs().format(' HH:mm');

vditor.value?.insertValue(text)
// 设置新的光标位置
// text.length
let countArr = [1, 2, 3, 4, 5];
countArr.forEach(() => simulateArrowRightOnTarget(event))

}

function simulateArrowRightOnTarget(event) {
const editableElement = event.target;
console.log('simulateArrowRightOnTarget:', editableElement)
// if (!editableElement || editableElement.getAttribute('contenteditable') !== 'true') return console.warn('simulateArrowRightOnTarget return');

// 创建一个键盘事件
const keyEvent = new KeyboardEvent('keydown', {
    key: 'ArrowRight',
    code: 'ArrowRight',
    keyCode: 39, // 'ArrowRight'的 keyCode
    which: 39,   // 'ArrowRight'的 which
    bubbles: true,
    composed: true
});

// 触发事件
editableElement.dispatchEvent(keyEvent);

}

其他信息

链接:https://ld246.com/article/1692667212627

@Vanessa219 Vanessa219 changed the title insertValue 插入字符后,需要光标在字符串后面。 insertValue 后光标移动到插入的字符串之后 Sep 5, 2023
@Vanessa219 Vanessa219 self-assigned this Sep 5, 2023
@Vanessa219 Vanessa219 added this to the 3.9 milestone Sep 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants