UNPKG

4.14 kBJavaScriptView Raw
1/**
2`kwc-art-player`
3Player UI component for make art shares.
4
5@demo demo/index-art.html
6*/
7/*
8FIXME(polymer-modulizer): the above comments were extracted
9from HTML and may be out of place here. Review them and
10then delete this comment!
11*/
12
13import '@kano/styles/typography.js';
14import '@kano/styles/color.js';
15import { PolymerElement, html } from '@polymer/polymer/polymer-element.js';
16import './elements/kwc-code-display.js';
17import './highlight-theme/art.js';
18
19class 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 * The code that creates this share.
51 */
52 _code: {
53 type: String,
54 value: null,
55 },
56 /**
57 * Flag to indicate whether the code display element is visible.
58 */
59 displayCode: {
60 type: Boolean,
61 value: false,
62 notify: true,
63 },
64 /**
65 * An array of line numbers for rendering the code display.
66 */
67 _lines: {
68 type: Array,
69 computed: '_computeLines(_mdCode)',
70 },
71 /**
72 * The markdown version of the share code to display in the
73 * code display element.
74 */
75 _mdCode: {
76 type: String,
77 value: null,
78 },
79 /**
80 * The current share to be played.
81 */
82 share: {
83 type: Object,
84 value: () => ({}),
85 },
86 };
87 }
88 static get observers() {
89 return [
90 '_shareChanged(share.*)',
91 ];
92 }
93 /** OBSERVERS * */
94 /**
95 * Computed the number of lines in the share's code and
96 * populated an array with the list of numbers.
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 /* Don't include the header */
108 for (let i = 1; i < lineCount; i += 1) {
109 lines.push(i);
110 }
111 return lines;
112 }
113 /**
114 * Load any code from storage and compute the markdown for
115 * display in the display code element.
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 /** EVENT HANDLERS* */
136 /**
137 * Set the property responsible for displaying and hiding the
138 * display code element.
139 */
140 _hideCode() {
141 this.set('displayCode', false);
142 this.dispatchEvent(new CustomEvent('hide-code'));
143 }
144}
145
146customElements.define('kwc-art-player', KwcArtPlayer);