|
|
|
@ -297,7 +297,6 @@ import { setConfig } from '../components/plugins'
|
|
|
|
|
// import {getUserConfigFromBackend,saveData,getPageContent,getAndApplyUserStyles} from './components/utils';
|
|
|
|
|
import { useStore } from 'vuex';
|
|
|
|
|
import router from '../router/index.js';
|
|
|
|
|
import axios from 'axios';
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
name: 'CkeditorView',
|
|
|
|
@ -407,24 +406,24 @@ export default {
|
|
|
|
|
// TODO 从返回文本中提取出css部分
|
|
|
|
|
preElement.textContent = `文心一言:\n` + preElement.textContent;
|
|
|
|
|
messageDiv.style.backgroundColor = '#bdc3c7';
|
|
|
|
|
// 预览生成的样式
|
|
|
|
|
this.previewStyleAtMessages(text);
|
|
|
|
|
}
|
|
|
|
|
// messagesDiv.scrollTop = messagesDiv.scrollHeight;
|
|
|
|
|
return preElement;
|
|
|
|
|
},
|
|
|
|
|
// 发送消息
|
|
|
|
|
sendMessage() {
|
|
|
|
|
async sendMessage() {
|
|
|
|
|
const userInput = document.getElementById('userInput');
|
|
|
|
|
const messageText = userInput.value;
|
|
|
|
|
if (messageText.trim() === '') return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 构造聊天历史
|
|
|
|
|
let chatHistory = [];
|
|
|
|
|
const messages = document.getElementById('messages').children;
|
|
|
|
|
for (let i = 0; i < messages.length; i++) {
|
|
|
|
|
if (i%4==0){
|
|
|
|
|
if (i % 4 == 0) {
|
|
|
|
|
chatHistory.push({ Role: 'user', Content: messages[i].textContent });
|
|
|
|
|
}else if (i%4==1){
|
|
|
|
|
} else if (i % 4 == 1) {
|
|
|
|
|
const assistantResponse = messages[i].textContent.replaceAll('文心一言:\n', '');
|
|
|
|
|
chatHistory.push({ Role: 'assistant', Content: assistantResponse });
|
|
|
|
|
}
|
|
|
|
@ -438,22 +437,49 @@ export default {
|
|
|
|
|
// 根据messages的内容构造聊天历史列表
|
|
|
|
|
// TODO
|
|
|
|
|
// const chat_history = []
|
|
|
|
|
try {
|
|
|
|
|
const response = await fetch('http://localhost:14514/admin/ai_layout/style_generate', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
user_input: userInput.value,
|
|
|
|
|
...(chatHistory.length > 0 && { chat_history: chatHistory })
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
axios({
|
|
|
|
|
url: 'http://localhost:14514/admin/ai_layout/style_generate',
|
|
|
|
|
method: 'POST',
|
|
|
|
|
data: {
|
|
|
|
|
user_input: userInput.value,
|
|
|
|
|
...(chatHistory.length > 0 && { chat_history: chatHistory }),
|
|
|
|
|
if (!response.body) {
|
|
|
|
|
// TODO
|
|
|
|
|
throw new Error('No response body');
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.then(response => {
|
|
|
|
|
console.log(response);
|
|
|
|
|
this.displayMessage(response.data.data, 'ai')
|
|
|
|
|
})
|
|
|
|
|
.catch(error => {
|
|
|
|
|
console.error('Error:', error);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const reader = response.body.getReader();
|
|
|
|
|
const decoder = new TextDecoder('utf-8');
|
|
|
|
|
let result = '';
|
|
|
|
|
var messageStream;
|
|
|
|
|
/* eslint-disable no-constant-condition */
|
|
|
|
|
while (true) {
|
|
|
|
|
const { done, value } = await reader.read();
|
|
|
|
|
if (done) break;
|
|
|
|
|
const slice = decoder.decode(value, { stream: true });
|
|
|
|
|
result += slice;
|
|
|
|
|
if (!messageStream) {
|
|
|
|
|
messageStream = this.displayMessage(slice, 'ai');
|
|
|
|
|
} else {
|
|
|
|
|
messageStream.textContent += slice;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
console.log('生成的样式:', result);
|
|
|
|
|
// 预览生成的样式
|
|
|
|
|
this.previewStyleAtMessages(result);
|
|
|
|
|
/* eslint-enable no-constant-condition */
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error:', error);
|
|
|
|
|
this.displayError(error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 清空输入框
|
|
|
|
|
userInput.value = '';
|
|
|
|
|
},
|
|
|
|
|
// 预览生成的样式
|
|
|
|
@ -495,15 +521,18 @@ export default {
|
|
|
|
|
while ((match = styleRegex.exec(cssText)) !== null) {
|
|
|
|
|
if (!previewElement) {
|
|
|
|
|
previewElement = document.createElement(match[1]);
|
|
|
|
|
// previewElement.textContent = 'AaBbCcDdEeFf';
|
|
|
|
|
const previewText = document.createElement('p');
|
|
|
|
|
previewText.textContent = 'AaBbCcDd';
|
|
|
|
|
previewElement.appendChild(previewText);
|
|
|
|
|
if (match[1] === 'span') {
|
|
|
|
|
previewElement.textContent = 'AaBbCcDdEeFf';
|
|
|
|
|
} else {
|
|
|
|
|
const previewText = document.createElement('p');
|
|
|
|
|
previewText.textContent = 'AaBbCcDd';
|
|
|
|
|
previewElement.appendChild(previewText);
|
|
|
|
|
}
|
|
|
|
|
previewWrapper.appendChild(previewElement);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// className后加一个随机数表明是新的class
|
|
|
|
|
const newClassName = match[2]+"_"+Math.floor(Math.random()*1000);
|
|
|
|
|
const newClassName = match[2] + "_" + Math.floor(Math.random() * 1000);
|
|
|
|
|
previewStyle.textContent = previewStyle.textContent.replaceAll(match[2], newClassName);
|
|
|
|
|
classNames.push(newClassName);
|
|
|
|
|
}
|
|
|
|
@ -539,6 +568,37 @@ export default {
|
|
|
|
|
console.log(styleDefinition);
|
|
|
|
|
console.log(cssText);// 保存的css代码
|
|
|
|
|
};
|
|
|
|
|
// 显示按钮
|
|
|
|
|
buttonsMessageDiv.appendChild(saveButton);
|
|
|
|
|
buttonsMessageDiv.appendChild(this.createClearButton());
|
|
|
|
|
messagesDiv.appendChild(buttonsMessageDiv);
|
|
|
|
|
},
|
|
|
|
|
// 错误信息展示
|
|
|
|
|
displayError(error) {
|
|
|
|
|
const messagesDiv = document.getElementById('messages');
|
|
|
|
|
const messageDiv = document.createElement('div');
|
|
|
|
|
messageDiv.className = 'message';
|
|
|
|
|
// 发生错误时背景颜色设置为浅红色
|
|
|
|
|
messageDiv.style.backgroundColor = 'mistyrose'; // mistyrose 是一种浅红色
|
|
|
|
|
// 使用 pre 元素来格式化显示 text
|
|
|
|
|
// 可以将css html代码内容新建一个pre set language 更好地展示输出
|
|
|
|
|
const preElement = document.createElement('pre');
|
|
|
|
|
// 错误信息设置为红色
|
|
|
|
|
preElement.textContent = error;
|
|
|
|
|
preElement.style = "color:red";
|
|
|
|
|
messageDiv.appendChild(preElement);
|
|
|
|
|
messagesDiv.appendChild(messageDiv);
|
|
|
|
|
|
|
|
|
|
// 创建按钮容器
|
|
|
|
|
const buttonsMessageDiv = document.createElement('div');
|
|
|
|
|
buttonsMessageDiv.className = 'preview-buttons';
|
|
|
|
|
buttonsMessageDiv.style.display = 'flex';
|
|
|
|
|
buttonsMessageDiv.style.justifyContent = 'flex-end';
|
|
|
|
|
buttonsMessageDiv.style.marginTop = '10px';
|
|
|
|
|
buttonsMessageDiv.appendChild(this.createClearButton());
|
|
|
|
|
messagesDiv.appendChild(buttonsMessageDiv);
|
|
|
|
|
},
|
|
|
|
|
createClearButton() {
|
|
|
|
|
// 创建 clear 按钮
|
|
|
|
|
const clearButton = document.createElement('el-button');
|
|
|
|
|
clearButton.innerHTML = '<svg t="1731509926355" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="8154" width="20" height="20"><path d="M1011.43552 981.92384l-68.4032-394.40384h23.10144c18.5856 0 33.54624-14.97088 33.54624-33.55648V306.16576c0-18.5856-14.97088-33.55648-33.54624-33.55648H648.6528V37.71392c0-18.5856-14.97088-33.55648-33.55648-33.55648H408.59648c-18.5856 0-33.55648 14.97088-33.55648 33.55648v234.88512H57.5488c-18.5856 0-33.54624 14.97088-33.54624 33.55648v247.79776c0 18.5856 14.97088 33.55648 33.54624 33.55648h23.10144L12.24704 981.9136c-0.38912 1.9456-0.512 3.87072-0.512 5.6832 0 18.5856 14.97088 33.54624 33.55648 33.54624h933.10976c1.93536 0 3.88096-0.12288 5.6832-0.512 18.31936-3.08224 30.57664-20.51072 27.35104-38.7072zM114.33984 362.94656h351.03744V94.50496h92.928v268.4416h351.03744v134.22592H114.33984V362.94656zM718.336 930.816V729.48736c0-5.6832-4.64896-10.33216-10.32192-10.33216h-61.952c-5.67296 0-10.32192 4.64896-10.32192 10.33216V930.816H387.9424V729.48736c0-5.6832-4.64896-10.33216-10.32192-10.33216h-61.952c-5.67296 0-10.32192 4.64896-10.32192 10.33216V930.816H112.78336l58.20416-335.55456h681.5744L910.76608 930.816H718.336z m0 0" fill="#2C2C2C" p-id="8155"></path></svg>';
|
|
|
|
@ -552,10 +612,7 @@ export default {
|
|
|
|
|
messagesDiv.removeChild(messagesDiv.firstChild);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
// 显示按钮
|
|
|
|
|
buttonsMessageDiv.appendChild(saveButton);
|
|
|
|
|
buttonsMessageDiv.appendChild(clearButton);
|
|
|
|
|
messagesDiv.appendChild(buttonsMessageDiv);
|
|
|
|
|
return clearButton;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
components: {
|
|
|
|
|