Add support for custom input modes

Signed-off-by: Danila Fedorin <danila.fedorin@gmail.com>
This commit is contained in:
Danila Fedorin 2024-09-15 19:48:32 -07:00
parent 84278b2e91
commit aae8912c08
4 changed files with 25 additions and 9 deletions

View File

@ -42,7 +42,7 @@ const ensureObjectLanguage = () => {
} }
return window.Bergamot.ObjectLanguage; return window.Bergamot.ObjectLanguage;
} }
const parseString = (str) => { const parseBergamotObjectLanguage = (str) => {
if (!(str in parsingPromiseResolvers)) { if (!(str in parsingPromiseResolvers)) {
parsingPromiseResolvers[str] = []; parsingPromiseResolvers[str] = [];
} }
@ -54,20 +54,21 @@ const parseString = (str) => {
} }
window.Bergamot = {}; 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({ var app = Elm.Main.init({
node: document.getElementById(nodeId), node: document.getElementById(nodeId),
flags: { flags: {
inputModes: { inputModes,
"Languge Term": { "custom": "Language Term" },
"Query": "query"
},
renderRules: window.Bergamot.renderPresets[renderPreset], renderRules: window.Bergamot.renderPresets[renderPreset],
rules, input rules, input
} }
}); });
app.ports.convertInput.subscribe(async ({ mode, 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) { if (query !== null) {
query = inputPrompt.replace("TERM", query); query = inputPrompt.replace("TERM", query);
app.ports.receiveConverted.send({ input, result: { 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) => { window.Bergamot.runPreset = (inputGroup, nodeId, presetName) => {
const preset = window.Bergamot.presets[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) => { window.Bergamot.close = (inputGroup, nodeId) => {
if (!(nodeId in loadedWidgets)) return; if (!(nodeId in loadedWidgets)) return;
@ -94,5 +95,8 @@ window.Bergamot.close = (inputGroup, nodeId) => {
delete loadedWidgets[nodeId]; delete loadedWidgets[nodeId];
setRunning(inputGroup, false); setRunning(inputGroup, false);
} }
window.Bergamot.inputModes = {
"Bergamot Object Language": parseBergamotObjectLanguage
};
window.Bergamot.presets = {}; window.Bergamot.presets = {};
window.Bergamot.renderPresets = {}; window.Bergamot.renderPresets = {};

View File

@ -0,0 +1,6 @@
<script>
window.addEventListener('load', function() {
window.Bergamot.inputModes['{{ .name }}'] = {{ .fn | safeJS }};
});
</script>

View File

@ -2,6 +2,7 @@
window.addEventListener('load', function() { window.addEventListener('load', function() {
window.Bergamot.presets['{{ .name }}'] = { window.Bergamot.presets['{{ .name }}'] = {
rules: {{ .file }}, rules: {{ .file }},
inputModes: {{ partial "bergamotparseinputmodes.js" .modes | safeJS }},
inputPrompt: '{{ .prompt }}', inputPrompt: '{{ .prompt }}',
query: '{{ .query }}', query: '{{ .query }}',
renderPreset: '{{ default "default" .renderPreset }}' renderPreset: '{{ default "default" .renderPreset }}'

View File

@ -72,10 +72,15 @@
{{ if .Params.bergamot.presets }} {{ if .Params.bergamot.presets }}
{{ range $name, $preset := .Params.bergamot.presets }} {{ range $name, $preset := .Params.bergamot.presets }}
{{ $file := default (resources.Get $preset.file) (page.Resources.Get $preset.file) }} {{ $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 }} {{ partial "bergamotpreset.html" $info }}
{{ end }} {{ end }}
{{ 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 }} {{ end }}
{{ with .Site.Params.plausibleAnalyticsDomain }} {{ with .Site.Params.plausibleAnalyticsDomain }}