feat: charjs style button trigger (#743)
# PR Checklist - [ ] Have you checked if it works normally in all models? *Ignore this if it doesn't use models.* - [ ] Have you checked if it works normally in all web, local, and node hosted versions? If it doesn't, have you blocked it in those versions? - [ ] Have you added type definitions? # Description This pull request reintroduces charJS style button event previously deprecated and removed, using luaEngine. This implementation diverges from current style by passing attribute data as parameters rather than function names.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { runTrigger } from "./process/triggers";
|
||||
import { sleep } from "./util";
|
||||
import { getCurrentCharacter, getCurrentChat, setCurrentChat } from "./storage/database.svelte";
|
||||
import { runLuaButtonTrigger } from "./process/lua";
|
||||
|
||||
|
||||
function nodeObserve(node:HTMLElement){
|
||||
@@ -35,6 +36,15 @@ function nodeObserve(node:HTMLElement){
|
||||
}
|
||||
|
||||
if(btnEvent){
|
||||
node.addEventListener('click', async () => {
|
||||
const currentChar = getCurrentCharacter()
|
||||
if(currentChar.type === 'group'){
|
||||
return;
|
||||
}
|
||||
await runLuaButtonTrigger(currentChar, btnEvent);
|
||||
}, {
|
||||
passive: true,
|
||||
});
|
||||
node.setAttribute('risu-observer', 'true');
|
||||
return
|
||||
}
|
||||
|
||||
@@ -457,6 +457,13 @@ export async function runLua(code:string, arg:{
|
||||
}
|
||||
break
|
||||
}
|
||||
case 'onButtonClick':{
|
||||
const func = luaEngine.global.get('onButtonClick')
|
||||
if(func){
|
||||
res = await func(accessKey, data)
|
||||
}
|
||||
break
|
||||
}
|
||||
case 'editRequest':
|
||||
case 'editDisplay':
|
||||
case 'editInput':
|
||||
@@ -675,4 +682,25 @@ export async function runLuaEditTrigger<T extends any>(char:character|groupChat|
|
||||
} catch (error) {
|
||||
return content
|
||||
}
|
||||
}
|
||||
|
||||
export async function runLuaButtonTrigger(char:character|groupChat|simpleCharacterArgument, data:string):Promise<T>{
|
||||
let runResult
|
||||
try {
|
||||
const triggers = char.type === 'group' ? getModuleTriggers() : char.triggerscript.concat(getModuleTriggers())
|
||||
const lowLevelAccess = char.type !== 'simple' ? char.lowLevelAccess ?? false : false
|
||||
for(let trigger of triggers){
|
||||
if(trigger?.effect?.[0]?.type === 'triggerlua'){
|
||||
runResult = await runLua(trigger.effect[0].code, {
|
||||
char: char,
|
||||
lowLevelAccess: lowLevelAccess,
|
||||
mode: 'onButtonClick',
|
||||
data: data
|
||||
})
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
throw(error)
|
||||
}
|
||||
return runResult
|
||||
}
|
||||
Reference in New Issue
Block a user