-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(settings): new settings and custom settings feature
See [forum post](https://narrat.discourse.group/t/custom-settings-feature/34)
- Loading branch information
Showing
21 changed files
with
428 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
packages/narrat/src/components/settings/setting-widget.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<template> | ||
<div class="settings-widget-container"> | ||
<label | ||
:for="settingId" | ||
class="setting-label" | ||
:id="`setting-label-${settingId}`" | ||
> | ||
{{ schema.name }} | ||
</label> | ||
<input | ||
v-if="isSettingNumber(schema) || isSettingInteger(schema)" | ||
ref="slider" | ||
class="number-slider setting-slider" | ||
type="range" | ||
:id="`setting-slider-${settingId}`" | ||
:name="settingId" | ||
:min="schema.minValue" | ||
:max="schema.maxValue" | ||
:step="schema.step" | ||
v-model="settingValue" | ||
/> | ||
<input | ||
v-else-if="isSettingBoolean(schema)" | ||
type="checkbox" | ||
class="setting-checkbox" | ||
:id="`setting-checkbox-${settingId}`" | ||
:name="settingId" | ||
v-model="settingValue" | ||
/> | ||
<input | ||
v-else-if="isSettingString(schema)" | ||
type="text" | ||
class="setting-text" | ||
:id="`setting-text-${settingId}`" | ||
:name="settingId" | ||
v-model="settingValue" | ||
/> | ||
<span class="mx-8">{{ settingValue }}</span> | ||
</div> | ||
<p class="text-left setting-description">{{ schema.description }}</p> | ||
<hr | ||
class="h-px my-8 bg-gray-200 border-0 dark:bg-gray-700 setting-separator" | ||
/> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import { computed, ref, watch } from 'vue'; | ||
import { useSettings } from '../../stores/settings-store'; | ||
import { | ||
isSettingNumber, | ||
isSettingBoolean, | ||
isSettingInteger, | ||
isSettingString, | ||
} from '../../config/settings-config'; | ||
export interface SettingWidgetProps { | ||
settingId: string; | ||
} | ||
const props = defineProps<SettingWidgetProps>(); | ||
const settings = useSettings(); | ||
const startValue = computed(() => { | ||
return settings.getSetting(props.settingId); | ||
}); | ||
const settingValue = ref(startValue.value); | ||
const schema = computed(() => { | ||
return settings.getSettingSchema(props.settingId)!; | ||
}); | ||
function valueChanged(newValue: number) { | ||
let value = newValue; | ||
if (isSettingInteger(schema.value)) { | ||
value = Math.round(value); | ||
} | ||
settings.setSetting(props.settingId, value); | ||
} | ||
watch(settingValue, (newValue: any) => { | ||
valueChanged(newValue); | ||
}); | ||
</script> | ||
<style> | ||
.setting-label { | ||
/* margin: 5px 20px; */ | ||
margin-right: 10px; | ||
font-size: 1.25rem; | ||
font-weight: 700; | ||
/* width: 180px; */ | ||
text-align: left; | ||
margin-right: 20px; | ||
} | ||
.setting-slider { | ||
flex-grow: 2; | ||
background-color: blue; | ||
} | ||
.settings-widget-container { | ||
width: 100%; | ||
font-size: 1.5rem; | ||
display: flex; | ||
align-items: center; | ||
justify-content: flex-start; | ||
} | ||
.setting-description { | ||
margin-left: 2rem; | ||
} | ||
.setting-separator { | ||
background-color: var(--separator-bg-color); | ||
width: var(--separator-width); | ||
height: var(--separator-height); | ||
margin-top: 0.5rem; | ||
margin-bottom: 0.5rem; | ||
text-align: center; | ||
margin-left: auto; | ||
margin-right: auto; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<template> | ||
<div class="container mx-auto settings-menu-container"> | ||
<h2 class="settings-menu-title subtitle text-center">Settings</h2> | ||
<SettingWidget | ||
v-for="(schema, id) in schemas" | ||
:settingId="(id as string)" | ||
:key="id" | ||
/> | ||
</div> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import { computed } from 'vue'; | ||
import { useSettings } from '../../stores/settings-store'; | ||
import SettingWidget from './setting-widget.vue'; | ||
const settings = useSettings(); | ||
const schemas = computed(() => { | ||
return settings.getAllSettingSchemas(); | ||
}); | ||
</script> | ||
|
||
<style> | ||
.settings-menu-title { | ||
margin-top: 1rem; | ||
margin-bottom: 1rem; | ||
} | ||
.settings-menu-container { | ||
position: relative; | ||
display: flex; | ||
flex-direction: column; | ||
align-items: flex-start; | ||
justify-content: center; | ||
background: var(--light-background); | ||
border: 1px dashed white; | ||
padding: 20px; | ||
margin: 20px 0; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { Type, Static } from '@sinclair/typebox'; | ||
|
||
export const CustomSettingGenericSchema = Type.Object({ | ||
name: Type.String(), | ||
description: Type.Optional(Type.String()), | ||
}); | ||
|
||
export const CustomSettingsNumberSchema = Type.Intersect([ | ||
CustomSettingGenericSchema, | ||
Type.Object({ | ||
type: Type.Literal('number'), | ||
defaultValue: Type.Number(), | ||
step: Type.Number(), | ||
minValue: Type.Number(), | ||
maxValue: Type.Number(), | ||
}), | ||
]); | ||
export type CustomSettingsNumber = Static<typeof CustomSettingsNumberSchema>; | ||
|
||
export const CustomSettingsIntegerSchema = Type.Intersect([ | ||
CustomSettingGenericSchema, | ||
Type.Object({ | ||
type: Type.Literal('integer'), | ||
defaultValue: Type.Number(), | ||
step: Type.Number(), | ||
minValue: Type.Number(), | ||
maxValue: Type.Number(), | ||
}), | ||
]); | ||
export type CustomSettingsInteger = Static<typeof CustomSettingsIntegerSchema>; | ||
|
||
export const CustomSettingsBooleanSchema = Type.Intersect([ | ||
CustomSettingGenericSchema, | ||
Type.Object({ | ||
type: Type.Literal('boolean'), | ||
defaultValue: Type.Boolean(), | ||
}), | ||
]); | ||
export type CustomSettingsBoolean = Static<typeof CustomSettingsBooleanSchema>; | ||
|
||
export const CustomSettingsStringSchema = Type.Intersect([ | ||
CustomSettingGenericSchema, | ||
Type.Object({ | ||
type: Type.Literal('string'), | ||
defaultValue: Type.String(), | ||
}), | ||
]); | ||
export type CustomSettingsString = Static<typeof CustomSettingsStringSchema>; | ||
|
||
export const CustomSettingSchema = Type.Union([ | ||
CustomSettingsNumberSchema, | ||
CustomSettingsIntegerSchema, | ||
CustomSettingsBooleanSchema, | ||
CustomSettingsStringSchema, | ||
]); | ||
export type CustomSetting = Static<typeof CustomSettingSchema>; | ||
export function isSettingNumber( | ||
setting: CustomSetting, | ||
): setting is CustomSettingsNumber { | ||
return setting.type === 'number'; | ||
} | ||
export function isSettingInteger( | ||
setting: CustomSetting, | ||
): setting is CustomSettingsInteger { | ||
return setting.type === 'integer'; | ||
} | ||
export function isSettingBoolean( | ||
setting: CustomSetting, | ||
): setting is CustomSettingsBoolean { | ||
return setting.type === 'boolean'; | ||
} | ||
export function isSettingString( | ||
setting: CustomSetting, | ||
): setting is CustomSettingsString { | ||
return setting.type === 'string'; | ||
} | ||
export const SettingsConfigSchema = Type.Object({ | ||
customSettings: Type.Optional( | ||
Type.Record(Type.String(), CustomSettingSchema), | ||
), | ||
}); | ||
export type SettingsConfig = Static<typeof SettingsConfigSchema>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.