99
1010import { deepMerge } from './objects' ;
1111import { fetchFileAsync } from './files' ;
12- import { menu , getSchema } from 'schemas' ; // located in /web/schemas.js
12+ import { menu , getSchema , getSchemaYaml } from 'schemas' ; // located in /web/schemas.js
13+ import { fetchAndProcessYaml , processYamlSchema } from './schema_induction' ;
1314
1415// Retrieves template given in browser URL by template= , OR returns first menu item Object.keys(menu)[0]
1516export function getTemplatePathInScope ( ) {
@@ -31,42 +32,57 @@ export function getTemplatePathInScope() {
3132 return templatePath ;
3233}
3334
34- // Running locally on user's computer so get schema from schemas.json
35- //E.g. window.location === http://localhost:8000
36- // However this may have to be reengineered when we do local running of app
37- // but with URL access to web for schema(s).
35+ // Fetches schema, preferring schema.yaml at runtime (avoids build step) and
36+ // falling back to pre-built schema.json when YAML is unavailable.
3837async function fetchSchema ( path ) {
3938
40- if ( window . location . href . startsWith ( 'http://localhost:' ) || window . location . protocol . startsWith ( 'file' ) ) {
41- const schema_path = path . replace ( / \/ t e m p l a t e s \/ ( .+ ) \/ s c h e m a .j s o n / , '$1' ) ;
42- return await getSchema ( schema_path ) ;
43- }
44- else if ( window . location . protocol . startsWith ( 'http' ) ) {
45-
46- // PROBLEM: local HTTPS server in /dist folder doesn't work
47- // path e.g. /templates/canada_covid19/schema.json
48- return await fetchFileAsync ( path ) ;
39+ // For any HTTP protocol (localhost or production), schema.yaml and schema.json
40+ // are both served as static files from web/dist/templates/ via CopyPlugin.
41+ // Fetch YAML first; if that fails, fetch JSON directly — no webpack bundle needed.
42+ if ( window . location . protocol . startsWith ( 'http' ) ) {
43+ const yamlPath = path . replace ( / s c h e m a \. j s o n $ / , 'schema.yaml' ) ;
44+ const yamlUrl = new URL ( yamlPath , window . location . href ) . href ;
45+ try {
46+ return await fetchAndProcessYaml ( yamlUrl ) ;
47+ } catch ( e ) {
48+ console . warn ( 'schema_induction: YAML load failed, falling back to schema.json:' , e . message ) ;
49+ }
50+ // JSON static-file fallback
51+ try {
52+ const response = await fetch ( path ) ;
53+ if ( response . ok ) return await response . json ( ) ;
54+ } catch ( e ) {
55+ console . warn ( 'fetchSchema: schema.json fetch also failed:' , e . message ) ;
56+ }
57+ return null ;
4958 }
5059
60+ // file:// protocol: browsers block fetch() for file:// origins.
61+ // Try webpack-bundled schema.json first; if absent, try webpack-bundled
62+ // schema.yaml (raw text, bundled as asset/source) processed synchronously.
63+ const schema_path = path . replace ( / \/ t e m p l a t e s \/ ( .+ ) \/ s c h e m a .j s o n / , '$1' ) ;
64+ const jsonSchema = await getSchema ( schema_path ) ;
65+ if ( jsonSchema ) return jsonSchema ;
66+
67+ const yamlText = await getSchemaYaml ( schema_path ) ;
68+ if ( yamlText ) return processYamlSchema ( yamlText ) ;
69+
5170 return null ;
5271}
5372
5473//Used only in this script in create()
5574function buildTemplateFromUploadedSchema ( schema ) {
56- const name = schema . name || '' ; // Brand new schema
75+ const name = schema . name || '' ;
5776 const locales = { } ;
5877
59- if ( schema . in_language ) {
60- const languages = Array . isArray ( schema . in_language )
61- ? schema . in_language
62- : [ schema . in_language ] ;
63-
64- // PROBABLY IGNORE THESE SINCE UPLOADED SCHEMA DOESN'T COME WITH locales
65- languages . forEach ( ( locale ) => {
66- locales [ locale ] = { schema } ;
67- locales ;
78+ // Extract any locales embedded in the schema via extensions.locales.value,
79+ // using the same deep-merge logic as the server-fetch path in create().
80+ // This applies to both uploaded .yaml files and to schemas passed by
81+ // loadSelectedTemplate when the file was loaded from disk.
82+ Object . entries ( schema . extensions ?. locales ?. value || { } )
83+ . forEach ( ( [ locale , locale_schema ] ) => {
84+ locales [ locale ] = { schema : deepMerge ( schema , locale_schema ) } ;
6885 } ) ;
69- }
7086
7187 return {
7288 name,
0 commit comments