All files / src/components FlowSpaceSettings.vue

100% Statements 39/39
97.29% Branches 36/37
100% Functions 17/17
100% Lines 36/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233      5x               2x       5x               2x           5x 66x               1x                       5x             2x   88x         5x           2x               5x                 1x         5x                       5x 41x           5x 5x                       2x                                                       18x                       18x   18x   18x   18x             21x 21x   18x 21x     18x     18x 18x 18x 19x             10x                 6x                                                    
<template>
  <!-- A tab of the flow form, which carries its "Public space" title -->
  <div class="space-settings mb-3" data-testid="space-settings">
    <p class="small text-muted mb-2">
      {{ t('space.settingsHelp') }}
      <a :href="publicUrl" target="_blank" rel="noopener" data-testid="space-public-link">{{
        publicUrl
      }}</a>
    </p>
    <p v-if="canEditPages" class="small mb-2">
      <router-link :to="flowRoute(slug, 'space-pages')" data-testid="space-manage-pages">
        <font-awesome-icon icon="file-lines" class="me-1" />{{ t('space.managePages') }}
      </router-link>
    </p>
 
    <div class="form-check form-switch mb-3">
      <input
        id="spaceEnabled"
        :checked="space.enabled"
        type="checkbox"
        role="switch"
        class="form-check-input"
        :disabled="disabled"
        @change="update({ enabled: ($event.target as HTMLInputElement).checked })"
      />
      <label class="form-check-label" for="spaceEnabled">{{ t('space.enabled') }}</label>
      <div v-if="!space.enabled" class="small text-muted">{{ t('space.notPublishedHint') }}</div>
    </div>
 
    <div class="row g-3">
      <div v-for="key in COLOR_KEYS" :key="key" class="col-sm-4">
        <label :for="`space-${key}`" class="form-label small">{{ t(`space.${key}`) }}</label>
        <input
          :id="`space-${key}`"
          :value="theme[key]"
          type="color"
          class="form-control form-control-color w-100"
          :disabled="disabled"
          @input="updateTheme({ [key]: ($event.target as HTMLInputElement).value })"
        />
        <div
          v-if="key !== 'secondaryColor' && isHardToRead(theme[key])"
          class="small text-warning-emphasis mt-1"
          :data-testid="`space-contrast-warning-${key}`"
        >
          <font-awesome-icon icon="exclamation-triangle" />
          {{ t('space.contrastWarning') }}
        </div>
      </div>
 
      <div class="col-sm-8">
        <label for="space-font" class="form-label small">{{ t('space.font') }}</label>
        <select
          id="space-font"
          :value="theme.font"
          class="form-select"
          :disabled="disabled"
          @change="updateTheme({ font: ($event.target as HTMLSelectElement).value as SpaceFont })"
        >
          <option v-for="font in SPACE_FONTS" :key="font" :value="font">
            {{ t(FONT_LABEL_KEYS[font]) }}
          </option>
        </select>
      </div>
      <div class="col-sm-4 d-flex align-items-end">
        <button
          type="button"
          class="btn btn-outline-secondary w-100"
          :disabled="disabled"
          data-testid="space-reset-theme"
          @click="updateTheme({ ...DEFAULT_SPACE_THEME, darkMode: theme.darkMode })"
        >
          <font-awesome-icon icon="rotate-left" />
          {{ t('space.resetTheme') }}
        </button>
      </div>
    </div>
 
    <div class="form-check form-switch mt-3">
      <input
        id="spaceDarkMode"
        :checked="theme.darkMode === 'auto'"
        type="checkbox"
        role="switch"
        class="form-check-input"
        :disabled="disabled"
        data-testid="space-dark-mode"
        @change="
          updateTheme({ darkMode: ($event.target as HTMLInputElement).checked ? 'auto' : 'off' })
        "
      />
      <label class="form-check-label" for="spaceDarkMode">{{ t('space.darkMode') }}</label>
      <div class="small text-muted">{{ t('space.darkModeHint') }}</div>
      <div
        v-if="theme.darkMode === 'auto' && hasDarkBackground(theme)"
        class="small text-info-emphasis mt-1"
        data-testid="space-dark-background-hint"
      >
        <font-awesome-icon icon="circle-info" />
        {{ t('space.darkBackgroundHint') }}
      </div>
    </div>
 
    <!-- The space as readers see it: light, and dark when it follows their mode -->
    <div class="row g-2 mt-1">
      <div v-for="scheme in previewSchemes" :key="scheme" class="col">
        <div
          class="space-preview rounded"
          :style="spaceThemeStyle(theme, scheme)"
          :data-testid="scheme === 'light' ? 'space-preview' : 'space-preview-dark'"
        >
          <div class="space-preview-band">{{ flowName }}</div>
          <div class="space-preview-body">
            {{ t(scheme === 'light' ? 'space.previewLight' : 'space.previewDark') }}
          </div>
        </div>
      </div>
    </div>
 
    <FlowSpaceJoinSettings
      v-if="flowId"
      :model-value="space.join"
      :flow-id="flowId"
      :disabled="disabled"
      @update:model-value="update({ join: $event })"
    />
  </div>
