-
Notifications
You must be signed in to change notification settings - Fork 0
/
medium-stories.html
297 lines (255 loc) · 7.61 KB
/
medium-stories.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<link rel="import" href="../polymer/polymer-element.html">
<link rel="import" href="../polymer/lib/elements/dom-if.html">
<link rel="import" href="../iron-ajax/iron-ajax.html">
<dom-module id="medium-stories">
<template>
<style>
:host {
display: block;
letter-spacing: 0;
font-weight: 400;
font-style: normal;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
-moz-font-feature-settings: "liga" on;
color: rgba(0, 0, 0, .84);
font-size: 20px;
line-height: 1.4;
max-width: 640px;
margin-left: auto;
margin-right: auto;
--x-height-multiplier: 0.375;
--baseline-multiplier: 0.17;
font-family: Georgia, Cambria, "Times New Roman", Times, serif;
letter-spacing: .01rem;
font-weight: 400;
font-style: normal;
font-size: 18px;
line-height: 1.58;
letter-spacing: -.004em;
}
.card {
background: #fff;
-webkit-box-shadow: 0 1px 4px rgba(0, 0, 0, .04);
box-shadow: 0 1px 4px rgba(0, 0, 0, .04);
border: 1px solid rgba(0, 0, 0, .09);
-webkit-border-radius: 3px;
border-radius: 3px;
padding-right: 20px;
padding-left: 20px;
padding-bottom: 15px;
padding-top: 10px;
margin-top: 20px;
@apply --medium-stories-card;
}
.title,
.sub-title,
.profile {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
}
.title {
font-size: 26px;
margin-left: -1.88px;
line-height: 1.04;
letter-spacing: -.015em;
@apply --medium-stories-title;
}
.sub-title {
color: rgba(0, 0, 0, .54);
font-size: 20px;
@apply --medium-stories-sub-title;
}
@media (max-width: 767px) {
.title {
line-height: 1.12;
letter-spacing: -.005em
}
}
.profile {
padding-top: 5px;
margin-bottom: 15px;
font-size: 13px;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
@apply --medium-stories-profile;
}
.profile-image {
width: 32px;
height: 32px;
display: inline-block;
vertical-align: middle;
-webkit-border-radius: 100%;
border-radius: 100%;
}
.profile-desc {
line-height: 1.4;
padding-left: 10px;
text-rendering: auto;
}
.profile-name,
.content {
text-decoration: inherit;
color: inherit;
}
.profile-name:visited,
.content:visited {
text-decoration: inherit;
color: inherit;
}
.profile-name {
color: #03a87c !important;
}
.profile-info {
color: rgba(0, 0, 0, .54);
fill: rgba(0, 0, 0, .54);
}
</style>
<template is="dom-repeat" items="[[posts]]" as="post">
<div class="card">
<div class="profile">
<img class="profile-image" src="[[userImage]]" alt="">
<div class="profile-desc">
<a class="profile-name" href="https://medium.com/@[[user.username]]" target="_blank" rel="noopener">[[user.name]]</a>
<div class="profile-info">
<span>[[_getDate(post.firstPublishedAt)]]</span>
<span>·</span>
<span>[[_roundUp(post.virtuals.readingTime)]] min read</span>
</div>
</div>
</div>
<a class="content" href="https://medium.com/@[[user.username]]/[[post.uniqueSlug]]" target="_blank" rel="noopener">
<template is="dom-repeat" items="[[post.previewContent.bodyModel.paragraphs]]" as="preview">
<template is="dom-if" if="[[_eql(preview.type,3)]]">
<h1 class="title">[[preview.text]]</h1>
</template>
<template is="dom-if" if="[[_eql(preview.type,2)]]">
<h2 class="">[[preview.text]]</h2>
</template>
<template is="dom-if" if="[[_notEql(preview.type,3,2)]]">
<p class$="[[_classForType(preview.type)]]">[[preview.text]]</p>
</template>
</template>
</a>
</div>
</template>
<iron-ajax auto url="[[url]]" handle-as="json" on-response="_handleResponse"></iron-ajax>
</template>
<script>
/**
* `medium-story-card`
* A card displaying and linking to a story published on medium
* ### Styling
*
* The following custom properties and mixins are available for styling:
*
* Custom property | Description | Default
* ----------------|-------------|----------
* `--medium-stories-card` | Mixin applied to the story card | `{}`
* `--medium-stories-title` | Mixin applied to titles | `{}`
* `--medium-stories-sub-title` | Mixin applied to sub titles | `{}`
* `--medium-stories-profile` | Mixin applied to the profile container | `{}`
*
* @customElement
* @polymer
* @demo demo/index.html
*/
class MediumStories extends Polymer.Element {
static get is() {return 'medium-stories';}
static get properties() {
return {
/**
* The URL to retrieve the latest medium stories of your profile.
* You need to create a proxy for that. (easy as pie)
* Go here to read how https://github.com/JaySunSyn/medium-firebase-proxy
*/
url: {
type: String,
},
response: {
type: Object,
},
posts: {
type: Array,
},
/**
* Controls which posts to display.
* Overwrite with a custom filter.
*/
postsFilterFn: {
type: Function,
value() {
return (posts) => {
return posts;
};
},
},
user: {
type: Object,
},
userImageSize: {
type: Number,
value: 100,
},
mediumImageBaseUrl: {
type: String,
value: 'https://cdn-images-1.medium.com/fit/c',
},
userImage: {
type: String,
computed: '_computeUserImage(user.imageId, userImageSize, mediumImageBaseUrl)',
},
};
}
_roundUp(num) {
return Math.ceil(num);
}
_getDate(num) {
const options = {
year: 'numeric',
month: 'short',
day: 'numeric',
};
return new Date(num).toLocaleString('en-us', options);
}
_classForType(type) {
if (type === 13) {
return 'sub-title';
}
}
_eql(a, b) {
return a === b;
}
_notEql(...args) {
const val = args.shift();
return !args.some(a => a === val);
}
_computeUserImage(imageId, size, base) {
return `${base}/${size}/${size}/${imageId}`;
}
_handleResponse(e) {
if (!e.detail.response) {
return;
}
this.response = e.detail.response;
const payload = this.response.payload;
if (!payload) {
return;
}
if (payload.references && payload.references.Post) {
this.posts = this.postsFilterFn(Object.keys(payload.references.Post).map(k => payload.references.Post[k]));
}
if (payload.user) {
this.user = payload.user;
}
}
}
window.customElements.define(MediumStories.is, MediumStories);
</script>
</dom-module>