Spaces:
Paused
Paused
refactor: Remove legacy worksheet inpainting rendering engine
Browse files- src/views/chat/chat-view.js +0 -223
src/views/chat/chat-view.js
CHANGED
|
@@ -3924,7 +3924,6 @@ body { margin: 0; padding: 0; background: #000; overflow: hidden; display: flex;
|
|
| 3924 |
|
| 3925 |
this.processWikiImages(wrapper);
|
| 3926 |
this.processGeminiImages(wrapper);
|
| 3927 |
-
this.processWorksheetInpainting(wrapper);
|
| 3928 |
|
| 3929 |
const imgs = wrapper.querySelectorAll('img');
|
| 3930 |
imgs.forEach(img => {
|
|
@@ -4471,228 +4470,6 @@ body { margin: 0; padding: 0; background: #000; overflow: hidden; display: flex;
|
|
| 4471 |
}
|
| 4472 |
}
|
| 4473 |
|
| 4474 |
-
async processWorksheetInpainting(container) {
|
| 4475 |
-
const asyncImages = container.querySelectorAll('.worksheet-inpainting-async:not(.processed)');
|
| 4476 |
-
if (asyncImages.length === 0) return;
|
| 4477 |
-
|
| 4478 |
-
let lastUserBase64 = null;
|
| 4479 |
-
const msgs = db.getMessages(this.currentConversationId);
|
| 4480 |
-
for (let i = msgs.length - 1; i >= 0; i--) {
|
| 4481 |
-
if (msgs[i].role === 'user') {
|
| 4482 |
-
const match = msgs[i].content.match(/data:image\/[^;]+;base64,[^)]+/);
|
| 4483 |
-
if (match) {
|
| 4484 |
-
lastUserBase64 = match[0].includes('base64,') ? match[0].split('base64,')[1] : match[0];
|
| 4485 |
-
break;
|
| 4486 |
-
}
|
| 4487 |
-
}
|
| 4488 |
-
}
|
| 4489 |
-
|
| 4490 |
-
if (!lastUserBase64) {
|
| 4491 |
-
for (const img of asyncImages) {
|
| 4492 |
-
img.classList.add('processed');
|
| 4493 |
-
const errDiv = document.createElement('div');
|
| 4494 |
-
errDiv.style = "position: absolute; top: 8px; left: 8px; right: 8px; color: #ff6b6b; font-size: 10px; padding: 6px; background: rgba(255,0,0,0.8); border-radius: 6px; font-family: monospace; z-index: 10; backdrop-filter: blur(4px); word-break: break-all;";
|
| 4495 |
-
errDiv.innerText = "Fehler: Kein Originalbild im Chatverlauf gefunden.";
|
| 4496 |
-
img.parentElement.appendChild(errDiv);
|
| 4497 |
-
if (img.previousElementSibling) img.previousElementSibling.style.display = 'none';
|
| 4498 |
-
if (img.previousElementSibling?.previousElementSibling) img.previousElementSibling.previousElementSibling.style.display = 'none';
|
| 4499 |
-
}
|
| 4500 |
-
return;
|
| 4501 |
-
}
|
| 4502 |
-
|
| 4503 |
-
const hfKey = db.getSetting('huggingface_api_key');
|
| 4504 |
-
|
| 4505 |
-
for (const img of asyncImages) {
|
| 4506 |
-
img.classList.add('processed');
|
| 4507 |
-
|
| 4508 |
-
const jsonStr = img.getAttribute('data-json');
|
| 4509 |
-
if (!jsonStr) continue;
|
| 4510 |
-
|
| 4511 |
-
try {
|
| 4512 |
-
const jsonData = JSON.parse(jsonStr.replace(/"/g, '"'));
|
| 4513 |
-
|
| 4514 |
-
// Dynamischer Import von fluxClient, um Circular Dependencies zu vermeiden
|
| 4515 |
-
const { fluxClient } = await import('../../ai/flux-client.js');
|
| 4516 |
-
|
| 4517 |
-
// Original Image laden, um Dimensionen zu bekommen
|
| 4518 |
-
const sourceImage = new Image();
|
| 4519 |
-
sourceImage.src = 'data:image/jpeg;base64,' + lastUserBase64;
|
| 4520 |
-
await new Promise((resolve, reject) => {
|
| 4521 |
-
sourceImage.onload = resolve;
|
| 4522 |
-
sourceImage.onerror = () => reject(new Error("Quellbild konnte nicht geladen werden (evtl. defektes Base64)."));
|
| 4523 |
-
});
|
| 4524 |
-
|
| 4525 |
-
const width = sourceImage.width;
|
| 4526 |
-
const height = sourceImage.height;
|
| 4527 |
-
|
| 4528 |
-
// --- ADVANCED CANVAS RENDERING ENGINE ---
|
| 4529 |
-
// Dynamically load a beautiful handwriting font
|
| 4530 |
-
if (!document.getElementById('font-kalam')) {
|
| 4531 |
-
const link = document.createElement('link');
|
| 4532 |
-
link.id = 'font-kalam';
|
| 4533 |
-
link.href = 'https://fonts.googleapis.com/css2?family=Kalam:wght@400;700&display=swap';
|
| 4534 |
-
link.rel = 'stylesheet';
|
| 4535 |
-
document.head.appendChild(link);
|
| 4536 |
-
}
|
| 4537 |
-
|
| 4538 |
-
try {
|
| 4539 |
-
await document.fonts.load('16px "Kalam"');
|
| 4540 |
-
} catch(e) {
|
| 4541 |
-
console.warn("Font loading took too long, fallback may be used.");
|
| 4542 |
-
}
|
| 4543 |
-
|
| 4544 |
-
const finalCanvas = document.createElement('canvas');
|
| 4545 |
-
finalCanvas.width = width;
|
| 4546 |
-
finalCanvas.height = height;
|
| 4547 |
-
const fCtx = finalCanvas.getContext('2d');
|
| 4548 |
-
|
| 4549 |
-
// Draw original image
|
| 4550 |
-
fCtx.drawImage(sourceImage, 0, 0);
|
| 4551 |
-
|
| 4552 |
-
// Enable multiply blend mode to make ink look realistic on paper
|
| 4553 |
-
fCtx.globalCompositeOperation = 'multiply';
|
| 4554 |
-
|
| 4555 |
-
jsonData.forEach(item => {
|
| 4556 |
-
let x = (item.x / 1000) * width;
|
| 4557 |
-
let y = (item.y / 1000) * height;
|
| 4558 |
-
let w = (item.width / 1000) * width || 100;
|
| 4559 |
-
let h = (item.height / 1000) * height || 30;
|
| 4560 |
-
|
| 4561 |
-
// Slight padding
|
| 4562 |
-
x += w * 0.02;
|
| 4563 |
-
y += h * 0.05;
|
| 4564 |
-
w *= 0.96;
|
| 4565 |
-
h *= 0.90;
|
| 4566 |
-
|
| 4567 |
-
// Realistic ink color (dark slate blue)
|
| 4568 |
-
fCtx.fillStyle = '#1e293b';
|
| 4569 |
-
fCtx.textAlign = 'center';
|
| 4570 |
-
fCtx.textBaseline = 'middle';
|
| 4571 |
-
|
| 4572 |
-
// Calculate font size
|
| 4573 |
-
const isLongText = item.text.split(' ').length > 4;
|
| 4574 |
-
let fontSize = isLongText ? Math.max(14, Math.min(24, w / 15)) : Math.max(16, Math.min(28, h * 0.6));
|
| 4575 |
-
fCtx.font = `400 ${fontSize}px "Kalam", "Comic Sans MS", cursive`;
|
| 4576 |
-
|
| 4577 |
-
fCtx.save();
|
| 4578 |
-
// Slight random rotation for human feel (-0.8 to +0.8 degrees)
|
| 4579 |
-
const angle = (Math.random() - 0.5) * 1.6 * Math.PI / 180;
|
| 4580 |
-
fCtx.translate(x + w/2, y + h/2);
|
| 4581 |
-
fCtx.rotate(angle);
|
| 4582 |
-
fCtx.translate(-(x + w/2), -(y + h/2));
|
| 4583 |
-
|
| 4584 |
-
if (!isLongText) {
|
| 4585 |
-
// Short text: just center it
|
| 4586 |
-
fCtx.fillText(item.text, x + w/2, y + h/2);
|
| 4587 |
-
} else {
|
| 4588 |
-
// Long text: Word Wrapping
|
| 4589 |
-
const words = item.text.split(' ');
|
| 4590 |
-
let line = '';
|
| 4591 |
-
const lines = [];
|
| 4592 |
-
|
| 4593 |
-
for(let n = 0; n < words.length; n++) {
|
| 4594 |
-
const testLine = line + words[n] + ' ';
|
| 4595 |
-
const metrics = fCtx.measureText(testLine);
|
| 4596 |
-
if (metrics.width > w && n > 0) {
|
| 4597 |
-
lines.push(line);
|
| 4598 |
-
line = words[n] + ' ';
|
| 4599 |
-
} else {
|
| 4600 |
-
line = testLine;
|
| 4601 |
-
}
|
| 4602 |
-
}
|
| 4603 |
-
lines.push(line);
|
| 4604 |
-
|
| 4605 |
-
const lineHeight = fontSize * 1.4;
|
| 4606 |
-
const totalHeight = lines.length * lineHeight;
|
| 4607 |
-
let startY = y + (h - totalHeight) / 2 + (lineHeight / 2);
|
| 4608 |
-
|
| 4609 |
-
lines.forEach(l => {
|
| 4610 |
-
fCtx.fillText(l.trim(), x + w/2, startY);
|
| 4611 |
-
startY += lineHeight;
|
| 4612 |
-
});
|
| 4613 |
-
}
|
| 4614 |
-
fCtx.restore();
|
| 4615 |
-
});
|
| 4616 |
-
|
| 4617 |
-
// --- PHOTOREALISTIC DESK PRESENTATION LAYER ---
|
| 4618 |
-
// Instead of just returning the flat scan, we create a beautiful mockup
|
| 4619 |
-
// of the paper lying on a wooden desk with shadows and lighting.
|
| 4620 |
-
|
| 4621 |
-
const mockupCanvas = document.createElement('canvas');
|
| 4622 |
-
// We make the final image a standard landscape photo ratio (e.g., 1600x1200)
|
| 4623 |
-
const mw = Math.max(1600, width * 1.5);
|
| 4624 |
-
const mh = Math.max(1200, height * 1.2);
|
| 4625 |
-
mockupCanvas.width = mw;
|
| 4626 |
-
mockupCanvas.height = mh;
|
| 4627 |
-
const mCtx = mockupCanvas.getContext('2d');
|
| 4628 |
-
|
| 4629 |
-
// 1. Draw blurred, dark aesthetic background (simulating a premium desk/surface)
|
| 4630 |
-
// A rich dark gradient
|
| 4631 |
-
const bgGrad = mCtx.createRadialGradient(mw/2, mh/2, mh/4, mw/2, mh/2, mw);
|
| 4632 |
-
bgGrad.addColorStop(0, '#2d3748');
|
| 4633 |
-
bgGrad.addColorStop(1, '#1a202c');
|
| 4634 |
-
mCtx.fillStyle = bgGrad;
|
| 4635 |
-
mCtx.fillRect(0, 0, mw, mh);
|
| 4636 |
-
|
| 4637 |
-
// 2. Add subtle grain/texture to background
|
| 4638 |
-
mCtx.fillStyle = 'rgba(255,255,255,0.015)';
|
| 4639 |
-
for(let i=0; i<10000; i++) {
|
| 4640 |
-
mCtx.fillRect(Math.random()*mw, Math.random()*mh, 2, 2);
|
| 4641 |
-
}
|
| 4642 |
-
|
| 4643 |
-
// 3. Draw the paper with drop shadow and rotation
|
| 4644 |
-
mCtx.save();
|
| 4645 |
-
|
| 4646 |
-
// Paper rotation (-3 to +3 degrees)
|
| 4647 |
-
const paperAngle = (Math.random() - 0.5) * 6 * Math.PI / 180;
|
| 4648 |
-
mCtx.translate(mw/2, mh/2);
|
| 4649 |
-
mCtx.rotate(paperAngle);
|
| 4650 |
-
|
| 4651 |
-
// Scale paper so it fits nicely
|
| 4652 |
-
const scale = Math.min((mw * 0.75) / width, (mh * 0.85) / height);
|
| 4653 |
-
const drawW = width * scale;
|
| 4654 |
-
const drawH = height * scale;
|
| 4655 |
-
|
| 4656 |
-
// Drop shadow
|
| 4657 |
-
mCtx.shadowColor = 'rgba(0, 0, 0, 0.45)';
|
| 4658 |
-
mCtx.shadowBlur = 40;
|
| 4659 |
-
mCtx.shadowOffsetX = 15;
|
| 4660 |
-
mCtx.shadowOffsetY = 25;
|
| 4661 |
-
|
| 4662 |
-
// Draw the flat image (which now contains the ink) onto the mockup
|
| 4663 |
-
mCtx.drawImage(finalCanvas, -drawW/2, -drawH/2, drawW, drawH);
|
| 4664 |
-
|
| 4665 |
-
// 4. Add realistic lighting gradient over the paper
|
| 4666 |
-
mCtx.shadowColor = 'transparent'; // turn off shadow for lighting
|
| 4667 |
-
mCtx.globalCompositeOperation = 'overlay';
|
| 4668 |
-
const lightGrad = mCtx.createLinearGradient(-drawW/2, -drawH/2, drawW/2, drawH/2);
|
| 4669 |
-
lightGrad.addColorStop(0, 'rgba(255,255,255,0.15)'); // Light from top-left
|
| 4670 |
-
lightGrad.addColorStop(1, 'rgba(0,0,0,0.1)'); // Shadow at bottom-right
|
| 4671 |
-
mCtx.fillStyle = lightGrad;
|
| 4672 |
-
mCtx.fillRect(-drawW/2, -drawH/2, drawW, drawH);
|
| 4673 |
-
|
| 4674 |
-
mCtx.restore();
|
| 4675 |
-
|
| 4676 |
-
const resultBase64 = mockupCanvas.toDataURL('image/jpeg', 0.92);
|
| 4677 |
-
img.src = resultBase64;
|
| 4678 |
-
|
| 4679 |
-
const loadingText = img.previousElementSibling;
|
| 4680 |
-
if (loadingText) {
|
| 4681 |
-
loadingText.innerHTML = "Handschrift generiert!";
|
| 4682 |
-
setTimeout(() => { loadingText.style.display = 'none'; }, 2000);
|
| 4683 |
-
}
|
| 4684 |
-
|
| 4685 |
-
} catch (e) {
|
| 4686 |
-
console.error("Inpainting failed", e);
|
| 4687 |
-
const errDiv = document.createElement('div');
|
| 4688 |
-
errDiv.style = "position: absolute; top: 8px; left: 8px; right: 8px; color: #ff6b6b; font-size: 10px; padding: 6px; background: rgba(255,0,0,0.8); border-radius: 6px; font-family: monospace; z-index: 10; backdrop-filter: blur(4px); word-break: break-all;";
|
| 4689 |
-
errDiv.innerText = `Inpainting Fehler: ${e.message}`;
|
| 4690 |
-
img.parentElement.appendChild(errDiv);
|
| 4691 |
-
if (img.previousElementSibling) img.previousElementSibling.style.display = 'none';
|
| 4692 |
-
if (img.previousElementSibling?.previousElementSibling) img.previousElementSibling.previousElementSibling.style.display = 'none';
|
| 4693 |
-
}
|
| 4694 |
-
}
|
| 4695 |
-
}
|
| 4696 |
|
| 4697 |
async processWikiImages(container) {
|
| 4698 |
const asyncImages = container.querySelectorAll('.wiki-async-image');
|
|
|
|
| 3924 |
|
| 3925 |
this.processWikiImages(wrapper);
|
| 3926 |
this.processGeminiImages(wrapper);
|
|
|
|
| 3927 |
|
| 3928 |
const imgs = wrapper.querySelectorAll('img');
|
| 3929 |
imgs.forEach(img => {
|
|
|
|
| 4470 |
}
|
| 4471 |
}
|
| 4472 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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