</template>
 
<script setup lang="ts">
// @implements UC-SPACE-001.1, UC-SPACE-017.1
import { computed } from 'vue'
import FlowSpaceJoinSettings from '@/components/FlowSpaceJoinSettings.vue'
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
import { useUILanguage } from '@/composables/useUILanguage'
import { usePermissions } from '@/composables/usePermissions'
import { useAuthStore } from '@/stores/auth'
import { flowRoute } from '@/router/flowRoutes'
import {
  DEFAULT_SPACE_THEME,
  SPACE_FONTS,
  followsDarkMode,
  hasDarkBackground,
  isHardToRead,
  resolveSpaceTheme,
  spaceThemeStyle,
  type ColorScheme,
  type FlowSpace,
  type SpaceFont,
  type SpaceTheme
} from '@/utils/spaceTheme'
 
const props = withDefaults(
  defineProps<{
    modelValue?: FlowSpace
    slug: string
    flowName?: string
    disabled?: boolean
    /** The edited flow: its entities and roles feed the membership request settings */
    flowId?: string
  }>(),
  { modelValue: undefined, flowName: '', disabled: false, flowId: undefined }
)
 
const emit = defineEmits<{ 'update:modelValue': [value: FlowSpace] }>()
 
const { t } = useUILanguage()
 
const COLOR_KEYS = ['primaryColor', 'secondaryColor', 'backgroundColor'] as const
 
const FONT_LABEL_KEYS: Record<SpaceFont, string> = {
  sans: 'space.fontSans',
  serif: 'space.fontSerif',
  rounded: 'space.fontRounded',
  mono: 'space.fontMono'
}
 
const space = computed<FlowSpace>(() => props.modelValue ?? { enabled: false })
const theme = computed(() => resolveSpaceTheme(space.value.theme))
// One preview when both modes look the same (turned off, or a dark background)
const previewSchemes = computed<ColorScheme[]>(() =>
  followsDarkMode(theme.value) ? ['light', 'dark'] : ['light']
)
 
const publicUrl = computed(() => `${window.location.origin}/space/${props.slug}`)
 
// The pages are edited in the active flow (UC-SPACE-009.1)
const authStore = useAuthStore()
const { hasPermission } = usePermissions()
const canEditPages = computed(
  () => !!props.flowId && props.flowId === authStore.currentFlow && hasPermission('pages:update')
)
 
// The full theme is always sent, so what the form shows is exactly what the
// public page will use, even for a flow that never saved a theme. The join
// settings ride along: the API replaces the whole space.
function update(patch: Partial<FlowSpace>) {
  emit('update:modelValue', {
    enabled: space.value.enabled,
    theme: theme.value,
    ...(space.value.join && { join: space.value.join }),
    ...patch
  })
}
 
function updateTheme(patch: Partial<SpaceTheme>) {
  update({ theme: { ...theme.value, ...patch } })
}
</script>
 
<style scoped>
.space-preview {
  overflow: hidden;
  border: 1px solid var(--bs-border-color);
  background: var(--space-background);
  color: var(--space-text);
  font-family: var(--space-font);
}
 
.space-preview-band {
  background: var(--space-primary);
  color: var(--space-on-primary);
  border-bottom: 4px solid var(--space-secondary);
  padding: 0.5rem 0.75rem;
  font-weight: 600;
}
 
.space-preview-body {
  padding: 0.5rem 0.75rem;
  font-size: 0.875rem;
}
</style>