-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontent.js
executable file
·963 lines (827 loc) · 22.9 KB
/
content.js
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
$(function () {
var ADDING_TAG_DELAY = 0
// The app
var $root
var timerId
var ROOT_SELECTOR = '#tumblrTag'
var ROOT_ELEM = '<div id="tumblrTag"></div>'
var ROOT_CSS = {
'position': 'absolute',
'left': '1250px',
'top': '400px',
'-webkit-transform': 'translateX(-50%) translateY(-100%)',
'z-index': '1011',
'width': '200px',
'color': '#fff',
'margin-bottom': '1em',
'-webkit-transition': 'top .3s ease-in-out'
}
var SHOULD_RENDER_CHECK_TIMER_MS = 1000
// The actions container
var $actions
var ACTIONS_SELECTOR = '#tumblrTag-actions'
var ACTIONS_ELEM = '<div id="tumblrTag-actions"></div>'
var ACTIONS_ELEM_CSS = {
'font-size': '1.5em',
'cursor': 'pointer',
'margin-bottom': '.5em'
}
// The icon to create the lists
var $createList
var CREATE_LIST_SELECTOR = '#tumblrTag-createListButton'
var CREATE_LIST_BUTTON_ELEM = '<button id="tumblrTag-createListButton">Create</button>'
var CREATE_LIST_BUTTON_CSS = {
'border-radius': '2px',
'background-color': '#00b8ff',
'padding': '0.3em 0.6em',
'font-size': '.8em',
'font-weight': '600',
'margin-bottom': '0.5em',
'display': 'block'
}
// The editor container
var $editor
var EDITOR_ELEM = '<div id="tumblrTag-editor"></div>'
var EDITOR_HEIGHT = 250
var EDITOR_CSS = {
'width': '200px',
'height': EDITOR_HEIGHT + 'px',
'background-color': '#fff',
'box-sizing': 'border-box',
'padding': '1em'
}
// The input to edit the list name
var $editorListName
var EDITOR_LIST_NAME_SELECTOR = '#tumblrTag-editListNameInput'
var EDITOR_LIST_NAME_ELEM = '<input id="tumblrTag-editListNameInput"/>'
var EDITOR_LIST_NAME_CSS = {
'width': '100%',
'margin-bottom': '1em'
}
// The textarea to edit the list content
var $editorListContent
var EDITOR_LIST_CONTENT_SELECTOR = '#tumblrTag-editListTagsTextArea'
var EDITOR_LIST_CONTENT_ELEM = '<textarea id="tumblrTag-editListTagsTextArea"></textarea>'
var EDITOR_LIST_CONTENT_CSS = {
'width': '100%',
'height': '100px'
}
// The button to create/edit the list
var $doneButton
var EDITOR_BUTTON_SELECTOR = '#tumblrTag-doneEditingListButton'
var EDITOR_BUTTON_ELEM = '<button id="tumblrTag-doneEditingListButton">Done</button>'
var EDITOR_BUTTON_CSS = {
'border-radius': '2px',
'padding': '0.3em 0.6em',
'display': 'block',
'font-weight': '600',
'font-size': '.8em',
'margin': '0 auto',
'background-color': '#00b8ff'
}
// The lists
var $lists
var LISTS_SELECTOR = '#tumblrTag-lists'
var LISTS_ELEM = '<ul id="tumblrTag-lists"></ul>'
function getListsHeight () {
return window.innerHeight / 2
}
function getListsCss () {
return {
'max-height': getListsHeight() + 'px',
'overflow-y': 'scroll',
'padding-left': 0
}
}
//
// var LISTS_CSS = {
// 'max-height': '250px',
// 'overflow-y': 'scroll'
// }
// A list
var LIST_SELECTOR = '.tumblrTag-list'
var LIST_CSS = {
'cursor': 'pointer',
'padding': '12px',
'border-radius': '6px',
'background-color': 'rgba(0, 0, 0, 0.2)',
'margin-bottom': '1em',
'display': 'flex',
'align-items': 'center',
'height': '24px',
'line-height': '20px'
}
// The icon to delete a list
var DELETE_LIST_BUTTON_SELECTOR = '.tumblrTag-deleteListButton'
// var DELETE_LIST_BUTTON_ELEM = '<i class="tumblrTag-deleteListButton
// icon_close"></i>'
var DELETE_LIST_BUTTON_ELEM = `
<button class='tumblrTag-deleteListButton' aria-label='Dismiss Recommendation'><span class='' tabindex='-1'><svg xmlns='http://www.w3.org/2000/svg' height='14' width='14' role='presentation' style='--icon-color-primary: white'><use href='#managed-icon__close'></use></svg></span></button>
`
var DELETE_LIST_BUTTON_CSS = {
'float': 'right',
'font-size': '1.3em',
'padding': '0.1em',
'border-radius': '2px',
'line-height': '1em'
}
// The icon to edit a list
var EDIT_LIST_BUTTON_SELECTOR = '.tumblrTag-editListButton'
var EDIT_LIST_BUTTON_ELEM = '<i class="tumblrTag-editListButton icon_edit_pencil"><svg xmlns="http://www.w3.org/2000/svg" height="14" width="16" role="presentation"><use href="#managed-icon__post"></use></svg></i>'
var EDIT_LIST_BUTTON_ELEM_CSS = {
'float': 'right',
'margin-right': '.4em',
'font-size': '1.3em',
'padding': '0.1em',
'border-radius': '2px',
'line-height': '1em'
}
// Other Selectors
var POST_CONTAINER_SELECTOR = '.post-container'
var POST_CONTAINER_TAGS_INPUT_SELECTOR = '#glass-container textarea'
var POST_CONTAINER_TAGS_INPUT_WRAPPER_SELECTOR_LEGACY = '.post-form--tag-editor .editor-plaintext'
var POST_CONTAINER_TAGS_INPUT_SELECTOR_LEGACY = '.post-form--tag-editor .editor-plaintext span'
var CONTAINER_WRAPPER_SELECTOR = 'div[data-testid="base-container-wrapper"]'
var BUTTON_OVERLAY_SELECTOR = '#glass-container button[aria-label="Close"]'
var BUTTON_OVERLAY_WRAPPER_SELECTOR = '#glass-container div[role="dialog"]'
var SETTINGS_ICON = 'button[aria-label="Settings"]'
var SETTINGS_ICON_LEGACY = '.post-settings'
function getSettingsIcon () {
return (
document.querySelector(SETTINGS_ICON) ||
document.querySelector(SETTINGS_ICON_LEGACY)
)
}
function getLegacyTagInput () {
return document.querySelector(POST_CONTAINER_TAGS_INPUT_SELECTOR_LEGACY)
}
function getLegacyTagInputWrapper () {
return document.querySelector(
POST_CONTAINER_TAGS_INPUT_WRAPPER_SELECTOR_LEGACY)
}
function getTagInput () {
return (
document.querySelector(POST_CONTAINER_TAGS_INPUT_SELECTOR) ||
getLegacyTagInput()
)
}
// Events
var EVENT = {
DOM_SUBTREE_MODIFIED: 'DOMSubtreeModified',
RESIZE: 'resize',
CLICK: 'click',
FOCUS: 'focus',
BLUR: 'blur'
}
/**
* The list's name being edited.
* @type {String}
*/
var editingList
/**
* Contains the tags.
* It gets saved in the `chrome.storage`.
* @type {Object}
*/
var store
/**
* Whether or not the app is in debug mode.
* TODO
* @type {Boolean}
*/
var isDebugging
/**
* Inits the app.
*/
function init () {
// Init the debug flags
initDebug()
debug('init')
// Get the store
setTimeout(() => {
initStore()
}, 1000)
}
/**
* Gets the stored data.
*/
function initStore () {
debug('initStore')
get(onAfterGetStore)
}
/**
* Gets the stored data.
* If there is no data stored, create an example
* and set it in the storage.
*
* @param {Object} storage
*/
function onAfterGetStore (storage) {
debug('onAfterGetStore: storage %o', storage)
// If there are tags store, return them
if (storage && storage.tags && storage.tags.length) {
store = storage
}
// If there are no tags stored, set the example
else {
setExample()
}
shouldRender()
}
/**
* Sets the example in the storage.
*/
function setExample () {
debug('setExample')
store = {
tags: [
{
name: 'Example',
list: [
'tag1',
'tag2',
'tag3'
]
}
]
}
set()
}
/**
* Inits the debug flag.
*/
function initDebug () {
// TODO handle `isDebugging` flag
isDebugging = true
}
/**
* Renders the app.
*/
function render () {
debug('render: store %o', store)
if ($root) {
unmount()
}
$('body').append(ROOT_ELEM)
$root = $(ROOT_SELECTOR)
$root.css(ROOT_CSS)
renderActions()
renderLists()
renderEditor()
updatePosition()
// Bind the events
bind()
}
/**
* Renders the actions.
*/
function renderActions () {
debug('renderActions')
// Append the actions container
$root.append(ACTIONS_ELEM)
$actions = $(ACTIONS_SELECTOR)
$actions.css(ACTIONS_ELEM_CSS)
// Append the add/edit button
$actions.append(CREATE_LIST_BUTTON_ELEM)
$createList = $(CREATE_LIST_SELECTOR)
$createList.css(CREATE_LIST_BUTTON_CSS)
}
/**
* Renders the lists.
*/
function renderLists () {
debug('renderLists')
// Append the lists container
$root.append(LISTS_ELEM)
// Append the lists
$lists = $(LISTS_SELECTOR)
var listsCount = store.tags.length
store.tags.forEach(function (tagObj) {
var elementStr
if (listsCount > 1) {
elementStr = '<li class="' + LIST_SELECTOR.slice(1) + '"><span style="display: inline-block; flex: 1; max-width: 70%; overflow: hidden; white-space: nowrap; text-overflow: ellipsis">' + tagObj.name + '</span>' + EDIT_LIST_BUTTON_ELEM + DELETE_LIST_BUTTON_ELEM + '</li>'
} else {
elementStr = '<li class="' + LIST_SELECTOR.slice(1) + '"><span style="display: inline-block; flex: 1; max-width: 70%; overflow: hidden; white-space: nowrap; text-overflow: ellipsis">' + tagObj.name + '</span>' + EDIT_LIST_BUTTON_ELEM + '</li>'
}
$lists.append(elementStr)
})
$lists.css(getListsCss())
$(LIST_SELECTOR).css(LIST_CSS)
$(DELETE_LIST_BUTTON_SELECTOR).css(DELETE_LIST_BUTTON_CSS)
$(EDIT_LIST_BUTTON_SELECTOR).css(EDIT_LIST_BUTTON_ELEM_CSS)
}
/**
* Creates the editor.
*/
function renderEditor () {
debug('renderEditor')
// Create the editor
$editor = $(EDITOR_ELEM)
$editor.hide()
// Container
$createList.after($editor)
$editor.css(EDITOR_CSS)
// List name
$editor.append(EDITOR_LIST_NAME_ELEM)
$editorListName = $(EDITOR_LIST_NAME_SELECTOR)
$editorListName.css(EDITOR_LIST_NAME_CSS)
$editorListName.before(
'<p style="font-size:13px;font-weight:700;color:#444">List name</p>')
// List tags
$editor.append(EDITOR_LIST_CONTENT_ELEM)
$editorListContent = $(EDITOR_LIST_CONTENT_SELECTOR)
$editorListContent.css(EDITOR_LIST_CONTENT_CSS)
$editorListContent.before(
'<p style="font-size:13px;font-weight:700;color:#444">List tags</p>')
// Validate List
$editor.append(EDITOR_BUTTON_ELEM)
$doneButton = $(EDITOR_BUTTON_SELECTOR)
$doneButton.css(EDITOR_BUTTON_CSS)
}
function showOverlay () {
const overlayWrapper = document.querySelector(
BUTTON_OVERLAY_WRAPPER_SELECTOR)
debug('showOverlay: overlayWrapper=', overlayWrapper)
if (!overlayWrapper) {
debug('showOverlay: [noop] no overlayWrapper')
return
}
overlayWrapper.style['background-color'] = 'rgba(0, 25, 53, 0.85)'
}
function hideOverlay () {
const overlayWrapper = document.querySelector(
BUTTON_OVERLAY_WRAPPER_SELECTOR)
debug('hideOverlay: overlayWrapper=', overlayWrapper)
if (!overlayWrapper) {
debug('hideOverlay: [noop] no overlayWrapper')
return
}
overlayWrapper.style['background-color'] = 'initial'
}
function hideTumblrOverlayButton () {
const element = document.querySelector(BUTTON_OVERLAY_SELECTOR)
debug('hideTumblrOverlayButton: element=', element)
if (!element) {
debug('hideTumblrOverlayButton: [noop] no element')
return
}
element.style.display = 'none'
showOverlay()
}
function showTumblrOverlayButton () {
const element = document.querySelector(BUTTON_OVERLAY_SELECTOR)
debug('showTumblrOverlayButton: element=', element)
if (!element) {
debug('showTumblrOverlayButton: [noop] no element')
return
}
element.style.display = 'block'
hideOverlay()
}
/**
* Renders the editor.
*/
function updateEditorData () {
debug('updateEditorData')
if (!isEditorVisible()) {
showEditor()
}
// Get the list data
var name
var list
if (editingList) {
name = editingList
list = findList(editingList).list
} else {
name = 'Blogging'
list = [
'tag1',
'tag2',
'tag3'
]
}
$editorListName.val(name)
$editorListContent.val(list.join('\n'))
}
/**
* Unbinds the events and remove all the elements from the dom.
*/
function unmount () {
debug('unmount')
// Unbind the events
unbind()
// Cleanup
$createList.remove()
$actions.remove()
$editor.remove()
$editorListName.remove()
$editorListContent.remove()
$doneButton.remove()
$root.remove()
$createList = null
$actions = null
$editor = null
$editorListName = null
$editorListContent = null
$doneButton = null
$root = null
}
/**
* Periodically check if `tumblrTag` should be rendered.
*/
function shouldRender () {
clearTimeout(timerId)
timerId = setTimeout(function () {
var hasTagsInput = !!getTagInput()
debug('shouldRender#check hasTagsInput: ', hasTagsInput)
if (hasTagsInput && !$root) {
debug('shouldRender#render')
render()
} else if (!hasTagsInput && $root) {
debug('shouldRender#unmount')
unmount()
} else if (hasTagsInput && $root) {
debug('shouldRender#updatePosition')
updatePosition()
}
shouldRender()
}, SHOULD_RENDER_CHECK_TIMER_MS)
}
/**
* Sets the store in the `chrome.storage`.
*/
function set () {
sort()
debug('set: store %o', store)
chrome.storage.sync.set(store)
}
/**
* Gets the `chrome.storage`
* @param {Function} callback
*/
function get (callback) {
chrome.storage.sync.get('tags', callback)
}
/**
* Adds the list to the store.
* @param {String} name
* @param {String[]} list
*/
function addList (name, list) {
debug('addList: name %o, list %o', name, list)
store = {
tags: store.tags.concat([
{
name: name,
list: list
}
])
}
}
/**
* Edits the list with the given name.
* @param {String} name
* @param {String[]} list
*/
function editList (name, list) {
debug('editList: name %o, list %o', name, list)
// Remove the old list
store.tags = store.tags.filter(function (obj) {
return obj.name !== editingList
})
// Add the new list
addList(name, list)
}
/**
* Binds events on the element created.
*/
function bind () {
debug('bind')
// Tumblr
$(POST_CONTAINER_SELECTOR).on(
EVENT.DOM_SUBTREE_MODIFIED,
handleDOMSubtreeModified
)
window.addEventListener(EVENT.RESIZE, handleWindowResize)
// Add list
$(CREATE_LIST_SELECTOR).on(EVENT.CLICK, handleClickCreateList)
// List
$(LIST_SELECTOR).on(EVENT.CLICK, handleClickList)
// List item actions
$(DELETE_LIST_BUTTON_SELECTOR).on(EVENT.CLICK, handleClickDeleteList)
$(EDIT_LIST_BUTTON_SELECTOR).on(EVENT.CLICK, handleClickEditList)
// Editor
$doneButton.on(EVENT.CLICK, handleClickDone)
}
/**
* Unbinds evens on the element created.
*/
function unbind () {
debug('unbind')
// Tumblr
$(POST_CONTAINER_SELECTOR).off(
EVENT.DOM_SUBTREE_MODIFIED,
handleDOMSubtreeModified
)
window.removeEventListener(EVENT.RESIZE, handleWindowResize)
// Add/edit list
$(CREATE_LIST_SELECTOR).off(EVENT.CLICK, handleClickCreateList)
// List
$(LIST_SELECTOR).off(EVENT.CLICK, handleClickList)
$(DELETE_LIST_BUTTON_SELECTOR).off(EVENT.CLICK, handleClickDeleteList)
// Editor
if ($doneButton) {
$doneButton.off(EVENT.CLICK, handleClickDone)
}
}
/**
* Callback triggered when a list is clicked.
* @param {Event} event
* @return {Boolean}
*/
function handleClickList (event) {
debug('handleClickList: event ', event)
let index = $(event.currentTarget).index()
if (isEditorVisible()) {
// If the add list selector is open, edit the list
getListAndRender(index)
} else {
// If the add list selector is closed, append the tags
addTagsToPost(index)
}
return false
}
/**
* Sets the `editingList` and renders the editor.
* @param {Number} index
*/
function getListAndRender (index) {
debug('getListAndRender: index', index)
// Get the list being edited
var list = store.tags[index]
editingList = list.name
// Render the list
updateEditorData()
}
/**
* Delete the list.
* @param {String} name
*/
function deleteList (name) {
debug('deleteList: name ', name)
// Delete the list that match the given name
store.tags = store.tags.filter(function (obj) {
return obj.name !== name
})
// Set the list to the storage
set()
// Re-render
render()
}
/**
* Callback triggered when the delete list icon is clicked.
* @param {Event} event
* @return {Boolean}
*/
function handleClickDeleteList (event) {
const currentTarget = event.currentTarget
debug('handleClickDeleteList: event', event)
debug('handleClickDeleteList: currentTarget', currentTarget)
const tagList = currentTarget.closest(LIST_SELECTOR)
if (!tagList) {
debug('handleClickDeleteList: [noop] no tagList')
return
}
event.preventDefault()
deleteList(tagList.querySelector('span').textContent)
return false
}
/**
* Callback triggered when the edit list icon is clicked.
* @param {Event} event
* @return {Boolean}
*/
function handleClickEditList (event) {
debug('handleClickEditList: event', event)
var currentTarget = event.currentTarget.parentElement
var ulElement = event.currentTarget.parentElement.parentElement.children
var listIndex = Array.prototype.indexOf.call(ulElement, currentTarget)
if (listIndex === -1) {
listIndex = 0
}
getListAndRender(listIndex)
event.preventDefault()
return false
}
/**
* Callback triggered when the post container's subtree is modified.
* It adjusts the position of the `tumblrTag` element.
*/
function handleDOMSubtreeModified () {
updatePosition()
}
function handleWindowResize () {
updatePosition()
}
/**
* Callback triggered when the pencil is clicked.
* It shows/hide the editor.
*/
function handleClickCreateList () {
toggleEditor()
}
/**
* Callback triggered when the editor button is clicked.
* It adds/edits the list and set it to the storage.
* @return {Boolean}
*/
function handleClickDone () {
let listName = getUniqueListName($editorListName.val())
let tags = $editorListContent.val().replace(/\r\n/g, '\n').split('\n')
tags = tags.filter(function (tag) { return !!tag })
if (editingList) {
editList(listName, tags)
editingList = null
} else {
addList(listName, tags)
}
set()
render()
return false
}
function simulateReactTextAreaChangeEvent (element, text) {
var nativeInputValueSetter = Object.getOwnPropertyDescriptor(
window.HTMLTextAreaElement.prototype,
'value'
).set
nativeInputValueSetter.call(element, text)
var ev2 = new Event('input', { bubbles: true })
element.dispatchEvent(ev2)
}
function simulateEnterKey (element) {
const ke = new KeyboardEvent('keydown', {
bubbles: true,
cancelable: true,
keyCode: 13
})
element.dispatchEvent(ke)
}
function addTagsOneByOne (tags) {
const tag = tags.shift()
debug('addTagsToPost: tags=', tags)
debug('addTagsToPost: tag=', tag)
if (!tag) {
return
}
const legacyTagInput = getLegacyTagInput()
const legacyTagInputWrapper = getLegacyTagInput()
if (legacyTagInput && legacyTagInputWrapper) {
legacyTagInput.textContent = tag
simulateEnterKey(legacyTagInputWrapper)
} else {
const tagTextArea = getTagInput()
simulateReactTextAreaChangeEvent(tagTextArea, tag)
const suggestionButton = document.querySelector(`${CONTAINER_WRAPPER_SELECTOR} button[aria-label="${tag}"]`)
debug('addTagsToPost: suggestionButton=', suggestionButton)
if (suggestionButton) {
suggestionButton.click()
} else {
simulateEnterKey(tagTextArea)
}
}
setTimeout(() => {
addTagsOneByOne(tags)
}, ADDING_TAG_DELAY)
}
/**
* Appends the tags to the post.
* @param {Number} listIndex
*/
function addTagsToPost (listIndex) {
debug('addTagsToPost: listIndex ', listIndex)
var obj = store.tags[listIndex]
const tags = obj.list.slice()
addTagsOneByOne(tags)
// HACK, validate the tags input
// setTimeout(function () {
// $(POST_CONTAINER_TAGS_INPUT_WRAPPER_SELECTOR_LEGACY).text('')
// }, 0)
}
/**
* Adjusts the position of the `tumblrTag` element.
*/
function updatePosition () {
var $settingsIcon = $(getSettingsIcon())
if (!$settingsIcon) {
debug('updatePosition: [noop] no settings icon')
return
}
var left = $settingsIcon.offset().left + 200
var top = $settingsIcon.offset().top + (
$lists.height() * 1.5
)
$root.css('top', top.toString() + 'px')
$root.css('left', left.toString() + 'px')
$lists.css(getListsCss())
}
/**
* Toggles the editor, creates it if necessary.
*/
function toggleEditor () {
// If the editor is visible, hide it
if (isEditorVisible()) {
hideEditor(updatePosition)
return
}
// If the editor doesn't exist, create it
if (!$editor) {
renderEditor()
}
// If the editor is hidden, show it
updateEditorData()
showEditor(updatePosition)
}
/**
* Hides the editor.
*/
function hideEditor (callback) {
$editor.hide('medium', callback)
showTumblrOverlayButton()
}
/**
* Shows the editor.
*/
function showEditor (callback) {
$editor.show('medium', callback)
hideTumblrOverlayButton()
}
/**
* Checks whether or not the editor is visible.
* @return {Boolean}
*/
function isEditorVisible () {
debug('isEditorVisible: ', $editor.css('display'))
return $editor && $editor.css('display') === 'block'
}
/**
* Gets an unique list name.
* @param {String} listName
*/
function getUniqueListName (listName) {
debug(
'getUniqueListName: listName %o, editingList %o',
listName,
editingList
)
// The list is being edited, noop
if (editingList) {
return listName
}
var loop = 5
existingName = findList(listName)
while (existingName && loop) {
listName = listName + '1'
existingName = findList(listName)
loop--
}
return listName
}
/**
* Logs the `arguments` if the `isDebugging` flag is on.
*/
var log = console.log.bind(null, '[tumblrTag]')
function debug () {
// If it's not in debug mode, noop
if (!isDebugging) {
return
}
// If it's debug mode, log it
log.apply(null, arguments)
}
/**
* Sorts the `store.tags` by alphabetical order.
*/
function sort () {
debug('sort: %o', store)
store.tags = store.tags.sort(function (obj1, obj2) {
return obj1.name.toLowerCase().localeCompare(obj2.name.toLowerCase())
})
}
/**
* Finds the list with the given `name`.
* @param {String} name
* @return {Object}
*/
function findList (name) {
debug('findList: %o', name)
return store.tags.find(function (obj) {
return obj.name === name
})
}
// Init
try {
init()
} catch (error) {
debug('[error]: ', error)
}
})