@_tc/template-core 0.2.0 → 0.2.1-bate.1

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 (87) hide show
  1. package/AGENT_README.md +5 -2
  2. package/README.md +76 -31
  3. package/cjs/app/controller/project.js +1 -1
  4. package/cjs/app/controller/view.js +1 -1
  5. package/cjs/app/extends/db.js +4 -4
  6. package/cjs/app/middleware.js +1 -1
  7. package/cjs/app/middlewares/error-handle.js +1 -1
  8. package/cjs/app/middlewares/project-handler.js +1 -1
  9. package/cjs/app/utils/i18n.js +1 -0
  10. package/cjs/app/view/entry.tpl +1 -0
  11. package/cjs/config/config.default.js +1 -1
  12. package/cjs/packages/common/i18n/default.js +1 -1
  13. package/cjs/packages/common/i18n/en-US.js +1 -1
  14. package/cjs/packages/common/index.js +1 -1
  15. package/esm/app/controller/project.js +12 -11
  16. package/esm/app/controller/view.js +2 -1
  17. package/esm/app/extends/db.js +64 -60
  18. package/esm/app/middleware.js +1 -1
  19. package/esm/app/middlewares/error-handle.js +16 -15
  20. package/esm/app/middlewares/project-handler.js +8 -7
  21. package/esm/app/utils/i18n.js +36 -0
  22. package/esm/app/view/entry.tpl +1 -0
  23. package/esm/config/config.default.js +2 -1
  24. package/esm/packages/common/i18n/default.js +13 -0
  25. package/esm/packages/common/i18n/en-US.js +13 -0
  26. package/esm/packages/common/index.js +9 -9
  27. package/fe/frontend/apps/dash/Dashboard.js +6 -4
  28. package/fe/frontend/apps/dash/dash.entry.js +2 -0
  29. package/fe/frontend/src/common/generateMenuData.d.ts +4 -1
  30. package/fe/frontend/src/common/generateMenuData.js +3 -3
  31. package/fe/frontend/src/common/language.d.ts +1 -2
  32. package/fe/frontend/src/common/language.js +10 -10
  33. package/fe/frontend/src/common/request.js +3 -1
  34. package/fe/frontend/src/components/AsyncSelect/AsyncSelect.js +10 -2
  35. package/fe/frontend/src/components/BasePage/HeaderView.js +2 -2
  36. package/fe/frontend/src/components/LanguageSwitch/LanguageSwitch.js +13 -9
  37. package/fe/frontend/src/components/ThemeSwitch/ThemeSwitch.js +4 -3
  38. package/fe/frontend/src/defaultPages/NotFoundPage/index.js +4 -3
  39. package/fe/frontend/src/defaultPages/SchemaPage/components/CallCom/DetailPanel.js +14 -9
  40. package/fe/frontend/src/defaultPages/SchemaPage/components/CallCom/PopFrom.js +8 -7
  41. package/fe/frontend/src/defaultPages/SchemaPage/components/SchemaSearch/index.js +6 -12
  42. package/fe/frontend/src/defaultPages/SchemaPage/components/SchemaTable/index.js +25 -18
  43. package/fe/frontend/src/defaultPages/SchemaPage/index.js +5 -3
  44. package/fe/frontend/src/defaultPages/SidebarSlotPageTmp.js +4 -2
  45. package/fe/frontend/src/hooks/useText.d.ts +3 -0
  46. package/fe/frontend/src/hooks/useText.js +14 -0
  47. package/fe/frontend/src/index.d.ts +1 -0
  48. package/fe/frontend/src/index.js +3 -2
  49. package/fe/frontend/src/language/index.d.ts +0 -2
  50. package/fe/frontend/src/language/index.js +1 -7
  51. package/fe/frontend/src/language/resources.d.ts +2 -0
  52. package/fe/frontend/src/language/resources.js +9 -0
  53. package/fe/packages/common/i18n/default.d.ts +15 -0
  54. package/fe/packages/common/i18n/default.js +16 -0
  55. package/fe/packages/common/i18n/en-US.d.ts +15 -0
  56. package/fe/packages/common/i18n/en-US.js +18 -2
  57. package/fe/packages/common/i18n/index.js +13 -0
  58. package/fe/packages/common/i18n/locales.js +7 -0
  59. package/fe/packages/react/ui/components/Date/Calendar.js +9 -5
  60. package/fe/packages/react/ui/components/Date/LocaleContext.d.ts +22 -1
  61. package/fe/packages/react/ui/components/Date/LocaleContext.js +28 -3
  62. package/fe/packages/react/ui/components/Date/LocaleProvider.d.ts +1 -1
  63. package/fe/packages/react/ui/components/Date/LocaleProvider.js +7 -16
  64. package/fe/packages/react/ui/components/Date/index.js +2 -2
  65. package/fe/packages/react/ui/components/Date/locales.d.ts +2 -0
  66. package/fe/packages/react/ui/components/Date/locales.js +29 -13
  67. package/fe/packages/react/ui/components/TableSearch/TableSearch.js +21 -1
  68. package/fe/packages/react/ui/components/TableSearch/lang.d.ts +2 -0
  69. package/fe/packages/react/ui/components/TableSearch/lang.js +18 -7
  70. package/model/frontend/src/common/language.d.ts +1 -2
  71. package/model/frontend/src/hooks/useText.d.ts +3 -0
  72. package/model/frontend/src/language/index.d.ts +0 -2
  73. package/model/frontend/src/language/resources.d.ts +2 -0
  74. package/model/packages/common/i18n/default.d.ts +15 -0
  75. package/model/packages/common/i18n/en-US.d.ts +15 -0
  76. package/model/packages/react/ui/components/Date/LocaleContext.d.ts +22 -1
  77. package/model/packages/react/ui/components/Date/LocaleProvider.d.ts +1 -1
  78. package/model/packages/react/ui/components/Date/locales.d.ts +2 -0
  79. package/model/packages/react/ui/components/TableSearch/lang.d.ts +2 -0
  80. package/package.json +1 -1
  81. package/types/app/utils/i18n.d.ts +12 -0
  82. package/types/config/config.default.d.ts +6 -0
  83. package/types/packages/common/i18n/default.d.ts +19 -0
  84. package/types/packages/common/i18n/en-US.d.ts +21 -2
  85. package/types/packages/common/i18n/index.d.ts +21 -0
  86. package/types/packages/common/i18n/locales.d.ts +10 -0
  87. package/types/packages/common/i18n/types.d.ts +40 -0
