【问题标题】:Froala Editor / Drag and DropFroala 编辑器/拖放
【发布时间】:2017-07-10 16:00:32
【问题描述】:

我正在尝试使用 Froala 编辑器实现一些自定义放置事件功能。他们在这里有一个关于 codepen 的非常基本的示例:Drag Drop Example

问题在于这不起作用 - 我无法触发自定义的“froalaEditor.drop”功能,我确信我需要在 HTML5 放置事件的某处添加某种“防止默认”,但我所做的一切尝试过杀死所有丢弃功能或什么都不做。自定义函数如下所示:

$('div#froala-editor').froalaEditor({
  toolbarButtons: ['bold', 'italic', 'underline', 'insertImage', 'insertLink', 'emoticons', 'undo', 'redo'],
  pluginsEnabled: ['image', 'link', 'draggable', 'emoticons']
})
.on ('froalaEditor.drop', function (e, editor, dropEvent) {
//// do stuff - nothing is firing

我正在通过标准使其可拖动:

draggable="true"

对此的任何帮助或指示将不胜感激!

谢谢,

标记

更新:::

这是我的 JS:

$(function() {
// For Firefox to work.
$("#drag-smile, #drag-text").on("dragstart", function(e) {
    e.originalEvent.dataTransfer.setData("Text", this.id);
});

$("div#froala-editor")
    .froalaEditor({
        toolbarButtons: [
            "bold",
            "italic",
            "underline",
            "insertImage",
            "insertLink",
            "emoticons",
            "undo",
            "redo"
        ],
        pluginsEnabled: ["image", "link", "draggable", "emoticons"]
    })
    .on("froalaEditor.drop", function(e, editor, dropEvent) {
        // Focus at the current posisiton.
        editor.markers.insertAtPoint(dropEvent.originalEvent);
        var $marker = editor.$el.find(".fr-marker");
        $marker.replaceWith($.FroalaEditor.MARKERS);
        editor.selection.restore();

        // Save into undo stack the current position.
        if (!editor.undo.canDo()) editor.undo.saveStep();

        // Insert HTML.
        if (dropEvent.originalEvent.dataTransfer.getData("Text") == "drag-smile") {
            editor.html.insert(
                '<span class="fr-emoticon fr-emoticon-img" style="background: url(https://cdnjs.cloudflare.com/ajax/libs/emojione/2.0.1/assets/svg/1f600.svg)">&nbsp;</span>'
            );
        } else {
            editor.html.insert("Hello!");
        }

        // Save into undo stack the changes.
        editor.undo.saveStep();

        // Stop event propagation.
        dropEvent.preventDefault();
        dropEvent.stopPropagation();
        return false;
    });
   });

还有我的 HTML:

   <div id="drag-smile" style="border: solid 1px #CCC; padding: 5px; width: 300px;" draggable="true"><img src="https://cdnjs.cloudflare.com/ajax/libs/emojione/2.0.1/assets/svg/1f600.svg" width="32" /> Drag Me to insert a smile.</div><br/>
<div id="drag-text" style="border: solid 1px #CCC; padding: 5px; width: 300px;" draggable="true">Drag Me to insert some text.</div><br/>
<div id="froala-editor">
<h3>Click here to edit the content</h3>
<p>The image can be dragged only between blocks and not inside them.
</p>
</div>

基本上,当您将其中一个可拖动的 div 拖入编辑器时,应将自定义 html 注入编辑器,但它只是将 div id 添加为文本。

【问题讨论】:

  • 你的 html 结构中有&lt;div id="froala-editor"&gt; 标签吗?
  • 嗨,是的,编辑器在具有该 ID 的 div 上正确初始化,只是 drop 事件似乎没有正确连接。
  • 如果你不介意分享你的整个 html 和 js 代码,这将有助于发现问题,因为我认为问题不在于当前共享的 sn-p
  • 您使用的是什么浏览器?我注意到来自 codepen 的示例在 Firefox 中运行良好,并且在 chrome 中存在一些问题
  • 你说得对,我刚刚在 Firefox 中进行了测试,它按预期工作,我只在 chrome 和 safari 中进行了测试......关于为什么会发生这种情况的任何想法?

标签: javascript jquery html froala


【解决方案1】:

这是因为 drop 事件在到达您的回调之前已停止。像这样更改它以确保始终首先调用您的事件:

$("div#froala-editor")
.on("froalaEditor.initialized", function(e, editor, dropEvent) {

    editor.events.on('drop', function (dropEvent) {
        // Focus at the current posisiton.
        editor.markers.insertAtPoint(dropEvent.originalEvent);
        var $marker = editor.$el.find(".fr-marker");
        $marker.replaceWith($.FroalaEditor.MARKERS);
        editor.selection.restore();

        // Save into undo stack the current position.
        if (!editor.undo.canDo()) editor.undo.saveStep();

        // Insert HTML.
        if (dropEvent.originalEvent.dataTransfer.getData("Text") == "drag-smile") {
            editor.html.insert(
                '<span class="fr-emoticon fr-emoticon-img" style="background: url(https://cdnjs.cloudflare.com/ajax/libs/emojione/2.0.1/assets/svg/1f600.svg)">&nbsp;</span>'
            );
        } else {
            editor.html.insert("Hello!");
        }

        // Save into undo stack the changes.
        editor.undo.saveStep();

        // Stop event propagation.
        dropEvent.preventDefault();
        dropEvent.stopPropagation();
        return false;
    }, true);
});
.froalaEditor({
    toolbarButtons: [
        "bold",
        "italic",
        "underline",
        "insertImage",
        "insertLink",
        "emoticons",
        "undo",
        "redo"
    ],
    pluginsEnabled: ["image", "link", "draggable", "emoticons"]
})

});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-21
    • 2019-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多