File size: 2,195 Bytes
03d9a71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { memo, useCallback, useState } from 'react';
import { useTranslation } from 'react-i18next';

import TagList, { PromptType, TagItem } from './TagList';
import { useStyles } from './style';
import { formatPrompt } from './utils';

interface PromptProps {
  type: PromptType;
}

const Prompt = memo<PromptProps>(({ type }) => {
  const [tags, setTags] = useState<TagItem[]>([]);
  const { styles } = useStyles();
  const { t } = useTranslation();

  const id =
    type === 'positive' ? "[id$='2img_prompt'] textarea" : "[id$='2img_neg_prompt'] textarea";

  const getValue = useCallback(() => {
    try {
      const textarea = get_uiCurrentTabContent().querySelector(id) as HTMLTextAreaElement;
      if (textarea) setTags(formatPrompt(textarea.value));
    } catch (error) {
      console.debug(error);
    }
  }, []);

  const setValue = useCallback(() => {
    try {
      const newValue = tags.map((t) => t.text).join(', ');
      const textarea = get_uiCurrentTabContent().querySelector(id) as HTMLTextAreaElement;
      if (textarea) textarea.value = newValue;
      updateInput(textarea);
    } catch (error) {
      console.debug(error);
    }
  }, [tags, type]);

  const setCurrentValue = useCallback((currentTags: TagItem[]) => {
    try {
      const textarea = get_uiCurrentTabContent().querySelector(id) as HTMLTextAreaElement;
      if (textarea) textarea.value = currentTags.map((t) => t.text).join(', ');
      updateInput(textarea);
    } catch (error) {
      console.debug(error);
    }
  }, []);

  return (
    <div className={styles.promptView}>
      <TagList setTags={setTags} setValue={setCurrentValue} tags={tags} type={type} />
      <div className={styles.buttonGroup}>
        <button
          className="lg secondary gradio-button tool svelte-1ipelgc"
          onClick={getValue}
          title={t('loadPrompt')}
          type="button"
        >
          🔄
        </button>
        <button
          className="lg secondary gradio-button tool svelte-1ipelgc"
          onClick={setValue}
          title={t('setPrompt')}
          type="button"
        >
          ➡️
        </button>
      </div>
    </div>
  );
});

export default Prompt;