This repository has been archived by the owner on Feb 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
structure-display.html
119 lines (110 loc) · 3.51 KB
/
structure-display.html
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
<!--
@license
Copyright 2016 The Advanced REST client authors
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy of
the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under
the License.
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../markdown-styles/markdown-styles.html">
<link rel="import" href="../prism-element/prism-highlighter.html">
<!--
A `<pre>` element that displays highlighted text.
## Usage
```
<pre is="[[structure-display]]" display="[[json]]"></pre>
```
## Theming
Custom property | Description | Default
----------------|-------------|----------
`--code-block` | Mixin applied to the code block. | `{}`
`--code-background-color` | Background color of the code block | `#f5f2f0`
@element structure-display
@demo demo/docs-body-parameters-table.html
-->
<dom-module id="structure-display">
<template strip-whitespace>
<style include="markdown-styles"></style>
<style>
:host {
background-color: var(--code-background-color, #f5f2f0) !important;
}
#output {
@apply --code-block;
}
</style>
<prism-highlighter></prism-highlighter>
<div id="output" class="markdown-html"></div>
</template>
<script>
Polymer({
is: 'structure-display',
extends: 'pre',
properties: {
// A content to parse, highlight and display.
display: String,
// Data type to pass to the highlight function. js, xml etc.
dataType: {
type: String,
value: 'js'
},
// If set automatically hides the element if the `display` is empty
autoHide: {
type: Boolean,
value: false
},
// Set to true when the element has been initialized.
_elementReady: Boolean
},
observers: [
'_highlightSchema(display, dataType)',
'_autoHideChanged(autoHide, display)'
],
ready: function() {
this.classList.add('markdown-html');
this._elementReady = true;
// This callbacks are called before the element ready event is fired.
// So they must be called again.
this._highlightSchema(this.display, this.dataType);
},
_autoHideChanged: function(state, display) {
if (state) {
if (!display) {
if (!this.hasAttribute('hidden')) {
this.setAttribute('hidden', true);
}
} else {
if (this.hasAttribute('hidden')) {
this.removeAttribute('hidden');
}
}
} else {
if (this.hasAttribute('hidden')) {
this.removeAttribute('hidden');
}
}
},
_highlightSchema: function(str) {
if (!this._elementReady) {
return;
}
if (!str) {
this.$.output.innerHTML = '';
return;
}
this.debounce('structure-display-highlight', function() {
this.$.output.innerHTML = this.highlight(str, this.dataType);
}, 10);
},
highlight: function(code, lang) {
return this.fire('syntax-highlight', {code: code, lang: lang}).detail.code;
},
});
</script>
</dom-module>