-
Notifications
You must be signed in to change notification settings - Fork 29
/
demo-snippet.js
215 lines (180 loc) · 5.92 KB
/
demo-snippet.js
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at
http://polymer.github.io/LICENSE.txt The complete set of authors may be found at
http://polymer.github.io/AUTHORS.txt The complete set of contributors may be
found at http://polymer.github.io/CONTRIBUTORS.txt Code distributed by Google as
part of the polymer project is also subject to an additional IP rights grant
found at http://polymer.github.io/PATENTS.txt
*/
import '@polymer/polymer/polymer-legacy.js';
import '@polymer/marked-element/marked-element.js';
import '@polymer/prism-element/prism-highlighter.js';
import '@polymer/prism-element/prism-theme-default.js';
import {Polymer} from '@polymer/polymer/lib/legacy/polymer-fn.js';
import {dom} from '@polymer/polymer/lib/legacy/polymer.dom.js';
import {html} from '@polymer/polymer/lib/utils/html-tag.js';
/**
`demo-snippet` is a helper element that displays the source of a code snippet
and its rendered demo. It can be used for both native elements and Polymer
elements.
Example of a native element demo
<demo-snippet>
<template>
<input type="date">
</template>
</demo-snippet>
Example of a Polymer <paper-checkbox> demo
<demo-snippet>
<template>
<paper-checkbox>Checkbox</paper-checkbox>
<paper-checkbox checked>Checkbox</paper-checkbox>
</template>
</demo-snippet>
### Styling
The following custom properties and mixins are available for styling:
Custom property | Description | Default
----------------|-------------|----------
`--demo-snippet` | Mixin applied to the entire element | `{}`
`--demo-snippet-demo` | Mixin applied to just the demo section | `{}`
`--demo-snippet-code` | Mixin applied to just the code section | `{}`
@element demo-snippet
@demo demo/index.html
*/
Polymer({
_template: html`
<style include="prism-theme-default">
:host {
display: block;
box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14),
0 1px 5px 0 rgba(0, 0, 0, 0.12),
0 3px 1px -2px rgba(0, 0, 0, 0.2);
margin-bottom: 40px;
@apply --demo-snippet;
}
.demo {
display: block;
border-bottom: 1px solid #e0e0e0;
background-color: white;
margin: 0;
padding: 20px;
@apply --demo-snippet-demo;
}
.code-container {
margin: 0;
background-color: #f5f5f5;
font-size: 13px;
overflow: auto;
position: relative;
padding: 0 20px;
@apply --demo-snippet-code;
}
.code {
padding: 20px;
margin: 0;
background-color: var(--google-grey-100);
font-size: 13px;
overflow: auto;
@apply --demo-snippet-code;
}
.code > pre {
margin: 0;
padding: 0 0 10px 0;
}
button {
position: absolute;
top: 0;
right: 0px;
text-transform: uppercase;
border: none;
cursor: pointer;
background: #e0e0e0;
}
</style>
<prism-highlighter></prism-highlighter>
<div class="demo">
<slot id="content"></slot>
</div>
<div class="code-container">
<marked-element markdown="[[_markdown]]" id="marked">
<div class="code" slot="markdown-html" id="code"></div>
</marked-element>
<button id="copyButton" title="copy to clipboard" on-tap="_copyToClipboard">Copy</button>
</div>
`,
is: 'demo-snippet',
properties: {
/**
* Fired when the demo-snippet is ready, i.e. when it has injected the code to demo
* in the DOM and it can be interacted with
*
* @event dom-ready
*/
/** @private */
_markdown: {
type: String,
},
},
attached: function() {
this._observer = dom(this.$.content).observeNodes(function(info) {
this._updateMarkdown();
}.bind(this));
},
detached: function() {
if (this._observer) {
dom(this.$.content).unobserveNodes(this._observer);
this._observer = null;
}
},
_updateMarkdown: function() {
var template = dom(this).queryDistributedElements('template')[0];
// If there's no template, render empty code.
if (!template) {
this._markdown = '';
return;
}
var snippet = this.$.marked.unindent(template.innerHTML);
// Hack: In safari + shady dom, sometime we get an empty 'class' attribute.
// if we do, delete it.
snippet = snippet.replace(/ class=""/g, '');
// Boolean properties are displayed as checked="", so remove the ="" bit.
snippet = snippet.replace(/=""/g, '');
this._markdown = '```\n' + snippet + '\n' +
'```';
// Stamp the template.
if (!template.hasAttribute('is')) {
// Don't need to listen for more changes (since stamping the template
// will trigger an observeNodes)
dom(this.$.content).unobserveNodes(this._observer);
this._observer = null;
dom(this).appendChild(document.importNode(template.content, true));
}
this.dispatchEvent(new CustomEvent('dom-ready'));
},
_copyToClipboard: function() {
// From
// https://github.com/google/material-design-lite/blob/master/docs/_assets/snippets.js
var snipRange = document.createRange();
snipRange.selectNodeContents(this.$.code);
var selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(snipRange);
var result = false;
try {
result = document.execCommand('copy');
this.$.copyButton.textContent = 'done';
} catch (err) {
// Copy command is not available
console.error(err);
this.$.copyButton.textContent = 'error';
}
// Return to the copy button after a second.
setTimeout(this._resetCopyButtonState.bind(this), 1000);
selection.removeAllRanges();
return result;
},
_resetCopyButtonState: function() {
this.$.copyButton.textContent = 'copy';
}
});