Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix truths generation #1027

Merged
merged 6 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Next Release

- Cursed die support when rolling oracles ([#1026](https://github.com/ben/foundry-ironsworn/pull/1026))
- Fix an issue with the truths dialog resulting in blank outputs ([#1027](https://github.com/ben/foundry-ironsworn/pull/1027))

## 1.24.2

Expand Down
18 changes: 13 additions & 5 deletions src/module/vue/components/truth/custom-truth.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@
type="radio"
:name="radioGroup"
class="nogrow"
@change="select" />
@change="select"
/>
<MceEditor
ref="editor"
v-model="state.html"
:style="{ height: state.height, 'min-height': state.height }"
@save="emitHtml" />
@save="emitHtml"
/>
</label>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { onBeforeUnmount, reactive, ref } from 'vue'
import MceEditor from '../mce-editor.vue'

const props = defineProps<{
defineProps<{
radioGroup: string
}>()

Expand All @@ -35,11 +37,17 @@ function select() {
state.height = '300px'
}

// Do not emit change events from the editor if we've been unmounted
let emitChanges = true
onBeforeUnmount(() => {
emitChanges = false
})

const $emit = defineEmits<{
change: [string]
}>()
function emitHtml() {
$emit('change', state.html)
if (emitChanges) $emit('change', state.html)
}
</script>

Expand Down
23 changes: 16 additions & 7 deletions src/module/vue/components/truth/truth-category.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ const props = defineProps<{
je: () => IronswornJournalEntry
}>()

const model = defineModel()

type NonTruthPage = IronswornJournalPage<Exclude<JournalEntryPageType, 'truth'>>

const truthPages = props.je()?.pageTypes.truth
Expand All @@ -57,32 +59,39 @@ const state = reactive<{
title?: string
text?: string
html?: string
custom?: boolean
}>({})
function valueChange(title: string, text: string) {
async function valueChange(title: string, text: string) {
state.title = title
state.text = text
state.html = undefined
model.value = await selectedValue()
}
function customValueChange(html: string) {
async function customValueChange(html: string) {
state.title = undefined
state.text = undefined
state.html = html
model.value = await selectedValue()
}

async function selectedValue() {
let html = state.html
if (!html) {
html = `
${await enrichMarkdown(`**${state.title}**`)}
${await enrichMarkdown(state.text)}
`
if (state.title) {
html = `
${await enrichMarkdown(`**${state.title}**`)}
${await enrichMarkdown(state.text)}
`
} else {
html = await enrichMarkdown(state.text)
}
}
html += nonTruthPages?.map((x) => x.text.content).join('\n\n')

return {
title: props.je()?.name,
html: html.trim(),
valid: !!(state.title || state.html)
valid: true
}
}

Expand Down
12 changes: 10 additions & 2 deletions src/module/vue/components/truth/truth-selectable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ const $emit = defineEmits<{
change: [string, string] // title, text
}>()
function emitValue() {
let text = `${pageSystem.Description} ${suboption ?? ''}\n\n_${
// Some pages (i.e. Classic) don't have a title
let title = props.page.name ?? ''
if (title?.startsWith('truth.option')) {
title = ''
}

let text = `${pageSystem.Description} ${suboption.value ?? ''}\n\n_${
pageSystem.Quest
}_`

Expand All @@ -83,7 +89,7 @@ function emitValue() {
template.Description.replace(/{{table>.*?}}/, `> ${suboption.value}`) +
`\n\n_${pageSystem.Quest}_`
}
$emit('change', props.page.name ?? '???', text.trim())
$emit('change', title, text.trim())
}

const suboptions = ref<HTMLElement[]>([])
Expand All @@ -104,6 +110,8 @@ async function selectAndRandomize() {
)
suboptions.value[selectedIndex]?.click()
}

emitValue()
}

defineExpose({ selectAndRandomize })
Expand Down
30 changes: 18 additions & 12 deletions src/module/vue/sf-truths.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
ref="categoryComponents"
:key="(truth.je()._id as string)"
:je="truth.je"
v-model="categoryModels[truth.je()._id]"
/>
</section>
</div>
Expand All @@ -57,6 +58,13 @@ const props = defineProps<{
}>()

const categoryComponents = ref<(typeof TruthCategory)[]>([])
const categoryModels = ref<
Record<string, { title?: string; html?: string; valid: boolean }>
>(
Object.fromEntries(
props.data.truths.map((x) => [x.je()._id, { valid: false }])
)
)

function scrollToCategory(i: number) {
categoryComponents.value[i]?.scrollIntoView()
Expand All @@ -65,19 +73,17 @@ function scrollToCategory(i: number) {
const $localEmitter = inject($LocalEmitterKey)
async function saveTruths() {
// Fetch values from the category components
const allValues = await Promise.all(
categoryComponents.value.map((x) => x.selectedValue())
)
const values = allValues.filter((x) => x.valid)
const contentSections: string[] = []
for (const t of props.data.truths) {
const model = categoryModels.value[t.je()._id]
if (model.valid)
contentSections.push(
`<h2>${model.title}</h2>
${model.html}`
)
}

const content = values
.map(
({ title, html }) => `
<h2>${title}</h2>
${html}
`
)
.join('\n\n')
const content = contentSections.join('\n\n')

const title = game.i18n.localize('IRONSWORN.JOURNALENTRYPAGES.TypeTruth')
const journal = await IronswornJournalEntry.create({
Expand Down
Loading