Add TOML handling to JS modules

Signed-off-by: Justin Georgi <justin.georgi@gmail.com>
This commit is contained in:
2025-05-01 19:18:04 -07:00
parent 95647069eb
commit 5561038c25
4 changed files with 1167 additions and 14 deletions

View File

@@ -73,9 +73,10 @@
"glmv-prev.css" "glmv-prev.css"
], ],
"packageFiles": [ "packageFiles": [
"glmv-prev.js", "mini-st.js",
"glmv-mvconfig.js", "glmv-mvconfig.js",
"glmv-hs.js" "glmv-hs.js",
"glmv-prev.js"
] ]
} }
} }

View File

@@ -1,3 +1,5 @@
const TOML = require('./mini-st.js')
let deleteHotspot = null let deleteHotspot = null
let grabHotspot = null let grabHotspot = null
@@ -34,7 +36,7 @@ clickAddHotspot = function(e) {
hsOutput['data-target'] = `${targetObj.x.toFixed(5)}m ${targetObj.y.toFixed(5)}m ${targetObj.z.toFixed(5)}m` hsOutput['data-target'] = `${targetObj.x.toFixed(5)}m ${targetObj.y.toFixed(5)}m ${targetObj.z.toFixed(5)}m`
hsOutput['field-of-view'] = Number.parseFloat(targetModel.getFieldOfView()).toFixed(5) + 'deg' hsOutput['field-of-view'] = Number.parseFloat(targetModel.getFieldOfView()).toFixed(5) + 'deg'
mvconfig.annotations['Hotspot ' + (Object.keys(mvconfig.annotations).length + 1)] = hsOutput mvconfig.annotations['Hotspot ' + (Object.keys(mvconfig.annotations).length + 1)] = hsOutput
let newText = currentText.replace(/(.*?<mvconfig>)[\S\s]*?(<\/mvconfig>.*)/,`$1\n${JSON.stringify(mvconfig, null, 2)}\n$2`) let newText = currentText.replace(/(.*?<mvconfig>)[\S\s]*?(<\/mvconfig>.*)/,`$1\n${TOML.stringify(mvconfig, null, 2)}\n$2`)
$('#wpTextbox1').val(newText) $('#wpTextbox1').val(newText)
} }
readMvconfig() readMvconfig()
@@ -147,7 +149,7 @@ releaseAnnotation = function(e) {
"data-target": newTarg, "data-target": newTarg,
"field-of-view": newFov "field-of-view": newFov
} }
const newText = currentText.replace(/(.*?<mvconfig>)[\S\s]*?(<\/mvconfig>.*)/,`$1\n${JSON.stringify(mvconfig, null, 2)}\n$2`) const newText = currentText.replace(/(.*?<mvconfig>)[\S\s]*?(<\/mvconfig>.*)/,`$1\n${TOML.stringify(mvconfig, null, 2)}\n$2`)
$('#wpTextbox1').val(newText) $('#wpTextbox1').val(newText)
} }
grabHotspot = null grabHotspot = null

View File

