@antelopejs/dms-lang 0.0.6 → 0.1.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.
Files changed (245) hide show
  1. package/README.md +12 -6
  2. package/dist/pages/actions.d.ts +7 -0
  3. package/dist/pages/actions.js +15 -0
  4. package/dist/pages/actions.js.map +1 -0
  5. package/dist/pages/component-action.d.ts +2 -0
  6. package/dist/pages/component-action.js +10 -0
  7. package/dist/pages/component-action.js.map +1 -0
  8. package/dist/pages/header-actions.d.ts +9 -0
  9. package/dist/pages/header-actions.js +146 -0
  10. package/dist/pages/header-actions.js.map +1 -0
  11. package/dist/pages/index.d.ts +6 -0
  12. package/dist/pages/index.js +6 -0
  13. package/dist/pages/index.js.map +1 -1
  14. package/dist/pages/language.d.ts +10 -0
  15. package/dist/pages/language.js +89 -0
  16. package/dist/pages/language.js.map +1 -0
  17. package/dist/pages/languages-table.d.ts +2 -0
  18. package/dist/pages/languages-table.js +120 -0
  19. package/dist/pages/languages-table.js.map +1 -0
  20. package/dist/pages/module.d.ts +7 -1
  21. package/dist/pages/module.js +28 -2
  22. package/dist/pages/module.js.map +1 -1
  23. package/dist/pages/overview.d.ts +2 -1
  24. package/dist/pages/overview.js +83 -8
  25. package/dist/pages/overview.js.map +1 -1
  26. package/dist/pages/page-actions.d.ts +6 -0
  27. package/dist/pages/page-actions.js +10 -0
  28. package/dist/pages/page-actions.js.map +1 -0
  29. package/dist/pages/quick-actions.d.ts +1 -0
  30. package/dist/pages/quick-actions.js +56 -0
  31. package/dist/pages/quick-actions.js.map +1 -0
  32. package/dist/pages/translations.d.ts +7 -1
  33. package/dist/pages/translations.js +47 -10
  34. package/dist/pages/translations.js.map +1 -1
  35. package/dist/pages/workspace-guard.d.ts +1 -0
  36. package/dist/pages/workspace-guard.js +35 -0
  37. package/dist/pages/workspace-guard.js.map +1 -0
  38. package/dist/pages/workspace.d.ts +18 -0
  39. package/dist/pages/workspace.js +162 -0
  40. package/dist/pages/workspace.js.map +1 -0
  41. package/dist/pages/workspaces.d.ts +14 -0
  42. package/dist/pages/workspaces.js +88 -0
  43. package/dist/pages/workspaces.js.map +1 -0
  44. package/dist/routes/publicLocales.d.ts +3 -2
  45. package/dist/routes/publicLocales.js +39 -18
  46. package/dist/routes/publicLocales.js.map +1 -1
  47. package/dist/routes/translations.d.ts +59 -63
  48. package/dist/routes/translations.js +400 -119
  49. package/dist/routes/translations.js.map +1 -1
  50. package/dist/utils/announce.d.ts +9 -0
  51. package/dist/utils/announce.js +29 -0
  52. package/dist/utils/announce.js.map +1 -0
  53. package/dist/utils/blocks.d.ts +8 -0
  54. package/dist/utils/blocks.js +213 -0
  55. package/dist/utils/blocks.js.map +1 -0
  56. package/dist/utils/body.d.ts +1 -0
  57. package/dist/utils/body.js +21 -0
  58. package/dist/utils/body.js.map +1 -0
  59. package/dist/utils/cache.d.ts +3 -0
  60. package/dist/utils/cache.js +32 -0
  61. package/dist/utils/cache.js.map +1 -0
  62. package/dist/utils/catalog.d.ts +7 -0
  63. package/dist/utils/catalog.js +62 -0
  64. package/dist/utils/catalog.js.map +1 -0
  65. package/dist/utils/constants.d.ts +6 -0
  66. package/dist/utils/constants.js +10 -0
  67. package/dist/utils/constants.js.map +1 -0
  68. package/dist/utils/coverage.d.ts +2 -0
  69. package/dist/utils/coverage.js +11 -0
  70. package/dist/utils/coverage.js.map +1 -0
  71. package/dist/utils/dialogs.d.ts +7 -0
  72. package/dist/utils/dialogs.js +103 -0
  73. package/dist/utils/dialogs.js.map +1 -0
  74. package/dist/utils/errors.d.ts +40 -0
  75. package/dist/utils/errors.js +60 -0
  76. package/dist/utils/errors.js.map +1 -0
  77. package/dist/utils/export.d.ts +17 -0
  78. package/dist/utils/export.js +129 -0
  79. package/dist/utils/export.js.map +1 -0
  80. package/dist/utils/json.d.ts +10 -1
  81. package/dist/utils/json.js +127 -19
  82. package/dist/utils/json.js.map +1 -1
  83. package/dist/utils/key-stores.d.ts +3 -0
  84. package/dist/utils/key-stores.js +24 -0
  85. package/dist/utils/key-stores.js.map +1 -0
  86. package/dist/utils/keys.d.ts +23 -0
  87. package/dist/utils/keys.js +252 -0
  88. package/dist/utils/keys.js.map +1 -0
  89. package/dist/utils/languages.d.ts +27 -0
  90. package/dist/utils/languages.js +121 -0
  91. package/dist/utils/languages.js.map +1 -0
  92. package/dist/utils/locale-codes.d.ts +5 -0
  93. package/dist/utils/locale-codes.js +56 -0
  94. package/dist/utils/locale-codes.js.map +1 -0
  95. package/dist/utils/locale-discovery.d.ts +1 -0
  96. package/dist/utils/locale-discovery.js +19 -1
  97. package/dist/utils/locale-discovery.js.map +1 -1
  98. package/dist/utils/message-check.d.ts +1 -0
  99. package/dist/utils/message-check.js +93 -0
  100. package/dist/utils/message-check.js.map +1 -0
  101. package/dist/utils/message-syntax.d.ts +6 -0
  102. package/dist/utils/message-syntax.js +38 -0
  103. package/dist/utils/message-syntax.js.map +1 -0
  104. package/dist/utils/namespaces.d.ts +12 -0
  105. package/dist/utils/namespaces.js +52 -0
  106. package/dist/utils/namespaces.js.map +1 -0
  107. package/dist/utils/notices.d.ts +4 -0
  108. package/dist/utils/notices.js +60 -0
  109. package/dist/utils/notices.js.map +1 -0
  110. package/dist/utils/own.d.ts +2 -3
  111. package/dist/utils/own.js +17 -57
  112. package/dist/utils/own.js.map +1 -1
  113. package/dist/utils/public-url.d.ts +8 -0
  114. package/dist/utils/public-url.js +87 -0
  115. package/dist/utils/public-url.js.map +1 -0
  116. package/dist/utils/registry.d.ts +3 -1
  117. package/dist/utils/registry.js +26 -10
  118. package/dist/utils/registry.js.map +1 -1
  119. package/dist/utils/resolve.d.ts +18 -15
  120. package/dist/utils/resolve.js +98 -151
  121. package/dist/utils/resolve.js.map +1 -1
  122. package/dist/utils/review.d.ts +5 -0
  123. package/dist/utils/review.js +64 -0
  124. package/dist/utils/review.js.map +1 -0
  125. package/dist/utils/snippets.d.ts +7 -0
  126. package/dist/utils/snippets.js +129 -0
  127. package/dist/utils/snippets.js.map +1 -0
  128. package/dist/utils/summary.d.ts +23 -0
  129. package/dist/utils/summary.js +65 -0
  130. package/dist/utils/summary.js.map +1 -0
  131. package/dist/utils/tree.d.ts +2 -17
  132. package/dist/utils/tree.js +21 -126
  133. package/dist/utils/tree.js.map +1 -1
  134. package/dist/utils/workspaces.d.ts +45 -15
  135. package/dist/utils/workspaces.js +256 -164
  136. package/dist/utils/workspaces.js.map +1 -1
  137. package/dist/validation/key.schema.d.ts +71 -0
  138. package/dist/validation/key.schema.js +67 -0
  139. package/dist/validation/key.schema.js.map +1 -0
  140. package/dist/validation/query.schema.d.ts +17 -0
  141. package/dist/validation/query.schema.js +44 -0
  142. package/dist/validation/query.schema.js.map +1 -0
  143. package/dist/validation/validate.d.ts +2 -0
  144. package/dist/validation/validate.js +24 -0
  145. package/dist/validation/validate.js.map +1 -0
  146. package/dist/validation/workspace.schema.d.ts +145 -0
  147. package/dist/validation/workspace.schema.js +83 -0
  148. package/dist/validation/workspace.schema.js.map +1 -0
  149. package/frontend-vue/app/build/components/KeyAddModal.vue +340 -0
  150. package/frontend-vue/app/build/components/KeyEditorDrawer.vue +951 -0
  151. package/frontend-vue/app/build/components/KeyRenameModal.vue +172 -0
  152. package/frontend-vue/app/build/components/LanguageAddModal.vue +199 -0
  153. package/frontend-vue/app/build/components/LanguagePicker.vue +233 -0
  154. package/frontend-vue/app/build/components/LocaleTile.vue +52 -0
  155. package/frontend-vue/app/build/components/PlaceholderText.vue +42 -0
  156. package/frontend-vue/app/build/components/ShortcutKbds.vue +22 -0
  157. package/frontend-vue/app/build/components/WorkspaceCreateModal.vue +193 -0
  158. package/frontend-vue/app/build/components/WorkspaceRenameModal.vue +107 -0
  159. package/frontend-vue/app/build/components/form/FormFieldRow.vue +70 -0
  160. package/frontend-vue/app/build/components/form/FormRows.vue +25 -0
  161. package/frontend-vue/app/build/components/form/WorkspaceDetailsFields.vue +44 -0
  162. package/frontend-vue/app/build/components/matrix/MatrixCell.vue +488 -0
  163. package/frontend-vue/app/build/components/matrix/MatrixColumns.vue +177 -0
  164. package/frontend-vue/app/build/components/matrix/MatrixGroupHeader.vue +57 -0
  165. package/frontend-vue/app/build/components/matrix/MatrixKeyCell.vue +83 -0
  166. package/frontend-vue/app/build/components/matrix/MatrixToolbar.vue +167 -0
  167. package/frontend-vue/app/build/components/queue/QueueActions.vue +60 -0
  168. package/frontend-vue/app/build/components/queue/QueueDone.vue +185 -0
  169. package/frontend-vue/app/build/components/queue/QueueEditor.vue +374 -0
  170. package/frontend-vue/app/build/components/queue/QueueHeader.vue +162 -0
  171. package/frontend-vue/app/build/components/queue/QueueUpNext.vue +61 -0
  172. package/frontend-vue/app/build/composables/useCellEditor.ts +324 -0
  173. package/frontend-vue/app/build/composables/useCellMoves.ts +155 -0
  174. package/frontend-vue/app/build/composables/useCellSaves.ts +339 -0
  175. package/frontend-vue/app/build/composables/useKeyPanel.ts +284 -0
  176. package/frontend-vue/app/build/composables/useKeySaves.ts +280 -0
  177. package/frontend-vue/app/build/composables/useMatrixFilters.ts +213 -0
  178. package/frontend-vue/app/build/composables/useMatrixRows.ts +104 -0
  179. package/frontend-vue/app/build/composables/useMatrixView.ts +181 -0
  180. package/frontend-vue/app/build/composables/useQueueContext.ts +31 -0
  181. package/frontend-vue/app/build/composables/useQueueEditing.ts +53 -0
  182. package/frontend-vue/app/build/composables/useQueueOrphans.ts +54 -0
  183. package/frontend-vue/app/build/composables/useQueueSave.ts +290 -0
  184. package/frontend-vue/app/build/composables/useQueueShortcuts.ts +106 -0
  185. package/frontend-vue/app/build/composables/useQueueToasts.ts +99 -0
  186. package/frontend-vue/app/components/LanguageNamespaces.vue +77 -0
  187. package/frontend-vue/app/components/NamespaceCoverage.vue +222 -0
  188. package/frontend-vue/app/components/TranslationMatrix.vue +809 -0
  189. package/frontend-vue/app/components/TranslationQueue.vue +212 -0
  190. package/frontend-vue/app/components/WorkspaceDangerZone.vue +81 -0
  191. package/frontend-vue/app/components/WorkspaceGeneral.vue +232 -0
  192. package/frontend-vue/app/components/WorkspaceList.vue +454 -0
  193. package/frontend-vue/app/components/WorkspaceScope.vue +384 -0
  194. package/frontend-vue/app/composables/translation/useFirstRun.ts +8 -0
  195. package/frontend-vue/app/composables/translation/useI18nWorkspaces.ts +221 -133
  196. package/frontend-vue/app/composables/translation/useKeyActions.ts +224 -0
  197. package/frontend-vue/app/composables/translation/useLangFormat.ts +10 -0
  198. package/frontend-vue/app/composables/translation/useLangLinks.ts +80 -0
  199. package/frontend-vue/app/composables/translation/useLanguageActions.ts +159 -0
  200. package/frontend-vue/app/composables/translation/useLanguageQueue.ts +232 -0
  201. package/frontend-vue/app/composables/translation/useLocaleNames.ts +166 -0
  202. package/frontend-vue/app/composables/translation/useMutationToast.ts +51 -17
  203. package/frontend-vue/app/composables/translation/useQueueDrafts.ts +115 -0
  204. package/frontend-vue/app/composables/translation/useQueueSession.ts +182 -0
  205. package/frontend-vue/app/composables/translation/useServedConfirm.ts +152 -0
  206. package/frontend-vue/app/composables/translation/useWorkspaceActions.ts +259 -0
  207. package/frontend-vue/app/composables/translation/useWorkspaceContext.ts +46 -15
  208. package/frontend-vue/app/composables/translation/useWorkspaceFlat.ts +322 -70
  209. package/frontend-vue/app/composables/translation/useWorkspaceNameCheck.ts +55 -0
  210. package/frontend-vue/app/composables/translation/useWorkspaceRoute.ts +129 -0
  211. package/frontend-vue/app/config/shortcuts-registry.ts +72 -0
  212. package/frontend-vue/app/types/lang.ts +130 -0
  213. package/frontend-vue/app/utils/boundedCache.ts +22 -0
  214. package/frontend-vue/app/utils/download.ts +16 -0
  215. package/frontend-vue/app/utils/keySaveErrors.ts +61 -0
  216. package/frontend-vue/app/utils/keySaves.ts +108 -0
  217. package/frontend-vue/app/utils/languages.ts +272 -0
  218. package/frontend-vue/app/utils/matrixExport.ts +47 -0
  219. package/frontend-vue/app/utils/queueDrafts.ts +115 -0
  220. package/frontend-vue/app/utils/queuePass.ts +145 -0
  221. package/frontend-vue/app/utils/queueView.ts +22 -0
  222. package/frontend-vue/app/utils/shortcuts.ts +75 -0
  223. package/frontend-vue/app/utils/translations.ts +756 -0
  224. package/frontend-vue/app/utils/workspaceBackup.ts +119 -0
  225. package/frontend-vue/app/utils/workspaces.ts +45 -0
  226. package/frontend-vue/dms.frontend.build.ts +5 -0
  227. package/frontend-vue/dms.frontend.ts +9 -20
  228. package/frontend-vue/i18n/locales/lang-en-GB.json +792 -69
  229. package/frontend-vue/i18n/locales/lang-fr-FR.json +792 -69
  230. package/frontend-vue/package.json +10 -5
  231. package/package.json +10 -8
  232. package/dist/utils/mutations.d.ts +0 -3
  233. package/dist/utils/mutations.js +0 -35
  234. package/dist/utils/mutations.js.map +0 -1
  235. package/frontend-vue/app/build/components/pages/lang/TranslationMatrix.vue +0 -284
  236. package/frontend-vue/app/components/LanguageAddModal.vue +0 -78
  237. package/frontend-vue/app/components/WorkspaceEndpoint.vue +0 -29
  238. package/frontend-vue/app/components/WorkspaceSelector.vue +0 -180
  239. package/frontend-vue/app/components/WorkspaceStatus.vue +0 -24
  240. package/frontend-vue/app/composables/translation/useLanguageCatalog.ts +0 -90
  241. package/frontend-vue/app/composables/translation/useLocaleMeta.ts +0 -18
  242. package/frontend-vue/app/composables/translation/useTranslationProgress.ts +0 -73
  243. package/frontend-vue/app/custom-pages/lang/overview.vue +0 -397
  244. package/frontend-vue/app/custom-pages/lang/translations.vue +0 -498
  245. package/frontend-vue/app/plugins/workspace-sidebar-widget.ts +0 -12
