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

在 contentEditable 元素上拖放

Drag-n-Drop on contentEditable elements(在 contentEditable 元素上拖放)
本文介紹了在 contentEditable 元素上拖放的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

互聯網上有許多所見即所得的編輯器,但我還沒有找到一個實現某種形式的拖放實現.

There are numerous WYSIWYG editors available on the internet, but I'm yet to find one that implements some form of drag-n-drop implementation.

創建自己的編輯器很容易,但我希望用戶能夠從可編輯區域外拖動元素(即標記),并讓他們將其放置在可編輯區域內他們選擇的位置.

It is easy to create one's own editor, but I want to the user to be able to drag elements (ie. tokens) from outside the editable area and have them drop it at a location of their choice inside the editable area.

在可編輯元素的特定位置注入 html 很容易,但是當用戶在可編輯區域中的某個元素上拖動 DIV 時,如何確定插入符號的位置.為了更好地說明我要解釋的內容,請參見以下場景.

It is easy to inject html at a specific location of an editable element, but how do one determine where the caret should be when the user is dragging a DIV over some element in the editable area. To better illustrate what I'm trying to explain, see the following scenario.

可編輯區域(處于編輯模式的 IFRAME 或 contentEditable 屬性設置為 true 的 DIV)已包含以下文本:

The editable area (either an IFRAME in edit mode or a DIV with its contentEditable attribute set to true) already contains the following text:

親愛的,請注意……"

用戶現在從元素列表中將一個表示某個標記的元素拖到可編輯區域上,將光標移動到文本上,直到插入符號出現在文本中的逗號 (,) 之前,如上所示.當用戶在該位置釋放鼠標按鈕時,將注入 HTML,這可能會導致如下結果:

The user now drags an element representing some token from a list of elements, over the editable area, moving the cursor over the text until the caret appear just before the comma (,) in the text as shown above. When the user releases the mouse button at that location, HTML will be injected which could result in something like this:

親愛的 {UserFirstName},請注意……".

"Dear {UserFirstName}, please take note of ...".

我不知道是否有人做過類似的事情,或者至少知道如何使用 JavaScript 來做這件事.

I do not know if anyone has ever done anything similar to this, or at least know of how one would go about doing this using JavaScript.

任何幫助將不勝感激.

推薦答案

這是我解決可編輯元素上的自定義拖動元素問題的方法.最大的問題是當鼠標懸停在可編輯元素上時,無法確定鼠標光標的文本偏移量.我曾嘗試假裝單擊鼠標以將插入符號設置在所需位置,但這不起作用.即使這樣做了,在拖動時也不會直觀地看到插入符號的位置,而只會看到結果拖放.

Here is my approach to solving the issue of custom drag elements on editable elements. The big issue is that one cannot determine the text offset of the mouse cursor when hovering over the editable element. I have tried faking a mouse click to set the caret at the desired position but that did not work. Even if it did, one would not visually see the placement of the caret while dragging, but only the resulting drop.

由于可以將鼠標懸停事件綁定到元素而不是文本節點,因此可以將可編輯元素設置為暫時不可編輯.查找所有元素并將每個文本節點包裝在一個跨度中,以免破壞文本流.每個 span 都應該有一個類名,以便我們可以再次找到它們.

Since one can bind mouse-over events to elements and not text-nodes, one can set the editable element to be temporarily un-editable. Find all elements and wrap each text-node in a span as to not breaking the flow of the text. Each span should be given a classname so we can find them again.

包裝之后,應該再次找到所有包裝的文本節點,并將每個字符與另一個帶有類名的跨度包裝在一起,以便再次找到它們.

After the wrapping, one should again find all the wrapped text-nodes and wrap each character with another span with a classname that one can find them again.

使用事件委托可以向主可編輯元素添加一個事件,該事件將為每個字符范圍應用一種樣式,以顯示插入符號,一個閃爍的 GIF 圖像作為背景.

Using event delegation one can add an event to the main editable element that will apply a style to each character span that will display the caret, a blinking GIF image as a background.

