Files
myprojplanet_vite/src/components/CModifTrafiletto/CModifTrafiletto.ts
2025-12-05 17:56:05 +01:00

252 lines
6.2 KiB
TypeScript
Executable File

import { PropType, computed, defineComponent, onMounted, ref, watch } from 'vue';
import draggable from 'vuedraggable';
import { tools } from '@tools';
import { useGlobalStore } from '@/store/globalStore';
import { CMyEditorAI } from '@/components/CMyEditorAI';
import { CAITools } from '@/components/CAITools';
import { costanti } from '@costanti';
import type { IMyScheda, IProduct, IRecFields } from '@/model';
import { shared_consts } from 'app/src/common/shared_vuejs';
import { useProducts } from 'app/src/store/Products';
import { useI18n } from 'vue-i18n';
import { useQuasar } from 'quasar';
export default defineComponent({
name: 'CModifTrafiletto',
emits: ['updateproductmodif', 'close', 'Savedb'],
components: {
CMyEditorAI,
CAITools,
},
props: {
modelValue: {
type: Object as PropType<IProduct>,
required: true,
},
table: {
type: String,
required: true,
},
mykey: {
type: String,
required: true,
},
mysubkey: {
type: String,
required: false,
default: '',
},
titolo: {
type: String,
required: false,
default: '',
},
type: {
type: Number,
required: false,
default: 0,
},
canModify: {
type: Boolean,
required: false,
default: false,
},
maxlength: {
type: Number,
required: false,
default: 0,
},
},
setup(props, { emit }) {
// Copia locale della lista_prodotti per manipolazione interna
const $q = useQuasar();
const { t } = useI18n();
const globalStore = useGlobalStore();
const products = useProducts();
const mytab = ref('descr');
const loading = ref(false);
const myscrapingbook = ref(null);
const updatetogm = ref(false);
const field_updated_toGM = ref('');
const myproduct = ref<IProduct>({ ...props.modelValue });
const id = computed(() => myproduct.value._id);
const myvalue = computed<string>(() => {
if (props.mysubkey) {
return myproduct.value[props.mykey][props.mysubkey];
} else {
return myproduct.value[props.mykey];
}
});
watch(
() => props.modelValue,
(newVal) => {
myproduct.value = { ...newVal };
},
{ deep: false }
);
const loadMyScrapingBook = async () => {
loading.value = true;
try {
const myscrapingbookrec = await products.loadMyScrapingBook(
myproduct.value.isbn,
false
);
myscrapingbook.value = myscrapingbookrec;
} catch (error) {
console.error('Errore caricamento MyScrapingBook:', error);
} finally {
loading.value = false;
}
};
async function mounted() {
// carico anche altri dati del product
await loadMyScrapingBook();
}
/*
// Aggiorna la copia locale quando il prop cambia
watch(
() => props.lista_prodotti,
(newVal) => {
internalProducts.value = [...newVal];
}
);
*/
function updateproductmodif(element: any) {
// console.log('CModifTrafiletto updateproductmodif ', element)
emit('updateproductmodif', element);
}
function Savedb(element: any) {
tools.saveInDBForTypes(
$q,
props.mykey,
element,
props.type,
true,
props.table,
props.mysubkey,
id.value,
null,
''
);
}
async function updateproduct(load?: boolean) {
myproduct.value = await products.getProductById(myproduct.value._id, load);
}
const copyToClipboard = (text) => {
navigator.clipboard
.writeText(text)
.then(() => {
$q.notify({
message: 'Testo copiato negli appunti!',
color: 'positive',
icon: 'check',
position: 'top',
});
})
.catch((err) => {
console.error('Errore durante la copia:', err);
$q.notify({
message: 'Errore nella copia',
color: 'negative',
icon: 'error',
position: 'top',
});
});
};
function handleShowAndSave(payload: any) {
Savedb(payload);
updateproductmodif(payload);
}
function getPrompt() {
// Prompt:
let mydescr =
"Scrivimi la sinossi del libro che ti indicherò, che andrà in un catalogo libri, la lunghezza del testo finale dev'essere compresa tra 760 e 780 caratteri, senza spiegazione, senza titolo iniziale, solo la sinossi. Togli eventuali riferimenti a chi ha fatto la prefazione. Rendilo un po' accattivante, ma non troppo. Senza immagine. Non inserire nessun link o riferimenti esterni o citazioni esterne. L'output è solo la sinossi, senza altre spiegazioni. \n\n";
return mydescr;
}
function copyDescrizioneFromScrapingData() {
let mydescr = '';
const data = myscrapingbook.value;
if (!data) return false;
mydescr = getPrompt();
mydescr += 'Titolo Libro: ' + data.titolo + '\n';
mydescr +=
'Autore Libro: ' +
(data?.autore.trim()
? data?.autore.trim()
: products.getAutoriByArrayAuthors(myproduct?.value.productInfo?.authors)) +
'\n';
mydescr += 'DESCRIZIONE LIBRO: \n';
mydescr += data.descrizione_lunga;
tools.copyStringToClipboard($q, mydescr, false);
}
function copyDescrizioneFromGruppoMacro() {
let mydescr = '';
mydescr = getPrompt();
mydescr += 'Titolo Libro: ' + myproduct?.value.productInfo?.name + '\n';
mydescr +=
'Autore Libro: ' +
products.getAutoriByArrayAuthors(myproduct?.value.productInfo?.authors) +
'\n';
mydescr += 'DESCRIZIONE LIBRO: \n';
mydescr += myproduct?.value.productInfo?.descrizione_completa_macro;
tools.copyStringToClipboard($q, mydescr, false);
}
onMounted(mounted);
return {
tools,
globalStore,
costanti,
shared_consts,
t,
products,
mytab,
myproduct,
updateproductmodif,
Savedb,
updatetogm,
field_updated_toGM,
loading,
copyToClipboard,
myvalue,
handleShowAndSave,
copyDescrizioneFromScrapingData,
copyDescrizioneFromGruppoMacro,
myscrapingbook,
};
},
});