Engee 文档

复制到剪贴板按钮

通常,您需要创建一个按钮,将某些内容复制到剪贴板中。

行为

  • 拥有多个卡片矢量

  • 每个卡片都有一个小复制按钮

  • 如果用户点击该按钮,只有相关卡片的内容会被复制到剪贴板中

示例由 Jan Siml贡献。

@app begin
    @in my_texts = [Dict(:id => id, :content => content) for (id, content) in zip(1:3, ["abc", "def", "ghi"])]
    @in copied_index = 0
end

function ui()
  [
    h3("Some Text"),
    card(class = "q-my-md", @for("(item, index) in my_texts"), key = :index, [
      cardsection([
        btn(icon = "content_paste", "", :flat, :dense, class = "absolute-right", @click("""
          this.copyToClipboard(this.my_texts[index].content);
          copied_index = index
          this.\$q.notify('Copied ' + index + '!')
        """)),
        "{{item.content}}",
      ]),
    ]),
    htmldiv("Copied Index: {{ copied_index }}")
  ]
end

@methods begin
    """
    copyToClipboard: function(str) {
        const el = document.createElement('textarea');  // Create a <textarea> element
        el.value = str;                                 // Set its value to the string that you want copied
        el.setAttribute('readonly', '');                // Make it readonly to be tamper-proof
        el.style.position = 'absolute';
        el.style.left = '-9999px';                      // Move outside the screen to make it invisible
        document.body.appendChild(el);                  // Append the <textarea> element to the HTML document
        const selected = this.my_text
        el.select();                                    // Select the <textarea> content
        document.execCommand('copy');                   // Copy - only works as a result of a user action (e.g. click events)
        document.body.removeChild(el);                  // Remove the <textarea> element
    }
"""
end