-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Json controls and Canvas update (#1038)
* Updated with 2 things Added create primitive mod Added seperator mod * Updated these with lint * Updated these with lint * Updated canvas_create_primitive_MOD.js * Added json_read_MOD added json_write_MOD.js * Added json_read_MOD added json_write_MOD.js * Added json_read_MOD added json_write_MOD.js * Added json_read_MOD added json_write_MOD.js * Fixed lint issue I hope * Fixed lint issue I hope * Fixed prettier issues Fixed linting issues inside json write Fixed issue with canvas paint not using cases * Added json random
- Loading branch information
1 parent
49e3b9c
commit fbcd592
Showing
4 changed files
with
488 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
module.exports = { | ||
name: 'Pick Random JSON Item', | ||
section: 'File Stuff', | ||
fields: ['filepath', 'title', 'storage', 'varName'], | ||
|
||
subtitle(data) { | ||
return `Pick random item from JSON file "${data.filepath}"`; | ||
}, | ||
|
||
variableStorage(data, varType) { | ||
if (parseInt(data.storage, 10) !== varType) return; | ||
return [data.varName, 'Text']; | ||
}, | ||
|
||
html() { | ||
return ` | ||
<div> | ||
<div style="padding: 10px;"> | ||
<span class="dbminputlabel">File Path</span> | ||
<input id="filepath" class="round" type="text" placeholder="./data.json"> | ||
</div> | ||
<div style="padding: 10px;"> | ||
<span class="dbminputlabel">Title</span> | ||
<input id="title" class="round" type="text" placeholder="Title (optional)"> | ||
</div> | ||
<div style="padding: 10px;"> | ||
<store-in-variable dropdownLabel="Store Result In" selectId="storage" variableContainerId="varNameContainer" variableInputId="varName"></store-in-variable> | ||
</div> | ||
</div> | ||
`; | ||
}, | ||
|
||
init() {}, | ||
|
||
async action(cache) { | ||
const data = cache.actions[cache.index]; | ||
let filepath = this.evalMessage(data.filepath, cache); | ||
const title = this.evalMessage(data.title, cache); | ||
const storage = parseInt(data.storage, 10); | ||
const varName = this.evalMessage(data.varName, cache); | ||
|
||
if (filepath.startsWith('./')) { | ||
filepath = path.join(__dirname, '..', filepath.substring(2)); | ||
} | ||
|
||
let jsonData; | ||
|
||
try { | ||
if (fs.existsSync(filepath)) { | ||
const fileData = fs.readFileSync(filepath); | ||
if (fileData.length === 0) { | ||
console.warn('JSON file is empty.'); | ||
this.storeValue(undefined, storage, varName, cache); | ||
return this.callNextAction(cache); | ||
} | ||
jsonData = JSON.parse(fileData); | ||
} else { | ||
throw new Error('File does not exist'); | ||
} | ||
} catch (error) { | ||
console.error(`Error reading JSON file: ${error}`); | ||
this.storeValue(undefined, storage, varName, cache); | ||
return this.callNextAction(cache); | ||
} | ||
|
||
let result; | ||
|
||
try { | ||
if (title) { | ||
const titleData = jsonData.find((item) => item.Title === title); | ||
if (!titleData) throw new Error('Title not found'); | ||
|
||
const keys = Object.keys(titleData).filter((key) => key !== 'Title'); | ||
if (keys.length === 0) throw new Error('No items found under specified title'); | ||
|
||
const randomKey = keys[Math.floor(Math.random() * keys.length)]; | ||
result = randomKey; | ||
} else { | ||
const items = jsonData.flatMap((item) => Object.keys(item).filter((key) => key !== 'Title')); | ||
if (items.length === 0) throw new Error('No items found in JSON'); | ||
|
||
const randomItem = items[Math.floor(Math.random() * items.length)]; | ||
result = randomItem; | ||
} | ||
} catch (error) { | ||
console.error(`Error accessing data: ${error}`); | ||
this.storeValue(undefined, storage, varName, cache); | ||
return this.callNextAction(cache); | ||
} | ||
|
||
this.storeValue(result, storage, varName, cache); | ||
this.callNextAction(cache); | ||
}, | ||
|
||
mod() {}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
module.exports = { | ||
name: 'Read JSON File', | ||
section: 'File Stuff', | ||
fields: ['filepath', 'title', 'contentTitle', 'storage', 'varName'], | ||
|
||
subtitle(data) { | ||
return `Read JSON file "${data.filepath}"`; | ||
}, | ||
|
||
variableStorage(data, varType) { | ||
if (parseInt(data.storage, 10) !== varType) return; | ||
return [data.varName, 'Unknown']; | ||
}, | ||
|
||
html() { | ||
return ` | ||
<div> | ||
<div style="padding: 10px;"> | ||
<span class="dbminputlabel">File Path</span> | ||
<input id="filepath" class="round" type="text" placeholder="./data.json"> | ||
</div> | ||
<div style="padding: 10px;"> | ||
<span class="dbminputlabel">Title</span> | ||
<input id="title" class="round" type="text" placeholder="Title"> | ||
</div> | ||
<div style="padding: 10px;"> | ||
<span class="dbminputlabel">Content Title</span> | ||
<input id="contentTitle" class="round" type="text" placeholder="Content Title"> | ||
</div> | ||
<div style="padding: 10px;"> | ||
<store-in-variable dropdownLabel="Store Result In" selectId="storage" variableContainerId="varNameContainer" variableInputId="varName"></store-in-variable> | ||
</div> | ||
</div> | ||
`; | ||
}, | ||
|
||
init() {}, | ||
|
||
async action(cache) { | ||
const data = cache.actions[cache.index]; | ||
let filepath = this.evalMessage(data.filepath, cache); | ||
const title = this.evalMessage(data.title, cache); | ||
const contentTitle = this.evalMessage(data.contentTitle, cache); | ||
const storage = parseInt(data.storage, 10); | ||
const varName = this.evalMessage(data.varName, cache); | ||
|
||
if (filepath.startsWith('./')) { | ||
filepath = path.join(__dirname, '..', filepath.substring(2)); | ||
} | ||
|
||
let jsonData; | ||
|
||
try { | ||
if (fs.existsSync(filepath)) { | ||
const fileData = fs.readFileSync(filepath); | ||
if (fileData.length === 0) { | ||
console.warn('JSON file is empty.'); | ||
this.storeValue(undefined, storage, varName, cache); | ||
return this.callNextAction(cache); | ||
} | ||
jsonData = JSON.parse(fileData); | ||
} else { | ||
throw new Error('File does not exist'); | ||
} | ||
} catch (error) { | ||
console.error(`Error reading JSON file: ${error}`); | ||
this.storeValue(undefined, storage, varName, cache); | ||
return this.callNextAction(cache); | ||
} | ||
|
||
let result; | ||
try { | ||
const titleData = jsonData.find((item) => item.Title === title); | ||
if (!titleData) throw new Error('Title not found'); | ||
|
||
if (contentTitle.includes('/')) { | ||
const contentKeys = contentTitle.split('/'); | ||
result = {}; | ||
for (const key of contentKeys) { | ||
if (titleData[key] !== undefined) { | ||
result[key] = titleData[key]; | ||
} | ||
} | ||
} else { | ||
if (titleData[contentTitle] === undefined) throw new Error('Content Title not found'); | ||
result = titleData[contentTitle]; | ||
} | ||
} catch (error) { | ||
console.error(`Error accessing data: ${error}`); | ||
this.storeValue(undefined, storage, varName, cache); | ||
return this.callNextAction(cache); | ||
} | ||
|
||
this.storeValue(result, storage, varName, cache); | ||
this.callNextAction(cache); | ||
}, | ||
|
||
mod() {}, | ||
}; |
Oops, something went wrong.