_textAreas.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Utility functions to select text areas the script should work on,
  2. // including third party options.
  3. // Supported third party options so far:
  4. // - Dataset Tag Editor
  5. // Core text area selectors
  6. const core = [
  7. "#txt2img_prompt > label > textarea",
  8. "#img2img_prompt > label > textarea",
  9. "#txt2img_neg_prompt > label > textarea",
  10. "#img2img_neg_prompt > label > textarea"
  11. ];
  12. // Third party text area selectors
  13. const thirdParty = {
  14. "dataset-tag-editor": {
  15. "base": "#tab_dataset_tag_editor_interface",
  16. "hasIds": false,
  17. "selectors": [
  18. "Caption of Selected Image",
  19. "Interrogate Result",
  20. "Edit Caption",
  21. "Edit Tags"
  22. ]
  23. }
  24. }
  25. function getTextAreas() {
  26. // First get all core text areas
  27. let textAreas = [...gradioApp().querySelectorAll(core.join(", "))];
  28. for (const [key, entry] of Object.entries(thirdParty)) {
  29. if (entry.hasIds) { // If the entry has proper ids, we can just select them
  30. textAreas = textAreas.concat([...gradioApp().querySelectorAll(entry.selectors.join(", "))]);
  31. } else { // Otherwise, we have to find the text areas by their adjacent labels
  32. let base = gradioApp().querySelector(entry.base);
  33. // Safety check
  34. if (!base) continue;
  35. let allTextAreas = [...base.querySelectorAll("textarea")];
  36. // Filter the text areas where the adjacent label matches one of the selectors
  37. let matchingTextAreas = allTextAreas.filter(ta => [...ta.parentElement.childNodes].some(x => entry.selectors.includes(x.innerText)));
  38. textAreas = textAreas.concat(matchingTextAreas);
  39. }
  40. };
  41. return textAreas;
  42. }
  43. const thirdPartyIdSet = new Set();
  44. // Get the identifier for the text area to differentiate between positive and negative
  45. function getTextAreaIdentifier(textArea) {
  46. let txt2img_p = gradioApp().querySelector('#txt2img_prompt > label > textarea');
  47. let txt2img_n = gradioApp().querySelector('#txt2img_neg_prompt > label > textarea');
  48. let img2img_p = gradioApp().querySelector('#img2img_prompt > label > textarea');
  49. let img2img_n = gradioApp().querySelector('#img2img_neg_prompt > label > textarea');
  50. let modifier = "";
  51. switch (textArea) {
  52. case txt2img_p:
  53. modifier = ".txt2img.p";
  54. break;
  55. case txt2img_n:
  56. modifier = ".txt2img.n";
  57. break;
  58. case img2img_p:
  59. modifier = ".img2img.p";
  60. break;
  61. case img2img_n:
  62. modifier = ".img2img.n";
  63. break;
  64. default:
  65. // If the text area is not a core text area, it must be a third party text area
  66. // Add it to the set of third party text areas and get its index as a unique identifier
  67. if (!thirdPartyIdSet.has(textArea))
  68. thirdPartyIdSet.add(textArea);
  69. modifier = `.thirdParty.ta${[...thirdPartyIdSet].indexOf(textArea)}`;
  70. break;
  71. }
  72. return modifier;
  73. }