【问题标题】:How to insert at caret position of contenteditable using TypeScript如何使用 TypeScript 在 contenteditable 的插入符号位置插入
【发布时间】:2016-01-24 22:35:07
【问题描述】:

我曾经有这样的代码:

this.insertNodeAtCaret = function(node) {

            var sel, range, html;

            function containerIsEditable(selection) {
                return $(selection.anchorNode).parent().hasClass("editable");
            }

            if (window.getSelection) {
                sel = window.getSelection();
                // only if it is a caret otherwise it inserts
                // anywhere!
                if (containerIsEditable(sel) && sel.getRangeAt
                        && sel.rangeCount) {
                    var previousPosition = sel.getRangeAt(0).startOffset;
                    sel.getRangeAt(0).insertNode(node);
                }
            } 
            else if (document.selection
                    && document.selection.createRange) {
                range = document.selection.createRange();
                html = (node.nodeType == 3) ? node.data
                        : node.outerHTML;
                range.pasteHTML(html);  

            }

        };

但在 TypeScript 1.5 中,Selection 已从 Document (https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes) 中删除,所以我不知道如何让它工作。我尝试使用 window.getSelection() 但没有结果

任何帮助将不胜感激:)

谢谢, 迈克尔

【问题讨论】:

    标签: typescript contenteditable caret


    【解决方案1】:

    但在 TypeScript 1.5 中,选择已从文档中删除

    这是特定于 Internet Explorer 的并非对所有浏览器都通用。所以它被删除了。但是,您可以很容易地使用它不安全(将document 视为any)。这是您的代码示例,经过重构后可以正确编译:

    const insertNodeAtCaret = function (node) {
        const doc = document as any;
    
        var sel, range, html;
    
        function containerIsEditable(selection) {
            return $(selection.anchorNode).parent().hasClass("editable");
        }
    
        if (window.getSelection) {
            sel = window.getSelection();
            // only if it is a caret otherwise it inserts
            // anywhere!
            if (containerIsEditable(sel) && sel.getRangeAt
                && sel.rangeCount) {
                var previousPosition = sel.getRangeAt(0).startOffset;
                sel.getRangeAt(0).insertNode(node);
            }
        }
        else if (doc.selection
            && doc.selection.createRange) {
            range = doc.selection.createRange();
            html = (node.nodeType == 3) ? node.data
                : node.outerHTML;
            range.pasteHTML(html);
        }
    };
    

    当然,这假设您知道自己在做什么,比编译器知道的要多。

    更新

    如何查看浏览器之间的兼容性以及可用的内容

    window.getSelection 的兼容性图可以看这里:https://developer.mozilla.org/en-US/docs/Web/API/Window/getSelection

    document.selection 仅适用于 IE,已被删除:https://msdn.microsoft.com/en-us/library/ms535869(v=vs.85).aspx

    【讨论】:

    • 谢谢。其他方式可以使其与所有浏览器兼容?
    • 需要运行时支持。意味着你需要一个运行时函数。选项为polyfillshim。你所拥有的是一个垫片,它已经足够好了。
    • 如何查看浏览器之间的兼容性以及可用的内容?谢谢
    猜你喜欢
    • 2012-05-11
    • 1970-01-01
    • 2016-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    • 1970-01-01
    • 2021-08-28
    相关资源
    最近更新 更多