| 1 | export type EditSummaryLocale = 'zh' | 'en' |
| 2 | |
| 3 | export type EditSummaryPageFact = { |
| 4 | pageNumber: number |
| 5 | } |
| 6 | |
| 7 | export type SuccessfulEditSummaryCoreInput = { |
| 8 | appLocale: EditSummaryLocale |
| 9 | changedPages: EditSummaryPageFact[] |
| 10 | editScope: 'page' | 'selector' | 'deck' |
| 11 | failedPageLabels?: string[] |
| 12 | } |
| 13 | |
| 14 | const uiText = (locale: EditSummaryLocale, zh: string, en: string): string => |
| 15 | locale === 'en' ? en : zh |
| 16 | |
| 17 | const pageLabel = (locale: EditSummaryLocale, pageNumber: number): string => |
| 18 | uiText(locale, '第' + pageNumber + '页', 'page ' + pageNumber) |
| 19 | |
| 20 | export const buildLocalSuccessfulEditSummary = (args: SuccessfulEditSummaryCoreInput): string => { |
| 21 | const { appLocale, changedPages, editScope, failedPageLabels = [] } = args |
| 22 | const changedLabels = changedPages |
| 23 | .map((page) => pageLabel(appLocale, page.pageNumber)) |
| 24 | .join(uiText(appLocale, '、', ', ')) |
| 25 | const failedLabels = failedPageLabels.join(uiText(appLocale, '、', ', ')) |
| 26 | |
| 27 | if (changedPages.length > 0 && failedPageLabels.length > 0) { |
| 28 | return uiText( |
| 29 | appLocale, |
| 30 | '部分修改完成:成功 ' + changedLabels + ';失败 ' + failedLabels + '。', |
| 31 | 'Partial edit completed: succeeded on ' + changedLabels + '; failed on ' + failedLabels + '.' |
| 32 | ) |
| 33 | } |
| 34 | if (changedPages.length === 0 && failedPageLabels.length > 0) { |
| 35 | return uiText( |
| 36 | appLocale, |
| 37 | '页面修改失败:' + failedLabels + '。', |
| 38 | 'Page edit failed: ' + failedLabels + '.' |
| 39 | ) |
| 40 | } |
| 41 | if (changedPages.length === 0) { |
| 42 | return uiText( |
| 43 | appLocale, |
| 44 | '这次没有检测到需要保存的页面变化。', |
| 45 | 'No page changes needed to be saved this time.' |
| 46 | ) |
| 47 | } |
| 48 | if (editScope === 'selector') { |
| 49 | return uiText( |
| 50 | appLocale, |
| 51 | '已完成' + changedLabels + '的选中元素修改。', |
| 52 | changedPages.length === 1 |
| 53 | ? 'Updated the selected element on ' + changedLabels + '.' |
| 54 | : 'Updated selected elements on ' + changedLabels + '.' |
| 55 | ) |
| 56 | } |
| 57 | return uiText(appLocale, '修改完成:' + changedLabels + '。', 'Edit completed: ' + changedLabels + '.') |
| 58 | } |
| 59 |