USA

Here is a full Yii2 widget that encapsulates the functionality of copy, regenerate, and edit controls similar to ChatGPT’s UI. This widget allows you to integrate it anywhere in your Yii2 app.


Step 1: Create the Widget Class

Create a new file at widgets/ChatResponseWidget.php:

php

<?php

namespace app\widgets;

use Yii;
use yii\base\Widget;
use yii\helpers\Html;
use yii\helpers\Url;

class ChatResponseWidget extends Widget
{
public $initialText = “Click Regenerate to get a response…”;
public $generateUrl = [‘/chat/generate’]; // URL for AJAX regeneration

public function run()
{
$this->registerAssets();
return Html::tag(‘div’,
Html::tag(‘pre’, $this->initialText, [‘id’ => ‘chat-output’, ‘class’ => ‘whitespace-pre-wrap p-4 bg-gray-100 rounded-lg shadow-md’]) .
Html::tag(‘div’,
Html::button(‘Copy’, [‘id’ => ‘copyBtn’, ‘class’ => ‘px-3 py-1 bg-blue-500 text-white rounded hover:bg-blue-600’]) .
Html::button(‘Regenerate’, [‘id’ => ‘regenerateBtn’, ‘class’ => ‘px-3 py-1 bg-gray-500 text-white rounded hover:bg-gray-600 ml-2’]) .
Html::button(‘Edit’, [‘id’ => ‘editBtn’, ‘class’ => ‘px-3 py-1 bg-yellow-500 text-white rounded hover:bg-yellow-600 ml-2’]),
[‘class’ => ‘flex gap-2 mt-2’]
),
[‘class’ => ‘relative’]
);
}

protected function registerAssets()
{
$generateUrl = Url::to($this->generateUrl);

$js = <<<JS
document.getElementById(‘copyBtn’).addEventListener(‘click’, function() {
navigator.clipboard.writeText(document.getElementById(‘chat-output’).textContent);
alert(“Copied to clipboard!”);
});

document.getElementById(‘regenerateBtn’).addEventListener(‘click’, function() {
fetch(‘$generateUrl‘)
.then(response => response.json())
.then(data => document.getElementById(‘chat-output’).textContent = data.response)
.catch(error => console.error(“Error:”, error));
});

document.getElementById(‘editBtn’).addEventListener(‘click’, function() {
let newText = prompt(“Edit response:”, document.getElementById(‘chat-output’).textContent);
if (newText !== null) document.getElementById(‘chat-output’).textContent = newText;
});
JS;

$this->view->registerJs($js);
}
}


Step 2: Create a Yii2 Controller for Generating Responses

Create a new file at controllers/ChatController.php:

php

<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;
use yii\web\Response;

class ChatController extends Controller
{
public function actionGenerate()
{
Yii::$app->response->format = Response::FORMAT_JSON;
return [‘response’ => “This is a regenerated response…”];
}
}


Step 3: Use the Widget in Your View

In your views/site/index.php or any other view file, include:

php

use app\widgets\ChatResponseWidget;

echo ChatResponseWidget::widget();


How It Works

  1. Copy Button: Copies the response text to the clipboard.
  2. Regenerate Button: Fetches a new response from the Yii2 backend (ChatController::actionGenerate()).
  3. Edit Button: Allows inline editing of the generated response.

Now, your Yii2 app has a ChatGPT-style response box with a reusable widget!

Mr. Arch Brooks, Software Engineer, Brooks Computing Systems, LLC authored this article.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.


Page dynamically generated on

Brooks Computing Systems, LLC
Quality, Reliability & Service