|
5 | 5 | requireConfirmation: false, |
6 | 6 | }; |
7 | 7 |
|
| 8 | + // helper function to get the innerText of all elements matching a selector |
| 9 | + const getAllInnerText = (selector) => Array.from(document.querySelectorAll(selector)) |
| 10 | + .map((el) => el.innerText) |
| 11 | + .filter((text) => text.trim() !== ""); |
| 12 | + |
8 | 13 | // On image page, get data about gallery (image's position within gallery, next/prev image IDs), |
9 | 14 | // add arrow buttons to page, and register arrow keypress handlers, |
10 | 15 | async function setupTagCopyPaste(objType) { |
|
24 | 29 | } |
25 | 30 |
|
26 | 31 | function insertCopyPasteButtons(objID, objType) { |
| 32 | + // listen for copy and paste events within tag input box |
| 33 | + // find tag input box |
| 34 | + const tagInputBox = document.querySelector("label[for='tag_ids'] + div .react-select__value-container"); |
| 35 | + if (tagInputBox) { |
| 36 | + tagInputBox.addEventListener("copy", (event) => { |
| 37 | + event.preventDefault(); |
| 38 | + handleCopyClick(); |
| 39 | + }); |
| 40 | + tagInputBox.addEventListener("paste", (event) => { |
| 41 | + event.preventDefault(); |
| 42 | + handlePasteClick(objID, objType); |
| 43 | + }); |
| 44 | + } |
| 45 | + |
27 | 46 | var copyButton = document.createElement("button"); |
28 | 47 | copyButton.className = "imageGalleryNav-copyButton btn btn-secondary"; |
29 | 48 | copyButton.innerText = "Copy"; |
30 | | - copyButton.addEventListener("click", (e) => { |
31 | | - e.preventDefault(); |
| 49 | + copyButton.onclick = (event) => { |
| 50 | + event.preventDefault(); |
32 | 51 | handleCopyClick(); |
33 | | - }); |
| 52 | + } |
34 | 53 |
|
35 | 54 | var pasteButton = document.createElement("button"); |
36 | 55 | pasteButton.className = "imageGalleryNav-pasteButton btn btn-secondary"; |
37 | 56 | pasteButton.innerText = "Paste"; |
38 | | - pasteButton.addEventListener("click", (e) => { |
39 | | - e.preventDefault(); |
| 57 | + pasteButton.onclick = (event) => { |
| 58 | + event.preventDefault(); |
40 | 59 | handlePasteClick(objID, objType); |
41 | | - }); |
| 60 | + } |
42 | 61 |
|
43 | 62 | if (document.querySelector("button.imageGalleryNav-pasteButton") == null) { |
44 | 63 | document.querySelector("label[for='tag_ids']").append(pasteButton); |
|
50 | 69 |
|
51 | 70 | // Handle copy click. Return delimited list of current tags. |
52 | 71 | async function handleCopyClick() { |
53 | | - // Get tags from input box. |
54 | | - var tagList = []; |
55 | | - document |
56 | | - .querySelectorAll( |
57 | | - "label[for='tag_ids'] + div .react-select__multi-value__label" |
58 | | - ) |
59 | | - .forEach((item) => { |
60 | | - tagList.push(item.innerText); |
61 | | - }); |
62 | | - |
63 | | - // Join tags as comma delimited list and write to clipboard. |
64 | | - await navigator.clipboard.writeText(tagList.join(",")); |
| 72 | + // Get tags from input box |
| 73 | + // join as comma delimited list |
| 74 | + const tagList = getAllInnerText("label[for='tag_ids'] + div .react-select__multi-value__label").join(",") |
| 75 | + // write to clipboard. |
| 76 | + navigator.clipboard.writeText(tagList); |
65 | 77 | } |
66 | 78 |
|
67 | 79 | // Handle paste click. |
68 | 80 | async function handlePasteClick(objID, objType) { |
69 | | - var inputTagList = []; |
70 | | - |
71 | 81 | // Parse tag list from comma delimited string. |
72 | | - var tagInput = await navigator.clipboard.readText(); |
73 | | - tagInput.split(",").forEach((item) => { |
74 | | - if (!inputTagList.includes(item)) { |
75 | | - inputTagList.push(item); |
76 | | - } |
77 | | - }); |
| 82 | + const tagInput = await navigator.clipboard.readText(); |
| 83 | + var inputTagList = tagInput.split(",") // do de-duplication later |
78 | 84 |
|
79 | 85 | // Get tags from input box and also add to tag list. |
80 | | - document |
81 | | - .querySelectorAll( |
82 | | - "label[for='tag_ids'] + div .react-select__multi-value__label" |
83 | | - ) |
84 | | - .forEach((item) => { |
85 | | - if (!inputTagList.includes(item)) { |
86 | | - inputTagList.push(item.innerText); |
87 | | - } |
88 | | - }); |
| 86 | + const existingTagList = getAllInnerText("label[for='tag_ids'] + div .react-select__multi-value__label") |
89 | 87 |
|
90 | | - inputTagList.sort(); |
| 88 | + inputTagList = [...new Set([...inputTagList, ...existingTagList])].sort(); |
91 | 89 |
|
92 | 90 | var missingTags = []; |
93 | 91 | var existingTags = []; |
94 | 92 | var tagUpdateList = []; |
95 | 93 |
|
96 | 94 | // Search for tag ID for each tag. If exists, add to tag ID list. If not exists, create new tag and add to tag ID list. |
97 | | - for (let i = 0; i < inputTagList.length; i++) { |
98 | | - var inputTag = inputTagList[i]; |
99 | | - var tagID = await getTagByName(inputTag); |
100 | | - if (tagID != null && tagID.length) { |
| 95 | + for (const inputTag of inputTagList) { |
| 96 | + const tagID = await getTagByName(inputTag.trim()); |
| 97 | + if (tagID && tagID.length) { |
101 | 98 | existingTags.push(inputTag); |
102 | 99 | tagUpdateList.push(tagID[0]); |
103 | 100 | } else { |
104 | 101 | missingTags.push(inputTag); |
105 | 102 | } |
106 | 103 | } |
107 | 104 |
|
| 105 | + |
108 | 106 | if (pluginSettings.requireConfirmation) { |
109 | | - var msg = ""; |
110 | | - if (pluginSettings.createIfNotExists) { |
111 | | - msg = `Missing Tags that will be created:\n${missingTags.join( |
112 | | - ", " |
113 | | - )}\n\nExisting Tags that will be saved: \n${existingTags.join( |
114 | | - ", " |
115 | | - )}\n\nContinue?`; |
116 | | - } else { |
117 | | - msg = `Missing Tags that will be skipped:\n${missingTags.join( |
118 | | - ", " |
119 | | - )}\n\nExisting Tags that will be saved: \n${existingTags.join( |
120 | | - ", " |
121 | | - )}\n\nContinue?`; |
122 | | - } |
123 | 107 |
|
124 | | - var userConfirmed = confirm(msg); |
125 | | - if (!userConfirmed) { |
| 108 | + const missingTagsStr = missingTags.join(", "); |
| 109 | + const existingTagsStr = existingTags.join(", "); |
| 110 | + const msg = pluginSettings.createIfNotExists |
| 111 | + ? `Missing Tags that will be created:\n${missingTagsStr}\n\nExisting Tags that will be saved: \n${existingTagsStr}\n\nContinue?` |
| 112 | + : `Missing Tags that will be skipped:\n${missingTagsStr}\n\nExisting Tags that will be saved: \n${existingTagsStr}\n\nContinue?`; |
| 113 | + |
| 114 | + if (!confirm(msg)) { |
126 | 115 | return; |
127 | 116 | } |
128 | 117 | } |
129 | 118 |
|
130 | 119 | if (pluginSettings.createIfNotExists && missingTags.length) { |
131 | | - for (let i = 0; i < missingTags.length; i++) { |
132 | | - var newTagName = missingTags[i]; |
133 | | - var newTagID = await createNewTag(newTagName); |
134 | | - if (newTagID != null) { |
135 | | - tagUpdateList.push(newTagID); |
136 | | - } |
| 120 | + for (const missingTag of missingTags) { |
| 121 | + const newTagID = await createNewTag(missingTag); |
| 122 | + if (newTagID != null) tagUpdateList.push(newTagID); |
137 | 123 | } |
138 | 124 | } |
139 | 125 |
|
140 | 126 | // Update tags on object with new tag ID list. |
141 | 127 | await updateObjTags( |
142 | 128 | objID, |
143 | 129 | tagUpdateList, |
144 | | - objType.toLowerCase() + "Update", |
145 | | - objType + "UpdateInput" |
| 130 | + `${objType.toLowerCase()}Update`, |
| 131 | + `${objType}UpdateInput` |
146 | 132 | ); |
147 | 133 |
|
148 | 134 | window.location.reload(); |
|
183 | 169 | .then((data) => data.findTags.tags.map((item) => item.id)); |
184 | 170 | } |
185 | 171 |
|
186 | | - // Wait for scenes page. |
187 | | - csLib.PathElementListener("/scenes/", "[id*='-edit-details']", () => { |
188 | | - setupTagCopyPaste("Scene"); |
189 | | - }); // PathElementListener is from cs-ui-lib.js |
190 | | - |
191 | | - // Wait for studios page. |
192 | | - csLib.PathElementListener("/studios/", "[id='studio-edit']", () => { |
193 | | - setupTagCopyPaste("Studio"); |
194 | | - }); // PathElementListener is from cs-ui-lib.js |
195 | | - |
196 | | - // Wait for groups page. |
197 | | - csLib.PathElementListener("/groups/", "[id='group-edit']", () => { |
198 | | - setupTagCopyPaste("Group"); |
199 | | - }); // PathElementListener is from cs-ui-lib.js |
200 | | - |
201 | | - // Wait for performers page. |
202 | | - csLib.PathElementListener("/performers/", "[id='performer-edit']", () => { |
203 | | - setupTagCopyPaste("Performer"); |
204 | | - }); // PathElementListener is from cs-ui-lib.js |
205 | | - |
206 | | - // Wait for galleries page. |
207 | | - csLib.PathElementListener("/galleries/", "[id*='-edit-details']", () => { |
208 | | - setupTagCopyPaste("Gallery"); |
209 | | - }); // PathElementListener is from cs-ui-lib.js |
210 | | - |
211 | | - // Wait for images page. |
212 | | - csLib.PathElementListener("/images/", "[id*='-edit-details']", () => { |
213 | | - setupTagCopyPaste("Image"); |
214 | | - }); // PathElementListener is from cs-ui-lib.js |
| 172 | + // listener arrays |
| 173 | + [ |
| 174 | + [ "/scenes/", "[id*='-edit-details']", "Scene" ], |
| 175 | + [ "/studios/", "[id='studio-edit']", "Studio" ], |
| 176 | + [ "/groups/", "[id='group-edit']", "Group" ], |
| 177 | + [ "/performers/", "[id='performer-edit']", "Performer" ], |
| 178 | + [ "/galleries/", "[id*='-edit-details']", "Gallery" ], |
| 179 | + [ "/images/", "[id*='-edit-details']", "Image" ] |
| 180 | + ].forEach(([path, selector, objType]) => { |
| 181 | + // Wait for the page to load and the element to be present. |
| 182 | + csLib.PathElementListener(path, selector, () => { |
| 183 | + setupTagCopyPaste(objType); |
| 184 | + }); // PathElementListener is from cs-ui-lib.js |
| 185 | + }); |
215 | 186 | })(); |
0 commit comments