-
Notifications
You must be signed in to change notification settings - Fork 1
/
html-editor.html
290 lines (248 loc) · 14.2 KB
/
html-editor.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
<link rel="import" href="../../lib/polymer/polymer-element.html">
<link rel="import" href="../../lib/iron-icons/editor-icons.html">
<link rel="import" href="../../lib/paper-icon-button/paper-icon-button.html">
<link rel="import" href="../../lib/paper-menu-button/paper-menu-button.html">
<link rel="import" href="../../lib/polymer/lib/elements/dom-if.html">
<link rel="import" href="../../lib/polymer/lib/elements/dom-repeat.html">
<script type="module" src="sandbox-editor.js">
</script>
<dom-module id="html-editor">
<template>
<style>
:host {
/*--swatch-colour-size: 20px;*/
display:block;
position: relative;
overflow: hidden;
}
paper-icon-button {
width: 24px;
height: 24px;
padding: 3px;
}
sandbox-editor {
width: 100%;
height: calc(100% - 22px); /* 20px for the label and 2px for the grow-bar */
}
paper-swatch-picker {
width: 24px;
height: 24px;
padding: -8px;
--paper-swatch-picker-icon-size: 12px;
}
paper-menu-button {
padding: 0;
}
.colour-swatch {
padding: 0;
display: grid;
grid-template-columns: repeat(18, var(--swatch-colour-size, 20px));
grid-template-rows: repeat(10, var(--swatch-colour-size, 20px));
width: calc(18 * var(--swatch-colour-size, 20px));
height: calc(10 * var(--swatch-colour-size, 20px));
font-size: 0;
line-height: 0;
overflow: hidden;
}
/* If we just scale the paper-item when hovering, this will end up adding scrollbars to the container that are hard to get rid of.
* An easy workaround is to use an :after pseudo element instead. */
.colour {
box-sizing: border-box;
width: var(--swatch-colour-size, 20px);
height: var(--swatch-colour-size, 20px);
display: inline-block;
padding: 0;
margin: 0;
cursor: pointer;
font-size: 0;
position: relative;
background-color: currentColor;
}
.colour:after {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: currentColor;
content: '';
-webkit-transition: -webkit-transform 0.2s;
transition: transform .2s;
z-index: 0;
}
.colour:hover:after,
.colour:focus:after {
-webkit-transform: scale(1.3, 1.3);
transform: scale(1.3, 1.3);
outline: none;
z-index: 1;
}
.underline {
height: 2px;
position: relative;
}
.underline > .focused-line {
@apply --layout-fit;
border-bottom: 2px solid var(--paper-input-container-focus-color, var(--primary-color));
-webkit-transform-origin: center center;
transform-origin: center center;
-webkit-transform: scale3d(0,1,1);
transform: scale3d(0,1,1);
@apply --paper-input-container-underline-focus;
}
.underline[focused] > .focused-line {
-webkit-transform: none;
transform: none;
-webkit-transition: -webkit-transform 0.25s;
transition: transform 0.25s;
@apply --paper-transition-easing;
}
.underline > .unfocused-line {
@apply --layout-fit;
border-bottom: 1px solid var(--paper-input-container-color, var(--secondary-text-color));
@apply --paper-input-container-underline;
}
label {
font-size: 12px;
line-height: 20px;
height: 20px;
}
#toolbar {
position: absolute;
bottom: 0;
background-color: var(--html-editor-background-colour, #fff);
color: var(--html-editor-colour, #000);
padding: .5em;
box-shadow: rgba(0, 0, 0, 0.14) 0px -2px 2px 0px, rgba(0, 0, 0, 0.12) 0px -1px 5px 0px, rgba(0, 0, 0, 0.2) 0px -3px 1px -2px;
transform: translateY(44px);
transition: transform .2s ease-in-out;
}
#toolbar[focused] {
transform: none;
}
</style>
<label hidden$="[[!label]]" aria-hidden="true" for="sandbox" slot="label">[[label]]</label>
<sandbox-editor id="sandbox" content={{value}} slot="input" on-focused-changed="_focusChanged" on-content-changed="_contentChanged"></sandbox-editor>
<div id="toolbar" focused$=[[focused]]>
<slot>
<paper-icon-button icon="undo" editor-command="undo" on-tap="_fireAction" title="Undo"></paper-icon-button>
<paper-icon-button icon="redo" editor-command="redo" on-tap="_fireAction" title="Redo"></paper-icon-button>
|
<paper-icon-button icon="editor:format-bold" editor-command="bold" on-tap="_fireAction" title="Bold"></paper-icon-button>
<paper-icon-button icon="editor:format-italic" editor-command="italic" on-tap="_fireAction" title="Italic"></paper-icon-button>
<paper-icon-button icon="editor:format-underlined" editor-command="underline" on-tap="_fireAction" title="Underline"></paper-icon-button>
<paper-icon-button icon="editor:format-clear" editor-command="removeFormat" on-tap="_fireAction" title="Clear formatting"></paper-icon-button>
|
<paper-icon-button icon="editor:format-list-bulleted" editor-command="insertUnorderedList" on-tap="_fireAction" title="Bullet points"></paper-icon-button>
<paper-icon-button icon="editor:format-list-numbered" editor-command="insertOrderedList" on-tap="_fireAction" title="Numbered list"></paper-icon-button>
|
<paper-icon-button icon="editor:format-indent-decrease" editor-command="outdent" on-tap="_fireAction" title="Decrease indent"></paper-icon-button>
<paper-icon-button icon="editor:format-indent-increase" editor-command="indent" on-tap="_fireAction" title="Increase indent"></paper-icon-button>
|
<paper-icon-button icon="editor:format-align-center" editor-command="justifyCenter" on-tap="_fireAction" title="Align centre"></paper-icon-button>
<paper-icon-button icon="editor:format-align-justify" editor-command="justifyFull" on-tap="_fireAction" title="Full justify"></paper-icon-button>
<paper-icon-button icon="editor:format-align-left" editor-command="justifyLeft" on-tap="_fireAction" title="Align left"></paper-icon-button>
<paper-icon-button icon="editor:format-align-right" editor-command="justifyRight" on-tap="_fireAction" title="Align right"></paper-icon-button>
|
<paper-menu-button vertical-align="bottom" horizontal-align="right" on-paper-dropdown-open="_initColours">
<paper-icon-button icon="editor:format-color-text" slot="dropdown-trigger" editor-command="foreColor" title="Font colour"></paper-icon-button>
<div slot="dropdown-content" class="dropdown-content colour-swatch">
<template is="dom-repeat" items="{{colours}}" as="colour">
<span class="colour" on-tap="_setForeColour">{{colour}}</span>
</template>
</div>
</paper-menu-button>
<paper-menu-button vertical-align="bottom" horizontal-align="right" on-paper-dropdown-open="_initColours">
<paper-icon-button icon="editor:format-color-fill" slot="dropdown-trigger" editor-command="backColor" title="Background colour"></paper-icon-button>
<div slot="dropdown-content" class="dropdown-content colour-swatch">
<template is="dom-repeat" items="{{colours}}" as="colour">
<span class="colour" on-tap="_setBackColour">{{colour}}</span>
</template>
</div>
</paper-menu-button>
</slot>
</div>
<div class="underline" focused$=[[focused]]>
<div class="unfocused-line"></div>
<div class="focused-line"></div>
</div>
</template>
<script>
// @ts-check
/** Material design colours, hues across, intensity down */
const materialDesignColours = [
'#ffebee', '#fce4ec', '#f3e5f5', '#ede7f6', '#e8eaf6', '#e3f2fd', '#e1f5fe', '#e0f7fa', '#e0f2f1', '#e8f5e9', '#f1f8e9', '#f9fbe7', '#fffde7', '#fff8e1', '#fff3e0', '#fbe9e7', '#efebe9', '#fafafa',
'#ffcdd2', '#f8bbd0', '#e1bee7', '#d1c4e9', '#c5cae9', '#bbdefb', '#b3e5fc', '#b2ebf2', '#b2dfdb', '#c8e6c9', '#dcedc8', '#f0f4c3', '#fff9c4', '#ffecb3', '#ffe0b2', '#ffccbc', '#d7ccc8', '#f5f5f5',
'#ef9a9a', '#f48fb1', '#ce93d8', '#b39ddb', '#9fa8da', '#90caf9', '#81d4fa', '#80deea', '#80cbc4', '#a5d6a7', '#c5e1a5', '#e6ee9c', '#fff59d', '#ffe082', '#ffcc80', '#ffab91', '#bcaaa4', '#eeeeee',
'#e57373', '#f06292', '#ba68c8', '#9575cd', '#7986cb', '#64b5f6', '#4fc3f7', '#4dd0e1', '#4db6ac', '#81c784', '#aed581', '#dce775', '#fff176', '#ffd54f', '#ffb74d', '#ff8a65', '#a1887f', '#e0e0e0',
'#ef5350', '#ec407a', '#ab47bc', '#7e57c2', '#5c6bc0', '#42a5f5', '#29b6f6', '#26c6da', '#26a69a', '#66bb6a', '#9ccc65', '#d4e157', '#ffee58', '#ffca28', '#ffa726', '#ff7043', '#8d6e63', '#bdbdbd',
'#f44336', '#e91e63', '#9c27b0', '#673ab7', '#3f51b5', '#2196f3', '#03a9f4', '#00bcd4', '#009688', '#4caf50', '#8bc34a', '#cddc39', '#ffeb3b', '#ffc107', '#ff9800', '#ff5722', '#795548', '#9e9e9e',
'#e53935', '#d81b60', '#8e24aa', '#5e35b1', '#3949ab', '#1e88e5', '#039be5', '#00acc1', '#00897b', '#43a047', '#7cb342', '#c0ca33', '#fdd835', '#ffb300', '#fb8c00', '#f4511e', '#6d4c41', '#757575',
'#d32f2f', '#c2185b', '#7b1fa2', '#512da8', '#303f9f', '#1976d2', '#0288d1', '#0097a7', '#00796b', '#388e3c', '#689f38', '#afb42b', '#fbc02d', '#ffa000', '#f57c00', '#e64a19', '#5d4037', '#616161',
'#c62828', '#ad1457', '#6a1b9a', '#4527a0', '#283593', '#1565c0', '#0277bd', '#00838f', '#00695c', '#2e7d32', '#558b2f', '#9e9d24', '#f9a825', '#ff8f00', '#ef6c00', '#d84315', '#4e342e', '#424242',
'#b71c1c', '#880e4f', '#4a148c', '#311b92', '#1a237e', '#0d47a1', '#01579b', '#006064', '#004d40', '#1b5e20', '#33691e', '#827717', '#f57f17', '#ff6f00', '#e65100', '#bf360c', '#3e2723', '#212121'
];
class HtmlEditor extends Polymer.Element {
static get is() { return 'html-editor'; }
static get properties() {
return {
value: { type: String, notify: true },
colours: { type: Array, value: materialDesignColours },
label: String,
focused: { type: Boolean, value: false, observer: '_focusChanged' }
}
}
_contentChanged(e) {
this.dispatchEvent(new CustomEvent('change', { detail: { value: this.value } }));
}
_focusChanged(e) {
if (!e.detail)
return;
if (e.detail.value) {
this.focused = true;
// When focusing on the editor pane close any overlay menus
for (const menu of this.shadowRoot.querySelectorAll('paper-menu-button'))
menu.close();
}
// document.activeElement will return the parent light DOM, we want the element in this component
// Read more https://medium.com/dev-channel/focus-inside-shadow-dom-78e8a575b73
const newFocus = this.shadowRoot.activeElement;
// If we have a new focus in the same shadow root then we've switched sub-controls, overall focus is still on the component
if (newFocus)
this.focused = true;
else
// Focus has left the shadow root too, lose focus flag
this.focused = false;
}
_setColour(target, action) {
const menuButton = target.parentElement.parentElement;
const colour = target.textContent;
menuButton.style.color = colour;
menuButton.close();
this.$.sandbox.editorAction(action, colour);
}
_setForeColour(e) {
this._setColour(e.currentTarget, 'foreColor');
}
_setBackColour(e) {
this._setColour(e.currentTarget, 'backColor');
}
_initColours(e) {
// Fill in the colors if we haven't already.
if (e.currentTarget._renderedColors)
return;
// Find all the colours selectors and set their current colour to be the content
const colourItems = e.currentTarget.querySelectorAll('.colour');
for (var i = 0; i < colourItems.length; i++)
// Use the forecolour so that we can use the currentColor pseudo CSS variable
colourItems[i].style.color = colourItems[i].textContent;
e.currentTarget._renderedColors = true;
}
_fireAction(e) {
const command = e.currentTarget.getAttribute('editor-command');
this.$.sandbox.editorAction(command);
}
}
customElements.define(HtmlEditor.is, HtmlEditor);
</script>
</dom-module>