@lightnet/sveltia-admin 4.1.0 → 4.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @lightnet/sveltia-admin
2
2
 
3
+ ## 4.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#425](https://github.com/LightNetDev/LightNet/pull/425) [`d21b3c1`](https://github.com/LightNetDev/LightNet/commit/d21b3c18fe5368555be56015625632b391c29dd8) - Added summaryLocale config option to support choosing the language for entry summaries.
8
+
3
9
  ## 4.1.0
4
10
 
5
11
  ### Minor Changes
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "Admin UI for LightNet based on Sveltia CMS.",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
- "version": "4.1.0",
6
+ "version": "4.2.0",
7
7
  "publishConfig": {
8
8
  "access": "public"
9
9
  },
@@ -167,6 +167,16 @@ export const adminConfigSchema = z.object({
167
167
  * @default true
168
168
  */
169
169
  showSlugField: z.boolean().default(true),
170
+ /**
171
+ * Set the language used for entry summaries.
172
+ *
173
+ * The value must match the `code` of one site language configured for this site.
174
+ * Make sure all entry labels include a translation for this language.
175
+ *
176
+ * @example "en"
177
+ */
178
+ summaryLocale: z.string().optional(),
179
+
170
180
  /**
171
181
  * Use Cloudflare r2 for content uploads.
172
182
  */
@@ -1,9 +1,8 @@
1
1
  import type { Collection } from "@sveltia/cms"
2
- import config from "virtual:lightnet/config"
3
2
  import adminConfig from "virtual:lightnet/sveltiaAdminConfig"
4
3
 
5
4
  import { projectPath } from "../utils/paths"
6
- import { inlineTranslation } from "./fields/inline-translation"
5
+ import { inlineTranslation, translatedLabel } from "./fields/inline-translation"
7
6
 
8
7
  export const categoriesCollection: Collection = {
9
8
  name: "categories",
@@ -17,8 +16,8 @@ export const categoriesCollection: Collection = {
17
16
  format: "json",
18
17
  slug: adminConfig.experimental.showSlugField
19
18
  ? "{{fields._slug}}"
20
- : `{{label.${config.defaultLocale}}}`,
21
- summary: `{{label.${config.defaultLocale}}}`,
19
+ : translatedLabel(),
20
+ summary: translatedLabel(),
22
21
  fields: [
23
22
  inlineTranslation({ name: "label", label: "Name" }),
24
23
  {
@@ -1,10 +1,13 @@
1
1
  import type { Field, ObjectField } from "@sveltia/cms"
2
2
  import config from "virtual:lightnet/config"
3
+ import adminConfig from "virtual:lightnet/sveltiaAdminConfig"
3
4
 
4
5
  type Options = Partial<ObjectField> & {
5
6
  name: string
6
7
  }
7
8
 
9
+ const summaryLocale = adminConfig.experimental.summaryLocale
10
+
8
11
  const locales = [
9
12
  config.defaultLocale,
10
13
  ...config.locales.filter((l) => l !== config.defaultLocale),
@@ -14,12 +17,17 @@ const locales = [
14
17
  }))
15
18
 
16
19
  export const inlineTranslation = (options: Options): Field => ({
17
- summary: `{{${config.defaultLocale}}}`,
20
+ summary: `{{${summaryLocale ?? config.defaultLocale}}}`,
18
21
  ...options,
19
22
  widget: "object",
20
23
  fields: locales.map((locale) => ({
21
24
  ...locale,
22
25
  widget: "string",
23
- required: locale.name === config.defaultLocale,
26
+ required:
27
+ locale.name === config.defaultLocale || locale.name === summaryLocale,
24
28
  })),
25
29
  })
30
+
31
+ export const translatedLabel = (fieldName = "label") => {
32
+ return `{{${fieldName}.${summaryLocale ?? config.defaultLocale}}}`
33
+ }
@@ -5,6 +5,8 @@ import adminConfig from "virtual:lightnet/sveltiaAdminConfig"
5
5
  import { projectPath } from "../utils/paths"
6
6
  import { inlineTranslation } from "./fields/inline-translation"
7
7
 
8
+ const { summaryLocale } = adminConfig.experimental
9
+
8
10
  export const languagesSelect = () => {
9
11
  const commonProperties = {
10
12
  name: "language",
@@ -20,7 +22,7 @@ export const languagesSelect = () => {
20
22
  file: "languages",
21
23
  value_field: "{{languages.*.code}}",
22
24
  display_fields: [
23
- `{{languages.*.label.${config.defaultLocale}}} ({{languages.*.code}})`,
25
+ `{{languages.*.label.${summaryLocale ?? config.defaultLocale}}} ({{languages.*.code}})`,
24
26
  ],
25
27
  }
26
28
  } else {
@@ -29,7 +31,7 @@ export const languagesSelect = () => {
29
31
  widget: "select",
30
32
  options: config.languages.map(({ code, label }) => {
31
33
  return {
32
- label: `${label[config.defaultLocale]} (${code})`,
34
+ label: `${label[summaryLocale ?? config.defaultLocale]} (${code})`,
33
35
  value: code,
34
36
  }
35
37
  }),
@@ -60,7 +62,7 @@ const languagesCollection: CollectionFile = {
60
62
  allow_reorder: false,
61
63
  root: true,
62
64
  collapsed: "auto",
63
- summary: `{{label.${config.defaultLocale}}} ({{code}})`,
65
+ summary: `{{label.${summaryLocale ?? config.defaultLocale}}} ({{code}})`,
64
66
  fields: [
65
67
  {
66
68
  name: "code",
@@ -1,9 +1,8 @@
1
1
  import type { Collection } from "@sveltia/cms"
2
- import config from "virtual:lightnet/config"
3
2
  import adminConfig from "virtual:lightnet/sveltiaAdminConfig"
4
3
 
5
4
  import { projectPath } from "../utils/paths"
6
- import { inlineTranslation } from "./fields/inline-translation"
5
+ import { inlineTranslation, translatedLabel } from "./fields/inline-translation"
7
6
 
8
7
  export const mediaCollectionCollection: Collection = {
9
8
  name: "media-collections",
@@ -17,8 +16,8 @@ export const mediaCollectionCollection: Collection = {
17
16
  format: "json",
18
17
  slug: adminConfig.experimental.showSlugField
19
18
  ? "{{fields._slug}}"
20
- : `{{label.${config.defaultLocale}}}`,
21
- summary: `{{label.${config.defaultLocale}}}`,
19
+ : translatedLabel(),
20
+ summary: translatedLabel(),
22
21
  fields: [
23
22
  inlineTranslation({
24
23
  name: "label",
@@ -4,7 +4,7 @@ import adminConfig from "virtual:lightnet/sveltiaAdminConfig"
4
4
 
5
5
  import { isDefined } from "../utils/is-defined"
6
6
  import { projectPath } from "../utils/paths"
7
- import { inlineTranslation } from "./fields/inline-translation"
7
+ import { inlineTranslation, translatedLabel } from "./fields/inline-translation"
8
8
  import { languagesSelect } from "./languages"
9
9
 
10
10
  export const mediaItemCollection: Collection = {
@@ -38,7 +38,7 @@ export const mediaItemCollection: Collection = {
38
38
  widget: "relation",
39
39
  hint: "Choose the kind of media this content represents.",
40
40
  collection: "media-types",
41
- display_fields: [`{{label.${config.defaultLocale}}} ({{slug}})`],
41
+ display_fields: [translatedLabel()],
42
42
  },
43
43
  languagesSelect(),
44
44
  {
@@ -145,7 +145,7 @@ export const mediaItemCollection: Collection = {
145
145
  widget: "relation",
146
146
  multiple: true,
147
147
  collection: "categories",
148
- display_fields: [`{{label.${config.defaultLocale}}}`],
148
+ display_fields: [translatedLabel()],
149
149
  },
150
150
  {
151
151
  name: "description",
@@ -1,9 +1,8 @@
1
1
  import type { Collection } from "@sveltia/cms"
2
- import config from "virtual:lightnet/config"
3
2
  import adminConfig from "virtual:lightnet/sveltiaAdminConfig"
4
3
 
5
4
  import { projectPath } from "../utils/paths"
6
- import { inlineTranslation } from "./fields/inline-translation"
5
+ import { inlineTranslation, translatedLabel } from "./fields/inline-translation"
7
6
 
8
7
  export const mediaTypeCollection: Collection = {
9
8
  name: "media-types",
@@ -16,8 +15,8 @@ export const mediaTypeCollection: Collection = {
16
15
  hide: adminConfig.experimental.hideMediaTypesCollection,
17
16
  slug: adminConfig.experimental.showSlugField
18
17
  ? "{{fields._slug}}"
19
- : `{{label.${config.defaultLocale}}}`,
20
- summary: `{{label.${config.defaultLocale}}}`,
18
+ : translatedLabel(),
19
+ summary: translatedLabel(),
21
20
  fields: [
22
21
  inlineTranslation({ name: "label", label: "Name" }),
23
22
  {
@@ -1,7 +1,26 @@
1
1
  import type { APIRoute } from "astro"
2
+ import { AstroError } from "astro/errors"
3
+ import config from "virtual:lightnet/config"
4
+ import adminConfig from "virtual:lightnet/sveltiaAdminConfig"
2
5
 
3
6
  import { createConfig } from "./sveltia.config"
4
7
 
5
8
  export const GET: APIRoute = () => {
9
+ validateAdminConfig()
6
10
  return new Response(JSON.stringify(createConfig()))
7
11
  }
12
+
13
+ /**
14
+ * When validating the admin config structure in integration.ts, we do not
15
+ * have access to the resolved LightNet config. This function acts as a hook
16
+ * for more detailed validation that depends on the resolved LightNet config.
17
+ */
18
+ const validateAdminConfig = () => {
19
+ const { summaryLocale } = adminConfig.experimental
20
+ if (summaryLocale && !config.locales.includes(summaryLocale)) {
21
+ throw new AstroError(
22
+ "Invalid summaryLocale",
23
+ `summaryLocale must match one of your site languages: ${JSON.stringify(config.locales)}.`,
24
+ )
25
+ }
26
+ }