1
1
{% js %}
2
2
3
- $('.chatgpt-button').each(function () {
4
- let that = $(this);
5
- let input = that.parents('.field').find('.input input, .input textarea');
6
- if (that.parents('.field').attr('data-type') == 'craft\\redactor\\Field') {
7
- let textareaId = input.attr('id');
8
- $('#textareaId').css('padding-right', '100px');
9
- } else {
10
- input.css('padding-right', '100px');
11
- }
3
+ $(document).ready(function() {
12
4
13
- });
5
+ $('.chatgpt-button').each(function () {
6
+ let that = $(this);
7
+ //let hash = $(this).attr('data-hash');
8
+ let input = that.parents('.field').find('.input input, .input textarea');
9
+ if (that.parents('.field').attr('data-type') == 'craft\\redactor\\Field') {
10
+ let textareaId = input.attr('id');
11
+ $('#textareaId').css('padding-right', '100px');
12
+ } else {
13
+ input.css('padding-right', '100px');
14
+ }
15
+ });
14
16
15
- $('.doAi').click(function(e) {
16
- e.preventDefault();
17
- let that = $(this);
18
- let prompt = that.attr('data-prompt');
19
- let input = $('button[data-hash="' + that.attr('data-hash') + '"]').parents('.field').find('.input input, .input textarea');
20
- let query = input.val();
21
- let hash = that.attr('data-hash');
22
-
23
- if ($('button[data-hash="' + that.attr('data-hash') + '"]').parents('.field').attr('data-type') == 'craft\\redactor\\Field') {
24
- let textareaId = input.attr('id');
25
- $R('#' + textareaId, 'source.setCode', 'Working ...');
26
- } else {
27
- input.val('Working ...');
17
+ function setPadding() {
18
+ $('.chatgpt-button').each(function () {
19
+ let that = $(this);
20
+ let input = that.parents('.field').find('.input input, .input textarea');
21
+ if (that.parents('.field').attr('data-type') == 'craft\\redactor\\Field') {
22
+ let textareaId = input.attr('id');
23
+ $('#textareaId').css('padding-right', '100px');
24
+ } else {
25
+ input.css('padding-right', '100px');
26
+ }
27
+ });
28
28
}
29
29
30
- sendRequest(prompt, query, input, hash);
31
- //$('.chatgpt-button button').attr('aria-expanded', false);
30
+ $('body').on('click', '.chatgpt-menu a', function(clickEvent) {
31
+ clickEvent.stopPropagation();
32
+ clickEvent.preventDefault();
33
+ let that = $(this);
34
+ let prompt = that.attr('data-prompt');
35
+ let input = $('button[data-hash="' + that.attr('data-hash') + '"]').closest('.input').find('input, textarea').first();
36
+ //$('.field[data-layout-element="' + that.attr('data-hash') + '"] .input').find('input, textarea');
37
+ let query = input.val();
38
+ let hash = that.attr('data-hash');
39
+
40
+ if ($('button[data-hash="' + that.attr('data-hash') + '"]').closest('.field').attr('data-type') == 'craft\\redactor\\Field') {
41
+ let textareaId = input.attr('id');
42
+ $R('#' + textareaId, 'source.setCode', 'Working ...');
43
+ } else {
44
+ input.val('Working ...');
45
+ }
46
+
47
+ sendRequest(prompt, query, input, hash);
48
+
49
+ });
50
+
32
51
});
33
52
34
53
/*$('.open-modal').click(function(e) {
@@ -51,6 +70,7 @@ function sendRequest(prompt, query, textField, hash) {
51
70
$.ajax({
52
71
type: "POST",
53
72
url: "https://api.openai.com/v1/chat/completions",
73
+ //url: "https://api.openai.com/v1/edits",
54
74
beforeSend: function (xhr) {
55
75
xhr.setRequestHeader("Authorization", "Bearer {{ craft .app .getPlugins .getPlugin (' chatgpt-integration' ).settings.getAccessToken }}");
56
76
},
@@ -64,7 +84,7 @@ function sendRequest(prompt, query, textField, hash) {
64
84
"presence_penalty": 0
65
85
}),
66
86
success: function(data) {
67
- if ($('button[data-hash="' + hash + '"]').parents ('.field').attr('data-type') == 'craft\\redactor\\Field') {
87
+ if ($('button[data-hash="' + hash + '"]').closest ('.field').attr('data-type') == 'craft\\redactor\\Field') {
68
88
let textareaId = textField.attr('id');
69
89
$R('#' + textareaId, 'source.setCode', data.choices[0].message.content);
70
90
} else {
@@ -77,7 +97,7 @@ function sendRequest(prompt, query, textField, hash) {
77
97
78
98
}).fail(function(data) {
79
99
alert(data.responseJSON.error.message);
80
- if ($('button[data-hash="' + hash + '"]').parents ('.field').attr('data-type') == 'craft\\redactor\\Field') {
100
+ if ($('button[data-hash="' + hash + '"]').closest ('.field').attr('data-type') == 'craft\\redactor\\Field') {
81
101
let textareaId = textField.attr('id');
82
102
$R('#' + textareaId, 'source.setCode', query);
83
103
} else {
0 commit comments