Add CurrentCharacter import

This commit is contained in:
kwaroran
2024-07-15 19:48:51 +09:00
parent 1da936f2f0
commit 00a56ab447

View File

@@ -5,7 +5,7 @@ import { getFileSrc } from './storage/globalApi';
import { processScriptFull } from './process/scripts'; import { processScriptFull } from './process/scripts';
import { get } from 'svelte/store'; import { get } from 'svelte/store';
import css from '@adobe/css-tools' import css from '@adobe/css-tools'
import { CurrentChat, SizeStore, selectedCharID } from './stores'; import { CurrentCharacter, CurrentChat, SizeStore, selectedCharID } from './stores';
import { calcString } from './process/infunctions'; import { calcString } from './process/infunctions';
import { findCharacterbyId, parseKeyValue, sfc32, sleep, uuidtoNumber } from './util'; import { findCharacterbyId, parseKeyValue, sfc32, sleep, uuidtoNumber } from './util';
import { getInlayImage, writeInlayImage } from './process/files/image'; import { getInlayImage, writeInlayImage } from './process/files/image';
@@ -242,79 +242,101 @@ async function renderHighlightableMarkdown(data:string) {
} }
export const assetRegex = /{{(raw|img|video|audio|bg|emotion|asset|video-img)::(.+?)}}/g export const assetRegex = /{{(raw|img|video|audio|bg|emotion|asset|video-img|source)::(.+?)}}/g
async function parseAdditionalAssets(data:string, char:simpleCharacterArgument|character, mode:'normal'|'back', mode2:'unset'|'pre'|'post' = 'unset'){ async function parseAdditionalAssets(data:string, char:simpleCharacterArgument|character, mode:'normal'|'back', mode2:'unset'|'pre'|'post' = 'unset'){
const db = get(DataBase) const db = get(DataBase)
const assetWidthString = (db.assetWidth && db.assetWidth !== -1 || db.assetWidth === 0) ? `max-width:${db.assetWidth}rem;` : '' const assetWidthString = (db.assetWidth && db.assetWidth !== -1 || db.assetWidth === 0) ? `max-width:${db.assetWidth}rem;` : ''
if(char.additionalAssets || char.emotionImages){ let assetPaths:{[key:string]:{
path:string
ext?:string
}} = {}
let emoPaths:{[key:string]:{
path:string
}} = {}
let assetPaths:{[key:string]:{ if(char.additionalAssets){
path:string for(const asset of char.additionalAssets){
ext?:string const assetPath = await getFileSrc(asset[1])
}} = {} assetPaths[asset[0].toLocaleLowerCase()] = {
let emoPaths:{[key:string]:{ path: assetPath,
path:string ext: asset[2]
}} = {}
if(char.additionalAssets){
for(const asset of char.additionalAssets){
const assetPath = await getFileSrc(asset[1])
assetPaths[asset[0].toLocaleLowerCase()] = {
path: assetPath,
ext: asset[2]
}
} }
} }
if(char.emotionImages){ }
for(const emo of char.emotionImages){ if(char.emotionImages){
const emoPath = await getFileSrc(emo[1]) for(const emo of char.emotionImages){
emoPaths[emo[0].toLocaleLowerCase()] = { const emoPath = await getFileSrc(emo[1])
path: emoPath, emoPaths[emo[0].toLocaleLowerCase()] = {
} path: emoPath,
} }
} }
const videoExtention = ['mp4', 'webm', 'avi', 'm4p', 'm4v'] }
data = data.replaceAll(assetRegex, (full:string, type:string, name:string) => { const videoExtention = ['mp4', 'webm', 'avi', 'm4p', 'm4v']
name = name.toLocaleLowerCase() let needsSourceAccess = false
if(type === 'emotion'){ data = data.replaceAll(assetRegex, (full:string, type:string, name:string) => {
const path = emoPaths[name]?.path name = name.toLocaleLowerCase()
if(!path){ if(type === 'emotion'){
return '' const path = emoPaths[name]?.path
}
return `<img src="${path}" alt="${path}" style="${assetWidthString} "/>`
}
const path = assetPaths[name]
if(!path){ if(!path){
return '' return ''
} }
switch(type){ return `<img src="${path}" alt="${path}" style="${assetWidthString} "/>`
case 'raw': }
return path.path if(type === 'source'){
case 'img': needsSourceAccess = true
return `<img src="${path.path}" alt="${path.path}" style="${assetWidthString} "/>` switch(name){
case 'video': case 'char':{
return `<video controls autoplay loop><source src="${path.path}" type="video/mp4"></video>` return '\uE9b4CHAR\uE9b4'
case 'video-img': }
return `<video autoplay muted loop><source src="${path.path}" type="video/mp4"></video>` case 'user': {
case 'audio': return '\uE9b4USER\uE9b4'
return `<audio controls autoplay loop><source src="${path.path}" type="audio/mpeg"></audio>`
case 'bg':
if(mode === 'back'){
return `<div style="width:100%;height:100%;background: linear-gradient(rgba(0, 0, 0, 0.8), rgba(0, 0, 0, 0.8)),url(${path.path}); background-size: cover;"></div>`
}
break
case 'asset':{
if(path.ext && videoExtention.includes(path.ext)){
return `<video autoplay muted loop><source src="${path.path}" type="video/mp4"></video>`
}
return `<img src="${path.path}" alt="${path.path}" style="${assetWidthString} "/>`
} }
} }
}
const path = assetPaths[name]
if(!path){
return '' return ''
}) }
switch(type){
case 'raw':
return path.path
case 'img':
return `<img src="${path.path}" alt="${path.path}" style="${assetWidthString} "/>`
case 'video':
return `<video controls autoplay loop><source src="${path.path}" type="video/mp4"></video>`
case 'video-img':
return `<video autoplay muted loop><source src="${path.path}" type="video/mp4"></video>`
case 'audio':
return `<audio controls autoplay loop><source src="${path.path}" type="audio/mpeg"></audio>`
case 'bg':
if(mode === 'back'){
return `<div style="width:100%;height:100%;background: linear-gradient(rgba(0, 0, 0, 0.8), rgba(0, 0, 0, 0.8)),url(${path.path}); background-size: cover;"></div>`
}
break
case 'asset':{
if(path.ext && videoExtention.includes(path.ext)){
return `<video autoplay muted loop><source src="${path.path}" type="video/mp4"></video>`
}
return `<img src="${path.path}" alt="${path.path}" style="${assetWidthString} "/>`
}
}
return ''
})
if(needsSourceAccess){
const chara = get(CurrentCharacter)
if(chara.image){}
data = data.replace(/\uE9b4CHAR\uE9b4/g,
chara.image ? (await getFileSrc(chara.image)) : ''
)
data = data.replace(/\uE9b4USER\uE9b4/g,
db.userIcon ? (await getFileSrc(db.userIcon)) : ''
)
} }
return data return data
} }