javascript - 复制渲染表的功能随机停止工作,页面刷新修复了这个问题

标签 javascript reactjs typescript

我似乎无法弄清楚为什么这个功能停止工作。刷新页面可以解决问题。它所做的是按 ID 获取表格,剥离类并对样式进行一些优化。用户正在将生成的表格复制并粘贴到他们的电子邮件中。

您能找出它偶尔会失败的原因吗?当它发生时,我没有在控制台中看到任何错误,而且我似乎可以弄清楚为什么它在大多数情况下都能正常工作。

页面刷新解决了这个问题。

(此函数由 React 中的按钮调用 onPress。它已经工作了一年多,但似乎在上周偶尔停止工作,我找不到任何可能影响它的软件包更新)

function copyByElementId(element: HTMLElement | null) {
    if (!element) return false;

    // Clone the node so we don't interrupt the view
    let el = element.cloneNode(true) as HTMLTableElement;
    el.className = ""; // Remove classes from parent Div

    if (comparedScenarios.length > 1) {
      // Delete last row if the arrows are present
      el.deleteRow(el.children[0].children.length - 1);
    }

    // Add default fonts to the table
    el.style.fontSize = "inherit";
    el.style.fontFamily = "San Francisco, Helvetica, Arial, sans-serif";

    // This works for clients other than Outlook grrr...
    el.style.borderCollapse = "collapse";
    el.style.borderSpacing = "16px 4px";
    el.style.border = "0";

    let rows = el.children[0].children; // Rows in the table

    // Add padding to the left side of each cell other than the 1st column
    for (let i = 0; i < rows.length; i++) {
      let cells = rows[i].children;
      rows[i].className = ""; // Remove classes from rows
      for (let i = 0; i < cells.length; i++) {
        cells[i].className = ""; // Remove classes from cells
        // Only the 2nd column and after
        let cell = cells[i] as HTMLTableCellElement; // TD element
        cell.style.border = "0";
        if (i > 0) {
          cell.style.paddingTop = "0";
          cell.style.paddingRight = "0";
          cell.style.paddingBottom = "0";
          cell.style.paddingLeft = "20px";
        }
      }
    }

    // Create our hidden div element
    let hiddenDiv = document.createElement("div");
    hiddenDiv.style.position = "absolute";
    hiddenDiv.style.left = "-9999px";
    hiddenDiv.style.backgroundColor = "";

    // Render the table of the hidden div
    hiddenDiv.appendChild(el);

    // Check and see if the user had a text selection range
    let currentRange;
    let documentSelection = document.getSelection();
    if (documentSelection?.rangeCount && documentSelection?.rangeCount > 0) {
      // The user has a text selection range, store it
      currentRange = document.getSelection()?.getRangeAt(0);
      // Remove the current selection
      window.getSelection()?.removeAllRanges();
    }

    // Append the div to the body
    document.body.appendChild(hiddenDiv);
    // Create a selection range
    let CopyRange = document.createRange();
    // Set the copy range to be the hidden div
    CopyRange.selectNode(hiddenDiv);
    // Add the copy range
    window.getSelection()?.addRange(CopyRange);

    //since not all browsers support this, use a try block
    try {
      //copy the text
      document.execCommand("copy");
    } catch (err) {
      window.alert("Your browser doesn't support copy and paste! Error:" + err);
    }
    //remove the selection range (Chrome throws a warning if we don't.)
    window.getSelection()?.removeAllRanges();
    //remove the hidden div
    document.body.removeChild(hiddenDiv);

    //return the old selection range
    if (currentRange) {
      window.getSelection()?.addRange(currentRange);
    }
    runButtonStates();
  }

最佳答案

该问题是由于另一个 React 组件在首次安装时阻塞了视口(viewport)(模态覆盖)。起初这似乎并不直观,这可能会导致浏览器中的 native 复制功能出现问题。

升级组件并重新设计叠加层后,问题自行解决。

关于javascript - 复制渲染表的功能随机停止工作,页面刷新修复了这个问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64306396/

相关文章:

javascript - ReactJS 单元测试 - TypeError : this. props.onChange 不是函数

angular - 在 canActivate Guard 中访问 ActivatedRoute 的组件

javascript - 更改可观察值

javascript - 是否可以使用 Detox 或 Jest 获取匹配元素的数量?

php - 确定广告是否显示以及是否被点击

javascript - 这个导出是什么意思呢?这是一个执行两个函数的对象吗?

javascript - isRequired 对于嵌套 PropType 是必需的吗?

javascript - 扩展 typescriptdefineTyped 定义文件

javascript - 不重复前一个数字的数学随机数

javascript - 如何使用JS获取href值?