From aae8912c089e25e33b0f9f94196a09f5e9501323 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Sun, 15 Sep 2024 19:48:32 -0700 Subject: [PATCH] Add support for custom input modes Signed-off-by: Danila Fedorin --- assets/js/bergamot-helpers.js | 20 ++++++++++++-------- layouts/partials/bergamotinputmode.html | 6 ++++++ layouts/partials/bergamotpreset.html | 1 + layouts/partials/head.html | 7 ++++++- 4 files changed, 25 insertions(+), 9 deletions(-) create mode 100644 layouts/partials/bergamotinputmode.html diff --git a/assets/js/bergamot-helpers.js b/assets/js/bergamot-helpers.js index 05aecf6..9a84f79 100644 --- a/assets/js/bergamot-helpers.js +++ b/assets/js/bergamot-helpers.js @@ -42,7 +42,7 @@ const ensureObjectLanguage = () => { } return window.Bergamot.ObjectLanguage; } -const parseString = (str) => { +const parseBergamotObjectLanguage = (str) => { if (!(str in parsingPromiseResolvers)) { parsingPromiseResolvers[str] = []; } @@ -54,20 +54,21 @@ const parseString = (str) => { } window.Bergamot = {}; -window.Bergamot.run = (inputGroup, nodeId, inputPrompt, rules, renderPreset, input) => { +window.Bergamot.run = (inputGroup, nodeId, inputModes, inputPrompt, rules, renderPreset, input) => { var app = Elm.Main.init({ node: document.getElementById(nodeId), flags: { - inputModes: { - "Languge Term": { "custom": "Language Term" }, - "Query": "query" - }, + inputModes, renderRules: window.Bergamot.renderPresets[renderPreset], rules, input } }); app.ports.convertInput.subscribe(async ({ mode, input }) => { - let query = await parseString(input); + if (!(mode in window.Bergamot.inputModes)) { + app.ports.receiveConverted.send({ input, result: { error: "Improperly configured desugaring function (this is the website developer's fault)" } }); + } + + let query = await (window.Bergamot.inputModes[mode])(input); if (query !== null) { query = inputPrompt.replace("TERM", query); app.ports.receiveConverted.send({ input, result: { query } }); @@ -80,7 +81,7 @@ window.Bergamot.run = (inputGroup, nodeId, inputPrompt, rules, renderPreset, inp }; window.Bergamot.runPreset = (inputGroup, nodeId, presetName) => { const preset = window.Bergamot.presets[presetName]; - window.Bergamot.run(inputGroup, nodeId, preset.inputPrompt, preset.rules, preset.renderPreset, preset.query || ""); + window.Bergamot.run(inputGroup, nodeId, preset.inputModes, preset.inputPrompt, preset.rules, preset.renderPreset, preset.query || ""); }; window.Bergamot.close = (inputGroup, nodeId) => { if (!(nodeId in loadedWidgets)) return; @@ -94,5 +95,8 @@ window.Bergamot.close = (inputGroup, nodeId) => { delete loadedWidgets[nodeId]; setRunning(inputGroup, false); } +window.Bergamot.inputModes = { + "Bergamot Object Language": parseBergamotObjectLanguage +}; window.Bergamot.presets = {}; window.Bergamot.renderPresets = {}; diff --git a/layouts/partials/bergamotinputmode.html b/layouts/partials/bergamotinputmode.html new file mode 100644 index 0000000..9f684f5 --- /dev/null +++ b/layouts/partials/bergamotinputmode.html @@ -0,0 +1,6 @@ + + diff --git a/layouts/partials/bergamotpreset.html b/layouts/partials/bergamotpreset.html index 85a55cd..789841c 100644 --- a/layouts/partials/bergamotpreset.html +++ b/layouts/partials/bergamotpreset.html @@ -2,6 +2,7 @@ window.addEventListener('load', function() { window.Bergamot.presets['{{ .name }}'] = { rules: {{ .file }}, + inputModes: {{ partial "bergamotparseinputmodes.js" .modes | safeJS }}, inputPrompt: '{{ .prompt }}', query: '{{ .query }}', renderPreset: '{{ default "default" .renderPreset }}' diff --git a/layouts/partials/head.html b/layouts/partials/head.html index 791ac04..f7872f1 100644 --- a/layouts/partials/head.html +++ b/layouts/partials/head.html @@ -72,10 +72,15 @@ {{ if .Params.bergamot.presets }} {{ range $name, $preset := .Params.bergamot.presets }} {{ $file := default (resources.Get $preset.file) (page.Resources.Get $preset.file) }} - {{ $info := dict "name" $name "prompt" $preset.prompt "query" $preset.query "file" $file.Content "renderPreset" $preset.render_preset }} + {{ $info := dict "name" $name "prompt" $preset.prompt "query" $preset.query "file" $file.Content "renderPreset" $preset.render_preset "inputModes" $preset.input_modes }} {{ partial "bergamotpreset.html" $info }} {{ end }} {{ end }} + {{ if .Params.bergamot.input_modes }} + {{ range $mode := .Params.bergamot.input_modes }} + {{ partial "bergamotinputmode.html" (dict "name" $mode.name "fn" $mode.fn) }} + {{ end }} + {{ end }} {{ end }} {{ with .Site.Params.plausibleAnalyticsDomain }}