Repository navigation
Expand file tree
/
Copy pathtoken.html
More file actions
159 lines (143 loc) · 4.21 KB
/
Copy pathtoken.html
File metadata and controls
159 lines (143 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>draft-extend</title>
<link rel="stylesheet" href="../node_modules/draft-js/dist/Draft.css" />
<link rel="stylesheet" href="../dist/draft-extend.css" />
<style>
#target {
font-family: sans-serif;
margin: 20px;
border: solid 1px #ccc;
}
</style>
</head>
<body>
<div id="target"></div>
<script src="../node_modules/react/dist/react.min.js"></script>
<script src="../node_modules/react-dom/dist/react-dom.min.js"></script>
<script src="../node_modules/immutable/dist/immutable.min.js"></script>
<script src="../node_modules/es6-shim/es6-shim.js"></script>
<script src="../node_modules/babel-standalone/babel.min.js"></script>
<script src="../node_modules/draft-js/dist/Draft.js"></script>
<script src="../dist/draft-extend.js"></script>
<script src="../node_modules/draft-convert/dist/draft-convert.js"></script>
<script src="./ToolbarButton.js"></script>
<script type="text/babel">
const {
EditorState,
Modifier,
Entity
} = Draft;
const {
Editor,
createPlugin,
pluginUtils: {entityStrategy}
} = window.DraftExtend;
const {
convertToHTML,
convertFromHTML
} = window.DraftConvert;
const ENTITY_TYPE = 'token';
const TokenDecorator = {
strategy: entityStrategy(ENTITY_TYPE),
component: (props) => {
return (
<span
style={{
padding: '2px',
borderRadius: '2px',
background: '#de6262',
color: 'white'
}}
>
{props.children}
</span>
);
}
};
const entityToHTML = (entity, originalText) => {
if (entity.type === ENTITY_TYPE) {
return `{{ ${entity.data.value} }}`;
}
return originalText;
};
const textToEntity = (text) => {
const results = [];
const pattern = /\{\{\s?(\w+)\s?\}\}/gi;
text.replace(pattern, (match, value, offset) => {
results.push({
offset,
length: match.length,
entity: Entity.create(ENTITY_TYPE, 'IMMUTABLE', {value}),
result: value
});
});
return results;
};
const TokenButton = ({editorState, onChange}) => {
const insertToken = () => {
const contentState = Modifier.insertText(
editorState.getCurrentContent(),
editorState.getSelection(),
'Token Label',
null,
Entity.create(ENTITY_TYPE, 'IMMUTABLE', {
value: 'token'
})
);
onChange(
EditorState.push(
editorState,
contentState,
'insert-characters'
)
);
};
return (
<ToolbarButton
label="Insert token"
onClick={insertToken}
/>
);
};
const TokenPlugin = createPlugin({
decorators: TokenDecorator,
buttons: TokenButton,
textToEntity,
entityToHTML
});
const WithPlugin = TokenPlugin(Editor);
const toHTML = TokenPlugin(convertToHTML);
const fromHTML = TokenPlugin(convertFromHTML);
const TokenExample = React.createClass({
getInitialState() {
return {
editorState: EditorState.createWithContent(
fromHTML('<div>Token example {{ token }}</div>')
)
};
},
onChange(editorState) {
console.log(toHTML(editorState.getCurrentContent()));
this.setState({editorState});
},
render() {
return (
<WithPlugin
editorState={this.state.editorState}
onChange={this.onChange}
/>
);
}
});
ReactDOM.render(
<TokenExample />,
document.getElementById('target')
);
</script>
<script>
eval(Babel.transform(document.querySelector('script[type="text/babel"]').innerText, { presets: ['es2015', 'react']}).code)
</script>
</body>