MapComplete/src/UI/Studio/QuestionPreview.svelte

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

128 lines
4.1 KiB
Svelte
Raw Normal View History

<script lang="ts">
2023-11-09 16:30:26 +01:00
import type { ConfigMeta } from "./configMeta"
import EditLayerState from "./EditLayerState"
import * as questions from "../../assets/generated/layers/questions.json"
import { ImmutableStore, Store } from "../../Logic/UIEventSource"
import TagRenderingEditable from "../Popup/TagRendering/TagRenderingEditable.svelte"
import TagRenderingConfig from "../../Models/ThemeConfig/TagRenderingConfig"
import nmd from "nano-markdown"
import type { QuestionableTagRenderingConfigJson } from "../../Models/ThemeConfig/Json/QuestionableTagRenderingConfigJson.js"
import type { TagRenderingConfigJson } from "../../Models/ThemeConfig/Json/TagRenderingConfigJson"
import FromHtml from "../Base/FromHtml.svelte"
import ShowConversionMessage from "./ShowConversionMessage.svelte"
import NextButton from "../Base/NextButton.svelte"
2023-11-09 16:30:26 +01:00
export let state: EditLayerState
export let path: ReadonlyArray<string | number>
export let schema: ConfigMeta
let value = state.getStoreFor(path)
2023-11-09 16:30:26 +01:00
let perId: Record<string, TagRenderingConfigJson[]> = {}
2023-11-05 12:05:00 +01:00
for (let tagRendering of questions.tagRenderings) {
if (tagRendering.labels) {
2023-11-05 12:05:00 +01:00
for (let label of tagRendering.labels) {
2023-11-09 16:30:26 +01:00
perId[label] = (perId[label] ?? []).concat(tagRendering)
}
}
2023-11-09 16:30:26 +01:00
perId[tagRendering.id] = [tagRendering]
}
2023-11-09 16:30:26 +01:00
let configJson: Store<QuestionableTagRenderingConfigJson[]> = value.map((x) => {
if (typeof x === "string") {
2023-11-09 16:30:26 +01:00
return perId[x]
} else {
2023-11-09 16:30:26 +01:00
return [x]
}
2023-11-09 16:30:26 +01:00
})
let configs: Store<TagRenderingConfig[]> = configJson.map((configs) => {
2023-11-05 12:05:00 +01:00
if (!configs) {
2023-11-09 16:30:26 +01:00
return [{ error: "No configuartions found" }]
2023-11-02 04:35:32 +01:00
}
2023-11-09 16:30:26 +01:00
console.log("Regenerating configs")
return configs.map((config) => {
2023-11-05 12:05:00 +01:00
try {
2023-11-09 16:30:26 +01:00
return new TagRenderingConfig(config)
2023-11-05 12:05:00 +01:00
} catch (e) {
2023-11-09 16:30:26 +01:00
return { error: e }
2023-11-05 12:05:00 +01:00
}
2023-11-09 16:30:26 +01:00
})
})
let id: Store<string> = value.mapD((c) => {
2023-11-02 04:35:32 +01:00
if (c?.id) {
2023-11-09 16:30:26 +01:00
return c.id
}
if (typeof c === "string") {
2023-11-09 16:30:26 +01:00
return c
}
2023-11-09 16:30:26 +01:00
return undefined
})
2023-11-09 16:30:26 +01:00
let tags = state.testTags
2023-11-09 16:30:26 +01:00
let messages = state.messagesFor(path)
2023-11-09 16:30:26 +01:00
let description = schema.description
2023-11-05 12:05:00 +01:00
if (description) {
try {
2023-11-09 16:30:26 +01:00
description = nmd(description)
2023-11-05 12:05:00 +01:00
} catch (e) {
2023-11-09 16:30:26 +01:00
console.error("Could not convert description to markdown", { description })
2023-11-02 04:35:32 +01:00
}
}
</script>
<div class="flex">
2023-11-09 16:30:26 +01:00
<div class="interactive border-interactive m-4 flex w-full flex-col">
{#if $id}
TagRendering {$id}
{/if}
2023-11-09 16:30:26 +01:00
<NextButton clss="primary" on:click={() => state.highlightedItem.setData({ path, schema })}>
{#if schema.hints.question}
{schema.hints.question}
{/if}
</NextButton>
2023-11-02 04:35:32 +01:00
{#if description}
<FromHtml src={description} />
{/if}
{#each $messages as message}
2023-11-05 12:05:00 +01:00
<ShowConversionMessage {message} />
{/each}
2023-11-09 16:30:26 +01:00
<slot class="my-4 self-end" />
</div>
2023-11-09 16:30:26 +01:00
<div class="m-4 flex w-full flex-col">
<h3>Preview of this question</h3>
{#each $configs as config}
2023-11-05 12:05:00 +01:00
{#if config.error !== undefined}
<div class="alert">Could not create a preview of this tagRendering: {config.error}</div>
2023-11-09 16:30:26 +01:00
{:else if config.condition && !config.condition.matchesProperties($tags)}
This tagRendering is currently not shown. It will appear if the feature matches the
condition
<b>
<FromHtml src={config.condition.asHumanString(true, false, {})} />
</b>
Try to answer the relevant question above
{:else if config.metacondition && !config.metacondition.matchesProperties($tags)}
This tagRendering is currently not shown. It will appear if the feature matches the
metacondition
<b>
<FromHtml src={config.metacondition.asHumanString(true, false, {})} />
</b>
For a breakdown of usable meta conditions, go to a mapcomplete theme > settings and enable debug-data.
The meta-tags will appear at the bottom
2023-11-05 12:05:00 +01:00
{:else}
2023-11-09 16:30:26 +01:00
<TagRenderingEditable
selectedElement={state.exampleFeature}
{config}
editingEnabled={new ImmutableStore(true)}
showQuestionIfUnknown={true}
{state}
{tags}
/>
2023-11-05 12:05:00 +01:00
{/if}
{/each}
</div>
</div>