@antelopejs/dms-lang 0.0.7 → 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 +8 -6
  231. package/package.json +8 -6
  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
@@ -0,0 +1,280 @@
1
+ import type { Ref } from 'vue'
2
+ import type { FlatTranslationRow, KeyRowResponse } from '../../types/lang'
3
+ import {
4
+ keySaveFailure,
5
+ type KeySaveFailure,
6
+ type KeySaveFailureKind,
7
+ type ApiMessageTranslate,
8
+ } from '../../utils/keySaveErrors'
9
+ import {
10
+ queueTexts,
11
+ saveIdOf,
12
+ savedText,
13
+ storedText,
14
+ withEntry,
15
+ type ExpectedText,
16
+ type SaveState,
17
+ type WriteLink,
18
+ } from '../../utils/keySaves'
19
+
20
+ /** The key a write goes to. */
21
+ export interface KeyTarget {
22
+ workspace: string
23
+ key: string
24
+ }
25
+
26
+ /** Texts of one key to save, each with the text its edit began on. */
27
+ export interface KeyWrite extends KeyTarget {
28
+ /** The typed text of each language (spaces only: no text). */
29
+ values: Record<string, string>
30
+ /** What each text expects to replace, frozen when its edit began. */
31
+ expected: Record<string, ExpectedText>
32
+ }
33
+
34
+ interface KeyWriteSaved {
35
+ status: 'saved'
36
+ }
37
+
38
+ export interface KeyWriteFailed {
39
+ status: 'failed'
40
+ failure: KeySaveFailure
41
+ /** The sentence to show: the server's problems with the text, else what failed. */
42
+ message: string
43
+ /** What the write expected: a later attempt expects the same. */
44
+ expected: Record<string, ExpectedText>
45
+ error: unknown
46
+ }
47
+
48
+ export type KeyWriteOutcome = KeyWriteSaved | KeyWriteFailed
49
+
50
+ interface KeySaveContext {
51
+ updateKey: ReturnType<typeof useI18nWorkspaces>['updateKey']
52
+ postReviewed: ReturnType<typeof useI18nWorkspaces>['markReviewed']
53
+ translations: ReturnType<typeof useWorkspaceFlat>
54
+ errorMessage: (error: unknown) => string
55
+ translate: ApiMessageTranslate
56
+ }
57
+
58
+ /** The row a refused write leaves on screen, from what the refusal tells. */
59
+ type FailureChange = (
60
+ row: FlatTranslationRow,
61
+ failure: KeySaveFailure,
62
+ ) => FlatTranslationRow
63
+
64
+ const SAVED_FLASH_MS = 1800
65
+ const SAVED: KeyWriteSaved = { status: 'saved' }
66
+
67
+ const links = new Map<string, Promise<WriteLink>>()
68
+
69
+ /** The texts a conflict says the server holds now. */
70
+ function applyCurrent(
71
+ row: FlatTranslationRow,
72
+ failure: KeySaveFailure,
73
+ ): FlatTranslationRow {
74
+ const current = Object.entries(failure.current).map(([code, value]) => [
75
+ code,
76
+ savedText(value) ?? undefined,
77
+ ])
78
+ return { ...row, values: { ...row.values, ...Object.fromEntries(current) } }
79
+ }
80
+
81
+ const FAILURE_CHANGES: Partial<Record<KeySaveFailureKind, FailureChange>> = {
82
+ conflict: applyCurrent,
83
+ }
84
+
85
+ function textsOf(values: Record<string, string>): Record<string, unknown> {
86
+ return Object.fromEntries(
87
+ Object.entries(values).map(([code, text]) => [code, storedText(text)]),
88
+ )
89
+ }
90
+
91
+ function checksOf(
92
+ expected: Record<string, ExpectedText>,
93
+ ): Record<string, unknown> | undefined {
94
+ const checks = Object.entries(expected).filter(
95
+ ([, text]) => text !== undefined,
96
+ )
97
+ return checks.length ? Object.fromEntries(checks) : undefined
98
+ }
99
+
100
+ function applyResponse(
101
+ context: KeySaveContext,
102
+ target: KeyTarget,
103
+ response: KeyRowResponse,
104
+ ): KeyWriteOutcome {
105
+ context.translations.applyRow(target.workspace, target.key, response)
106
+ return SAVED
107
+ }
108
+
109
+ function failed(
110
+ context: KeySaveContext,
111
+ target: KeyTarget,
112
+ expected: Record<string, ExpectedText>,
113
+ error: unknown,
114
+ ): KeyWriteFailed {
115
+ const failure = keySaveFailure(error, context.translate)
116
+ const change = FAILURE_CHANGES[failure.kind]
117
+ const row = context.translations
118
+ .flatOf(target.workspace)
119
+ .rows.find((entry) => entry.key === target.key)
120
+ if (change && row)
121
+ context.translations.replaceRow(
122
+ target.workspace,
123
+ target.key,
124
+ change(row, failure),
125
+ )
126
+ const message = failure.details.join(' ') || context.errorMessage(error)
127
+ return { status: 'failed', failure, message, expected, error }
128
+ }
129
+
130
+ async function send(
131
+ context: KeySaveContext,
132
+ request: KeyWrite,
133
+ expected: Record<string, ExpectedText>,
134
+ ): Promise<KeyWriteOutcome> {
135
+ try {
136
+ const response = await context.updateKey(
137
+ request.workspace,
138
+ request.key,
139
+ textsOf(request.values),
140
+ checksOf(expected),
141
+ )
142
+ return applyResponse(context, request, response)
143
+ } catch (error) {
144
+ return failed(context, request, expected, error)
145
+ }
146
+ }
147
+
148
+ function queueWrite(
149
+ context: KeySaveContext,
150
+ request: KeyWrite,
151
+ ): Promise<KeyWriteOutcome> {
152
+ const codes = Object.keys(request.values)
153
+ const texts = codes.map((code) => ({
154
+ id: saveIdOf(request.workspace, request.key, code),
155
+ text: request.values[code],
156
+ expected: request.expected[code],
157
+ }))
158
+ const sendCarried = (expected: ExpectedText[]) =>
159
+ send(
160
+ context,
161
+ request,
162
+ Object.fromEntries(codes.map((code, index) => [code, expected[index]])),
163
+ )
164
+ return queueTexts(
165
+ links,
166
+ texts,
167
+ sendCarried,
168
+ (outcome) => outcome.status === 'saved',
169
+ )
170
+ }
171
+
172
+ async function markReviewed(
173
+ context: KeySaveContext,
174
+ target: KeyTarget,
175
+ code: string,
176
+ ): Promise<KeyWriteOutcome> {
177
+ try {
178
+ const response = await context.postReviewed(target.workspace, target.key, [
179
+ code,
180
+ ])
181
+ return applyResponse(context, target, response)
182
+ } catch (error) {
183
+ return failed(context, target, {}, error)
184
+ }
185
+ }
186
+
187
+ /** Whether a text typed in the base language clears a base text that was there. */
188
+ export function clearsBaseText(
189
+ code: string,
190
+ base: string,
191
+ draft: string,
192
+ stored: string,
193
+ ): boolean {
194
+ return code === base && storedText(draft) === null && stored !== ''
195
+ }
196
+
197
+ /**
198
+ * The one way the module saves the texts of a key (matrix cells, key panel,
199
+ * translation queue): the writes of a text run in order, each expecting the
200
+ * text its edit began on (a change made elsewhere meanwhile is a conflict,
201
+ * never overwritten), the translations every page shows follow what the
202
+ * server stored, and a refused write says why (conflict, deleted, invalid).
203
+ */
204
+ export function useKeySaves() {
205
+ const { updateKey, markReviewed: postReviewed } = useI18nWorkspaces()
206
+ const translations = useWorkspaceFlat()
207
+ const { errorMessage } = useMutationToast()
208
+ const { confirm } = useConfirm()
209
+ const { t } = useI18n()
210
+ const context: KeySaveContext = {
211
+ updateKey,
212
+ postReviewed,
213
+ translations,
214
+ errorMessage,
215
+ translate: (key, params) => t(key, params),
216
+ }
217
+
218
+ return {
219
+ /** Queues texts of a key behind the earlier writes of the same texts. */
220
+ write: (request: KeyWrite) => queueWrite(context, request),
221
+ /** Keeps a translation as it is although the base text changed. */
222
+ markReviewed: (target: KeyTarget, code: string) =>
223
+ markReviewed(context, target, code),
224
+ /** Takes a key deleted elsewhere off the translations every page shows. */
225
+ dropRow: (target: KeyTarget) =>
226
+ translations.replaceRow(target.workspace, target.key, undefined),
227
+ /** Asks before a base text is emptied: the key would go missing everywhere. */
228
+ confirmClearBase: (key: string) =>
229
+ confirm({
230
+ title: t('dms_lang.matrix.clear_base_title', { key }),
231
+ description: t('dms_lang.matrix.clear_base_description'),
232
+ color: 'warning',
233
+ confirmLabel: t('dms_lang.matrix.clear_base_confirm'),
234
+ }),
235
+ }
236
+ }
237
+
238
+ /** The save state of each text of a set, a save flashing "Saved" for a moment. */
239
+ export function useSaveStates(states: Ref<Record<string, SaveState>>) {
240
+ function stateOf(id: string): SaveState {
241
+ return states.value[id] ?? 'idle'
242
+ }
243
+
244
+ function setState(id: string, state: SaveState) {
245
+ states.value = withEntry(states.value, id, state)
246
+ }
247
+
248
+ function flashSaved(id: string) {
249
+ setState(id, 'saved')
250
+ setTimeout(() => {
251
+ if (states.value[id] === 'saved') setState(id, 'idle')
252
+ }, SAVED_FLASH_MS)
253
+ }
254
+
255
+ return { stateOf, setState, flashSaved }
256
+ }
257
+
258
+ /** The text each edit began on, kept until its text is saved or the edit ends. */
259
+ export function useEditStarts() {
260
+ const starts = ref<Record<string, ExpectedText>>({})
261
+
262
+ return {
263
+ has: (id: string) => id in starts.value,
264
+ of: (id: string): ExpectedText => starts.value[id],
265
+ /** Remembers what a text shows as its edit begins (once per edit). */
266
+ begin(id: string, text: ExpectedText) {
267
+ if (!(id in starts.value))
268
+ starts.value = withEntry(starts.value, id, text)
269
+ },
270
+ set(id: string, text: ExpectedText) {
271
+ starts.value = { ...starts.value, [id]: text }
272
+ },
273
+ end(id: string) {
274
+ starts.value = withEntry(starts.value, id, undefined)
275
+ },
276
+ clear() {
277
+ starts.value = {}
278
+ },
279
+ }
280
+ }
@@ -0,0 +1,213 @@
1
+ import type { Ref } from 'vue'
2
+ import type {
3
+ FlatTranslationRow,
4
+ FlatTranslations,
5
+ TranslationFilter,
6
+ } from '../../types/lang'
7
+
8
+ export interface MatrixGroup {
9
+ namespace: string
10
+ keys: number
11
+ missing: number
12
+ rows: FlatTranslationRow[]
13
+ }
14
+
15
+ export interface MatrixFilterState {
16
+ filter: TranslationFilter
17
+ /** The language being translated: the matrix shows it next to the base only. */
18
+ missingIn: string
19
+ namespace: string
20
+ search: string
21
+ }
22
+
23
+ export type MatrixCounts = Record<TranslationFilter, number> & {
24
+ /** Empty cells of the counted languages: the missing translations. */
25
+ cells: number
26
+ }
27
+
28
+ interface RowStatus {
29
+ missing: string[]
30
+ hasIssue: boolean
31
+ }
32
+
33
+ const FILTERS: TranslationFilter[] = ['all', 'missing', 'complete', 'issues']
34
+
35
+ export const EMPTY_FILTERS: MatrixFilterState = {
36
+ filter: 'all',
37
+ missingIn: '',
38
+ namespace: '',
39
+ search: '',
40
+ }
41
+
42
+ export function parseFilter(value: string | undefined): TranslationFilter {
43
+ return FILTERS.find((filter) => filter === value) ?? 'all'
44
+ }
45
+
46
+ /**
47
+ * The languages the counts and the filters look at: the one being translated,
48
+ * else the target languages shown as columns, else every target language.
49
+ */
50
+ export function targetLocales(
51
+ flat: FlatTranslations,
52
+ missingIn: string,
53
+ shown: string[],
54
+ ): string[] {
55
+ if (missingIn && flat.locales.includes(missingIn)) return [missingIn]
56
+ const targets = flat.locales.filter((code) => code !== flat.defaultLocale)
57
+ const visible = targets.filter((code) => shown.includes(code))
58
+ return visible.length ? visible : targets
59
+ }
60
+
61
+ function rowStatus(
62
+ row: FlatTranslationRow,
63
+ base: string,
64
+ targets: string[],
65
+ ): RowStatus {
66
+ const missing = targets.filter((code) => isRowValueMissing(row, code))
67
+ const hasIssue =
68
+ isRowValueMissing(row, base) ||
69
+ (row.issues?.includes(base) ?? false) ||
70
+ targets.some((code) => needsReview(row, code))
71
+ return { missing, hasIssue }
72
+ }
73
+
74
+ function searchText(row: FlatTranslationRow): string {
75
+ const values = Object.values(row.values).map(stringValue)
76
+ return [row.key, ...values].join('\n').toLowerCase()
77
+ }
78
+
79
+ function countRows(
80
+ rows: FlatTranslationRow[],
81
+ statusOf: (row: FlatTranslationRow) => RowStatus,
82
+ ): MatrixCounts {
83
+ const counts: MatrixCounts = {
84
+ all: rows.length,
85
+ missing: 0,
86
+ complete: 0,
87
+ issues: 0,
88
+ cells: 0,
89
+ }
90
+ for (const row of rows) {
91
+ const status = statusOf(row)
92
+ counts.cells += status.missing.length
93
+ if (status.missing.length) counts.missing += 1
94
+ else counts.complete += 1
95
+ if (status.hasIssue) counts.issues += 1
96
+ }
97
+ return counts
98
+ }
99
+
100
+ type RowPredicate = (row: FlatTranslationRow) => boolean
101
+
102
+ const NO_STATUS: RowStatus = { missing: [], hasIssue: false }
103
+
104
+ function useRowStatuses(
105
+ flat: Ref<FlatTranslations>,
106
+ state: Ref<MatrixFilterState>,
107
+ shown: Ref<string[]>,
108
+ ) {
109
+ const targets = computed(() =>
110
+ targetLocales(flat.value, state.value.missingIn, shown.value),
111
+ )
112
+ const statuses = computed(() => {
113
+ const base = flat.value.defaultLocale
114
+ return new Map(
115
+ flat.value.rows.map((row) => [
116
+ row.key,
117
+ rowStatus(row, base, targets.value),
118
+ ]),
119
+ )
120
+ })
121
+ const statusOf = (row: FlatTranslationRow): RowStatus =>
122
+ statuses.value.get(row.key) ?? NO_STATUS
123
+ const isRowMissing = (row: FlatTranslationRow): boolean =>
124
+ statusOf(row).missing.length > 0
125
+ const predicates: Record<TranslationFilter, RowPredicate> = {
126
+ all: () => true,
127
+ missing: isRowMissing,
128
+ complete: (row) => !isRowMissing(row),
129
+ issues: (row) => statusOf(row).hasIssue,
130
+ }
131
+ return { targets, statusOf, isRowMissing, predicates }
132
+ }
133
+
134
+ function useScopedRows(
135
+ flat: Ref<FlatTranslations>,
136
+ state: Ref<MatrixFilterState>,
137
+ ) {
138
+ const haystacks = computed(
139
+ () => new Map(flat.value.rows.map((row) => [row.key, searchText(row)])),
140
+ )
141
+ const namespaces = computed(() => [
142
+ ...new Set(flat.value.rows.map((row) => namespaceOf(row.key))),
143
+ ])
144
+ const namespace = computed(() =>
145
+ namespaces.value.includes(state.value.namespace)
146
+ ? state.value.namespace
147
+ : '',
148
+ )
149
+ const scoped = computed(() => {
150
+ const needle = state.value.search.trim().toLowerCase()
151
+ return flat.value.rows.filter(
152
+ (row) =>
153
+ (!namespace.value || namespaceOf(row.key) === namespace.value) &&
154
+ (!needle || haystacks.value.get(row.key)?.includes(needle)),
155
+ )
156
+ })
157
+ return { namespaces, scoped }
158
+ }
159
+
160
+ /**
161
+ * Which keys the matrix shows, and how many each filter would show. A key
162
+ * listed in `pinned` stays shown after it stops matching (a translation just
163
+ * saved), until the filters change: rows never move under the user's hands.
164
+ */
165
+ export function useMatrixFilters(
166
+ flat: Ref<FlatTranslations>,
167
+ state: Ref<MatrixFilterState>,
168
+ shown: Ref<string[]>,
169
+ pinned: Ref<Set<string>>,
170
+ ) {
171
+ const { targets, statusOf, isRowMissing, predicates } = useRowStatuses(
172
+ flat,
173
+ state,
174
+ shown,
175
+ )
176
+ const { namespaces, scoped } = useScopedRows(flat, state)
177
+ const matches = (row: FlatTranslationRow) =>
178
+ predicates[state.value.filter](row)
179
+ const rows = computed(() =>
180
+ scoped.value.filter((row) => matches(row) || pinned.value.has(row.key)),
181
+ )
182
+
183
+ return {
184
+ rows,
185
+ groups: computed(() => groupRows(rows.value, isRowMissing)),
186
+ counts: computed(() => countRows(scoped.value, statusOf)),
187
+ targets,
188
+ namespaces,
189
+ isRowDone: (row: FlatTranslationRow) =>
190
+ pinned.value.has(row.key) && !matches(row),
191
+ }
192
+ }
193
+
194
+ export function groupRows(
195
+ rows: FlatTranslationRow[],
196
+ isRowMissing: (row: FlatTranslationRow) => boolean,
197
+ ): MatrixGroup[] {
198
+ const groups = new Map<string, MatrixGroup>()
199
+ for (const row of rows) {
200
+ const namespace = namespaceOf(row.key)
201
+ const group = groups.get(namespace) ?? {
202
+ namespace,
203
+ keys: 0,
204
+ missing: 0,
205
+ rows: [],
206
+ }
207
+ group.keys += 1
208
+ if (isRowMissing(row)) group.missing += 1
209
+ group.rows.push(row)
210
+ groups.set(namespace, group)
211
+ }
212
+ return [...groups.values()]
213
+ }
@@ -0,0 +1,104 @@
1
+ import type { Ref } from 'vue'
2
+ import type { FlatTranslationRow } from '../../types/lang'
3
+ import type { MatrixGroup } from './useMatrixFilters'
4
+
5
+ interface RenderedGroup extends MatrixGroup {
6
+ collapsed: boolean
7
+ /** The rows drawn so far: more come as the user scrolls. */
8
+ shown: FlatTranslationRow[]
9
+ }
10
+
11
+ const ROW_BATCH = 100
12
+ const COLLAPSE_ABOVE = 300
13
+
14
+ interface RowsContext {
15
+ groups: Ref<MatrixGroup[]>
16
+ toggled: Ref<Set<string>>
17
+ limit: Ref<number>
18
+ foldsByDefault: Ref<boolean>
19
+ }
20
+
21
+ function isCollapsed(context: RowsContext, namespace: string): boolean {
22
+ return context.foldsByDefault.value !== context.toggled.value.has(namespace)
23
+ }
24
+
25
+ function toggle(context: RowsContext, namespace: string) {
26
+ const next = new Set(context.toggled.value)
27
+ if (next.has(namespace)) next.delete(namespace)
28
+ else next.add(namespace)
29
+ context.toggled.value = next
30
+ }
31
+
32
+ function costOf(context: RowsContext, group: MatrixGroup): number {
33
+ return isCollapsed(context, group.namespace) ? 1 : group.rows.length + 1
34
+ }
35
+
36
+ function renderedGroups(context: RowsContext): RenderedGroup[] {
37
+ let budget = context.limit.value
38
+ const result: RenderedGroup[] = []
39
+ for (const group of context.groups.value) {
40
+ if (budget <= 0) break
41
+ const collapsed = isCollapsed(context, group.namespace)
42
+ const shown = collapsed ? [] : group.rows.slice(0, budget - 1)
43
+ budget -= shown.length + 1
44
+ result.push({ ...group, collapsed, shown })
45
+ }
46
+ return result
47
+ }
48
+
49
+ function reveal(context: RowsContext, key: string) {
50
+ let position = 0
51
+ for (const group of context.groups.value) {
52
+ const index = group.rows.findIndex((row) => row.key === key)
53
+ if (index >= 0) {
54
+ if (isCollapsed(context, group.namespace))
55
+ toggle(context, group.namespace)
56
+ context.limit.value = Math.max(
57
+ context.limit.value,
58
+ position + index + ROW_BATCH,
59
+ )
60
+ return
61
+ }
62
+ position += costOf(context, group)
63
+ }
64
+ }
65
+
66
+ /**
67
+ * Which rows of the matrix are drawn: namespaces fold by default on large
68
+ * workspaces, and rows are drawn in batches as the end of the list scrolls
69
+ * into view (no "Show more" to click), so thousands of keys stay fluid.
70
+ */
71
+ export function useMatrixRows(
72
+ groups: Ref<MatrixGroup[]>,
73
+ rowCount: Ref<number>,
74
+ ) {
75
+ const context: RowsContext = {
76
+ groups,
77
+ toggled: ref<Set<string>>(new Set()),
78
+ limit: ref(ROW_BATCH),
79
+ foldsByDefault: computed(
80
+ () => rowCount.value > COLLAPSE_ABOVE && groups.value.length > 1,
81
+ ),
82
+ }
83
+ const rendered = computed(() => renderedGroups(context))
84
+ const cost = computed(() =>
85
+ groups.value.reduce((sum, group) => sum + costOf(context, group), 0),
86
+ )
87
+
88
+ return {
89
+ rendered,
90
+ renderedRows: computed(() =>
91
+ rendered.value.flatMap((group) => group.shown),
92
+ ),
93
+ hasMore: computed(() => cost.value > context.limit.value),
94
+ toggle: (namespace: string) => toggle(context, namespace),
95
+ showMore() {
96
+ context.limit.value += ROW_BATCH
97
+ },
98
+ reveal: (key: string) => reveal(context, key),
99
+ reset() {
100
+ context.toggled.value = new Set()
101
+ context.limit.value = ROW_BATCH
102
+ },
103
+ }
104
+ }