@@ -1,3 +1,5 @@
const TOML = require('./mini-st.js')
/** /**
* Convert text in the preview text editor to js object * Convert text in the preview text editor to js object
* *
@@ -6,7 +8,7 @@
extractMvconfig = function() { extractMvconfig = function() {
const editText = $('#wpTextbox1').val() const editText = $('#wpTextbox1').val()
const extractConfig = editText.match(/<mvconfig>([\S\s]*?)<\/mvconfig>/) const extractConfig = editText.match(/<mvconfig>([\S\s]*?)<\/mvconfig>/)
let mvconfig = (extractConfig.length >= 2) ? JSON.parse(extractConfig[1]) : {viewerConfig: {}, annotations: {}, annotationSets: {}} let mvconfig = (extractConfig.length >= 2) ? TOML.parse(extractConfig[1]) : {viewerConfig: {}, annotations: {}, annotationSets: {}}
if (mvconfig.viewerConfig === undefined) { if (mvconfig.viewerConfig === undefined) {
mvconfig.viewerConfig = { mvconfig.viewerConfig = {
default: { default: {
@@ -24,7 +26,7 @@ extractMvconfig = function() {
} }
/** /**
* Reads the json string in the edit panel * Reads the TOML string in the edit panel
* and updates hotspot elements and menu settings * and updates hotspot elements and menu settings
* *
* @return {bool|object} arrays of view and set names on successful read and update false on failure * @return {bool|object} arrays of view and set names on successful read and update false on failure
@@ -90,8 +92,8 @@ readMvconfig = function() {
} }
/** /**
* Parses the current hotspots into json object * Parses the current hotspots into TOML object
* and writes the json string to the edit panel * and writes the TOML string to the edit panel
* *
* @return {bool} true on successful write to edit panel * @return {bool} true on successful write to edit panel
*/ */
@@ -109,7 +111,7 @@ writeMvconfig = function () {
if (Object.keys(annotationsObj).length === 0) return false if (Object.keys(annotationsObj).length === 0) return false
const [currentText, mvconfig] = extractMvconfig() const [currentText, mvconfig] = extractMvconfig()
mvconfig.annotations = annotationsObj mvconfig.annotations = annotationsObj
const newText = currentText.replace(/(.*?<mvconfig>)[\S\s]*?(<\/mvconfig>.*)/,`$1\n${JSON.stringify(mvconfig, null, 2)}\n$2`) const newText = currentText.replace(/(.*?<mvconfig>)[\S\s]*?(<\/mvconfig>.*)/,`$1\n${TOML.stringify(mvconfig, null, 2)}\n$2`)
$('#wpTextbox1').val(newText) $('#wpTextbox1').val(newText)
return true return true
} }
@@ -158,7 +160,7 @@ toggleCameraControl = function(view) {
} else { } else {
delete mvconfig.viewerConfig[currentView]['camera-controls'] delete mvconfig.viewerConfig[currentView]['camera-controls']
} }
const textUpdate = currentText.replace(/(?<=<mvconfig>)([\S\s]*?)(?=<\/mvconfig>)/gm,`\n${JSON.stringify(mvconfig, null, 2)}\n`) const textUpdate = currentText.replace(/(?<=<mvconfig>)([\S\s]*?)(?=<\/mvconfig>)/gm,`\n${TOML.stringify(mvconfig, null, 2)}\n`)
$('#wpTextbox1').val(textUpdate) $('#wpTextbox1').val(textUpdate)
return newControl return newControl
} }
@@ -172,7 +174,7 @@ addAnnotationSet = function(newSet) {
const mView = $('model-viewer')[0] const mView = $('model-viewer')[0]
let [currentText, mvconfig] = extractMvconfig() let [currentText, mvconfig] = extractMvconfig()
mvconfig.annotationSets[newSet] = [] mvconfig.annotationSets[newSet] = []
const textUpdate = currentText.replace(/(?<=<mvconfig>)([\S\s]*?)(?=<\/mvconfig>)/gm,`\n${JSON.stringify(mvconfig, null, 2)}\n`) const textUpdate = currentText.replace(/(?<=<mvconfig>)([\S\s]*?)(?=<\/mvconfig>)/gm,`\n${TOML.stringify(mvconfig, null, 2)}\n`)
$('#wpTextbox1').val(textUpdate) $('#wpTextbox1').val(textUpdate)
selectAnnotationSet(newSet) selectAnnotationSet(newSet)
} }
@@ -186,7 +188,7 @@ addViewConfig = function(newView) {
const mView = $('model-viewer')[0] const mView = $('model-viewer')[0]
let [currentText, mvconfig] = extractMvconfig() let [currentText, mvconfig] = extractMvconfig()
mvconfig.viewerConfig[newView] = { "camera-controls": true } mvconfig.viewerConfig[newView] = { "camera-controls": true }
const textUpdate = currentText.replace(/(?<=<mvconfig>)([\S\s]*?)(?=<\/mvconfig>)/gm,`\n${JSON.stringify(mvconfig, null, 2)}\n`) const textUpdate = currentText.replace(/(?<=<mvconfig>)([\S\s]*?)(?=<\/mvconfig>)/gm,`\n${TOML.stringify(mvconfig, null, 2)}\n`)
$('#wpTextbox1').val(textUpdate) $('#wpTextbox1').val(textUpdate)
selectViewConfig(newView) selectViewConfig(newView)
} }
@@ -255,7 +257,7 @@ writeCameraOrbit = function() {
mvconfig.viewerConfig.default['camera-orbit'] = newOrbit mvconfig.viewerConfig.default['camera-orbit'] = newOrbit
mvconfig.viewerConfig.default['camera-target'] = newTarget mvconfig.viewerConfig.default['camera-target'] = newTarget
mvconfig.viewerConfig.default['field-of-view'] = newField mvconfig.viewerConfig.default['field-of-view'] = newField
const textUpdate = currentText.replace(/(?<=<mvconfig>)([\S\s]*?)(?=<\/mvconfig>)/gm,`\n${JSON.stringify(mvconfig, null, 2)}\n`) const textUpdate = currentText.replace(/(?<=<mvconfig>)([\S\s]*?)(?=<\/mvconfig>)/gm,`\n${TOML.stringify(mvconfig, null, 2)}\n`)
$('#wpTextbox1').val(textUpdate) $('#wpTextbox1').val(textUpdate)
} }
@@ -277,6 +279,6 @@ writeCameraLimit = function(axis, limit) {
oldOrbitVals[valueIndex] = newOrbitVals[valueIndex] oldOrbitVals[valueIndex] = newOrbitVals[valueIndex]
mvconfig.viewerConfig.default[`${limit}-camera-orbit`] = oldOrbitVals.join(' ') mvconfig.viewerConfig.default[`${limit}-camera-orbit`] = oldOrbitVals.join(' ')
mView.setAttribute(`${limit}-camera-orbit`, oldOrbitVals.join(' ')) mView.setAttribute(`${limit}-camera-orbit`, oldOrbitVals.join(' '))
const textUpdate = currentText.replace(/(?<=<mvconfig>)([\S\s]*?)(?=<\/mvconfig>)/gm,`\n${JSON.stringify(mvconfig, null, 2)}\n`) const textUpdate = currentText.replace(/(?<=<mvconfig>)([\S\s]*?)(?=<\/mvconfig>)/gm,`\n${TOML.stringify(mvconfig, null, 2)}\n`)
$('#wpTextbox1').val(textUpdate) $('#wpTextbox1').val(textUpdate)
} }

1148
modules/mini-st.js Normal file

File diff suppressed because it is too large Load Diff