From 3e92d82a2c05bd821c1bca9e94a6b28b6568fe72 Mon Sep 17 00:00:00 2001 From: Steffen Henschel Date: Tue, 16 Jul 2024 10:58:08 +0200 Subject: [PATCH] Enabled custom path for template updates, updated templates --- .editorconfig | 3 + README.md | 13 +- package.json | 4 +- scripts/load_templates.js | 24 ++- src/main.css | 2 - src/templates/templates.js | 337 +++++++++++++++++++++++++++++++++++-- 6 files changed, 355 insertions(+), 28 deletions(-) diff --git a/.editorconfig b/.editorconfig index a290ba8..99657df 100644 --- a/.editorconfig +++ b/.editorconfig @@ -8,3 +8,6 @@ indent_style = tab insert_final_newline = true tab_width = 2 trim_trailing_whitespace = true + +[*.md] +trim_trailing_whitespace = false diff --git a/README.md b/README.md index 25bc6f6..f4cd9ed 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Data Contract Editor -The [Data Contract Editor](https://editor.datacontract.com) is like VS Code, but for [data contracts](https://datacontract.com). It's enterprise-friendly as it stores your data contracts in your browser. +The [Data Contract Editor](https://editor.datacontract.com) is like VS Code, but for [data contracts](https://datacontract.com). It's enterprise-friendly as it stores your data contracts in your browser. **Features** @@ -34,8 +34,15 @@ npm run build ## How to update the templates -Templates are taken from the Data Contract CLI and are fetched directly from the repository. -If you want to update them locally run `npm run update-templates`, whic will also be automatically run in the build. +Templates are taken from the Data Contract CLI and are fetched directly from the repository. +If you want to update them locally run `npm run update-templates`, which will also be automatically run in the build. + +Optionally, you can specify a location to the templates folder of a local datacontract-cli repository (or any other location). +Combined with `npm run dev`, the templates will be live reloaded. + +``` +npm run update-templates --datacontract-template-source="/my/path/to/datacontract-cli/datacontract/templates/" +``` ## License diff --git a/package.json b/package.json index 6f7c404..a033648 100644 --- a/package.json +++ b/package.json @@ -6,8 +6,8 @@ "scripts": { "dev": "vite src", "build": "vite build src --outDir ../dist --emptyOutDir", - "update-templates": "npm run load-templates && npm run precompile-templates", - "load-templates": "node scripts/load_templates.js", + "update-templates": "npm run load-templates %npm_config_datacontract-template-source% && npm run precompile-templates", + "load-templates": "node scripts/load_templates.js %npm_config_datacontract-template-source%", "precompile-templates": "node_modules/nunjucks/bin/precompile templates > src/templates/templates.js", "lint": "eslint" }, diff --git a/scripts/load_templates.js b/scripts/load_templates.js index daaf310..425080f 100644 --- a/scripts/load_templates.js +++ b/scripts/load_templates.js @@ -1,7 +1,18 @@ -import { existsSync, mkdirSync, writeFileSync } from 'node:fs'; +import { existsSync, mkdirSync, writeFileSync, readFileSync } from 'node:fs'; /** Utility functions */ +async function loadTemplate(sub_path) { + // eslint-disable-next-line no-undef + let base_path = process.env['npm_config_datacontract_template_source'] || 'https://raw.githubusercontent.com/datacontract/datacontract-cli/main/datacontract/templates/'; + let path = base_path + sub_path; + if (path.startsWith('http')) { + // eslint-disable-next-line no-undef + return await (await fetch(path)).text(); + } + return readFileSync(path) + ''; +} + // handle the replacement of all Python constructs into the JS equivalent function updateTemplateCode(template) { const updated_template = template @@ -21,11 +32,7 @@ function storeTemplate(path, content) { // download and process a partial async function processPartial(partial_name) { - const base_path = 'https://raw.githubusercontent.com/datacontract/datacontract-cli/main/datacontract/templates/'; - const url = `${base_path}${partial_name}`; - // eslint-disable-next-line no-undef - const raw_template = await fetch(url); - const template = await raw_template.text(); + const template = await loadTemplate(partial_name); const processed_template = updateTemplateCode(template); return processed_template; } @@ -33,9 +40,8 @@ async function processPartial(partial_name) { /** Main loop */ // fetch the main template, all other work is based on that -// eslint-disable-next-line no-undef -const raw_template = await fetch('https://raw.githubusercontent.com/datacontract/datacontract-cli/main/datacontract/templates/datacontract.html'); -const full_main_template = await raw_template.text(); +const full_main_template = await loadTemplate('datacontract.html'); +// const full_main_template = await raw_template.text(); // cut out the relevant part of the template (which is inside the
element) // note: this is a bit crude, but since this is a template we can't parse it as HTML const main_template = full_main_template.match(/
.*<\/main>/s)[0]; diff --git a/src/main.css b/src/main.css index 64f186d..52858a7 100644 --- a/src/main.css +++ b/src/main.css @@ -23,5 +23,3 @@ main { grid-column: 2; cursor: col-resize; } - - diff --git a/src/templates/templates.js b/src/templates/templates.js index fa722a9..ca57179 100644 --- a/src/templates/templates.js +++ b/src/templates/templates.js @@ -544,6 +544,178 @@ return { root: root }; +})(); +})(); +(function() {(window.nunjucksPrecompiled = window.nunjucksPrecompiled || {})["partials/datacontract_models.html"] = (function() { +function root(env, context, frame, runtime, cb) { +var lineno = 0; +var colno = 0; +var output = ""; +try { +var parentTemplate = null; +output += "
\n
\n

\n Data Model\n

\n

The logical data model

\n
\n
\n\n"; +frame = frame.push(); +var t_3 = runtime.memberLookup((runtime.contextOrFrameLookup(context, frame, "datacontract")),"models"); +if(t_3) {t_3 = runtime.fromIterator(t_3); +var t_1; +if(runtime.isArray(t_3)) { +var t_2 = t_3.length; +for(t_1=0; t_1 < t_3.length; t_1++) { +var t_4 = t_3[t_1][0]; +frame.set("[object Object]", t_3[t_1][0]); +var t_5 = t_3[t_1][1]; +frame.set("[object Object]", t_3[t_1][1]); +frame.set("loop.index", t_1 + 1); +frame.set("loop.index0", t_1); +frame.set("loop.revindex", t_2 - t_1); +frame.set("loop.revindex0", t_2 - t_1 - 1); +frame.set("loop.first", t_1 === 0); +frame.set("loop.last", t_1 === t_2 - 1); +frame.set("loop.length", t_2); +output += "\n\n
\n
\n
\n
\n\n \n \n \n \n\n \n \n \n "; +frame = frame.push(); +var t_8 = runtime.memberLookup((t_5),"fields"); +if(t_8) {t_8 = runtime.fromIterator(t_8); +var t_6; +if(runtime.isArray(t_8)) { +var t_7 = t_8.length; +for(t_6=0; t_6 < t_8.length; t_6++) { +var t_9 = t_8[t_6][0]; +frame.set("[object Object]", t_8[t_6][0]); +var t_10 = t_8[t_6][1]; +frame.set("[object Object]", t_8[t_6][1]); +frame.set("loop.index", t_6 + 1); +frame.set("loop.index0", t_6); +frame.set("loop.revindex", t_7 - t_6); +frame.set("loop.revindex0", t_7 - t_6 - 1); +frame.set("loop.first", t_6 === 0); +frame.set("loop.last", t_6 === t_7 - 1); +frame.set("loop.length", t_7); +output += "\n "; +output += runtime.suppressValue((lineno = 29, colno = 29, runtime.callWrap(runtime.contextOrFrameLookup(context, frame, "render_partial"), "render_partial", context, ["partials/model_field.html",runtime.makeKeywordArgs({"nested": false,"field_name": t_9,"field": t_10,"level": 0})])), env.opts.autoescape); +output += "\n "; +; +} +} else { +t_6 = -1; +var t_7 = runtime.keys(t_8).length; +for(var t_11 in t_8) { +t_6++; +var t_12 = t_8[t_11]; +frame.set("field_name", t_11); +frame.set("field", t_12); +frame.set("loop.index", t_6 + 1); +frame.set("loop.index0", t_6); +frame.set("loop.revindex", t_7 - t_6); +frame.set("loop.revindex0", t_7 - t_6 - 1); +frame.set("loop.first", t_6 === 0); +frame.set("loop.last", t_6 === t_7 - 1); +frame.set("loop.length", t_7); +output += "\n "; +output += runtime.suppressValue((lineno = 29, colno = 29, runtime.callWrap(runtime.contextOrFrameLookup(context, frame, "render_partial"), "render_partial", context, ["partials/model_field.html",runtime.makeKeywordArgs({"nested": false,"field_name": t_11,"field": t_12,"level": 0})])), env.opts.autoescape); +output += "\n "; +; +} +} +} +frame = frame.pop(); +output += "\n \n
\n "; +output += runtime.suppressValue(t_4, env.opts.autoescape); +output += "\n "; +output += runtime.suppressValue(runtime.memberLookup((t_5),"type"), env.opts.autoescape); +output += "\n
"; +output += runtime.suppressValue(runtime.memberLookup((t_5),"description"), env.opts.autoescape); +output += "
\n
\n
\n
\n
\n
\n"; +; +} +} else { +t_1 = -1; +var t_2 = runtime.keys(t_3).length; +for(var t_13 in t_3) { +t_1++; +var t_14 = t_3[t_13]; +frame.set("model_name", t_13); +frame.set("model", t_14); +frame.set("loop.index", t_1 + 1); +frame.set("loop.index0", t_1); +frame.set("loop.revindex", t_2 - t_1); +frame.set("loop.revindex0", t_2 - t_1 - 1); +frame.set("loop.first", t_1 === 0); +frame.set("loop.last", t_1 === t_2 - 1); +frame.set("loop.length", t_2); +output += "\n\n
\n
\n
\n
\n\n \n \n \n \n\n \n \n \n "; +frame = frame.push(); +var t_17 = runtime.memberLookup((t_14),"fields"); +if(t_17) {t_17 = runtime.fromIterator(t_17); +var t_15; +if(runtime.isArray(t_17)) { +var t_16 = t_17.length; +for(t_15=0; t_15 < t_17.length; t_15++) { +var t_18 = t_17[t_15][0]; +frame.set("[object Object]", t_17[t_15][0]); +var t_19 = t_17[t_15][1]; +frame.set("[object Object]", t_17[t_15][1]); +frame.set("loop.index", t_15 + 1); +frame.set("loop.index0", t_15); +frame.set("loop.revindex", t_16 - t_15); +frame.set("loop.revindex0", t_16 - t_15 - 1); +frame.set("loop.first", t_15 === 0); +frame.set("loop.last", t_15 === t_16 - 1); +frame.set("loop.length", t_16); +output += "\n "; +output += runtime.suppressValue((lineno = 29, colno = 29, runtime.callWrap(runtime.contextOrFrameLookup(context, frame, "render_partial"), "render_partial", context, ["partials/model_field.html",runtime.makeKeywordArgs({"nested": false,"field_name": t_18,"field": t_19,"level": 0})])), env.opts.autoescape); +output += "\n "; +; +} +} else { +t_15 = -1; +var t_16 = runtime.keys(t_17).length; +for(var t_20 in t_17) { +t_15++; +var t_21 = t_17[t_20]; +frame.set("field_name", t_20); +frame.set("field", t_21); +frame.set("loop.index", t_15 + 1); +frame.set("loop.index0", t_15); +frame.set("loop.revindex", t_16 - t_15); +frame.set("loop.revindex0", t_16 - t_15 - 1); +frame.set("loop.first", t_15 === 0); +frame.set("loop.last", t_15 === t_16 - 1); +frame.set("loop.length", t_16); +output += "\n "; +output += runtime.suppressValue((lineno = 29, colno = 29, runtime.callWrap(runtime.contextOrFrameLookup(context, frame, "render_partial"), "render_partial", context, ["partials/model_field.html",runtime.makeKeywordArgs({"nested": false,"field_name": t_20,"field": t_21,"level": 0})])), env.opts.autoescape); +output += "\n "; +; +} +} +} +frame = frame.pop(); +output += "\n \n
\n "; +output += runtime.suppressValue(t_13, env.opts.autoescape); +output += "\n "; +output += runtime.suppressValue(runtime.memberLookup((t_14),"type"), env.opts.autoescape); +output += "\n
"; +output += runtime.suppressValue(runtime.memberLookup((t_14),"description"), env.opts.autoescape); +output += "
\n
\n
\n
\n
\n
\n"; +; +} +} +} +frame = frame.pop(); +if(parentTemplate) { +parentTemplate.rootRenderFunc(env, context, frame, runtime, cb); +} else { +cb(null, output); +} +; +} catch (e) { + cb(runtime.handleError(e, lineno, colno)); +} +} +return { +root: root +}; + })(); })(); (function() {(window.nunjucksPrecompiled = window.nunjucksPrecompiled || {})["partials/datacontract_servicelevels.html"] = (function() { @@ -1086,6 +1258,62 @@ output += "\n "; } } frame = frame.pop(); +output += "\n "; +if(runtime.memberLookup((runtime.contextOrFrameLookup(context, frame, "definition")),"links")) { +output += "\n "; +frame = frame.push(); +var t_18 = runtime.memberLookup((runtime.contextOrFrameLookup(context, frame, "definition")),"links"); +if(t_18) {t_18 = runtime.fromIterator(t_18); +var t_16; +if(runtime.isArray(t_18)) { +var t_17 = t_18.length; +for(t_16=0; t_16 < t_18.length; t_16++) { +var t_19 = t_18[t_16][0]; +frame.set("[object Object]", t_18[t_16][0]); +var t_20 = t_18[t_16][1]; +frame.set("[object Object]", t_18[t_16][1]); +frame.set("loop.index", t_16 + 1); +frame.set("loop.index0", t_16); +frame.set("loop.revindex", t_17 - t_16); +frame.set("loop.revindex0", t_17 - t_16 - 1); +frame.set("loop.first", t_16 === 0); +frame.set("loop.last", t_16 === t_17 - 1); +frame.set("loop.length", t_17); +output += "\n "; +output += runtime.suppressValue(t_19, env.opts.autoescape); +output += "\n "; +; +} +} else { +t_16 = -1; +var t_17 = runtime.keys(t_18).length; +for(var t_21 in t_18) { +t_16++; +var t_22 = t_18[t_21]; +frame.set("name", t_21); +frame.set("href", t_22); +frame.set("loop.index", t_16 + 1); +frame.set("loop.index0", t_16); +frame.set("loop.revindex", t_17 - t_16); +frame.set("loop.revindex0", t_17 - t_16 - 1); +frame.set("loop.first", t_16 === 0); +frame.set("loop.last", t_16 === t_17 - 1); +frame.set("loop.length", t_17); +output += "\n "; +output += runtime.suppressValue(t_21, env.opts.autoescape); +output += "\n "; +; +} +} +} +frame = frame.pop(); +output += "\n "; +; +} output += "\n \n \n\n \n \n \n \n \n \n"; if(parentTemplate) { parentTemplate.rootRenderFunc(env, context, frame, runtime, cb); @@ -1359,11 +1587,11 @@ output += "\n "; } } frame = frame.pop(); -output += "\n \n \n\n\n"; -if(runtime.memberLookup((runtime.contextOrFrameLookup(context, frame, "field")),"fields")) { -output += "\n"; +output += "\n "; +if(runtime.memberLookup((runtime.contextOrFrameLookup(context, frame, "field")),"links")) { +output += "\n "; frame = frame.push(); -var t_14 = runtime.memberLookup((runtime.contextOrFrameLookup(context, frame, "field")),"fields"); +var t_14 = runtime.memberLookup((runtime.contextOrFrameLookup(context, frame, "field")),"links"); if(t_14) {t_14 = runtime.fromIterator(t_14); var t_12; if(runtime.isArray(t_14)) { @@ -1380,9 +1608,11 @@ frame.set("loop.revindex0", t_13 - t_12 - 1); frame.set("loop.first", t_12 === 0); frame.set("loop.last", t_12 === t_13 - 1); frame.set("loop.length", t_13); -output += "\n "; -output += runtime.suppressValue((lineno = 96, colno = 19, runtime.callWrap(runtime.contextOrFrameLookup(context, frame, "render_partial"), "render_partial", context, ["partials/model_field.html",runtime.makeKeywordArgs({"nested": true,"field_name": t_15,"field": t_16,"level": runtime.contextOrFrameLookup(context, frame, "level") + 1})])), env.opts.autoescape); -output += "\n"; +output += "\n "; +output += runtime.suppressValue(t_15, env.opts.autoescape); +output += "\n "; ; } } else { @@ -1391,8 +1621,8 @@ var t_13 = runtime.keys(t_14).length; for(var t_17 in t_14) { t_12++; var t_18 = t_14[t_17]; -frame.set("field_name", t_17); -frame.set("field", t_18); +frame.set("name", t_17); +frame.set("href", t_18); frame.set("loop.index", t_12 + 1); frame.set("loop.index0", t_12); frame.set("loop.revindex", t_13 - t_12); @@ -1400,15 +1630,98 @@ frame.set("loop.revindex0", t_13 - t_12 - 1); frame.set("loop.first", t_12 === 0); frame.set("loop.last", t_12 === t_13 - 1); frame.set("loop.length", t_13); -output += "\n "; -output += runtime.suppressValue((lineno = 96, colno = 19, runtime.callWrap(runtime.contextOrFrameLookup(context, frame, "render_partial"), "render_partial", context, ["partials/model_field.html",runtime.makeKeywordArgs({"nested": true,"field_name": t_17,"field": t_18,"level": runtime.contextOrFrameLookup(context, frame, "level") + 1})])), env.opts.autoescape); +output += "\n "; +output += runtime.suppressValue(t_17, env.opts.autoescape); +output += "\n "; +; +} +} +} +frame = frame.pop(); +output += "\n "; +; +} +output += "\n \n \n\n\n"; +var macro_t_19 = runtime.makeMacro( +["field_name", "field", "level"], +[], +function (l_field_name, l_field, l_level, kwargs) { +var callerFrame = frame; +frame = new runtime.Frame(); +kwargs = kwargs || {}; +if (Object.prototype.hasOwnProperty.call(kwargs, "caller")) { +frame.set("caller", kwargs.caller); } +frame.set("field_name", l_field_name); +frame.set("field", l_field); +frame.set("level", l_level); +var t_20 = "";t_20 += "\n"; +t_20 += runtime.suppressValue((lineno = 100, colno = 17, runtime.callWrap(runtime.contextOrFrameLookup(context, frame, "render_partial"), "render_partial", context, ["partials/model_field.html",runtime.makeKeywordArgs({"nested": true,"field_name": l_field_name,"field": l_field,"level": l_level + 1})])), env.opts.autoescape); +t_20 += "\n\n\n"; +; +frame = callerFrame; +return new runtime.SafeString(t_20); +}); +context.addExport("render_nested_partial"); +context.setVariable("render_nested_partial", macro_t_19); +output += "\n\n"; +if(runtime.memberLookup((runtime.contextOrFrameLookup(context, frame, "field")),"fields")) { +output += "\n"; +frame = frame.push(); +var t_23 = runtime.memberLookup((runtime.contextOrFrameLookup(context, frame, "field")),"fields"); +if(t_23) {t_23 = runtime.fromIterator(t_23); +var t_21; +if(runtime.isArray(t_23)) { +var t_22 = t_23.length; +for(t_21=0; t_21 < t_23.length; t_21++) { +var t_24 = t_23[t_21][0]; +frame.set("[object Object]", t_23[t_21][0]); +var t_25 = t_23[t_21][1]; +frame.set("[object Object]", t_23[t_21][1]); +frame.set("loop.index", t_21 + 1); +frame.set("loop.index0", t_21); +frame.set("loop.revindex", t_22 - t_21); +frame.set("loop.revindex0", t_22 - t_21 - 1); +frame.set("loop.first", t_21 === 0); +frame.set("loop.last", t_21 === t_22 - 1); +frame.set("loop.length", t_22); +output += "\n"; +output += runtime.suppressValue((lineno = 107, colno = 24, runtime.callWrap(macro_t_19, "render_nested_partial", context, [t_24,t_25,runtime.contextOrFrameLookup(context, frame, "level")])), env.opts.autoescape); +output += "\n"; +; +} +} else { +t_21 = -1; +var t_22 = runtime.keys(t_23).length; +for(var t_26 in t_23) { +t_21++; +var t_27 = t_23[t_26]; +frame.set("field_name", t_26); +frame.set("field", t_27); +frame.set("loop.index", t_21 + 1); +frame.set("loop.index0", t_21); +frame.set("loop.revindex", t_22 - t_21); +frame.set("loop.revindex0", t_22 - t_21 - 1); +frame.set("loop.first", t_21 === 0); +frame.set("loop.last", t_21 === t_22 - 1); +frame.set("loop.length", t_22); +output += "\n"; +output += runtime.suppressValue((lineno = 107, colno = 24, runtime.callWrap(macro_t_19, "render_nested_partial", context, [t_26,t_27,runtime.contextOrFrameLookup(context, frame, "level")])), env.opts.autoescape); output += "\n"; ; } } } frame = frame.pop(); -output += "\n\n\n\n"; +output += "\n"; +; +} +output += "\n\n"; +if(runtime.memberLookup((runtime.contextOrFrameLookup(context, frame, "field")),"items")) { +output += "\n"; +output += runtime.suppressValue((lineno = 112, colno = 24, runtime.callWrap(macro_t_19, "render_nested_partial", context, ["item",runtime.memberLookup((runtime.contextOrFrameLookup(context, frame, "field")),"items"),runtime.contextOrFrameLookup(context, frame, "level")])), env.opts.autoescape); +output += "\n"; ; } if(parentTemplate) {