-
Notifications
You must be signed in to change notification settings - Fork 30
/
sidebar.html
236 lines (214 loc) · 7.59 KB
/
sidebar.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
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
<!-- The CSS package above applies Google styling to buttons and other elements. -->
<style>
.branding-below {
bottom: 56px;
top: 0;
}
.branding-text {
left: 7px;
position: relative;
top: 3px;
}
.col-contain {
overflow: hidden;
}
.col-one {
float: left;
width: 50%;
}
.logo {
vertical-align: middle;
}
.radio-spacer {
height: 20px;
}
.width-100 {
width: 100%;
}
.success {
color: green;
}
.branding-title {
color: darkred;
display: inline;
}
.branding-tile {
margin-bottom: 5px;
}
</style>
<title></title>
</head>
<body>
<div class="sidebar branding-below">
<div class="branding-tile">
<svg class="logo" xmlns="http://www.w3.org/2000/svg" height="36" viewBox="0 0 36 36" width="36">
<path d="M0 0h24v24H0z" fill="none" />
<path
d="M2 17h2v.5H3v1h1v.5H2v1h3v-4H2v1zm1-9h1V4H2v1h1v3zm-1 3h1.8L2 13.1v.9h3v-1H3.2L5 10.9V10H2v1zm5-6v2h14V5H7zm0 14h14v-2H7v2zm0-6h14v-2H7v2z" />
</svg>
<h3 class="branding-title">autoNumberHeadings</h3>
</div>
<form>
<div class="block form-group">
<div>
<b>Action</b>
<div>
<input type="radio" name="action" id="radio-action-add" value="" checked="checked">
<label for="radio-action-add">Add numbers</label>
</div>
<div>
<input type="radio" name="action" id="radio-remove" value="remove">
<label for="radio-remove">Remove numbers</label>
</div>
<div>
<input type="radio" name="action" id="radio-promote" value="promote">
<label for="radio-promote">Promote <small>(H1➙Title ... H6➙H5)</small></label>
</div>
<div>
<input type="radio" name="action" id="radio-demote" value="demote">
<label for="radio-demote">Demote <small>(Title➙Title, H1➙H2 ... H6➙Normal)</small></label>
</div>
</div>
</div>
<div class="block">
<input type="checkbox" id="selection-only">
<label for="selection-only">Restrict to selected paragraphs</label>
</div>
<div class="block">
<input type="checkbox" id="titles-restart-numbering">
<label for="titles-restart-numbering">Titles restart numbering</label>
</div>
<div class="block">
<input type="checkbox" id="skip-headings">
<label for="skip-headings">Process only headings below</label>
</div>
<div class="block form-group">
<label for="skipped-levels"><b>Headings to process</b> (optional)</label>
<input class="width-100" id="skipped-levels" rows="10" placeholder="1 2 3"></input>
</div>
<div class="block">
<input type="checkbox" id="save-prefs">
<label for="save-prefs">Save options as default</label>
</div>
<div class="block" id="button-bar">
<button class="blue" id="run-autonumber">Run</button>
</div>
<div class="block form-group">
<label for="preview-text"><b>Plain text headings</b></label>
<textarea class="width-100" id="preview-text" rows="10"></textarea>
</div>
</form>
</div>
<div class="sidebar bottom">
<svg class="logo" xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24">
<path d="M0 0h24v24H0z" fill="none" />
<path
d="M2 17h2v.5H3v1h1v.5H2v1h3v-4H2v1zm1-9h1V4H2v1h1v3zm-1 3h1.8L2 13.1v.9h3v-1H3.2L5 10.9V10H2v1zm5-6v2h14V5H7zm0 14h14v-2H7v2zm0-6h14v-2H7v2z" />
</svg>
<span class="gray branding-text">autoNumberHeadings by <a href='https://github.com/lpanebr/autoNumberHeadings'
target='_blank'>lpanebr</a></span>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
/**
* On document load, assign click handlers to each button and try to load the
* user's preferences if previously set.
*/
$(function () {
$('#run-autonumber').click(runAutonumber);
$('#insert-text').click(insertText);
google.script.run.withSuccessHandler(loadPreferences)
.withFailureHandler(showError).getPreferences();
});
/**
* Callback function that populates the ui selection
* boxes with user preferences from the server.
*
* @param {Object} autonumberPrefs The saved user ui preferences.
*/
function loadPreferences(autonumberPrefs) {
$('input:radio[name="action"]')
.filter('[value=' + autonumberPrefs.action + ']')
.attr('checked', true);
$('#skip-headings').prop('checked', autonumberPrefs.skipHeadings)
$('#skipped-levels').val(autonumberPrefs.skippedLevels);
$('#titles-restart-numbering').prop('checked', autonumberPrefs.titlesRestartNumbering);
}
/**
* Runs a server-side function to translate the user-selected text and update
* the sidebar UI with the resulting result.
*/
function runAutonumber() {
this.disabled = true;
$('#error').remove();
const action = $('input[name=action]:checked').val();
const skipHeadings = $('#skip-headings').is(':checked')
const skippedLevels = $('#skipped-levels').val()
const titlesRestartNumbering = $('#titles-restart-numbering').is(':checked');
const selectionOnly = $('#selection-only').is(':checked');
const savePrefs = $('#save-prefs').is(':checked');
google.script.run
.withSuccessHandler(
function (textAndResult, element) {
$('#preview-text').val(textAndResult.after);
$('#success').remove()
showSuccess('Success! Use <b>Undo</b> if you need.', $('#button-bar'));
element.disabled = false;
})
.withFailureHandler(
function (msg, element) {
showError(msg, $('#button-bar'));
element.disabled = false;
})
.withUserObject(this)
.processHeadings(action, skipHeadings, skippedLevels, titlesRestartNumbering, selectionOnly, savePrefs);
}
/**
* Runs a server-side function to insert the translated text into the document
* at the user's cursor or selection.
*/
function insertText() {
this.disabled = true;
$('#error').remove();
google.script.run
.withSuccessHandler(
function (returnSuccess, element) {
element.disabled = false;
})
.withFailureHandler(
function (msg, element) {
showError(msg, $('#button-bar'));
element.disabled = false;
})
.withUserObject(this)
.insertText($('#preview-text').val());
}
/**
* Inserts a div that contains an error message after a given element.
*
* @param {string} msg The error message to display.
* @param {DOMElement} element The element after which to display the error.
*/
function showError(msg, element) {
const div = $('<div id="error" class="error">' + msg + '</div>');
$(element).after(div);
}
/**
* Inserts a div that contains an error message after a given element.
*
* @param {string} msg The error message to display.
* @param {DOMElement} element The element after which to display the error.
*/
function showSuccess(msg, element) {
const div = $('<div id="success" class="success">' + msg + '</div>');
$(element).after(div);
$(element).after(div);
}
</script>
</body>
</html>