Created
June 4, 2025 13:38
-
-
Save saifsultanc/3aa0de0f43722f52305f6e3853e248b7 to your computer and use it in GitHub Desktop.
gc-openai/gcoai-stream-to-paragraph-field.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Gravity Connect // OpenAI // Stream to Paragraph Field (Multi-field Support) | |
* https://gravitywiz.com/documentation/gravity-connect-openai/ | |
* | |
* Stream OpenAI responses from multiple Stream fields to Paragraph or Single Line Text fields. | |
* Users can edit the final result. | |
*/ | |
const streamFieldMappings = [ | |
{ | |
streamFieldId: 3, // Stream Field | |
promptFieldId: 1, // Prompt Field (optional) | |
responseFieldId: 4 // Paragraph/Text Field where content should go | |
}, | |
{ | |
streamFieldId: 6, | |
promptFieldId: 5, | |
responseFieldId: 7 | |
}, | |
// Add more mappings here... | |
]; | |
// Helper to initialize each mapping | |
streamFieldMappings.forEach(({ streamFieldId, promptFieldId, responseFieldId }) => { | |
const $streamFieldInput = $(`#input_GFFORMID_${streamFieldId}`); | |
const $streamButton = $streamFieldInput.closest('.gfield').find('.gcoai-trigger'); | |
// When the stream field changes (OpenAI output comes in) | |
$streamFieldInput.on('change', function () { | |
const $responseInput = $(`#input_GFFORMID_${responseFieldId}`); | |
const value = this.value; | |
const tiny = window.tinyMCE && tinyMCE.get($responseInput.attr('id')); | |
if (tiny) { | |
const html = $streamFieldInput.closest('.gfield').find('.gcoai-output').html(); | |
tiny.setContent(html); | |
} else { | |
$responseInput.val(value); | |
} | |
}); | |
// Add a secondary button that re-triggers OpenAI generation | |
const $newButton = $streamButton | |
.clone() | |
.attr('style', 'margin-top: var(--gf-label-space-primary, 8px);') | |
.on('click', function () { | |
$streamButton.trigger('click'); | |
}) | |
.insertAfter($(`#input_GFFORMID_${responseFieldId}`)); | |
const $wpEditor = $newButton.parents('.wp-editor-container'); | |
if ($wpEditor.length) { | |
$newButton.insertAfter($wpEditor); | |
} | |
// Optional: auto-trigger generation on prompt field blur | |
$(`#input_GFFORMID_${promptFieldId}`).on('blur', function () { | |
$streamButton.trigger('click'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment