Skip to content

Commit d9a46b8

Browse files
[tagCopyPaste] Fix issue where copy/paste handler could be attached multiple times. (#636)
1 parent a9d2d3f commit d9a46b8

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

plugins/tagCopyPaste/tagCopyPaste.js

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,52 @@
55
requireConfirmation: false,
66
};
77

8+
var objID = null;
9+
var objType = null;
10+
811
// helper function to get the innerText of all elements matching a selector
912
const getAllInnerText = (selector) => Array.from(document.querySelectorAll(selector))
1013
.map((el) => el.innerText.trim())
1114
.filter((text) => text !== "");
1215

1316
// On image page, get data about gallery (image's position within gallery, next/prev image IDs),
1417
// add arrow buttons to page, and register arrow keypress handlers,
15-
async function setupTagCopyPaste(objType) {
18+
async function setupTagCopyPaste(objTypeTriggered) {
1619
// Get plugin settings.
1720
const configSettings = await csLib.getConfiguration("tagCopyPaste", {}); // getConfiguration is from cs-ui-lib.js
1821
pluginSettings = {
1922
...defaultPluginSettings,
2023
...configSettings,
2124
};
2225

23-
var objID = window.location.pathname.split("/")[2];
26+
objID = window.location.pathname.split("/")[2];
27+
objType = objTypeTriggered;
2428

2529
// Add UI elements.
2630
if (objID !== "new") {
27-
insertCopyPasteButtons(objID, objType);
31+
insertCopyPasteButtons();
2832
}
2933
}
3034

31-
function insertCopyPasteButtons(objID, objType) {
35+
function copyEventHandler(event) {
36+
event.preventDefault();
37+
handleCopyClick();
38+
}
39+
40+
function pasteEventHandler(event) {
41+
event.preventDefault();
42+
handlePasteClick();
43+
}
44+
45+
function insertCopyPasteButtons() {
3246
// listen for copy and paste events within tag input box
3347
// find tag input box
3448
const tagInputBox = document.querySelector("label[for='tag_ids'] + div .react-select__value-container");
3549
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-
});
50+
tagInputBox.removeEventListener("copy", copyEventHandler);
51+
tagInputBox.removeEventListener("paste", pasteEventHandler);
52+
tagInputBox.addEventListener("copy", copyEventHandler);
53+
tagInputBox.addEventListener("paste", pasteEventHandler);
4454
}
4555

4656
var copyButton = document.createElement("button");
@@ -56,7 +66,7 @@
5666
pasteButton.innerText = "Paste";
5767
pasteButton.onclick = (event) => {
5868
event.preventDefault();
59-
handlePasteClick(objID, objType);
69+
handlePasteClick();
6070
}
6171

6272
if (document.querySelector("button.imageGalleryNav-pasteButton") == null) {
@@ -77,7 +87,7 @@
7787
}
7888

7989
// Handle paste click.
80-
async function handlePasteClick(objID, objType) {
90+
async function handlePasteClick() {
8191
// Parse tag list from comma delimited string.
8292
const tagInput = await navigator.clipboard.readText();
8393
var inputTagList = tagInput.split(/\r?\n|\r|,/).map(s => s.trim()).filter((text) => text !== "") // do de-duplication later
@@ -123,7 +133,6 @@
123133

124134
// Update tags on object with new tag ID list.
125135
await updateObjTags(
126-
objID,
127136
tagUpdateList,
128137
`${objType.toLowerCase()}Update`,
129138
`${objType}UpdateInput`
@@ -135,7 +144,7 @@
135144
// *** GQL Calls ***
136145

137146
// Update Object by ID, new tags list, and GQL mutation name.
138-
async function updateObjTags(objID, tags, fnName, inputName) {
147+
async function updateObjTags(tags, fnName, inputName) {
139148
const variables = { input: { id: objID, tag_ids: tags } };
140149
const query = `mutation UpdateObj($input:${inputName}!) { ${fnName}(input: $input) {id} }`;
141150
return await csLib.callGQL({ query, variables });
@@ -173,10 +182,10 @@
173182
[ "/performers/", "[id='performer-edit']", "Performer" ],
174183
[ "/galleries/", "[id*='-edit-details']", "Gallery" ],
175184
[ "/images/", "[id*='-edit-details']", "Image" ]
176-
].forEach(([path, selector, objType]) => {
185+
].forEach(([path, selector, objTypeTriggered]) => {
177186
// Wait for the page to load and the element to be present.
178187
csLib.PathElementListener(path, selector, () => {
179-
setupTagCopyPaste(objType);
188+
setupTagCopyPaste(objTypeTriggered);
180189
}); // PathElementListener is from cs-ui-lib.js
181190
});
182191
})();

plugins/tagCopyPaste/tagCopyPaste.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: tagCopyPaste
22
# requires: CommunityScriptsUILibrary
33
description: Adds Copy/Paste buttons to Tags field.
4-
version: 0.3
4+
version: 0.4
55
settings:
66
createIfNotExists:
77
displayName: Create If Not Exists

0 commit comments

Comments
 (0)