// Excel 风格表格剪贴板互操作
window.excelGrid = {
    init: function (element, dotNetHelper) {
        if (!element) return;

        element.addEventListener('copy', function (e) {
            var active = document.activeElement;
            var isInputFocused = active && (active.tagName === 'INPUT' || active.tagName === 'TEXTAREA') && element.contains(active);
            if (isInputFocused && active.selectionStart !== active.selectionEnd) return;

            dotNetHelper.invokeMethodAsync('OnCopyData').then(function (text) {
                if (text) {
                    e.clipboardData.setData('text/plain', text);
                    e.preventDefault();
                }
            });
        });

        element.addEventListener('paste', function (e) {
            var text = (e.clipboardData || window.clipboardData).getData('text');
            if (text && (text.indexOf('\t') >= 0 || text.indexOf('\n') >= 0)) {
                dotNetHelper.invokeMethodAsync('OnPasteData', text);
                e.preventDefault();
            }
        });
    }
};
