【发布时间】:2018-04-11 15:05:19
【问题描述】:
下面是我的应用程序的简化版本,其中在页面上呈现了一个模板,并在两个文本输入的帮助下填充。
这是一个“模板生成器”类型的应用程序,我希望能够复制页面上呈现的完整模板,以便将其粘贴到其他地方。要做到这一点,您通常会单击 + 拖动以突出显示呈现的 html,然后右键单击 + 复制或 ctrl + c。
如何将我的 HTML 转换为呈现的版本,以便我可以在 react-clipboard 按钮的帮助下复制到剪贴板?还是有另一种方法可以实现这一目标?
如果有任何不清楚的地方,请告诉我 - 提前致谢
TL;DR:目前,当您单击“复制到剪贴板”时,应用正在复制 html。我希望它复制内容,因为它会在网页上呈现。例如 hello world 转换和复制应该复制一个红色的 h1,上面写着 hello world。
import React, { Component } from 'react';
import { TextField } from 'material-ui';
import Clipboard from 'react-clipboard.js';
import './App.css';
const logo = 'https://www.google.co.uk/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png';
class App extends Component {
constructor() {
super();
this.state = {
name: 'Default Name',
title: 'Default Title'
};
}
componentDidMount() {
this.setState({template: this.createTemplate(this.state.name, this.state.title) });
}
createTemplate(name, title) {
const template = `<!DOCTYPE html>
<html>
<head>
<link href="https://fonts.googleapis.com/css?family=Montserrat:400,700" rel="stylesheet">
</head>
<body>
<h1>${name}</h1>
<h2>${title}</h2>
</body>
</html>`
this.setState({template});
}
updateValue(event, type) {
if(type === 'name') {
this.setState({name: event.target.value});
this.createTemplate(event.target.value, this.state.title);
} else {
this.setState({title: event.target.value});
this.createTemplate(this.state.name, event.target.value);
}
}
render() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">Signature Builder</h1>
</header>
<div>
<br/>
<TextField hintText="Name" onChange={(e) => this.updateValue(e, 'name')} />
<TextField hintText="Title" onChange={(e) => this.updateValue(e, 'title')} />
<div style={{textAlign: 'left', margin: 10}} dangerouslySetInnerHTML={{ __html: this.state.template }} />
<Clipboard data-clipboard-text={this.state.template}>
copy to clipboard
</Clipboard>
</div>
</div>
);
}
}
export default App;
【问题讨论】:
标签: javascript html reactjs clipboard