This repository has been archived by the owner on Jul 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhax-text-context.html
300 lines (296 loc) · 11.6 KB
/
hax-text-context.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
298
299
300
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../md-extra-icons/md-extra-icons.html">
<link rel="import" href="../app-layout/app-toolbar/app-toolbar.html">
<link rel="import" href="../paper-item/paper-item.html">
<link rel="import" href="../materializecss-styles/colors.html">
<link rel="import" href="../iron-icons/iron-icons.html">
<link rel="import" href="hax-context-item-menu.html">
<link rel="import" href="hax-context-item.html">
<link rel="import" href="hax-context-item-textop.html">
<link rel="import" href="hax-toolbar.html">
<!--
`hax-text-context`
A context menu that provides common text based authoring options.
@demo demo/index.html
@microcopy - the mental model for this element
- context menu - this is a menu of text based buttons and events for use in a larger solution.
-->
<dom-module id="hax-text-context">
<template>
<style include="materializecss-styles-colors">
:host {
display: block;
pointer-events: none;
background-color: white;
}
paper-item {
-webkit-justify-content: flex-start;
justify-content: flex-start;
height: 32px;
padding: 0 8px;
min-height: 32px;
}
paper-item:hover {
background-color: #d3d3d3;
cursor: pointer;
}
iron-icon {
padding: 8px;
}
paper-item strong {
padding: 8px;
font-size: 12px;
}
:host.hax-context-pin-top hax-toolbar {
position: fixed;
top: 64px;
opacity: .95;
}
:host.hax-context-pin-bottom hax-toolbar {
position: fixed;
bottom: 0;
opacity: .95;
}
</style>
<hax-toolbar selected="[[selection]]" hide-transform>
<hax-context-item-menu
corner="left"
slot="primary"
selected-value="{{selectedValue}}"
id="formatsize"
icon="text-format"
label="Format"
event-name="text-tag">
<paper-item value="p"><iron-icon icon="editor:format-textdirection-l-to-r"></iron-icon>Normal text <strong><P></strong></paper-item>
<paper-item value="h2"><iron-icon icon="editor:title"></iron-icon>Title <strong><H2></strong></paper-item>
<paper-item value="h3"><iron-icon icon="editor:title"></iron-icon>Content heading <strong><H3></strong></paper-item>
<paper-item value="h4"><iron-icon icon="editor:text-fields"></iron-icon>Subheading <strong><H4></strong></paper-item>
<paper-item value="h5"><iron-icon icon="editor:text-fields"></iron-icon>Deeper subheading <strong><H5></strong></paper-item>
<paper-item value="blockquote"><iron-icon icon="editor:format-quote"></iron-icon>Quote<strong><blockquote></strong></paper-item>
<paper-item value="code"><iron-icon icon="icons:code"></iron-icon>Code block<strong><code></strong></paper-item>
</hax-context-item-menu>
<hax-context-item-textop slot="primary" icon="editor:format-bold" label="Bold" event-name="text-bold"></hax-context-item-textop>
<hax-context-item-textop slot="primary" icon="editor:format-italic" label="Italic" event-name="text-italic"></hax-context-item-textop>
<hax-context-item-textop slot="primary" icon="editor:insert-link" label="Link" event-name="text-link"></hax-context-item-textop>
<hax-context-item-menu
slot="primary"
selected-value="{{justifyValue}}"
id="justify"
icon="[[justifyIcon]]"
label="Alignment">
<paper-item value="" hidden></paper-item>
<paper-item value="text-align-left">
<iron-icon icon="editor:format-align-left"></iron-icon>
</paper-item>
<paper-item value="text-align-right">
<iron-icon icon="editor:format-align-right"></iron-icon>
</paper-item>
</hax-context-item-menu>
<hax-context-item-textop
slot="primary"
icon="editor:format-list-numbered"
label="Numbered list"
event-name="text-list-numbered"
hidden$="[[!polyfillSafe]]"></hax-context-item-textop>
<hax-context-item-textop
slot="primary"
icon="editor:format-list-bulleted"
label="Bulleted list"
event-name="text-list-bulleted"
hidden$="[[!polyfillSafe]]"></hax-context-item-textop>
<hax-context-item-textop
slot="primary"
icon="editor:format-indent-increase"
label="Indent"
event-name="text-indent"
hidden$="[[!polyfillSafe]]"></hax-context-item-textop>
<hax-context-item-textop
slot="primary"
icon="editor:format-indent-decrease"
label="Outdent"
event-name="text-outdent"
hidden$="[[!polyfillSafe]]"></hax-context-item-textop>
<hax-context-item-textop slot="primary" icon="editor:format-clear" label="Remove format" event-name="text-remove-format"></hax-context-item-textop>
<hax-context-item slot="primary" icon="device:graphic-eq" label="Advanced item" event-name="insert-inline-gizmo"></hax-context-item>
<hax-context-item-textop menu slot="more" icon="mdextra:unlink" event-name="text-unlink">Remove link</hax-context-item-textop>
<hax-context-item-textop menu slot="more" icon="mdextra:subscript" event-name="text-subscript">Subscript</hax-context-item-textop>
<hax-context-item-textop menu slot="more" icon="mdextra:superscript" event-name="text-superscript">Superscript</hax-context-item-textop>
<hax-context-item-textop menu slot="more" icon="editor:format-strikethrough" event-name="text-strikethrough">Cross out</hax-context-item-textop>
</hax-toolbar>
</template>
<script>
Polymer({
is: 'hax-text-context',
listeners: {
'hax-context-item-selected': '_haxContextOperation',
},
properties: {
/**
* Justify icon to reflect state.
*/
justifyIcon: {
type: String,
value: 'editor:format-align-left',
},
/**
* Polyfill safe; this helps remove options from polyfilled platforms
* as far as text manipulation operations.
*/
polyfillSafe: {
type: Boolean,
},
/**
* Selected value to match format of the tag currently.
*/
selectedValue: {
type: String,
value: 'p',
notify: true,
},
/**
* Selected value to match text direction currently.
*/
justifyValue: {
type: String,
value: 'text-align-left',
notify: true,
},
/**
* Selection tracking
*/
selection: {
type: Boolean,
value: false,
},
},
/**
* Ready, figure out polyfill
*/
ready: function() {
this.polyfillSafe = Polymer.HaxStore.instance.computePolyfillSafe();
},
/**
* Respond to simple modifications.
*/
_haxContextOperation: function(e) {
let detail = e.detail;
let selection = window.getSelection();
// support a simple insert event to bubble up or everything else
switch(detail.eventName) {
// wow these are way too easy
case 'text-align-left':
this.justifyIcon = detail.target.children[0].attributes[0].value;
break;
case 'text-align-center':
this.justifyIcon = detail.target.children[0].attributes[0].value;
break;
case 'text-align-right':
this.justifyIcon = detail.target.children[0].attributes[0].value;
break;
case 'text-justify-full':
this.justifyIcon = detail.target.children[0].attributes[0].value;
break;
case 'close-menu':
this.$.justify.$.menu.hideMenu();
this.$.formatsize.$.menu.hideMenu();
break;
case 'insert-inline-gizmo':
// store placeholder because if this all goes through we'll want
// to kill the originating text
let values = {
text: Polymer.HaxStore.instance.activePlaceHolder.toString(),
};
let type = 'inline';
let haxElements = Polymer.HaxStore.guessGizmo(type, values);
// see if we got anything
if (haxElements.length > 0) {
// hand off to hax-app-picker to deal with the rest of this
Polymer.HaxStore.instance.haxAppPicker.presentOptions(haxElements, type, 'Transform selected text to..', 'gizmo');
}
break;
// wow these are way too easy
case 'text-bold':
document.execCommand('bold');
break;
case 'text-italic':
document.execCommand('italic');
break;
case 'text-underline':
document.execCommand('underline');
// silly hack to account for trigging a selection from
// inside the menu that isn't from a paper-item
this.$.toolbar.$.moremenu.$.menu.hideMenu();
break;
case 'text-subscript':
document.execCommand('subscript');
// silly hack to account for trigging a selection from
// inside the menu that isn't from a paper-item
this.$.toolbar.$.moremenu.$.menu.hideMenu();
break;
case 'text-superscript':
document.execCommand('superscript');
// silly hack to account for trigging a selection from
// inside the menu that isn't from a paper-item
this.$.toolbar.$.moremenu.$.menu.hideMenu();
break;
case 'text-remove-format':
document.execCommand('removeFormat');
break;
case 'text-strikethrough':
document.execCommand('strikeThrough');
// silly hack to account for trigging a selection from
// inside the menu that isn't from a paper-item
this.$.toolbar.$.moremenu.$.menu.hideMenu();
break;
case 'text-link':
var href = '';
if (typeof selection.focusNode.parentNode.href !== typeof undefined) {
href = selection.focusNode.parentNode.href;
}
// @todo put in a dialog instead of this
let url = prompt("Enter a URL:", href);
if (url) {
document.execCommand('createLink', false, url);
}
break;
case 'text-unlink':
document.execCommand('unlink');
break;
/**
* Our bad actors when it comes to polyfill'ed shadowDOM.
* Naughty, naughty shadyDOM. Fortunately this is only IE11/Edge
*/
case 'text-indent':
document.execCommand('indent');
break;
case 'text-outdent':
document.execCommand('outdent');
break;
case 'text-list-numbered':
document.execCommand('insertOrderedList');
break;
case 'text-list-bulleted':
document.execCommand('insertUnorderedList');
break;
}
},
/**
* Test for safari, if it is don't place things in the menu
*/
isSafari: function (typevalue) {
let ua = navigator.userAgent.toLowerCase();
// test to find safari to account for it's handling
// of what's been selected. This isn't great UX but
// there's literally nothing we can do for Safari
// because of https://github.com/LRNWebComponents/hax-body/issues/38
if (ua.indexOf('safari') != -1) {
if (ua.indexOf('chrome') > -1) {
} else {
return true;
}
}
return false;
},
});
</script>
</dom-module>