/
simpla-article.html
172 lines (147 loc) · 3.51 KB
/
simpla-article.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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../simpla-element-behavior/simpla-element-behavior.html">
<dom-module id="simpla-article">
<template>
<style>
:host {
display: block;
}
:host([contenteditable]) {
outline: none;
}
/* Clearfix for image floats */
:host::after {
content: '';
display: table;
clear: both;
}
/**
* Styles for images in edit mode
*/
:host ::slotted(img::selection) {
background: transparent;
}
:host ::slotted(img::-moz-selection) {
background: transparent;
}
:host([contenteditable]) ::slotted(img) {
cursor: pointer;
}
</style>
<slot></slot>
</template>
<script>
import initEditor from './behaviors/initEditor';
const { Element } = window.SimplaBehaviors,
SIMPLA_CONFIG = {
type: 'Article',
dataProperties: [ 'value' ]
};
Polymer({
is: 'simpla-article',
properties: {
/**
* Displayed when editable with no content
* @type {String}
*/
placeholder: {
type: String,
value: 'Start writing...'
},
/**
* Disable 'smart' typography
* @type {Boolean}
*/
noTypographer: {
type: Boolean,
value: false
},
/**
* Whether article is being edited
* @type {Boolean}
*/
active: {
type: Boolean,
notify: true,
value: false
},
/**
* HTML content of the article
* @type {String}
*/
value: String,
},
observers: [
'_render(value)',
'_ensureInactiveIfNotEditable(editable)'
],
listeners: {
'focus': '_goActive',
'blur': '_goInactive'
},
behaviors: [
SimplaBehaviors.Element(SIMPLA_CONFIG),
initEditor
],
/**
* Set value prop on attach
* @return {undefined}
*/
attached() {
this._refreshValue();
},
/**
* Refreshes the value prop with HTML content
* @return {undefined}
*/
_refreshValue() {
const { _editor: editor } = this;
this.__refreshingValue = true;
this.value = editor ? editor.getHTML() : this.innerHTML;
this.__refreshingValue = false;
},
/**
* Renders HTML content when value changes
* @param {String} value HTML string content
* @return {undefined}
*/
_render(value) {
const { _editor: editor } = this;
if (this.__refreshingValue) {
return;
}
if (editor) {
editor.setHTML(value);
} else {
this.innerHTML = value;
}
},
/**
* Makes the article editor active
* @return {undefined}
*/
_goActive() {
if (this.editable) {
this.active = true;
}
},
/**
* Makes article editor inactive
* @return {undefined}
*/
_goInactive() {
this.active = false;
},
/**
* Ensures active is false when editable is false
* @param {Boolean} editable Whether article is editable
* @return {undefined}
*/
_ensureInactiveIfNotEditable(editable) {
if (!editable) {
this.active = false;
}
},
});
</script>
</dom-module>