3

我们在 Angular Web 应用程序中使用 Code Mirror。当前,当用户突出显示一段文本并随后单击突出显示的部分时,Code Mirror 会将所选区域的副本放置到剪贴板中。这不是我们想要的行为。我们希望只是取消选择文本的正常行为。

我试图捕捉鼠标单击事件并返回 false 或将 codemirrorIgnore 设置为 true,但它不起作用。我还尝试重新定义“LeftDown”的键映射,但我找不到有关现有操作名称定义位置的信息。

任何人都可以帮忙吗?

这是我尝试更改 KeyMap 的代码:

$scope.editorOptions = {
    lineWrapping: true,
    lineNumbers: true,
    smartIndent: true,
    autoCloseTags: true,
    cursorScrollMargin: 25,
    styleActiveLine: true,
    mode: "text/html",
    theme: "default",
    matchBrackets: true,
    matchTags: {
        bothTags: true
    },
    extraKeys: {
        "F11": function (cm) {
            cm.setOption("fullScreen", !cm.getOption("fullScreen"));
        },
        "Esc": function (cm) {
            if (cm.getOption("fullScreen")) {
                cm.setOption("fullScreen", false);
            }
        },
        //Don't know where "autocomple", "findPersistent" are defined so I can see what is available
        "LeftClick": "LeftClick",
        "Ctrl-Space": "autocomplete",
        "Alt-B": "findPersistent",
        "Ctrl-J": "toMatchingTag"
    },
    viewportMargin: 10,
    textWrapping: true
};

我还尝试使用 onload 函数使用ui-codemirror="{ onLoad : codemirrorLoaded }"以下内容:

$scope.codemirrorLoaded = function(_editor) {
    _editor.on("mouseDown", function(cm, event) {
        cm.codemirrorIgnore = true;
        //also tried return false
    }
};

返回 false 什么也没做,设置codemirrorIgnore为 true 会阻止左键单击做任何事情。

4

1 回答 1

2

我发现这是 Code Mirror 的错误。这是代码镜像代码的第一位:

if (cm.options.dragDrop && dragAndDrop && !cm.isReadOnly() &&
    type == "single" && (contained = sel.contains(start)) > -1 &&
    (cmp((contained = sel.ranges[contained]).from(), start) < 0 || start.xRel > 0) &&
    (cmp(contained.to(), start) > 0 || start.xRel < 0))
  leftButtonStartDrag(cm, e, start, modifier);
else
  leftButtonSelect(cm, e, start, type, modifier);

据此,如果启用拖放(默认情况下),则将开始拖动。但在下一段代码中:

// Start a text drag. When it ends, see if any dragging actually
// happen, and treat as a click if it didn't.
function leftButtonStartDrag(cm, e, start, modifier) {

如果没有拖动,则将单击视为单击。我没有仔细查看代码是否可以修复它,但是由于拖动使用剪贴板,剪贴板已经被删除并替换为选定的文本。

因此,为了避免这种情况,您需要禁用拖放。

于 2017-12-13T15:56:15.397 回答