Skip to content

Commit 2d3bc4c

Browse files
committed
Neo compatability
1 parent 53df23d commit 2d3bc4c

File tree

2 files changed

+55
-31
lines changed

2 files changed

+55
-31
lines changed

src/templates/form.twig

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,26 @@
2121
{% set currentLang = "" %}
2222

2323
{% if craft.app.getPlugins.getPlugin('chatgpt-integration').settings.usePageLang %}
24-
{% for site in craft.app.sites.getAllSites () %}
24+
{% for site in craft.app.sites.getAllSites() %}
2525
{% if site.handle == craft.app.request.getQueryParam('site') %}
2626
{% set currentLang = 'Using the locale ' ~ site.language %}
2727
{% endif %}
2828
{% endfor %}
29+
{% if currentLang|length == 0 %}
30+
{% set currentLang = 'Using the locale ' ~ craft.app.sites.getAllSites()[0].language %}
31+
{% endif %}
2932
{% endif %}
3033

3134
{% if craft.app.getPlugins.getPlugin('chatgpt-integration').settings.getAccessToken|length %}
32-
{% if (" type=\"text\"" in input or "textarea" in input) and "neo-input" not in input %}
35+
{% if (" type=\"text\"" in input or "textarea" in input) and "neo-input" not in input and "matrix-field" not in input %}
36+
{{ input }}
3337
{% set prompts = craft.chatgptIntegration.getPrompts(true) %}
3438
<div class="chatgpt-button">
3539
<button {{ attr(actionBtnAttributes) }}>ChatGPT</button>
36-
<div id="1" class="menu menu--disclosure">
40+
<div id="1" class="menu menu--disclosure chatgpt-menu">
3741
<ul class="padded">
3842
{% for item in prompts %}
39-
<li><a href="#" class="doAi" type="button" role="button" data-hash="{{ hash }}" data-prompt="{{ currentLang }} {{ item.promptTemplate }}: " aria-label="{{ item.label }}">{{ item.label }}</a></li>
43+
<li><a href="javascript:void(0)" class="doAi" type="button" role="button" data-hash="{{ hash }}" data-prompt="{{ currentLang }} {{ item.promptTemplate }}: " aria-label="{{ item.label }}">{{ item.label }}</a></li>
4044
{% endfor %}
4145
</ul>
4246
<hr>

src/templates/functions.twig

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,53 @@
11
{% js %}
22

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() {
124

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+
});
1416

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+
});
2828
}
2929

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+
3251
});
3352

3453
/*$('.open-modal').click(function(e) {
@@ -51,6 +70,7 @@ function sendRequest(prompt, query, textField, hash) {
5170
$.ajax({
5271
type: "POST",
5372
url: "https://api.openai.com/v1/chat/completions",
73+
//url: "https://api.openai.com/v1/edits",
5474
beforeSend: function (xhr) {
5575
xhr.setRequestHeader("Authorization", "Bearer {{ craft.app.getPlugins.getPlugin('chatgpt-integration').settings.getAccessToken }}");
5676
},
@@ -64,7 +84,7 @@ function sendRequest(prompt, query, textField, hash) {
6484
"presence_penalty": 0
6585
}),
6686
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') {
6888
let textareaId = textField.attr('id');
6989
$R('#' + textareaId, 'source.setCode', data.choices[0].message.content);
7090
} else {
@@ -77,7 +97,7 @@ function sendRequest(prompt, query, textField, hash) {
7797

7898
}).fail(function(data) {
7999
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') {
81101
let textareaId = textField.attr('id');
82102
$R('#' + textareaId, 'source.setCode', query);
83103
} else {

0 commit comments

Comments
 (0)