@@ -1,177 +1,265 @@
1
- import { useDmsState } from '#dms/frontend-module'
1
+ import { useDmsCookie, useDmsState } from '#dms/frontend-module'
2
+ import type {
3
+ FlatCounts,
4
+ FlatTranslations,
5
+ KeyRowResponse,
6
+ ScopedWorkspace,
7
+ WorkspaceDetails,
8
+ WorkspaceLocale,
9
+ WorkspacePermissionIds,
10
+ WorkspaceStats,
11
+ WorkspaceSummary,
12
+ } from '../../types/lang'
13
+ import {
14
+ BACKUP_FORMAT,
15
+ type DetailsChange,
16
+ type WorkspaceBackup,
17
+ type WorkspaceImport,
18
+ } from '../../utils/workspaceBackup'
2
19
 
3
- export interface WorkspaceLocale {
4
- code: string
5
- name?: string
6
- flag?: string
7
- }
8
-
9
- export type WorkspaceKind = 'default' | 'modules' | 'added'
10
-
11
- export interface WorkspaceSummary {
12
- id: string
13
- kind: WorkspaceKind
20
+ interface WorkspacesResponse {
14
21
  editable: boolean
15
- defaultLocale: string
16
- locales: WorkspaceLocale[]
22
+ workspaces: ScopedWorkspace[]
23
+ permissions: WorkspacePermissionIds
24
+ publicBaseUrl: string
17
25
  }
18
26
 
19
- interface WorkspacesResponse {
27
+ interface SummaryResponse {
20
28
  editable: boolean
21
- workspaces: WorkspaceSummary[]
29
+ workspaces: WorkspaceStats[]
22
30
  }
23
31
 
24
- const API_BASE = '/api/lang/translations'
25
- const WORKSPACES_ENDPOINT = `${API_BASE}/workspaces`
26
- const WORKSPACE_ENDPOINT = `${API_BASE}/workspace`
27
- const WORKSPACE_RENAME_ENDPOINT = `${API_BASE}/workspace/rename`
28
- const LOCALE_ENDPOINT = `${API_BASE}/locale`
29
- const LOCALE_DEFAULT_ENDPOINT = `${API_BASE}/locale/default`
30
- const KEY_ENDPOINT = `${API_BASE}/key`
31
- const KEY_RENAME_ENDPOINT = `${API_BASE}/key/rename`
32
+ type KeyValues = Record<string, unknown>
33
+
34
+ /** The routes of the module's API. */
35
+ export const LANG_API = '/api/lang/translations'
36
+ const API_BASE = LANG_API
37
+ const ENDPOINTS = {
38
+ workspaces: `${API_BASE}/workspaces`,
39
+ flat: `${API_BASE}/flat`,
40
+ row: `${API_BASE}/row`,
41
+ summary: `${API_BASE}/summary`,
42
+ workspace: `${API_BASE}/workspace`,
43
+ workspaceRename: `${API_BASE}/workspace/rename`,
44
+ backup: `${API_BASE}/backup`,
45
+ import: `${API_BASE}/import`,
46
+ locale: `${API_BASE}/locale`,
47
+ languages: `${API_BASE}/languages`,
48
+ key: `${API_BASE}/key`,
49
+ keyRename: `${API_BASE}/key/rename`,
50
+ keyOverride: `${API_BASE}/key/override`,
51
+ keyReviewed: `${API_BASE}/key/reviewed`,
52
+ } as const
32
53
 
33
54
  export const DEFAULT_WORKSPACE = 'default'
55
+ export const MODULES_WORKSPACE = 'modules'
56
+
57
+ /** The route of one language of a workspace: removing it, making it the base, the dialogs that ask first. */
58
+ export function languageUrl(workspace: string, code: string): string {
59
+ return `${ENDPOINTS.languages}/${encodeURIComponent(workspace)}/${encodeURIComponent(code)}`
60
+ }
61
+
62
+ const LAST_WORKSPACE_COOKIE = 'dms-lang-workspace'
63
+ const LAST_WORKSPACE_MAX_AGE = 60 * 60 * 24 * 365
34
64
 
35
65
  const WORKSPACE_ID_RE = /^[a-z0-9][\w-]{0,63}$/
66
+ const RESERVED_WORKSPACE_IDS = new Set([DEFAULT_WORKSPACE, MODULES_WORKSPACE])
36
67
 
37
68
  export function normalizeWorkspaceId(raw: string): string {
38
69
  return raw.trim().toLowerCase()
39
70
  }
40
71
 
41
- export const useI18nWorkspaces = () => {
42
- const { $authFetch } = useAuthFetch()
72
+ export function isValidWorkspaceId(id: string): boolean {
73
+ return WORKSPACE_ID_RE.test(id) && !RESERVED_WORKSPACE_IDS.has(id)
74
+ }
43
75
 
44
- const workspaces = useDmsState<WorkspaceSummary[]>(
45
- 'dms-lang-workspaces',
46
- () => [],
47
- )
48
- const editable = useDmsState<boolean>(
49
- 'dms-lang-workspaces-editable',
50
- () => false,
51
- )
52
- const isLoading = useDmsState<boolean>(
53
- 'dms-lang-workspaces-loading',
54
- () => false,
55
- )
56
- const selectedWorkspace = useDmsState<string>(
57
- 'dms-lang-selected-workspace',
58
- () => DEFAULT_WORKSPACE,
59
- )
60
- const requestSeq = useDmsState<number>('dms-lang-workspaces-seq', () => 0)
76
+ function useLastWorkspace() {
77
+ return useDmsCookie<string>(LAST_WORKSPACE_COOKIE, {
78
+ default: () => DEFAULT_WORKSPACE,
79
+ maxAge: LAST_WORKSPACE_MAX_AGE,
80
+ path: '/',
81
+ sameSite: 'lax',
82
+ })
83
+ }
61
84
 
62
- function canUseWorkspaceId(id: string): boolean {
63
- return (
64
- WORKSPACE_ID_RE.test(id) &&
65
- !workspaces.value.some((workspace) => workspace.id === id)
66
- )
85
+ function useWorkspacesState() {
86
+ const lastWorkspace = useLastWorkspace()
87
+ return {
88
+ lastWorkspace,
89
+ workspaces: useDmsState<ScopedWorkspace[]>(
90
+ 'dms-lang-workspaces',
91
+ () => [],
92
+ ),
93
+ editable: useDmsState<boolean>('dms-lang-workspaces-editable', () => false),
94
+ permissions: useDmsState<WorkspacePermissionIds | null>(
95
+ 'dms-lang-workspaces-permissions',
96
+ () => null,
97
+ ),
98
+ publicBaseUrl: useDmsState<string>('dms-lang-public-base-url', () => ''),
99
+ isLoading: useDmsState<boolean>('dms-lang-workspaces-loading', () => false),
100
+ isLoaded: useDmsState<boolean>('dms-lang-workspaces-loaded', () => false),
101
+ selectedWorkspace: useDmsState<string>(
102
+ 'dms-lang-selected-workspace',
103
+ () => lastWorkspace.value || DEFAULT_WORKSPACE,
104
+ ),
105
+ requestSeq: useDmsState<number>('dms-lang-workspaces-seq', () => 0),
67
106
  }
107
+ }
68
108
 
69
- function ensureSelection(): void {
70
- if (workspaces.value.some((w) => w.id === selectedWorkspace.value)) return
71
- selectedWorkspace.value = workspaces.value[0]?.id ?? ''
72
- }
109
+ function useKeyWrites() {
110
+ const { $authFetch } = useAuthFetch()
111
+ const send = (url: string, method: string, body: Record<string, unknown>) =>
112
+ $authFetch<KeyRowResponse>(url, { method, body })
73
113
 
74
- async function fetchWorkspaces(): Promise<WorkspaceSummary[]> {
75
- requestSeq.value += 1
76
- const sequence = requestSeq.value
77
- isLoading.value = true
78
- try {
79
- const response = await $authFetch<WorkspacesResponse>(WORKSPACES_ENDPOINT)
80
- if (sequence !== requestSeq.value) return workspaces.value
81
- editable.value = response?.editable ?? false
82
- workspaces.value = response?.workspaces ?? []
83
- ensureSelection()
84
- return workspaces.value
85
- } catch {
86
- return workspaces.value
87
- } finally {
88
- if (sequence === requestSeq.value) isLoading.value = false
89
- }
114
+ return {
115
+ /** `before`: the path of the sibling the key goes right before (an undone deletion). */
116
+ createKey: (
117
+ workspace: string,
118
+ path: string,
119
+ values: KeyValues,
120
+ before?: string,
121
+ ) => send(ENDPOINTS.key, 'POST', { workspace, path, values, before }),
122
+ /** `expected`: the texts the change was made on, a 409 when they changed since. */
123
+ updateKey: (
124
+ workspace: string,
125
+ path: string,
126
+ values: KeyValues = {},
127
+ expected?: KeyValues,
128
+ ) => send(ENDPOINTS.key, 'PUT', { workspace, path, values, expected }),
129
+ /** Keeps translations as they are although the base text changed. */
130
+ markReviewed: (workspace: string, path: string, locales: string[]) =>
131
+ send(ENDPOINTS.keyReviewed, 'POST', { workspace, path, locales }),
132
+ revertOverride: (workspace: string, path: string, locales?: string[]) =>
133
+ send(ENDPOINTS.keyOverride, 'DELETE', { workspace, path, locales }),
134
+ renameKey: (workspace: string, path: string, newPath: string) =>
135
+ send(ENDPOINTS.keyRename, 'POST', { workspace, path, newPath }),
136
+ deleteKey: (workspace: string, path: string) =>
137
+ send(ENDPOINTS.key, 'DELETE', { workspace, path }),
90
138
  }
139
+ }
91
140
 
92
- function createWorkspace(id: string, locale: WorkspaceLocale) {
93
- return $authFetch(WORKSPACE_ENDPOINT, {
94
- method: 'POST',
95
- body: { id, locale },
96
- })
97
- }
141
+ function useWorkspaceWrites() {
142
+ const { $authFetch } = useAuthFetch()
143
+ const send = (url: string, method: string, body: Record<string, unknown>) =>
144
+ $authFetch(url, { method, body })
98
145
 
99
- function renameWorkspace(id: string, newId: string) {
100
- return $authFetch(WORKSPACE_RENAME_ENDPOINT, {
101
- method: 'POST',
102
- body: { id, newId },
103
- })
146
+ return {
147
+ createWorkspace: (
148
+ id: string,
149
+ locale: WorkspaceLocale,
150
+ details: WorkspaceDetails = {},
151
+ ) => send(ENDPOINTS.workspace, 'POST', { id, locale, ...details }),
152
+ renameWorkspace: (id: string, newId: string) =>
153
+ send(ENDPOINTS.workspaceRename, 'POST', { id, newId }),
154
+ deleteWorkspace: (id: string) =>
155
+ send(ENDPOINTS.workspace, 'DELETE', { id }),
156
+ updateWorkspace: (id: string, change: DetailsChange) =>
157
+ $authFetch<WorkspaceSummary>(ENDPOINTS.workspace, {
158
+ method: 'PATCH',
159
+ body: { id, ...change },
160
+ }),
161
+ importWorkspace: (id: string, backup: WorkspaceImport) =>
162
+ $authFetch<WorkspaceSummary>(ENDPOINTS.import, {
163
+ method: 'POST',
164
+ body: { ...backup, format: BACKUP_FORMAT, id },
165
+ }),
166
+ addLocale: (workspace: string, locale: WorkspaceLocale) =>
167
+ send(ENDPOINTS.locale, 'POST', { workspace, ...locale }),
168
+ removeLocale: (workspace: string, code: string) =>
169
+ $authFetch(languageUrl(workspace, code), { method: 'DELETE' }),
170
+ setDefaultLocale: (workspace: string, code: string) =>
171
+ $authFetch(`${languageUrl(workspace, code)}/base`, { method: 'POST' }),
104
172
  }
173
+ }
105
174
 
106
- function deleteWorkspace(id: string) {
107
- return $authFetch(WORKSPACE_ENDPOINT, {
108
- method: 'DELETE',
109
- body: { id },
110
- })
175
+ function useWorkspaceReads() {
176
+ const { $authFetch } = useAuthFetch()
177
+ return {
178
+ fetchSummary: () => $authFetch<SummaryResponse>(ENDPOINTS.summary),
179
+ fetchBackup: (workspace: string) =>
180
+ $authFetch<WorkspaceBackup>(ENDPOINTS.backup, { query: { workspace } }),
181
+ fetchFlat: (workspace: string) =>
182
+ $authFetch<FlatTranslations>(ENDPOINTS.flat, { query: { workspace } }),
183
+ fetchRow: (workspace: string, key: string) =>
184
+ $authFetch<KeyRowResponse>(ENDPOINTS.row, { query: { workspace, key } }),
111
185
  }
186
+ }
112
187
 
113
- function addLocale(workspace: string, locale: WorkspaceLocale) {
114
- return $authFetch(LOCALE_ENDPOINT, {
115
- method: 'POST',
116
- body: { workspace, ...locale },
117
- })
118
- }
188
+ /**
189
+ * The workspaces of the module, the one selected for every page, and the
190
+ * routes that read and change them. The selection follows the page URL
191
+ * (`?workspace=`, see useWorkspaceRoute): a workspace the URL names but the
192
+ * list lacks stays selected, so the page can say it does not exist.
193
+ */
194
+ export const useI18nWorkspaces = () => {
195
+ const { $authFetch } = useAuthFetch()
196
+ const state = useWorkspacesState()
119
197
 
120
- function removeLocale(workspace: string, code: string) {
121
- return $authFetch(LOCALE_ENDPOINT, {
122
- method: 'DELETE',
123
- body: { workspace, code },
124
- })
198
+ function canUseWorkspaceId(id: string): boolean {
199
+ return (
200
+ isValidWorkspaceId(id) &&
201
+ !state.workspaces.value.some((workspace) => workspace.id === id)
202
+ )
125
203
  }
126
204
 
127
- function setDefaultLocale(workspace: string, code: string) {
128
- return $authFetch(LOCALE_DEFAULT_ENDPOINT, {
129
- method: 'POST',
130
- body: { workspace, code },
131
- })
205
+ function applyResponse(response: WorkspacesResponse): void {
206
+ state.editable.value = response.editable
207
+ state.workspaces.value = response.workspaces
208
+ state.permissions.value = response.permissions
209
+ state.publicBaseUrl.value = response.publicBaseUrl
210
+ state.isLoaded.value = true
132
211
  }
133
212
 
134
- function upsertKey(
135
- workspace: string,
136
- path: string,
137
- values: Record<string, unknown> = {},
138
- defaultLocale?: string,
139
- ) {
140
- return $authFetch(KEY_ENDPOINT, {
141
- method: 'PUT',
142
- body: { workspace, path, values, defaultLocale },
143
- })
213
+ /** Takes the figures a key write answered into the workspace list (the scope bar). */
214
+ function patchCounts(id: string, counts: FlatCounts): void {
215
+ state.workspaces.value = state.workspaces.value.map((workspace) =>
216
+ workspace.id === id
217
+ ? {
218
+ ...workspace,
219
+ totalKeys: counts.totalKeys,
220
+ baseMissing: counts.baseMissing,
221
+ }
222
+ : workspace,
223
+ )
144
224
  }
145
225
 
146
- function renameKey(workspace: string, path: string, newPath: string) {
147
- return $authFetch(KEY_RENAME_ENDPOINT, {
148
- method: 'POST',
149
- body: { workspace, path, newPath },
150
- })
226
+ /** Remembers the workspace a page without `?workspace=` opens on (a new tab, a menu link). */
227
+ function rememberWorkspace(id: string): void {
228
+ if (state.lastWorkspace.value !== id) state.lastWorkspace.value = id
151
229
  }
152
230
 
153
- function deleteKey(workspace: string, path: string) {
154
- return $authFetch(KEY_ENDPOINT, {
155
- method: 'DELETE',
156
- body: { workspace, path },
157
- })
231
+ async function fetchWorkspaces(): Promise<ScopedWorkspace[]> {
232
+ state.requestSeq.value += 1
233
+ const sequence = state.requestSeq.value
234
+ state.isLoading.value = true
235
+ try {
236
+ const response = await $authFetch<WorkspacesResponse>(
237
+ ENDPOINTS.workspaces,
238
+ )
239
+ if (sequence !== state.requestSeq.value) return state.workspaces.value
240
+ applyResponse(response)
241
+ return state.workspaces.value
242
+ } catch {
243
+ return state.workspaces.value
244
+ } finally {
245
+ if (sequence === state.requestSeq.value) state.isLoading.value = false
246
+ }
158
247
  }
159
248
 
160
249
  return {
161
- workspaces,
162
- editable,
163
- selectedWorkspace,
164
- isLoading: readonly(isLoading),
250
+ workspaces: state.workspaces,
251
+ editable: state.editable,
252
+ permissions: readonly(state.permissions),
253
+ publicBaseUrl: readonly(state.publicBaseUrl),
254
+ selectedWorkspace: state.selectedWorkspace,
255
+ isLoading: readonly(state.isLoading),
256
+ isLoaded: readonly(state.isLoaded),
165
257
  fetchWorkspaces,
258
+ patchCounts,
166
259
  canUseWorkspaceId,
167
- createWorkspace,
168
- renameWorkspace,
169
- deleteWorkspace,
170
- addLocale,
171
- removeLocale,
172
- setDefaultLocale,
173
- upsertKey,
174
- renameKey,
175
- deleteKey,
260
+ rememberWorkspace,
261
+ ...useWorkspaceReads(),
262
+ ...useWorkspaceWrites(),
263
+ ...useKeyWrites(),
176
264
  }
177
265
  }
@@ -0,0 +1,224 @@
1
+ import KeyAddModal from '../../build/components/KeyAddModal.vue'
2
+ import KeyRenameModal from '../../build/components/KeyRenameModal.vue'
3
+ import type { FlatTranslationRow, KeyRowResponse } from '../../types/lang'
4
+
5
+ /** What the key actions share: the selected workspace, its keys and the DMS helpers. */
6
+ interface KeyActionsContext {
7
+ t: ReturnType<typeof useI18n>['t']
8
+ toast: ReturnType<typeof useToast>
9
+ overlay: ReturnType<typeof useOverlay>
10
+ confirm: ReturnType<typeof useConfirm>['confirm']
11
+ workspaces: ReturnType<typeof useI18nWorkspaces>
12
+ translations: ReturnType<typeof useWorkspaceFlat>
13
+ isDefault: ReturnType<typeof useWorkspaceContext>['isDefault']
14
+ tryMutate: ReturnType<typeof useMutationToast>['tryMutate']
15
+ }
16
+
17
+ /** A removal asked about in a dialog, then announced with an Undo. */
18
+ interface KeyRemoval {
19
+ path: string
20
+ /** The group of its texts (`key_delete`, `key_revert`). */
21
+ texts: string
22
+ color: 'error' | 'warning'
23
+ run: (workspace: string) => Promise<KeyRowResponse>
24
+ undo: (workspace: string, row: FlatTranslationRow) => Promise<void>
25
+ }
26
+
27
+ /** The written values of a row, without the languages it lacks. */
28
+ export function filledValues(row: FlatTranslationRow): Record<string, string> {
29
+ const values: Record<string, string> = {}
30
+ for (const [locale, value] of Object.entries(row.values)) {
31
+ if (!isLocaleValueMissing(value)) values[locale] = stringValue(value)
32
+ }
33
+ return values
34
+ }
35
+
36
+ function rowsOf(context: KeyActionsContext): FlatTranslationRow[] {
37
+ return context.translations.flat.value.rows
38
+ }
39
+
40
+ /** The keys the installed modules provide, which the project files can override. */
41
+ async function moduleRows(
42
+ context: KeyActionsContext,
43
+ ): Promise<FlatTranslationRow[]> {
44
+ if (!context.isDefault.value) return []
45
+ return (await context.translations.load(MODULES_WORKSPACE)).rows
46
+ }
47
+
48
+ async function addKey(context: KeyActionsContext): Promise<string[]> {
49
+ const { flat } = context.translations
50
+ const modal = context.overlay.create(KeyAddModal, {
51
+ props: {
52
+ workspace: context.workspaces.selectedWorkspace.value,
53
+ baseLocale: flat.value.defaultLocale,
54
+ locales: flat.value.locales,
55
+ existingKeys: rowsOf(context).map((row) => row.key),
56
+ moduleRows: await moduleRows(context),
57
+ },
58
+ destroyOnClose: true,
59
+ })
60
+ return ((await modal.open()) as string[] | undefined) ?? []
61
+ }
62
+
63
+ function notify(context: KeyActionsContext, title: string, icon: string) {
64
+ context.toast.add({ title, color: 'success', icon })
65
+ }
66
+
67
+ async function renameKey(
68
+ context: KeyActionsContext,
69
+ path: string,
70
+ ): Promise<string | null> {
71
+ const modules = await moduleRows(context)
72
+ const modal = context.overlay.create(KeyRenameModal, {
73
+ props: {
74
+ workspace: context.workspaces.selectedWorkspace.value,
75
+ path,
76
+ languageCount: context.translations.flat.value.locales.length,
77
+ existingKeys: rowsOf(context).map((row) => row.key),
78
+ moduleKeys: modules.map((row) => row.key),
79
+ },
80
+ destroyOnClose: true,
81
+ })
82
+ const renamed = (await modal.open()) as string | null
83
+ if (!renamed) return null
84
+ notify(
85
+ context,
86
+ context.t('dms_lang.key_rename.done', { path: renamed }),
87
+ 'i-ph-check-circle',
88
+ )
89
+ return renamed
90
+ }
91
+
92
+ function announceRemoval(
93
+ context: KeyActionsContext,
94
+ title: string,
95
+ undo: () => Promise<void>,
96
+ ): void {
97
+ context.toast.add({
98
+ title,
99
+ color: 'neutral',
100
+ icon: 'i-ph-trash',
101
+ actions: [
102
+ {
103
+ label: context.t('dms_lang.actions.undo'),
104
+ color: 'neutral',
105
+ variant: 'outline',
106
+ onClick: () => undo(),
107
+ },
108
+ ],
109
+ })
110
+ }
111
+
112
+ /** Asks, runs a removal, puts its answer in place, and offers to undo it with the row as it was. */
113
+ async function removeKeyTexts(
114
+ context: KeyActionsContext,
115
+ removal: KeyRemoval,
116
+ ): Promise<boolean> {
117
+ const { t } = context
118
+ const workspace = context.workspaces.selectedWorkspace.value
119
+ const row = rowsOf(context).find((entry) => entry.key === removal.path)
120
+ const filled = row ? Object.keys(filledValues(row)) : []
121
+ const count = filled.length
122
+ const params = {
123
+ path: removal.path,
124
+ workspace,
125
+ count,
126
+ locales: filled.join(', '),
127
+ }
128
+ const removed = await context.confirm({
129
+ title: t(`dms_lang.${removal.texts}.title`, params),
130
+ description: t(`dms_lang.${removal.texts}.description`, params, count),
131
+ color: removal.color,
132
+ confirmLabel: t(`dms_lang.${removal.texts}.submit`),
133
+ onConfirm: async () => {
134
+ const response = await removal.run(workspace)
135
+ context.translations.applyRow(workspace, removal.path, response)
136
+ },
137
+ })
138
+ if (!removed) return false
139
+ if (row)
140
+ announceRemoval(context, t(`dms_lang.${removal.texts}.done`, params), () =>
141
+ removal.undo(workspace, row),
142
+ )
143
+ return true
144
+ }
145
+
146
+ /** The row a removal took away, written back where it was. */
147
+ interface RowRestore {
148
+ workspace: string
149
+ row: FlatTranslationRow
150
+ before?: string
151
+ }
152
+
153
+ /** Writes a row back as it was and puts the answer in place. */
154
+ async function restoreRow(
155
+ context: KeyActionsContext,
156
+ { workspace, row, before }: RowRestore,
157
+ write: () => Promise<KeyRowResponse>,
158
+ ): Promise<boolean> {
159
+ return context.tryMutate(async () => {
160
+ const response = await write()
161
+ context.translations.applyRow(workspace, row.key, response, before)
162
+ })
163
+ }
164
+
165
+ function deleteKey(context: KeyActionsContext, path: string) {
166
+ const { deleteKey: remove, createKey } = context.workspaces
167
+ const before = nextSiblingPath(
168
+ rowsOf(context).map((row) => row.key),
169
+ path,
170
+ )
171
+ return removeKeyTexts(context, {
172
+ path,
173
+ texts: 'key_delete',
174
+ color: 'error',
175
+ run: (workspace) => remove(workspace, path),
176
+ undo: async (workspace, row) => {
177
+ const write = () =>
178
+ createKey(workspace, row.key, filledValues(row), before)
179
+ if (!(await restoreRow(context, { workspace, row, before }, write)))
180
+ return
181
+ notify(
182
+ context,
183
+ context.t('dms_lang.key_delete.restored', { path: row.key }),
184
+ 'i-ph-arrow-counter-clockwise',
185
+ )
186
+ },
187
+ })
188
+ }
189
+
190
+ /** Drops the project text of a module key: the module text shows again. */
191
+ function revertToModule(context: KeyActionsContext, path: string) {
192
+ const { revertOverride, updateKey } = context.workspaces
193
+ return removeKeyTexts(context, {
194
+ path,
195
+ texts: 'key_revert',
196
+ color: 'warning',
197
+ run: (workspace) => revertOverride(workspace, path),
198
+ undo: async (workspace, row) => {
199
+ await restoreRow(context, { workspace, row }, () =>
200
+ updateKey(workspace, row.key, filledValues(row)),
201
+ )
202
+ },
203
+ })
204
+ }
205
+
206
+ /** Add, rename and delete translation keys of the selected workspace. */
207
+ export const useKeyActions = () => {
208
+ const context: KeyActionsContext = {
209
+ t: useI18n().t,
210
+ toast: useToast(),
211
+ overlay: useOverlay(),
212
+ confirm: useConfirm().confirm,
213
+ workspaces: useI18nWorkspaces(),
214
+ translations: useWorkspaceFlat(),
215
+ isDefault: useWorkspaceContext().isDefault,
216
+ tryMutate: useMutationToast().tryMutate,
217
+ }
218
+ return {
219
+ addKey: () => addKey(context),
220
+ renameKey: (path: string) => renameKey(context, path),
221
+ deleteKey: (path: string) => deleteKey(context, path),
222
+ revertToModule: (path: string) => revertToModule(context, path),
223
+ }
224
+ }
@@ -0,0 +1,10 @@
1
+ /** Counts written in the user's language and regional format. */
2
+ export const useLangFormat = () => {
3
+ const { formatNumber } = useRegionalFormat()
4
+
5
+ function formatCount(value: number): string {
6
+ return String(formatNumber(value))
7
+ }
8
+
9
+ return { formatCount }
10
+ }