1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | import '@kano/styles/typography.js';
|
14 | import '@kano/styles/color.js';
|
15 | import { PolymerElement, html } from '@polymer/polymer/polymer-element.js';
|
16 | import './elements/kwc-code-display.js';
|
17 | import './highlight-theme/art.js';
|
18 |
|
19 | class KwcArtPlayer extends PolymerElement {
|
20 | static get template() {
|
21 | return html`
|
22 | <style>
|
23 | :host * {
|
24 | box-sizing: border-box;
|
25 | }
|
26 | .player {
|
27 | text-align: center;
|
28 | }
|
29 | img {
|
30 | display: inline-block;
|
31 | margin: 0 auto ;
|
32 | background-color: white;
|
33 | max-width: 100%;
|
34 | margin-bottom: 48px;
|
35 | box-shadow: 0 4px 4px 0px rgba(0, 0, 0, 0.15);
|
36 | }
|
37 | </style>
|
38 | <div class="player">
|
39 | <img id="image" src="[[share.cover_url]]" sizing="contain" preload fade>
|
40 | </div>
|
41 | <template is="dom-if" if="[[displayCode]]">
|
42 | <kwc-code-display code="[[_mdCode]]" lines="[[_lines]]" code-type="[[_computeCodeType(share.app)]]" on-hide-code="_hideCode">
|
43 | </kwc-code-display>
|
44 | </template>
|
45 | `;
|
46 | }
|
47 | static get properties() {
|
48 | return {
|
49 | |
50 |
|
51 |
|
52 | _code: {
|
53 | type: String,
|
54 | value: null,
|
55 | },
|
56 | |
57 |
|
58 |
|
59 | displayCode: {
|
60 | type: Boolean,
|
61 | value: false,
|
62 | notify: true,
|
63 | },
|
64 | |
65 |
|
66 |
|
67 | _lines: {
|
68 | type: Array,
|
69 | computed: '_computeLines(_mdCode)',
|
70 | },
|
71 | |
72 |
|
73 |
|
74 |
|
75 | _mdCode: {
|
76 | type: String,
|
77 | value: null,
|
78 | },
|
79 | |
80 |
|
81 |
|
82 | share: {
|
83 | type: Object,
|
84 | value: () => ({}),
|
85 | },
|
86 | };
|
87 | }
|
88 | static get observers() {
|
89 | return [
|
90 | '_shareChanged(share.*)',
|
91 | ];
|
92 | }
|
93 |
|
94 | |
95 |
|
96 |
|
97 |
|
98 | _computeLines(mdCode) {
|
99 | if (!mdCode) {
|
100 | return [];
|
101 | }
|
102 | const newLines = mdCode.match(/\n(?!$)/g);
|
103 |
|
104 |
|
105 | const lineCount = newLines ? newLines.length : 1;
|
106 | const lines = [];
|
107 |
|
108 | for (let i = 1; i < lineCount; i += 1) {
|
109 | lines.push(i);
|
110 | }
|
111 | return lines;
|
112 | }
|
113 | |
114 |
|
115 |
|
116 |
|
117 | _shareChanged() {
|
118 | if (!this.share) { return; }
|
119 | const attachment = this.share.attachment_url;
|
120 | if (attachment) {
|
121 | fetch(attachment)
|
122 | .then(r => r.text())
|
123 | .then((code) => {
|
124 | this._code = code;
|
125 | this._mdCode = `\`\`\`coffeescript\n${code}\n\`\`\``;
|
126 | });
|
127 | }
|
128 | }
|
129 | _computeCodeType(app) {
|
130 | if (app === 'make-light') {
|
131 | return 'Python';
|
132 | }
|
133 | return 'CoffeeScript';
|
134 | }
|
135 |
|
136 | |
137 |
|
138 |
|
139 |
|
140 | _hideCode() {
|
141 | this.set('displayCode', false);
|
142 | this.dispatchEvent(new CustomEvent('hide-code'));
|
143 | }
|
144 | }
|
145 |
|
146 | customElements.define('kwc-art-player', KwcArtPlayer);
|