@@ -40,6 +40,11 @@ export interface DefaultAppConfig {
40
40
  viewsPath?: string;
41
41
  nunjucks?: import('nunjucks').ConfigureOptions;
42
42
  notFoundRedirectCount?: number;
43
+ /**
44
+ * 静态资源缓存时间,单位 ms。
45
+ * @default 5 * 60 * 1000
46
+ */
47
+ resourceCacheTimeMs?: number;
43
48
  /**
44
49
  * 登陆验证相关
45
50
  */
@@ -67,5 +72,6 @@ declare const _default: {
67
72
  RTKey: string;
68
73
  };
69
74
  notFoundRedirectCount: number;
75
+ resourceCacheTimeMs: number;
70
76
  };
71
77
  export default _default;
@@ -4,6 +4,7 @@
4
4
  * 这里只放组件内置文案,业务项目可以通过 I18nProvider.resources 覆盖或追加。
5
5
  */
6
6
  export declare const defaultLanguageResources: {
7
+ /** 通用操作文案。 */
7
8
  readonly common: {
8
9
  readonly tip: "提示";
9
10
  readonly confirm: "确认";
@@ -12,6 +13,22 @@ export declare const defaultLanguageResources: {
12
13
  readonly submit: "提交";
13
14
  readonly reset: "重置";
14
15
  };
16
+ readonly server: {
17
+ readonly errors: {
18
+ readonly network: "网络错误";
19
+ readonly invalidRequest: "请求不合法";
20
+ readonly getProjectFailed: "获取项目异常";
21
+ readonly dbNodeSqliteUnavailable: "[db] 默认数据库依赖 Node.js 内置 {sqliteSpecifier},当前运行环境不可用。请升级 Node.js,或在业务项目覆盖 app/extends/db.ts。{cause}";
22
+ readonly dbDatabaseSyncUnavailable: "[db] {sqliteSpecifier} 未提供 DatabaseSync,无法初始化默认 SQLite 数据库。";
23
+ readonly dbNamespaceRequired: "[db] namespace 不能为空";
24
+ readonly dbKeyRequired: "[db] key 不能为空";
25
+ readonly dbLimitInvalid: "[db] limit 必须是大于 0 的整数";
26
+ readonly dbOffsetInvalid: "[db] offset 必须是大于等于 0 的整数";
27
+ readonly dbValueNotSerializable: "[db] value 必须可以被 JSON.stringify 序列化。{cause}";
28
+ readonly dbClosed: "[db] SQLite 数据库连接已关闭";
29
+ };
30
+ };
31
+ /** UI 组件内置文案,按组件名分组。 */
15
32
  readonly components: {
16
33
  readonly breadcrumb: {
17
34
  readonly close: "关闭";
@@ -59,6 +76,7 @@ export declare const defaultLanguageResources: {
59
76
  readonly rotateRight: "右旋转";
60
77
  readonly flipHorizontal: "左右镜像";
61
78
  readonly flipVertical: "上下镜像";
79
+ /** {index} 会在运行时由调用方传入的插值数据替换。 */
62
80
  readonly previewImage: "预览第{index}张";
63
81
  };
64
82
  readonly modal: {
@@ -88,4 +106,5 @@ export declare const defaultLanguageResources: {
88
106
  };
89
107
  };
90
108
  };
109
+ /** 默认中文语言资源类型,供调用方推导 key 结构使用。 */
91
110
  export type DefaultLanguageResources = typeof defaultLanguageResources;
@@ -1,9 +1,10 @@
1
1
  /**
2
- * UI component library default English language resources.
2
+ * UI 组件库默认英文语言资源。
3
3
  *
4
- * Only built-in component copy belongs here. Applications can override or extend it with I18nProvider.resources.
4
+ * 这里只放组件内置文案,业务项目可以通过 I18nProvider.resources 覆盖或追加。
5
5
  */
6
6
  export declare const defaultEnglishResources: {
7
+ /** 通用操作文案。 */
7
8
  readonly common: {
8
9
  readonly tip: "Prompt";
9
10
  readonly confirm: "Confirm";
@@ -12,6 +13,22 @@ export declare const defaultEnglishResources: {
12
13
  readonly submit: "Submit";
13
14
  readonly reset: "Reset";
14
15
  };
16
+ readonly server: {
17
+ readonly errors: {
18
+ readonly network: "Network error";
19
+ readonly invalidRequest: "Invalid request";
20
+ readonly getProjectFailed: "Failed to get project";
21
+ readonly dbNodeSqliteUnavailable: "[db] The default database depends on Node.js built-in {sqliteSpecifier}, which is unavailable in the current runtime. Upgrade Node.js or override app/extends/db.ts in the business project.{cause}";
22
+ readonly dbDatabaseSyncUnavailable: "[db] {sqliteSpecifier} does not provide DatabaseSync, so the default SQLite database cannot be initialized.";
23
+ readonly dbNamespaceRequired: "[db] namespace cannot be empty";
24
+ readonly dbKeyRequired: "[db] key cannot be empty";
25
+ readonly dbLimitInvalid: "[db] limit must be an integer greater than 0";
26
+ readonly dbOffsetInvalid: "[db] offset must be an integer greater than or equal to 0";
27
+ readonly dbValueNotSerializable: "[db] value must be serializable by JSON.stringify.{cause}";
28
+ readonly dbClosed: "[db] SQLite database connection is closed";
29
+ };
30
+ };
31
+ /** UI 组件内置文案,按组件名分组。 */
15
32
  readonly components: {
16
33
  readonly breadcrumb: {
17
34
  readonly close: "Close";
@@ -59,6 +76,7 @@ export declare const defaultEnglishResources: {
59
76
  readonly rotateRight: "Rotate right";
60
77
  readonly flipHorizontal: "Flip horizontal";
61
78
  readonly flipVertical: "Flip vertical";
79
+ /** {index} 会在运行时由调用方传入的插值数据替换。 */
62
80
  readonly previewImage: "Preview image {index}";
63
81
  };
64
82
  readonly modal: {
@@ -88,4 +106,5 @@ export declare const defaultEnglishResources: {
88
106
  };
89
107
  };
90
108
  };
109
+ /** 默认英文语言资源类型,供调用方推导 key 结构使用。 */
91
110
  export type DefaultEnglishResources = typeof defaultEnglishResources;
@@ -1,6 +1,24 @@
1
+ /**
2
+ * @module @tc/common/i18n
3
+ *
4
+ * 轻量 i18n 运行时,负责维护当前语言、兜底语言、语言资源、翻译函数和订阅通知。
5
+ *
6
+ * 设计说明:
7
+ * 1. 该模块位于 @tc/common,需要同时服务浏览器、Node、SSR 和非 React 代码。
8
+ * 2. 这里没有使用 zustand 是为了保持 common 层轻依赖。zustand/vanilla 可以在 Node 运行,
9
+ * 但当前 i18n 只需要 getState / setState / subscribe 这几个最小能力。
10
+ * 3. React 层通过 @tc/ui-react/i18n 适配这个 store,通用层不直接依赖 React 或 UI 状态库。
11
+ *
12
+ * 运行步骤:
13
+ * 1. 初始化 state:读取本地缓存语言,注入默认语言、兜底语言和内置语言包。
14
+ * 2. 翻译文本:按 key 从当前语言读取,缺失时读取兜底语言,再回退到 options.fallback 或原 key。
15
+ * 3. 资源更新:setResources 可替换或批量合并资源,addResources 可追加或替换单个语种资源。
16
+ * 4. 状态通知:语言或资源变更后通知订阅方,React hook 和非 React 代码都可复用同一个运行时。
17
+ */
1
18
  import type { I18nDictionary, I18nInterpolationValues, I18nMessage, I18nResources, I18nStore, I18nStoreListener, I18nTranslateOptions, I18nTranslateState } from "./types";
2
19
  export * from "./types";
3
20
  export * from "./locales";
21
+ /** 判断某个消息节点是否是可继续递归访问的普通字典。 */
4
22
  export declare const isPlainI18nDictionary: (value: I18nMessage) => value is I18nDictionary;
5
23
  /**
6
24
  * 深合并单个语言包。
@@ -27,6 +45,7 @@ export declare const interpolateI18nMessage: (value: string, data?: I18nInterpol
27
45
  * 优先级:指定语言 -> 兜底语言 -> options.fallback -> 原 key。
28
46
  */
29
47
  export declare const translate: <T = string>(state: I18nTranslateState, key: string, fillingData?: I18nInterpolationValues, options?: I18nTranslateOptions) => T | string;
48
+ /** 全局 i18n store,React 适配层和非 React 代码共享这一份状态。 */
30
49
  export declare const i18nStore: {
31
50
  getState: () => I18nStore;
32
51
  getInitialState: () => I18nStore;
@@ -38,5 +57,7 @@ export declare const i18nStore: {
38
57
  * 每次调用都会读取最新 state,适合在非 React 代码中直接使用。
39
58
  */
40
59
  export declare const i18n: <T = string>(key: string, fillingData?: I18nInterpolationValues, options?: I18nTranslateOptions) => string | T;
60
+ /** i18n 的简写别名。 */
41
61
  export declare const t: <T = string>(key: string, fillingData?: I18nInterpolationValues, options?: I18nTranslateOptions) => string | T;
62
+ /** 兼容历史调用的别名,行为等同于 i18n。 */
42
63
  export declare const getLanguage: <T = string>(key: string, fillingData?: I18nInterpolationValues, options?: I18nTranslateOptions) => string | T;
@@ -1,7 +1,17 @@
1
1
  import type { I18nResources } from "./types";
2
+ /** 默认语言,作为初始化语言和翻译兜底语言使用。 */
2
3
  export declare const defaultLanguage: "zh-CN";
4
+ /** 默认保存用户语言偏好的 localStorage key。 */
3
5
  export declare const languageLocalKey = "tc_language";
6
+ /**
7
+ * 内置语言资源。
8
+ *
9
+ * 业务项目可以通过 I18nProvider.resources、setResources 或 addResources 覆盖和追加。
10
+ */
4
11
  export declare const translations: I18nResources;
12
+ /**
13
+ * 内置语言联合类型,同时允许业务侧扩展自定义语言码。
14
+ */
5
15
  export type Language = keyof typeof translations | (string & {});
6
16
  export * from "./default";
7
17
  export * from "./en-US";
@@ -1,16 +1,39 @@
1
+ /**
2
+ * 语言标识。
3
+ *
4
+ * 推荐使用 BCP 47 风格的语言码,例如 zh-CN、en-US;业务侧也可以传入自定义语言码。
5
+ */
1
6
  export type I18nLanguage = string;
7
+ /** 插值和叶子文案允许使用的基础值。 */
2
8
  export type I18nPrimitive = string | number | boolean | null | undefined;
9
+ /**
10
+ * 单条翻译消息。
11
+ *
12
+ * 可以是字符串、基础值、数组,也可以继续嵌套为字典,便于通过 common.ok 这类点分路径访问。
13
+ */
3
14
  export type I18nMessage = I18nPrimitive | I18nMessage[] | readonly I18nMessage[] | I18nDictionary;
15
+ /** 单个语言包的字典结构。 */
4
16
  export interface I18nDictionary {
5
17
  [key: string]: I18nMessage;
6
18
  }
19
+ /** 按语言码组织的多语言资源集合。 */
7
20
  export type I18nResources<Language extends I18nLanguage = I18nLanguage> = Partial<Record<Language, I18nDictionary>>;
21
+ /**
22
+ * 文案插值数据。
23
+ *
24
+ * 例如 "Hello, {name}" 会从 { name: "Tom" } 中读取 name 并替换。
25
+ */
8
26
  export type I18nInterpolationValues = Record<string, I18nPrimitive>;
27
+ /** 执行一次翻译所需的最小状态。 */
9
28
  export interface I18nTranslateState {
29
+ /** 当前展示语言。 */
10
30
  language: I18nLanguage;
31
+ /** 当前语言缺失文案时使用的兜底语言。 */
11
32
  fallbackLanguage: I18nLanguage;
33
+ /** 当前已注册的全部语言资源。 */
12
34
  resources: I18nResources;
13
35
  }
36
+ /** i18n 运行时状态。 */
14
37
  export interface I18nState extends I18nTranslateState {
15
38
  /** 持久化语言时使用的 localStorage key。 */
16
39
  storageKey: string;
@@ -43,7 +66,13 @@ export interface I18nAddResourcesOptions {
43
66
  */
44
67
  merge?: boolean;
45
68
  }
69
+ /**
70
+ * 翻译函数签名。
71
+ *
72
+ * T 用于调用方在非字符串资源场景下指定返回类型,例如数组或对象。
73
+ */
46
74
  export type I18nTranslator = <T = string>(key: string, fillingData?: I18nInterpolationValues, options?: I18nTranslateOptions) => T | string;
75
+ /** i18n store 暴露的所有操作。 */
47
76
  export interface I18nActions {
48
77
  /** 设置当前展示语言。 */
49
78
  setLanguage: (language: I18nLanguage) => void;
@@ -60,11 +89,22 @@ export interface I18nActions {
60
89
  /** 挂在 store 上的翻译函数,方便直接读取。 */
61
90
  t: I18nTranslator;
62
91
  }
92
+ /** 完整 i18n store,包含状态和操作。 */
63
93
  export type I18nStore = I18nState & I18nActions;
94
+ /** store 状态变更订阅回调。 */
64
95
  export type I18nStoreListener = (state: I18nStore, previousState: I18nStore) => void;
96
+ /**
97
+ * 最小 store API。
98
+ *
99
+ * 形状接近 zustand/vanilla,方便 React 适配层或非 React 代码订阅同一份状态。
100
+ */
65
101
  export interface I18nStoreApi {
102
+ /** 获取当前 store 状态。 */
66
103
  getState: () => I18nStore;
104
+ /** 获取创建 store 时的初始状态。 */
67
105
  getInitialState: () => I18nStore;
106
+ /** 局部更新 store 状态。 */
68
107
  setState: (partial: Partial<I18nStore> | ((state: I18nStore) => Partial<I18nStore>)) => void;
108
+ /** 订阅状态变化,返回取消订阅函数。 */
69
109
  subscribe: (listener: I18nStoreListener) => () => void;
70
110
  }