@@ -158,93 +158,99 @@ class PaletteManagerV2 {
158158 loadContract ( sessionContext ) {
159159 console . log ( "[PALETTE_MANAGER_V2_CONTRACT_LOADED]" ) ;
160160 if ( ! sessionContext || typeof sessionContext !== "object" || Array . isArray ( sessionContext ) ) {
161- this . renderError ( "Session context is invalid. Expected an object containing paletteJson ." ) ;
161+ this . renderError ( "Session context is invalid. Expected an object containing payloadJson.paletteDocument ." ) ;
162162 return ;
163163 }
164164 const versionCheck = this . handleSessionVersion ( sessionContext ) ;
165165 if ( ! versionCheck . ok ) {
166166 this . renderError ( versionCheck . error ) ;
167167 return ;
168168 }
169- if ( ! versionCheck . payload . paletteJson || typeof versionCheck . payload . paletteJson !== "object " || Array . isArray ( versionCheck . payload . paletteJson ) ) {
170- this . renderError ( "Palette Manager V2 session data is invalid. Expected paletteJson only ." ) ;
169+ if ( typeof versionCheck . payload . toolId !== "string " || versionCheck . payload . toolId . trim ( ) !== "palette-manager-v2" ) {
170+ this . renderError ( "Palette Manager V2 session data is invalid. Expected toolId 'palette-manager-v2' ." ) ;
171171 return ;
172172 }
173- this . renderPalette ( versionCheck . payload . paletteJson , versionCheck . payload ) ;
173+ if ( Object . prototype . hasOwnProperty . call ( versionCheck . payload , "paletteJson" ) ) {
174+ this . renderError ( "Palette Manager V2 session data is invalid. paletteJson is not supported; use payloadJson.paletteDocument." ) ;
175+ return ;
176+ }
177+ if ( ! Object . prototype . hasOwnProperty . call ( versionCheck . payload , "payloadJson" ) || ! versionCheck . payload . payloadJson || typeof versionCheck . payload . payloadJson !== "object" || Array . isArray ( versionCheck . payload . payloadJson ) ) {
178+ this . renderError ( "Palette Manager V2 session data is invalid. Expected payloadJson only." ) ;
179+ return ;
180+ }
181+ if ( ! Object . prototype . hasOwnProperty . call ( versionCheck . payload . payloadJson , "paletteDocument" ) || ! versionCheck . payload . payloadJson . paletteDocument || typeof versionCheck . payload . payloadJson . paletteDocument !== "object" || Array . isArray ( versionCheck . payload . payloadJson . paletteDocument ) ) {
182+ this . renderError ( "Palette Manager V2 session data is invalid. Expected payloadJson.paletteDocument." ) ;
183+ return ;
184+ }
185+ this . renderPalette ( versionCheck . payload . payloadJson . paletteDocument , versionCheck . payload ) ;
174186 }
175187
176- renderPalette ( paletteJson , sessionContext ) {
177- if ( typeof paletteJson . name !== "string" || ! paletteJson . name . trim ( ) ) {
178- this . renderError ( "Palette Manager V2 session data is invalid. Expected paletteJson .name." ) ;
188+ renderPalette ( paletteDocument , sessionContext ) {
189+ if ( typeof paletteDocument . name !== "string" || ! paletteDocument . name . trim ( ) ) {
190+ this . renderError ( "Palette Manager V2 session data is invalid. Expected paletteDocument .name." ) ;
179191 return ;
180192 }
181- if ( ! Array . isArray ( paletteJson . colors ) ) {
182- this . renderError ( "Palette Manager V2 session data is invalid. Expected paletteJson.colors []." ) ;
193+ if ( ! Array . isArray ( paletteDocument . swatches ) ) {
194+ this . renderError ( "Palette Manager V2 session data is invalid. Expected paletteDocument.swatches []." ) ;
183195 return ;
184196 }
185197
186- const normalizedColors = [ ] ;
187- for ( let index = 0 ; index < paletteJson . colors . length ; index += 1 ) {
188- let colorValue = "" ;
189- let colorLabel = "" ;
190- if ( typeof paletteJson . colors [ index ] === "string" ) {
191- colorValue = paletteJson . colors [ index ] . trim ( ) . toUpperCase ( ) ;
192- colorLabel = colorValue ;
198+ const validatedSwatches = [ ] ;
199+ for ( let index = 0 ; index < paletteDocument . swatches . length ; index += 1 ) {
200+ const swatch = paletteDocument . swatches [ index ] ;
201+ if ( ! swatch || typeof swatch !== "object" || Array . isArray ( swatch ) ) {
202+ this . renderError ( "Palette Manager V2 session data is invalid. Every swatch must be an object." ) ;
203+ return ;
193204 }
194- if (
195- paletteJson . colors [ index ] &&
196- typeof paletteJson . colors [ index ] === "object" &&
197- ! Array . isArray ( paletteJson . colors [ index ] ) &&
198- typeof paletteJson . colors [ index ] . hex === "string"
199- ) {
200- colorValue = paletteJson . colors [ index ] . hex . trim ( ) . toUpperCase ( ) ;
201- colorLabel = typeof paletteJson . colors [ index ] . name === "string" && paletteJson . colors [ index ] . name . trim ( )
202- ? paletteJson . colors [ index ] . name . trim ( )
203- : colorValue ;
205+ const swatchKeys = Object . keys ( swatch ) ;
206+ if ( swatchKeys . some ( ( key ) => key !== "symbol" && key !== "hex" && key !== "name" ) ) {
207+ this . renderError ( "Palette Manager V2 session data is invalid. Swatches only allow symbol, hex, and name." ) ;
208+ return ;
204209 }
205- if (
206- paletteJson . colors [ index ] &&
207- typeof paletteJson . colors [ index ] === "object" &&
208- ! Array . isArray ( paletteJson . colors [ index ] ) &&
209- typeof paletteJson . colors [ index ] . color === "string"
210- ) {
211- colorValue = paletteJson . colors [ index ] . color . trim ( ) . toUpperCase ( ) ;
212- colorLabel = typeof paletteJson . colors [ index ] . name === "string" && paletteJson . colors [ index ] . name . trim ( )
213- ? paletteJson . colors [ index ] . name . trim ( )
214- : colorValue ;
210+ if ( typeof swatch . symbol !== "string" || swatch . symbol . length !== 1 ) {
211+ this . renderError ( "Palette Manager V2 session data is invalid. Every swatch requires a one-character symbol." ) ;
212+ return ;
213+ }
214+ if ( typeof swatch . name !== "string" || ! swatch . name . trim ( ) ) {
215+ this . renderError ( "Palette Manager V2 session data is invalid. Every swatch requires a name." ) ;
216+ return ;
215217 }
216- if ( ! / ^ # ( [ 0 - 9 A - F ] { 6 } | [ 0 - 9 A - F ] { 8 } ) $ / . test ( colorValue ) ) {
217- this . renderError ( "Palette Manager V2 session data is invalid. Every paletteJson.colors[] entry must include #RRGGBB or #RRGGBBAA." ) ;
218+ if ( typeof swatch . hex !== "string" || ! / ^ # ( [ 0 - 9 A - F a - f ] { 6 } | [ 0 - 9 A - F a - f ] { 8 } ) $ / . test ( swatch . hex ) ) {
219+ this . renderError ( "Palette Manager V2 session data is invalid. Every swatch requires #RRGGBB or #RRGGBBAA hex ." ) ;
218220 return ;
219221 }
220- normalizedColors . push ( { label : colorLabel , color : colorValue } ) ;
222+ validatedSwatches . push ( {
223+ symbol : swatch . symbol ,
224+ name : swatch . name . trim ( ) ,
225+ hex : swatch . hex . toUpperCase ( )
226+ } ) ;
221227 }
222228
223229 document . getElementById ( "paletteManagerSessionReadout" ) . textContent = `Session: loaded\nContext: ${ this . urlState . hostContextId } \nTool: ${ typeof sessionContext . toolId === "string" && sessionContext . toolId . trim ( ) ? sessionContext . toolId . trim ( ) : "not provided" } ${ this . optionalUrlStateSummary ( ) ? `\nURL State: ${ this . optionalUrlStateSummary ( ) } ` : "" } ` ;
224- document . getElementById ( "paletteManagerContractReadout" ) . textContent = "paletteJson loaded\npaletteJson.name valid\npaletteJson.colors [] valid" ;
230+ document . getElementById ( "paletteManagerContractReadout" ) . textContent = "payloadJson loaded\npayloadJson.paletteDocument valid\nswatches [] valid" ;
225231 document . getElementById ( "paletteManagerWorkspaceReadout" ) . textContent = "Workspace session context was read. Workspace writes are deferred for this isolated V2 entry." ;
226- document . getElementById ( "paletteManagerName" ) . textContent = paletteJson . name . trim ( ) ;
227- document . getElementById ( "paletteManagerCount" ) . textContent = `${ normalizedColors . length } color ${ normalizedColors . length === 1 ? "" : "s " } ` ;
228- document . getElementById ( "paletteManagerState" ) . textContent = normalizedColors . length === 0
229- ? "Palette Manager V2 loaded a valid session palette with zero colors ."
232+ document . getElementById ( "paletteManagerName" ) . textContent = paletteDocument . name . trim ( ) ;
233+ document . getElementById ( "paletteManagerCount" ) . textContent = `${ validatedSwatches . length } swatch ${ validatedSwatches . length === 1 ? "" : "es " } ` ;
234+ document . getElementById ( "paletteManagerState" ) . textContent = validatedSwatches . length === 0
235+ ? "Palette Manager V2 loaded a valid session palette with zero swatches ."
230236 : "Palette Manager V2 loaded the session palette." ;
231237 document . getElementById ( "paletteManagerEmptyState" ) . hidden = true ;
232238 document . getElementById ( "paletteManagerInvalidState" ) . hidden = true ;
233239 document . getElementById ( "paletteManagerValidState" ) . hidden = false ;
234240
235241 document . getElementById ( "paletteManagerSwatches" ) . replaceChildren ( ) ;
236- normalizedColors . forEach ( ( entry ) => {
242+ validatedSwatches . forEach ( ( entry ) => {
237243 const swatch = document . createElement ( "article" ) ;
238244 const chip = document . createElement ( "div" ) ;
239245 const swatchBody = document . createElement ( "div" ) ;
240246 const swatchName = document . createElement ( "div" ) ;
241247 const swatchColor = document . createElement ( "div" ) ;
242248 swatch . className = "palette-manager-v2-swatch" ;
243249 chip . className = "palette-manager-v2-chip" ;
244- chip . style . backgroundColor = entry . color ;
250+ chip . style . backgroundColor = entry . hex ;
245251 swatchBody . className = "palette-manager-v2-swatch-body" ;
246- swatchName . textContent = entry . label ;
247- swatchColor . textContent = entry . color ;
252+ swatchName . textContent = ` ${ entry . symbol } ${ entry . name } ` ;
253+ swatchColor . textContent = entry . hex ;
248254 swatchBody . append ( swatchName , swatchColor ) ;
249255 swatch . append ( chip , swatchBody ) ;
250256 document . getElementById ( "paletteManagerSwatches" ) . appendChild ( swatch ) ;
@@ -254,10 +260,10 @@ class PaletteManagerV2 {
254260 renderMissing ( message ) {
255261 this . logStructuredError ( "EMPTY" , message , { hostContextId : this . urlState . hostContextId || "" } ) ;
256262 document . getElementById ( "paletteManagerSessionReadout" ) . textContent = "Session: missing" ;
257- document . getElementById ( "paletteManagerContractReadout" ) . textContent = "paletteJson not loaded" ;
263+ document . getElementById ( "paletteManagerContractReadout" ) . textContent = "payloadJson.paletteDocument not loaded" ;
258264 document . getElementById ( "paletteManagerWorkspaceReadout" ) . textContent = "Workspace session context is not available." ;
259265 document . getElementById ( "paletteManagerName" ) . textContent = "No palette loaded" ;
260- document . getElementById ( "paletteManagerCount" ) . textContent = "0 colors " ;
266+ document . getElementById ( "paletteManagerCount" ) . textContent = "0 swatches " ;
261267 document . getElementById ( "paletteManagerEmptyState" ) . textContent = message ;
262268 document . getElementById ( "paletteManagerEmptyState" ) . hidden = false ;
263269 document . getElementById ( "paletteManagerInvalidState" ) . hidden = true ;
@@ -268,11 +274,11 @@ class PaletteManagerV2 {
268274 renderError ( message ) {
269275 this . logStructuredError ( "INVALID" , message , { hostContextId : this . urlState . hostContextId || "" } ) ;
270276 document . getElementById ( "paletteManagerSessionReadout" ) . textContent = "Session: read attempted" ;
271- document . getElementById ( "paletteManagerContractReadout" ) . textContent = "paletteJson invalid" ;
277+ document . getElementById ( "paletteManagerContractReadout" ) . textContent = "payloadJson.paletteDocument invalid" ;
272278 document . getElementById ( "paletteManagerWorkspaceReadout" ) . textContent = "Workspace writes are disabled for invalid Palette Manager V2 session data." ;
273279 document . getElementById ( "paletteManagerName" ) . textContent = "Palette Manager V2 error" ;
274- document . getElementById ( "paletteManagerCount" ) . textContent = "0 colors " ;
275- document . getElementById ( "paletteManagerInvalidState" ) . textContent = `${ message } Re-open Palette Manager V2 from a host session that provides paletteJson .` ;
280+ document . getElementById ( "paletteManagerCount" ) . textContent = "0 swatches " ;
281+ document . getElementById ( "paletteManagerInvalidState" ) . textContent = `${ message } Re-open Palette Manager V2 from a host session that provides payloadJson.paletteDocument .` ;
276282 document . getElementById ( "paletteManagerEmptyState" ) . hidden = true ;
277283 document . getElementById ( "paletteManagerInvalidState" ) . hidden = false ;
278284 document . getElementById ( "paletteManagerValidState" ) . hidden = true ;
0 commit comments