Skip to content

Commit 49515f9

Browse files
committed
Nested color defaults working
1 parent 674ce1b commit 49515f9

1 file changed

Lines changed: 40 additions & 38 deletions

File tree

src/main.js

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,15 @@ class EditorColorCustom extends Jedison.Editor {
3636
id: this.getIdFromPath(this.instance.path)
3737
});
3838

39-
const val = this.instance.getValue();
40-
this.control.input.value = val?.r !== undefined ? rgbToHex(val) : '#000000';
39+
const setColor = () => {
40+
const val = this.instance.getValue();
41+
this.control.input.value = val?.r !== undefined ? rgbToHex(val) : '#000000';
42+
};
43+
44+
setColor();
45+
46+
// We need to set a listener here in case of nested references, in which case the defaults might not be ready yet.
47+
this.instance.on("set-value", setColor);
4148
}
4249

4350
addEventListeners() {
@@ -69,46 +76,45 @@ async function loadEditorData(schemaUrl, exampleUrl) {
6976

7077
// --- Editor initialization ---
7178

72-
function createEditor(schema, data, containerId, outputId) {
79+
function createEditor(schema, containerId, outputId) {
7380
const container = document.querySelector(`#${containerId}`);
7481
const output = document.querySelector(`#${outputId}`);
7582

76-
requestAnimationFrame(() => {
77-
container.innerHTML = '';
78-
const jedison = new Jedison.Create({
79-
container,
80-
theme: new Jedison.ThemeBootstrap5(),
81-
iconLib: 'bootstrap-icons',
82-
btnContents: false,
83-
enableCollapseToggle: true,
84-
enablePropertiesToggle: true,
85-
deactivateNonRequired: false,
86-
customEditors: [EditorColorCustom],
87-
schema,
88-
...(data && { data })
89-
});
83+
container.innerHTML = '';
84+
85+
const jedison = new Jedison.Create({
86+
container,
87+
theme: new Jedison.ThemeBootstrap5(),
88+
iconLib: 'bootstrap-icons',
89+
btnContents: false,
90+
enableCollapseToggle: true,
91+
enablePropertiesToggle: true,
92+
deactivateNonRequired: false,
93+
customEditors: [EditorColorCustom],
94+
schema
95+
});
9096

91-
function updateOutput() {
92-
if (!jedison.getErrors().length) {
93-
output.value = JSON.stringify(jedison.getValue(), null, 2);
94-
output.dataset.valid = 'true';
95-
} else {
96-
output.value = 'Invalid config! Fix errors to see JSON output.';
97-
output.dataset.valid = 'false';
98-
}
97+
function updateOutput() {
98+
if (!jedison.getErrors().length) {
99+
output.value = JSON.stringify(jedison.getValue(), null, 2);
100+
output.dataset.valid = 'true';
101+
} else {
102+
output.value = 'Invalid config! Fix errors to see JSON output.';
103+
output.dataset.valid = 'false';
99104
}
105+
}
100106

101-
jedison.on('change', updateOutput);
102-
updateOutput();
103-
});
107+
jedison.on('change', updateOutput);
108+
109+
updateOutput();
104110
}
105111

106-
function initEditor({ schemaUrl, exampleUrl, containerId, outputId }) {
112+
function initEditor({ schemaUrl, containerId, outputId }) {
107113
const container = document.querySelector(`#${containerId}`);
108114
container.innerHTML = '<div class="loader"></div>';
109115

110-
loadEditorData(schemaUrl, exampleUrl)
111-
.then(({ schema, data }) => createEditor(schema, data, containerId, outputId))
116+
loadEditorData(schemaUrl)
117+
.then((schema) => createEditor(schema, containerId, outputId))
112118
.catch(err => {
113119
container.textContent = `Failed to load schema: ${err.message}`;
114120
});
@@ -119,8 +125,7 @@ function initCoordinatesEditor(repoBase, size) {
119125
container.innerHTML = '<div class="loader"></div>';
120126

121127
initEditor({
122-
schemaUrl: `${repoBase}/coordinates/wxhy.schema.json`,
123-
exampleUrl: `${repoBase}/coordinates/${size}.example.json`,
128+
schemaUrl: `${repoBase}/coordinates/${size}.schema.json`,
124129
containerId: 'jedison-wxhy',
125130
outputId: 'output-wxhy'
126131
});
@@ -133,24 +138,21 @@ function initEditors(version) {
133138
editorsController = new AbortController();
134139
const { signal } = editorsController;
135140

136-
const REPO_BASE = `https://raw.githubusercontent.com/MLB-LED-Scoreboard/mlb-led-scoreboard/${version}`;
141+
const REPO_BASE = `https://raw.githubusercontent.com/MLB-LED-Scoreboard/mlb-led-scoreboard/${version}/schemas`;
137142

138143
const lazyEditors = {
139144
'tab-config': {
140145
schemaUrl: `${REPO_BASE}/config.schema.json`,
141-
exampleUrl: `${REPO_BASE}/config.example.json`,
142146
containerId: 'jedison-config',
143147
outputId: 'output-config'
144148
},
145149
'tab-teams': {
146150
schemaUrl: `${REPO_BASE}/colors/teams.schema.json`,
147-
exampleUrl: `${REPO_BASE}/colors/teams.example.json`,
148151
containerId: 'jedison-teams',
149152
outputId: 'output-teams'
150153
},
151154
'tab-scoreboard': {
152155
schemaUrl: `${REPO_BASE}/colors/scoreboard.schema.json`,
153-
exampleUrl: `${REPO_BASE}/colors/scoreboard.example.json`,
154156
containerId: 'jedison-scoreboard',
155157
outputId: 'output-scoreboard'
156158
}
@@ -250,7 +252,7 @@ localSchemaInput.addEventListener('change', async () => {
250252
await refParser.dereference(schema);
251253
refParser.expandRecursive(schema);
252254

253-
createEditor(schema, null, 'jedison-local', 'output-local');
255+
createEditor(schema, 'jedison-local', 'output-local');
254256
} catch (err) {
255257
container.textContent = `Failed to load local schema: ${err.message}`;
256258
}

0 commit comments

Comments
 (0)