久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使 document.execCommand('insertText', false, 'me

make document.execCommand(#39;insertText#39;, false, #39;message#39;) work with draftjs?(使 document.execCommand(insertText, false, message) 與 Draftjs 一起使用?)
本文介紹了使 document.execCommand('insertText', false, 'message') 與 Draftjs 一起使用?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我正在開發(fā)一個(gè)需要將文本插入到 contenteditable="true" div 的應(yīng)用程序(準(zhǔn)確地說(shuō)是基于 Draftjs 的文本字段).

I'm working on an application which needs to insert text into a contenteditable="true" div (a Draftjs based textfield to be precise).

現(xiàn)在我知道 Draft.js 使用 react 并且應(yīng)該以這種方式使用它,但在這種情況下,應(yīng)用程序已經(jīng)存在,這是一個(gè)可以使用它的第三方電子應(yīng)用程序.

Now I am aware that Draft.js uses react and that it should be used that way, but in this case, the application already exists and this is a third party electron app that works with it.

我正在處理 macOS 上的內(nèi)聯(lián)通知回復(fù),因此我需要將該回復(fù)文本粘貼到 draftJS 字段中,但是,當(dāng)我這樣做時(shí):

I'm working on in-line notification replying on macOS, so I need that reply text to be pasted inside the draftJS field, however, when I do so using:

document.querySelector('div[contenteditable="true"]').focus();
document.execCommand('insertText', false, 'message');

它拋出一個(gè)錯(cuò)誤:

我能夠使用:

const event = document.createEvent('TextEvent');
event.initTextEvent('textInput', true, true, window, 'message', 0, locale);

但如果消息包含表情符號(hào),此 API 已棄用且無(wú)法正常工作.

but this API is deprecated and doesn't work properly if the message contains an emoji.

有什么方法可以做到這一點(diǎn)不會(huì)導(dǎo)致錯(cuò)誤?我發(fā)現(xiàn)應(yīng)該替換 initTextEvent 的新 API 只是 new Event() (參見 docs),但我不知道它是否支持 textInput 事件.

Is there any way to do this that doesn't cause an error? I found out that the new API that is supposed to replace initTextEvent is just new Event() (see docs), but I can't find out if it supports textInput events.

要使用它,您只需轉(zhuǎn)到 https://draftjs.org/ 并在chrome 開發(fā)工具.

To play around with it you can just go to https://draftjs.org/ and play with it in chrome dev tools.

我真的很感激這里的一些幫助,因?yàn)槲也恢涝撛趺醋霾拍茏屗ぷ髁?另外,我知道人們是 jquery 的粉絲,但我更喜歡原生 js 解決方案(盡管歡迎任何解決方案).

I would really appreciate some help here as I don't know what to do to make it work anymore. Also, I know people are a fan of jquery, but I'd prefer a native js solution (although any solution is welcome).

請(qǐng)注意:我沒(méi)有使用react,我要修改的輸入字段(draftjs)正在使用react,我想使用本機(jī)js向其中輸入文本.

Please note: I'm not using react, the input field I want to modify (draftjs) is using react and I want to input text into it using native js.

編輯 2:

對(duì)于遇到此問(wèn)題的其他人,我想將文本插入 Facebook Messenger 文本字段(使用 Draftjs).

For anyone else coming across this issue, I wanted to insert text into the Facebook messenger text field (which uses Draftjs).

我設(shè)法找到了一個(gè)可行的解決方法.它確實(shí)使用了已棄用的 API (event.initTextEvent),但這是我發(fā)現(xiàn)的唯一可行的方法,即使使用表情符號(hào)也是如此.如果您對(duì)此有更好的解決方案,請(qǐng)發(fā)布答案.它的工作原理是這樣的:

I managed to find a working workaround. It does use the deprecated API (event.initTextEvent), but it's the only way that I've found that works, even with emoji. Please do post an answer if you have a better solution to this. It works like this:

async function sendReply(message: string): Promise<void> {
       const inputField = document.querySelector('[contenteditable="true"]') as HTMLElement;
       if (inputField) {
               const previousMessage = inputField.textContent;
               // Send message
               inputField.focus();
               await insertMessageText(message, inputField);
               (await elementReady('._30yy._38lh._39bl')).click();
               // Restore (possible) previous message
               if (previousMessage) {
                       insertMessageText(previousMessage, inputField);
               }
       }
}

function insertMessageText(text: string, inputField: HTMLElement): void {
       // Workaround: insert placeholder value to get execCommand working
       if (!inputField.textContent) {
               const event = document.createEvent('TextEvent');
               event.initTextEvent('textInput', true, true, window, '_', 0, '');
               inputField.dispatchEvent(event);
       }

       document.execCommand('selectAll', false, undefined);
       document.execCommand('insertText', false, text);
}

這是打字稿代碼,因此您可能需要將其更改為使用 js.

它的工作原理是使用 event.initTextEvent 在 textField 中插入一個(gè)占位符值,然后將該文本替換為:

It works by inserting a placeholder value inside the textField using event.initTextEvent, and then replacing that text with:

document.execCommand('selectAll', false, undefined);
document.execCommand('insertText', false, 'text');

在 Chrome 中測(cè)試:版本 71.0.3578.98

推薦答案

雖然這個(gè)問(wèn)題是很久以前提出的,@JoniVR 找到了解決方法,但這可能對(duì)其他人有所幫助.

Although the question was asked a long ago and @JoniVR found a workaround, this may help someone else.

我在開發(fā)擴(kuò)展程序時(shí)也遇到了類似的問(wèn)題.我還嘗試了方法 document.execCommand('insertText', false, text).它在 LinkedIn 上有效,但在 Facebook 上無(wú)效.它在錯(cuò)誤的節(jié)點(diǎn)中插入文本.雖然 document.execCommand API 在某些地方有效,但它是現(xiàn)在已經(jīng)過(guò)時(shí)了.

I was also having a similar problem while working on an extension. I also tried the method document.execCommand('insertText', false, text). It worked on LinkedIn but not on Facebook. It was inserting text in the wrong node. Although document.execCommand API works in some places, it's obsolete now.

對(duì)于 Facebook 和任何其他使用 drafjs 編輯器的網(wǎng)站,我們需要使用 dataTransfer 和 clipBoardEvent APIs讓draftjs認(rèn)為文本被粘貼并進(jìn)行相應(yīng)處理.

For Facebook and any other sites using drafjs editor, We need to dispatch a paste event using dataTransfer and clipBoardEvent APIs to make draftjs think that the text is pasted and process accordingly.

const dataTransfer = new DataTransfer();

function dispatchPaste(target, text) {
  // this may be 'text/html' if it's required
  dataTransfer.setData('text/plain', text);

  target.dispatchEvent(
    new ClipboardEvent('paste', {
      clipboardData: dataTransfer,

      // need these for the event to reach Draft paste handler
      bubbles: true,
      cancelable: true
    })
  );

  // clear DataTransfer Data
  dataTransfer.clearData();
}

如果需要更多信息,請(qǐng)檢查此 鏈接.

Check this link in case more info needed.

這篇關(guān)于使 document.execCommand('insertText', false, 'message') 與 Draftjs 一起使用?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

discord.js v12: How do I await for messages in a DM channel?(discord.js v12:我如何等待 DM 頻道中的消息?)
how to make my bot mention the person who gave that bot command(如何讓我的機(jī)器人提及發(fā)出該機(jī)器人命令的人)
How to fix Must use import to load ES Module discord.js(如何修復(fù)必須使用導(dǎo)入來(lái)加載 ES 模塊 discord.js)
How to list all members from a specific server?(如何列出來(lái)自特定服務(wù)器的所有成員?)
Discord bot: Fix ‘FFMPEG not found’(Discord bot:修復(fù)“找不到 FFMPEG)
Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服務(wù)器時(shí)的歡迎消息)
主站蜘蛛池模板: 色免费看 | 日韩精品久久一区 | 国产在线中文字幕 | 亚洲区一区二 | 久久久久久久久久久蜜桃 | 九九热在线观看 | 日韩精品在线一区二区 | 国产精品国产三级国产aⅴ中文 | 国产偷自视频区视频 | 羞羞视频在线网站观看 | 亚洲精品美女 | 亚洲国产成人精品女人久久久 | 国产乱码高清区二区三区在线 | 国产在线视频一区 | 国产精品久久福利 | 最新中文字幕第一页视频 | 毛片免费在线观看 | 国产激情精品一区二区三区 | 中文字幕免费视频 | 久久国产精品-国产精品 | 国产成人99久久亚洲综合精品 | 亚洲成人免费 | 国产熟熟 | 久久久做 | 国产精品久久久久久久午夜 | 精品日韩在线 | 国产精品久久久久久久免费观看 | 国产日韩欧美激情 | 精品视频一区二区三区四区 | 日韩图区 | 亚洲一区二区三区免费在线观看 | 亚洲欧美视频一区 | 成人久久视频 | 国产精品成人一区二区 | 国产成人在线播放 | 黄色一级片aaa | 色婷婷在线视频 | 国产一区二区精品 | 色五月激情五月 | 国产欧美一区二区三区在线看 | 男女av|