@cbjwwwcode/common 0.0.7 → 0.0.8

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cbjwwwcode/common",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "license": "ISC",
@@ -67,6 +67,7 @@
67
67
  "dayjs": "^1.11.23",
68
68
  "dompurify": "^3.4.15",
69
69
  "es-toolkit": "^1.52.0",
70
+ "esthetic": "catalog:",
70
71
  "highlight.js": "^11.12.0",
71
72
  "hucre": "^1.1.0",
72
73
  "idb-keyval": "^6.3.0",
package/src/copy.ts ADDED
@@ -0,0 +1,56 @@
1
+ import { toast } from './toastify'
2
+ import { useClipboard } from '@vueuse/core'
3
+
4
+ interface UseCopyOptions {
5
+ successMessage?: string
6
+ errorMessage?: string
7
+ showMessage?: boolean
8
+ onSuccess?: (text: string) => void
9
+ onError?: (error: Error) => void
10
+ }
11
+
12
+ export function useCopy(options: UseCopyOptions = {}) {
13
+ const {
14
+ successMessage = '复制成功',
15
+ errorMessage = '复制失败',
16
+ showMessage = true,
17
+ onSuccess,
18
+ onError,
19
+ } = options
20
+
21
+ const { copy, copied, isSupported } = useClipboard()
22
+
23
+ /**
24
+ * 复制文本
25
+ */
26
+ const handleCopy = async (text: string | number): Promise<boolean> => {
27
+ if (!isSupported.value) {
28
+ showMessage && toast.warning('当前环境不支持复制')
29
+ return false
30
+ }
31
+
32
+ const content = String(text ?? '')
33
+
34
+ if (!content) {
35
+ showMessage && toast.warning('复制内容为空')
36
+ return false
37
+ }
38
+
39
+ try {
40
+ await copy(content)
41
+ showMessage && toast.success(successMessage)
42
+ onSuccess?.(content)
43
+ return true
44
+ } catch (error) {
45
+ showMessage && toast.error(errorMessage)
46
+ onError?.(error as Error)
47
+ return false
48
+ }
49
+ }
50
+
51
+ return {
52
+ copy: handleCopy,
53
+ copied,
54
+ isSupported,
55
+ }
56
+ }
package/src/format.ts ADDED
@@ -0,0 +1,71 @@
1
+ import type { Rules } from 'esthetic';
2
+ import esthetic from 'esthetic';
3
+ import { isString } from "es-toolkit/compat";
4
+
5
+ const formatMsps = new Set(['text','html','liquid','javascript','markdown','jsx','typescript','tsx','json','css','scss','sass','less','xml','unknown'])
6
+
7
+ export type {
8
+ Rules as FormatRule
9
+ }
10
+
11
+ export {
12
+ esthetic
13
+ }
14
+
15
+ /**
16
+ *
17
+ * @param code
18
+ * @param language
19
+ * @param rules
20
+ * indentSize number 缩进空格数,默认 2
21
+ * indentChar string 缩进字符,默认空格 " "
22
+ * wrap number 换行宽度控制,0 表示不自动换行
23
+ * preserveLine number 保留连续空行的最大数量,默认 3
24
+ * crlf boolean 是否使用 CRLF 换行符,默认 false
25
+ * endNewline boolean 文件末尾是否添加换行符
26
+ * markup.forceAttribute boolean 是否强制将属性换行显示(从代码片段看有这个选项)
27
+ * markup.attributeSort boolean 是否对 HTML 属性排序
28
+ */
29
+ export function formatCode(code: string, language: Rules['language'], rules?: Rules){
30
+ language = isString(language) && formatMsps.has(language) ? language : (language=='vue'?'html':'auto')
31
+ return esthetic.format(code, {
32
+ language,
33
+ // "default" | "recommended" | "strict" | "warrington" | "prettier"
34
+ // preset: 'recommended',
35
+ preset: 'recommended',
36
+ // indentSize: 2,
37
+ // wrap: 0,
38
+ preserveLine: 0,
39
+ // endNewline: true,
40
+ markup: {
41
+ attributeSort: true,
42
+ forceAttribute: 120, // 超过 120 字符才换行,相当于尽量不换
43
+ quoteConvert: 'double',
44
+ attributeCasing: 'preserve',
45
+ selfCloseSpace: false,
46
+ ignoreCSS: false,
47
+ ignoreJS: false,
48
+ preserveText: false
49
+ },
50
+ script:{
51
+ styleGuide: 'airbnb',
52
+ quoteConvert: 'single',
53
+ endComma: 'always',
54
+ variableList: 'each',
55
+ braceStyle: 'collapse',
56
+ noSemicolon: false
57
+ },
58
+ style:{
59
+ commentIndent: true, //注释是否跟随代码缩进
60
+ preserveComment: true, //是否保留注释。默认 true,设为 false 可能会在格式化时移除注释。
61
+ atRuleSpace: true, //@ 规则(如 @media、@import)后是否加空格,影响 @media screen 这类写法的间距。
62
+ sortProperties: false, //是否对 CSS 规则块内的属性排序。启用后会把 color、display、margin 等按字母或预设顺序排列。
63
+ sortSelectors: false, //是否对选择器列表排序。例如 .a, .b, .c 会被重新排列。
64
+ noLeadZero: true, //是否移除数字前导零。0.5 变为 .5,0.95rem 变为 .95rem。这是 CSS 压缩中常见的风格。
65
+ quoteConvert: 'double', //引号转换。"double" 转为双引号,"single" 转为单引号,"none" 不转换。CSS 中属性值(如字体名、content)的引号会受影响。
66
+ },
67
+ ...rules
68
+ })
69
+ }
70
+
71
+
package/src/index.ts CHANGED
@@ -18,4 +18,6 @@ export * from './zod';
18
18
  export * from './vtj';
19
19
  export * from './lowlight';
20
20
  export * from './hucre';
21
+ export * from './copy';
22
+ export * from './format';
21
23
 
package/src/vtj/index.ts CHANGED
@@ -1,2 +1,6 @@
1
1
  export * from './node';
2
2
  export * from './provider';
3
+
4
+ export * as VtjCore from '@vtj/core';
5
+ export * as VtjRenderer from '@vtj/renderer';
6
+ export * as VtjUtils from '@vtj/utils';