@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,324 @@
1
+ import type { Ref } from 'vue'
2
+ import { storedText } from '../../utils/keySaves'
3
+ import {
4
+ checkMessage,
5
+ escapeMessage,
6
+ messageIssueText,
7
+ } from '../../utils/translations'
8
+ import { clearsBaseText } from './useKeySaves'
9
+ import type { CellAddress, useCellSaves } from './useCellSaves'
10
+ import type { useKeyPanel } from './useKeyPanel'
11
+
12
+ /** Where the editor goes once a cell is saved. */
13
+ export type CellMove = 'stay' | 'down' | 'next' | 'previous'
14
+
15
+ export interface CellRef {
16
+ key: string
17
+ code: string
18
+ }
19
+
20
+ export interface CellEditorOptions {
21
+ saves: ReturnType<typeof useCellSaves>
22
+ panel: ReturnType<typeof useKeyPanel>
23
+ workspace: Ref<string>
24
+ baseLocale: () => string
25
+ locales: () => string[]
26
+ canEdit: () => boolean
27
+ /** The text a cell shows: the one being saved, else the stored one. */
28
+ shownValue: (key: string, code: string) => string
29
+ /** The cell a move goes to from a cell, if any. */
30
+ targetOf: (cell: CellRef, move: CellMove) => CellRef | null
31
+ onStart: (key: string) => void
32
+ /** A move found no cell to go to: the focus goes back to the cell left. */
33
+ onMoveEnd: (cell: CellRef) => void
34
+ }
35
+
36
+ interface EditingCell extends CellRef {
37
+ /** The text the cell opens with. */
38
+ draft: string
39
+ }
40
+
41
+ interface LiveDraft {
42
+ text: string | undefined
43
+ }
44
+
45
+ interface OpenedCell {
46
+ workspace: string
47
+ isBase: boolean
48
+ }
49
+
50
+ interface EditorContext {
51
+ options: CellEditorOptions
52
+ editing: Ref<EditingCell | null>
53
+ cursor: Ref<CellRef | null>
54
+ live: LiveDraft
55
+ isDirty: Ref<boolean>
56
+ opened: OpenedCell
57
+ blockingText: (draft: string) => string
58
+ clearBaseKept: () => string
59
+ }
60
+
61
+ function addressOf(
62
+ context: EditorContext,
63
+ key: string,
64
+ code: string,
65
+ workspace = context.options.workspace.value,
66
+ ): CellAddress {
67
+ return { workspace, key, code }
68
+ }
69
+
70
+ function isEditing(context: EditorContext, key: string, code: string) {
71
+ const cell = context.editing.value
72
+ return cell?.key === key && cell.code === code
73
+ }
74
+
75
+ function trackDraft(
76
+ context: EditorContext,
77
+ key: string,
78
+ code: string,
79
+ text: string,
80
+ ) {
81
+ if (!isEditing(context, key, code)) return
82
+ context.live.text = text
83
+ context.isDirty.value = text !== context.options.shownValue(key, code)
84
+ }
85
+
86
+ function open(context: EditorContext, key: string, code: string, draft: string) {
87
+ context.editing.value = { key, code, draft }
88
+ trackDraft(context, key, code, draft)
89
+ }
90
+
91
+ function close(context: EditorContext) {
92
+ context.editing.value = null
93
+ context.live.text = undefined
94
+ context.isDirty.value = false
95
+ }
96
+
97
+ function isClearingBase(
98
+ context: EditorContext,
99
+ key: string,
100
+ code: string,
101
+ draft: string,
102
+ ): boolean {
103
+ const { baseLocale, shownValue } = context.options
104
+ return clearsBaseText(code, baseLocale(), draft, shownValue(key, code))
105
+ }
106
+
107
+ function keepInvalid(
108
+ context: EditorContext,
109
+ address: CellAddress,
110
+ draft: string,
111
+ ): boolean {
112
+ const reason = context.blockingText(draft)
113
+ if (reason) context.options.saves.keep(address, draft, reason)
114
+ return reason !== ''
115
+ }
116
+
117
+ function moveFrom(
118
+ context: EditorContext,
119
+ key: string,
120
+ code: string,
121
+ move: CellMove,
122
+ ) {
123
+ const target = context.options.targetOf({ key, code }, move)
124
+ if (target) nextTick(() => startEdit(context, target.key, target.code))
125
+ else if (move !== 'stay') context.options.onMoveEnd({ key, code })
126
+ }
127
+
128
+ function startEdit(
129
+ context: EditorContext,
130
+ key: string,
131
+ code: string,
132
+ draft?: string,
133
+ ) {
134
+ const { options } = context
135
+ if (!options.canEdit() || isEditing(context, key, code)) return
136
+ commitEditing(context)
137
+ const address = addressOf(context, key, code)
138
+ const failure = options.saves.failureOf(address)
139
+ options.saves.begin(address)
140
+ context.opened.workspace = options.workspace.value
141
+ context.opened.isBase = code === options.baseLocale()
142
+ open(context, key, code, draft ?? failure?.draft ?? options.shownValue(key, code))
143
+ context.cursor.value = { key, code }
144
+ options.onStart(key)
145
+ }
146
+
147
+ function cancelEdit(context: EditorContext) {
148
+ const cell = context.editing.value
149
+ if (cell) context.options.saves.end(addressOf(context, cell.key, cell.code))
150
+ close(context)
151
+ }
152
+
153
+ function commitEditing(context: EditorContext) {
154
+ const cell = context.editing.value
155
+ if (cell)
156
+ void commitCell(context, cell, 'stay', context.live.text ?? cell.draft)
157
+ }
158
+
159
+ async function commitCell(
160
+ context: EditorContext,
161
+ cell: CellRef,
162
+ move: CellMove,
163
+ draft: string,
164
+ ) {
165
+ const { saves, workspace, shownValue } = context.options
166
+ const { key, code } = cell
167
+ const isOpen = isEditing(context, key, code)
168
+ if (isOpen && context.opened.workspace !== workspace.value)
169
+ return leaveEditing(context)
170
+ if (isOpen) close(context)
171
+ const address = addressOf(context, key, code)
172
+ if (draft === shownValue(key, code)) {
173
+ saves.end(address)
174
+ return moveFrom(context, key, code, move)
175
+ }
176
+ if (
177
+ isClearingBase(context, key, code, draft) &&
178
+ !(await saves.confirmClearBase(key))
179
+ )
180
+ return open(context, key, code, draft)
181
+ if (!keepInvalid(context, address, draft)) void saves.save(address, draft)
182
+ moveFrom(context, key, code, move)
183
+ }
184
+
185
+ function leaveEditing(context: EditorContext) {
186
+ const cell = context.editing.value
187
+ if (!cell) return
188
+ const { saves } = context.options
189
+ const { workspace, isBase } = context.opened
190
+ const address = addressOf(context, cell.key, cell.code, workspace)
191
+ const draft = context.live.text ?? cell.draft
192
+ close(context)
193
+ const origin = saves.originOf(address)
194
+ if (storedText(draft) === (origin ?? null)) return saves.end(address)
195
+ if (keepInvalid(context, address, draft)) return
196
+ if (isBase && storedText(draft) === null && origin)
197
+ return saves.keep(address, draft, context.clearBaseKept())
198
+ void saves.save(address, draft)
199
+ }
200
+
201
+ async function retryCell(context: EditorContext, key: string, code: string) {
202
+ const { saves } = context.options
203
+ const failure = saves.failureOf(addressOf(context, key, code))
204
+ if (!failure) return
205
+ if (context.blockingText(failure.draft)) return startEdit(context, key, code)
206
+ if (
207
+ isClearingBase(context, key, code, failure.draft) &&
208
+ !(await saves.confirmClearBase(key))
209
+ )
210
+ return
211
+ saves.retry(addressOf(context, key, code))
212
+ }
213
+
214
+ function escapeFailure(context: EditorContext, key: string, code: string) {
215
+ const address = addressOf(context, key, code)
216
+ const failure = context.options.saves.failureOf(address)
217
+ if (!failure) return
218
+ const escaped = escapeMessage(failure.draft)
219
+ if (!keepInvalid(context, address, escaped))
220
+ void context.options.saves.save(address, escaped)
221
+ }
222
+
223
+ async function openKey(
224
+ context: EditorContext,
225
+ key: string,
226
+ focusLocale?: string,
227
+ ) {
228
+ const { saves, panel, locales } = context.options
229
+ const failures = locales()
230
+ .map((code) => [
231
+ code,
232
+ saves.failureOf(addressOf(context, key, code))?.draft,
233
+ ])
234
+ .filter((entry): entry is [string, string] => entry[1] !== undefined)
235
+ const initialDrafts = failures.length
236
+ ? Object.fromEntries(failures)
237
+ : undefined
238
+ if (!(await panel.open({ key, focusLocale, initialDrafts }))) return
239
+ for (const [code, draft] of failures) {
240
+ const address = addressOf(context, key, code)
241
+ if (saves.failureOf(address)?.draft === draft) saves.discard(address)
242
+ }
243
+ }
244
+
245
+ async function reopenEditor(
246
+ context: EditorContext,
247
+ key: string,
248
+ code: string,
249
+ draft: string,
250
+ ) {
251
+ close(context)
252
+ await nextTick()
253
+ if (!context.editing.value) open(context, key, code, draft)
254
+ }
255
+
256
+ async function openFromCell(
257
+ context: EditorContext,
258
+ key: string,
259
+ code: string,
260
+ draft: string,
261
+ ) {
262
+ const { saves, panel, shownValue } = context.options
263
+ const address = addressOf(context, key, code)
264
+ const failure = saves.failureOf(address)
265
+ const text = draft !== shownValue(key, code) ? draft : failure?.draft
266
+ const initialDrafts = text === undefined ? undefined : { [code]: text }
267
+ const isShown = await panel.open({ key, focusLocale: code, initialDrafts })
268
+ if (!isEditing(context, key, code)) return
269
+ if (!isShown) return reopenEditor(context, key, code, draft)
270
+ saves.end(address)
271
+ if (failure) saves.discard(address)
272
+ close(context)
273
+ }
274
+
275
+ function useEditorContext(options: CellEditorOptions): EditorContext {
276
+ const { t } = useI18n()
277
+ return {
278
+ options,
279
+ editing: ref<EditingCell | null>(null),
280
+ cursor: ref<CellRef | null>(null),
281
+ live: { text: undefined },
282
+ isDirty: ref(false),
283
+ opened: { workspace: '', isBase: false },
284
+ blockingText: (draft) =>
285
+ checkMessage('', draft)
286
+ .filter((issue) => issue.severity === 'error')
287
+ .map((issue) => messageIssueText(issue, t))
288
+ .join(' '),
289
+ clearBaseKept: () => t('dms_lang.matrix.clear_base_kept'),
290
+ }
291
+ }
292
+
293
+ /**
294
+ * The inline editing of the matrix: one cell open at a time, saved when it
295
+ * is left (a text that would not compile stays "Not saved" in its cell, an
296
+ * emptied base text asks first), handed over to the key panel on demand, and
297
+ * saved in the workspace it was typed in when the page moves to another one.
298
+ */
299
+ export function useCellEditor(options: CellEditorOptions) {
300
+ const context = useEditorContext(options)
301
+
302
+ return {
303
+ cursor: readonly(context.cursor),
304
+ hasOpenDraft: readonly(context.isDirty),
305
+ isEditing: (key: string, code: string) => isEditing(context, key, code),
306
+ initialDraftOf: (key: string, code: string) =>
307
+ isEditing(context, key, code) ? (context.editing.value?.draft ?? '') : '',
308
+ trackDraft: (key: string, code: string, text: string) =>
309
+ trackDraft(context, key, code, text),
310
+ startEdit: (key: string, code: string) => startEdit(context, key, code),
311
+ cancelEdit: () => cancelEdit(context),
312
+ commitEditing: () => commitEditing(context),
313
+ commitCell: (key: string, code: string, move: CellMove, draft: string) =>
314
+ commitCell(context, { key, code }, move, draft),
315
+ leaveEditing: () => leaveEditing(context),
316
+ retryCell: (key: string, code: string) => retryCell(context, key, code),
317
+ escapeFailure: (key: string, code: string) =>
318
+ escapeFailure(context, key, code),
319
+ openKey: (key: string, focusLocale?: string) =>
320
+ openKey(context, key, focusLocale),
321
+ openFromCell: (key: string, code: string, draft: string) =>
322
+ openFromCell(context, key, code, draft),
323
+ }
324
+ }
@@ -0,0 +1,155 @@
1
+ import type { Ref } from 'vue'
2
+ import type { FlatTranslationRow } from '../../types/lang'
3
+ import type { CellMove, CellRef } from './useCellEditor'
4
+ import type { MatrixGroup } from './useMatrixFilters'
5
+ import type { useMatrixRows } from './useMatrixRows'
6
+
7
+ /** Forward or backward through the cells. */
8
+ export type JumpStep = 1 | -1
9
+
10
+ export interface CellMovesOptions {
11
+ view: ReturnType<typeof useMatrixRows>
12
+ groups: Ref<MatrixGroup[]>
13
+ columnCodes: Ref<string[]>
14
+ targets: Ref<string[]>
15
+ baseLocale: () => string
16
+ isPending: (key: string, code: string) => boolean
17
+ }
18
+
19
+ type MoveTarget = (options: CellMovesOptions, cell: CellRef) => CellRef | null
20
+
21
+ const TYPING_FIELDS = 'input, textarea, select, [contenteditable]'
22
+ const GRID_CELL = 'td[data-row]'
23
+ const ARROW_STEPS: Record<string, [number, number]> = {
24
+ ArrowUp: [-1, 0],
25
+ ArrowDown: [1, 0],
26
+ ArrowLeft: [0, -1],
27
+ ArrowRight: [0, 1],
28
+ }
29
+
30
+ function navigableCodes(options: CellMovesOptions, code: string): string[] {
31
+ const base = options.baseLocale()
32
+ const codes = options.columnCodes.value
33
+ if (code === base || codes.length === 1) return codes
34
+ return codes.filter((entry) => entry !== base)
35
+ }
36
+
37
+ function neighbourRow(
38
+ options: CellMovesOptions,
39
+ key: string,
40
+ step: JumpStep,
41
+ ): string | undefined {
42
+ const { view } = options
43
+ const shown = view.renderedRows.value
44
+ const index = shown.findIndex((row) => row.key === key)
45
+ if (step > 0 && index === shown.length - 1 && view.hasMore.value)
46
+ view.showMore()
47
+ return view.renderedRows.value[index + step]?.key
48
+ }
49
+
50
+ function sideCell(
51
+ options: CellMovesOptions,
52
+ cell: CellRef,
53
+ step: JumpStep,
54
+ ): CellRef | null {
55
+ const codes = navigableCodes(options, cell.code)
56
+ const next = codes[codes.indexOf(cell.code) + step]
57
+ if (next) return { key: cell.key, code: next }
58
+ const row = neighbourRow(options, cell.key, step)
59
+ if (!row) return null
60
+ return { key: row, code: step > 0 ? codes[0] : codes[codes.length - 1] }
61
+ }
62
+
63
+ const MOVES: Record<CellMove, MoveTarget> = {
64
+ stay: () => null,
65
+ down: (options, cell) => {
66
+ const row = neighbourRow(options, cell.key, 1)
67
+ return row ? { key: row, code: cell.code } : null
68
+ },
69
+ next: (options, cell) => sideCell(options, cell, 1),
70
+ previous: (options, cell) => sideCell(options, cell, -1),
71
+ }
72
+
73
+ function missingOfRow(
74
+ options: CellMovesOptions,
75
+ row: FlatTranslationRow,
76
+ codes: string[],
77
+ ): CellRef[] {
78
+ return codes
79
+ .filter(
80
+ (code) =>
81
+ isRowValueMissing(row, code) && !options.isPending(row.key, code),
82
+ )
83
+ .map((code) => ({ key: row.key, code }))
84
+ }
85
+
86
+ function missingCells(options: CellMovesOptions): CellRef[] {
87
+ const codes = options.columnCodes.value.filter((code) =>
88
+ options.targets.value.includes(code),
89
+ )
90
+ return options.groups.value.flatMap((group) =>
91
+ group.rows.flatMap((row) => missingOfRow(options, row, codes)),
92
+ )
93
+ }
94
+
95
+ function cellIndex(cell: CellRef, keys: string[], codes: string[]): number {
96
+ return keys.indexOf(cell.key) * codes.length + codes.indexOf(cell.code)
97
+ }
98
+
99
+ function pickMissing(
100
+ options: CellMovesOptions,
101
+ from: CellRef | null,
102
+ step: JumpStep,
103
+ ): CellRef | undefined {
104
+ const cells = missingCells(options)
105
+ if (!from) return step > 0 ? cells[0] : cells.at(-1)
106
+ const keys = options.groups.value.flatMap((group) =>
107
+ group.rows.map((row) => row.key),
108
+ )
109
+ const codes = options.columnCodes.value
110
+ const origin = cellIndex(from, keys, codes)
111
+ const ordered = step > 0 ? cells : [...cells].reverse()
112
+ const found = ordered.find(
113
+ (cell) => step * (cellIndex(cell, keys, codes) - origin) > 0,
114
+ )
115
+ return found ?? ordered[0]
116
+ }
117
+
118
+ /** Whether a key goes to a field being typed in. */
119
+ export function isTypingTarget(target: EventTarget | null): boolean {
120
+ return target instanceof HTMLElement && target.closest(TYPING_FIELDS) !== null
121
+ }
122
+
123
+ /** Arrow keys move the focus between the cells of the grid, as in a spreadsheet. */
124
+ export function focusNeighbourCell(
125
+ event: KeyboardEvent,
126
+ grid: HTMLElement | undefined,
127
+ ) {
128
+ const step = ARROW_STEPS[event.key]
129
+ const cell =
130
+ event.target instanceof HTMLElement
131
+ ? event.target.closest<HTMLElement>(GRID_CELL)
132
+ : null
133
+ if (!step || !cell || isTypingTarget(event.target)) return
134
+ const row = Number(cell.dataset.row) + step[0]
135
+ const column = Number(cell.dataset.col) + step[1]
136
+ const target = grid?.querySelector<HTMLElement>(
137
+ `td[data-row="${row}"][data-col="${column}"] button`,
138
+ )
139
+ if (!target) return
140
+ event.preventDefault()
141
+ target.focus()
142
+ }
143
+
144
+ /**
145
+ * Where the editor goes from a cell: down, to the next or the previous cell
146
+ * of the languages one translates (the base only when one edits it), or to
147
+ * the next empty cell of the counted languages.
148
+ */
149
+ export function useCellMoves(options: CellMovesOptions) {
150
+ return {
151
+ targetOf: (cell: CellRef, move: CellMove) => MOVES[move](options, cell),
152
+ nextMissing: (from: CellRef | null, step: JumpStep) =>
153
+ pickMissing(options, from, step),
154
+ }
155
+ }