同樣,使用事件委托,應該為每個字符上的鼠標向上事件(放下事件)添加一個事件.現在可以使用字符跨度在其父級(包裝的文本節點)中的位置(偏移量)來確定偏移量.現在可以撤消所有包裝,保留對計算偏移量的引用,并在撤消包裝時保留對適用文本節點的引用.

Again, using event delegation, one should add an event for the mouse-up event (drop event) on each character. One can now determine the offset using the character span's position (offset) within its parent (wrapped text-node). One can now undo all the wrapping, keeping a reference to the calculated offset and while undoing the wrapping keeping a reference to the applicable text-node.

使用范圍 &瀏覽器的選擇對象,現在可以使用計算的偏移量設置選擇到適用的文本節點,并在新設置的選擇(插入符號位置)處注入所需的 HTML,等等!

Using the range & selection objects of the browser, one can now set the selection using the calculated offset to the applicable text-node and inject the required HTML at the newly set selection (caret position), et viola!

下面是一個使用 jQuery 的片段,它將找到文本節點,將它們包裝起來:

Here follows a snippet using jQuery that will find textnodes, wrap them:

editElement.find("*:not(.text-node)").contents().filter(function(){ 
    return this.nodeType != 1;
}).wrap("<span class="text-node"/>");

要查找每個文本節點并包裝每個字符,請使用:

To find each text-node and wrap each character, use:

editElement.find(".text-node").each(function()
{
    var textnode = $(this), text = textnode.text(), result = [];

    for (var i = 0; i < text.length; i++) result.push(text.substr(i, 1));

    textnode.html("<span class="char">" 
        + result.join("</span><span class="char">") + "</span>");
});

要撤消包裝:

editElement.find(".text-node").each(function()
{
    this.parentNode.replaceChild(document.createTextNode($(this).text()), this);
});

希望這種方法可以幫助那些面臨類似挑戰的人

Hope this approach helps those having similar challenges

這篇關于在 contentEditable 元素上拖放的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

How can I get my jasmine tests fixtures to load before the javascript considers the document to be quot;readyquot;?(在 javascript 認為文檔“準備好之前,如何讓我的 jasmine 測試裝置加載?) - IT屋-程序員軟件開發技術
What do jasmine runs and waitsFor actually do?(jasmine 運行和等待實際上是做什么的?)
How to provide mock files to change event of lt;input type=#39;file#39;gt; for unit testing(如何提供模擬文件來更改 lt;input type=filegt; 的事件用于單元測試)
How to unit test a chained method using Jasmine(如何使用 Jasmine 對鏈式方法進行單元測試)
How do I inject $rootScope into an AngularJS unit test?(如何將 $rootScope 注入 AngularJS 單元測試?)
Jasmine - How to spy on a function call within a function?(Jasmine - 如何監視函數中的函數調用?)
主站蜘蛛池模板: 久久久青草婷婷精品综合日韩 | 青青久草| 成年人黄色一级片 | av在线免费观看网址 | 欧美日韩不卡在线 | 成人午夜免费视频 | 欧美日韩一 | 亚洲视频三 | 一级黄a视频 | 成人动慢| 一级少妇女片 | 欧美一区二区三区四区五区无卡码 | 欧美精品免费观看二区 | 国产精品一区二区久久 | 亚州影院| 在线免费观看a级片 | 蜜桃传媒一区二区 | 午夜影晥| 中文字幕在线免费观看 | www.久草.com | 国产精品激情 | 中文字幕 国产 | 成人国产精品色哟哟 | 福利在线看 | 中国av在线免费观看 | 伊人精品在线 | 久久午夜视频 | 国产成人精品一区二区三区四区 | 欧美日高清 | 999观看免费高清www | 四虎影院免费在线 | 国产日韩欧美一区二区在线播放 | 欧美高清视频一区 | 国产精品一区一区三区 | 欧美在线视频网 | 999久久久久久久久6666 | 91精品麻豆日日躁夜夜躁 | 天天曰天天干 | 天天插天天操 | 亚洲精品久久久一区二区三区 | 国产精品视频一二三区 |