Skip to content

Commit 131770d

Browse files
author
prima
committed
feat: Aethetic mode group images (after loading a character card)
1 parent 5eb0683 commit 131770d

File tree

1 file changed

+93
-16
lines changed

1 file changed

+93
-16
lines changed

klite.embd

Lines changed: 93 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5264,6 +5264,43 @@ Current version indicated by LITEVER below.
52645264
}
52655265
var decoded = JSON.parse(b64_decode_unicode(b64buf));
52665266
console.log(decoded);
5267+
try
5268+
{
5269+
// https://stackoverflow.com/questions/12710001/how-to-convert-uint8-array-to-base64-encoded-string
5270+
async function bufferToBase64(buffer) {
5271+
// use a FileReader to generate a base64 data URI:
5272+
const base64url = await new Promise(r => {
5273+
const reader = new FileReader()
5274+
reader.onload = () => r(reader.result)
5275+
reader.readAsDataURL(new Blob([buffer]))
5276+
});
5277+
// remove the `data:...;base64,` part from the start
5278+
return base64url.slice(base64url.indexOf(',') + 1);
5279+
}
5280+
5281+
bufferToBase64(data).then(b64 => {
5282+
b64Url = `data:image/png;base64,${b64}`
5283+
let charData = !!decoded?.data ? decoded.data : decoded, charName = charData.name;
5284+
let dataToSave = {
5285+
name: charName,
5286+
data: charData,
5287+
image: b64Url
5288+
}
5289+
indexeddb_save(`character_${charName}`, JSON.stringify(dataToSave)).then(() => {
5290+
indexeddb_load("characterList", "[]").then(charListJson => {
5291+
let charList = JSON.parse(charListJson);
5292+
charList.push(charName);
5293+
allCharacterNames = charList;
5294+
indexeddb_save("characterList", JSON.stringify(charList));
5295+
})
5296+
})
5297+
})
5298+
5299+
}
5300+
catch (e)
5301+
{
5302+
console.error(e)
5303+
}
52675304
return decoded;
52685305
} catch (e) {
52695306
console.log("Error decoding b64 in image: " + e);
@@ -24001,11 +24038,48 @@ Current version indicated by LITEVER below.
2400124038
imageData: "var(--img_worldTree)"
2400224039
}
2400324040
];
24041+
24042+
let allCharacterNames = []
24043+
window.addEventListener("load", async () => {
24044+
allCharacterNames = JSON.parse(await indexeddb_load("characterList", "[]"))
24045+
})
2400424046

24047+
function getImageUrlFromCharacter(characterName)
24048+
{
24049+
if (allCharacterNames.includes(characterName))
24050+
{
24051+
let charName = characterName.replaceAll(/[^\w]/g, "_"), id = `style_image_${charName}`;
24052+
let varName = `--img_character_${charName}`
24053+
if (!document.getElementById(id))
24054+
{
24055+
indexeddb_load(`character_${characterName}`, "{}").then(charData => {
24056+
let image = JSON.parse(charData)?.image;
24057+
let styleElem = document.createElement("style");
24058+
styleElem.id = id
24059+
styleElem.innerHTML = `:root{
24060+
${varName}:url(${image})
24061+
}`
24062+
document.body.appendChild(styleElem)
24063+
})
24064+
}
24065+
return `var(${varName})`
24066+
}
24067+
return undefined
24068+
}
24069+
2400524070
function getImageForSource(source, name)
2400624071
{
24007-
let images = sourceImagesForCharacters.filter(imageMeta => imageMeta?.source === source && imageMeta?.name === name);
24008-
return images.length > 0 ? images[0]?.imageData : undefined;
24072+
let imageFromIndexDB = getImageUrlFromCharacter(name)
24073+
if (imageFromIndexDB !== undefined)
24074+
{
24075+
return imageFromIndexDB;
24076+
}
24077+
if (source !== undefined)
24078+
{
24079+
let images = sourceImagesForCharacters.filter(imageMeta => imageMeta?.source === source && imageMeta?.name === name);
24080+
return images.length > 0 ? images[0]?.imageData : undefined;
24081+
}
24082+
return undefined
2400924083
}
2401024084

2401124085
function render_aesthetic_ui(input, isPreview) //class suffix string used to prevent defined styles from leaking into global scope
@@ -24019,7 +24093,7 @@ Current version indicated by LITEVER below.
2401924093
let radius = (as.border_style == 'Circle' ? '1000rem' : (as.border_style == 'Rounded' ? '1.6rem' : '0.1rem'));
2402024094
let width, height;
2402124095
let imgclassname = "";
24022-
let imageToUse = source !== undefined ? getImageForSource(source, name) : undefined;
24096+
let imageToUse = getImageForSource(source, name);
2402324097
if (imageToUse !== undefined)
2402424098
{
2402524099
width = as.portrait_width_AI;
@@ -24211,12 +24285,12 @@ Current version indicated by LITEVER below.
2421124285
newbodystr += `<div style='display:flex; align-items:stretch; flex-direction: row;'>`;
2421224286
if(curr.myturn)
2421324287
{
24214-
newbodystr += `${(showavatar?avatarImage(false, curr?.source, curr?.name):"")}
24288+
newbodystr += `${(showavatar? avatarImage(false, curr?.source, curr?.name):"")}
2421524289
<div class='aui_myturn_block'`;
2421624290
}
2421724291
else
2421824292
{
24219-
newbodystr += `${(showavatar?avatarImage(true, curr?.source, curr?.name):"")}
24293+
newbodystr += `${(showavatar? avatarImage(true, curr?.source, curr?.name):"")}
2422024294
<div class='aui_aiturn_block'`;
2422124295
}
2422224296
newbodystr += ` style='flex: 1; display:flex; padding: ${as.padding()}; margin: ${as.margin()}; min-height:${as.background_minHeight}px;`
@@ -36957,17 +37031,20 @@ flowchart TD\n${treeToViewOutput.outputText.trim()}`
3695737031
let scripts = [...document.querySelectorAll("button.unselectable[title=Copy]")].filter(el => el.checkVisibility() && !el.classList.contains("execScriptAdded")).map(el => extractCodeFromElem(el))
3695837032

3695937033
scripts.forEach(meta => {
36960-
let elem = meta.elem
36961-
let button = document.createElement("button")
36962-
button.title = "Execute code block"
36963-
button.classList.add("unselectable")
36964-
button.onclick = () => handleExecCode(meta)
36965-
button.style.color = "black"
36966-
button.style.display = "flex"
36967-
button.style.float = "right"
36968-
button.innerText = "⚡"
36969-
elem.classList.add("execScriptAdded")
36970-
elem.parentElement.prepend(button)
37034+
let elem = meta?.elem
37035+
if (!!elem)
37036+
{
37037+
let button = document.createElement("button")
37038+
button.title = "Execute code block"
37039+
button.classList.add("unselectable")
37040+
button.onclick = () => handleExecCode(meta)
37041+
button.style.color = "black"
37042+
button.style.display = "flex"
37043+
button.style.float = "right"
37044+
button.innerText = "⚡"
37045+
elem.classList.add("execScriptAdded")
37046+
elem.parentElement.prepend(button)
37047+
}
3697137048
})
3697237049
}
3697337050

0 commit comments

Comments
 (0)