@lxzy/code-snippet 0.0.15 → 0.0.18

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/index.html CHANGED
@@ -14,6 +14,6 @@
14
14
  </head>
15
15
  <body>
16
16
  <div id="root"></div>
17
- <script type="module" src="/src/main.tsx"></script>
17
+ <script type="module" src="/src/app-vite.tsx"></script>
18
18
  </body>
19
19
  </html>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lxzy/code-snippet",
3
- "version": "0.0.15",
3
+ "version": "0.0.18",
4
4
  "files": [
5
5
  "*",
6
6
  "!dist",
@@ -17,11 +17,11 @@
17
17
  "pub": "npm publish"
18
18
  },
19
19
  "dependencies": {
20
- "@ant-design/icons": "^5",
21
- "@ant-design/pro-components": "^2.6.0",
20
+ "@ant-design/icons": "^4",
21
+ "@ant-design/pro-components": "^1",
22
22
  "@ice/stark-app": "^1.5.0",
23
23
  "ahooks": "^3.1.9",
24
- "antd": "5.27.2",
24
+ "antd": "4",
25
25
  "axios": "^1.18.1",
26
26
  "classnames": "^2.3.0",
27
27
  "dayjs": "^1.11.0",
@@ -31,23 +31,27 @@
31
31
  "lodash-es": "^4.17.21",
32
32
  "mockjs": "^1.1.0",
33
33
  "moment": "^2.29.0",
34
- "react": "^18.0.0",
35
- "react-dom": "^18.0.0",
36
- "react-router-dom": "^6",
37
- "tslib": "^2.8.1",
34
+ "react": "^17",
35
+ "react-dom": "^17",
36
+ "react-router": "5.3.4",
37
+ "react-router-dom": "5.3.4",
38
38
  "unstated-next": "^1.1.0",
39
39
  "validator": "^13.7.0"
40
40
  },
41
41
  "devDependencies": {
42
42
  "@types/node": "^22.1.0",
43
- "@types/react": "^18.0.0",
44
- "@types/react-dom": "^18.0.0",
45
- "@vitejs/plugin-react": "^6.0.0",
43
+ "@vitejs/plugin-react-swc": "^3",
46
44
  "less": "^4.6.7",
47
45
  "prettier": "^2.2.0",
48
46
  "typescript": "^5.0.0",
49
- "vite": "^8.1.4",
47
+ "vite": "^6.0.0",
50
48
  "vite-plugin-external": "^6.2.2",
51
49
  "vite-plugin-mock": "^3.0.2"
50
+ },
51
+ "resolutions": {
52
+ "@types/react": "^17",
53
+ "@types/react-dom": "^17",
54
+ "@types/react-router": "5.1.20",
55
+ "@types/react-router-dom": "5.3.3"
52
56
  }
53
57
  }
package/src/app-vite.tsx CHANGED
@@ -1,20 +1,23 @@
1
1
  import { Suspense, lazy, useEffect } from 'react';
2
+ import ReactDom from 'react-dom';
2
3
  import {
3
- Routes,
4
+ Switch,
4
5
  Route,
5
- Navigate,
6
+ Redirect,
6
7
  useLocation,
7
- BrowserRouter,
8
+ HashRouter,
8
9
  } from 'react-router-dom';
9
-
10
10
  import history from '@/umi';
11
11
  import BasicLayout from '@/layouts/index';
12
12
  import { getBasename } from '@ice/stark-app';
13
13
  import routes from '../routes';
14
14
  import { PageLoading } from '@ant-design/pro-components';
15
15
  import { ConfigProvider } from 'antd';
16
+ import 'antd/es/notification/style/index.less';
16
17
  import moment from 'moment';
17
18
  import 'moment/dist/locale/zh-cn';
19
+ import './global.less';
20
+
18
21
  moment.locale('zh-cn');
19
22
 
20
23
  const pageModules = import.meta.glob([
@@ -43,32 +46,34 @@ const RouteWrapper = ({ componentPath }) => {
43
46
  const App = () => {
44
47
  return (
45
48
  <ConfigProvider>
46
- <BrowserRouter basename={getBasename()}>
49
+ <HashRouter>
47
50
  <BasicLayout>
48
51
  <Suspense fallback={<PageLoading />}>
49
- <Routes>
52
+ <Switch>
50
53
  {routes.map((route, i) =>
51
54
  route.redirect ? (
52
- <Route
53
- key={route.path || i}
54
- path={route.path}
55
- element={<Navigate to={route.redirect} replace />}
56
- />
55
+ <Route key={route.path || i} path={route.path} exact>
56
+ <Redirect to={route.redirect} />
57
+ </Route>
57
58
  ) : (
58
59
  <Route
59
60
  key={route.path || i}
60
61
  path={route.path}
61
- element={<RouteWrapper componentPath={route.component} />}
62
+ exact={route.path !== undefined}
63
+ render={() => (
64
+ <RouteWrapper componentPath={route.component} />
65
+ )}
62
66
  />
63
67
  ),
64
68
  )}
65
- <Route path="*" element={<Navigate to="/404" replace />} />
66
- </Routes>
69
+ </Switch>
67
70
  </Suspense>
68
71
  </BasicLayout>
69
- </BrowserRouter>
72
+ </HashRouter>
70
73
  </ConfigProvider>
71
74
  );
72
75
  };
73
76
 
74
77
  export default App;
78
+
79
+ ReactDom.render(<App></App>, document.getElementById('root'));
@@ -0,0 +1,46 @@
1
+ import React from 'react';
2
+ import { Button, Result } from 'antd';
3
+
4
+ class ErrorBoundary extends React.Component<any> {
5
+ state = { hasError: false, isChunkError: false, errorMessage: '' };
6
+
7
+ static getDerivedStateFromError(error) {
8
+ const isChunkError =
9
+ error?.name === 'ChunkLoadError' ||
10
+ error?.message?.includes('Loading chunk') ||
11
+ error?.message?.includes('Failed to fetch dynamically imported module');
12
+
13
+ return { hasError: true, isChunkError, errorMessage: error.message };
14
+ }
15
+
16
+ componentDidCatch(error, errorInfo) {
17
+ console.error('ErrorBoundary caught:', error, errorInfo);
18
+ }
19
+
20
+ handleRefresh = () => {
21
+ window.location.reload();
22
+ };
23
+
24
+ render() {
25
+ if (this.state.hasError) {
26
+ if (this.state.isChunkError) {
27
+ return (
28
+ <Result
29
+ status="warning"
30
+ title="资源加载失败"
31
+ subTitle="页面资源加载异常,可能是网络问题或版本更新导致"
32
+ extra={[
33
+ <Button type="primary" key="refresh" onClick={this.handleRefresh}>
34
+ 刷新浏览器
35
+ </Button>,
36
+ ]}
37
+ />
38
+ );
39
+ }
40
+ }
41
+
42
+ return this.props.children;
43
+ }
44
+ }
45
+
46
+ export default ErrorBoundary;
@@ -10,3 +10,11 @@ body,
10
10
  #root > div {
11
11
  height: 100%;
12
12
  }
13
+
14
+ .ant-modal-content {
15
+ border-radius: 8px;
16
+ }
17
+
18
+ .ant-modal-header {
19
+ border-radius: 8px;
20
+ }
@@ -4,6 +4,7 @@ import menuRoutes from '../../routes/menu';
4
4
  import getIcon from '@/utils/getIcon';
5
5
  import { useLocation, Link } from 'react-router-dom';
6
6
  import { isInIcestark } from '@ice/stark-app';
7
+ import ErrorBoundary from '@/components/ErrorBoundary';
7
8
 
8
9
  const menuDataRender = (menuList) => {
9
10
  return menuList.map((item) => {
@@ -33,6 +34,7 @@ const BasicLayout = (props) => {
33
34
  }}
34
35
  menuItemRender={(item, dom) => <Link to={item.path}>{dom}</Link>}
35
36
  locale="zh-CN"
37
+ ErrorBoundary={ErrorBoundary}
36
38
  >
37
39
  {children}
38
40
  </ProLayout>
@@ -1,11 +1,72 @@
1
- import React, { useMemo, useState } from 'react';
1
+ import React, { useEffect, useMemo, useState } from 'react';
2
2
  import styles from './index.module.less';
3
- import { DatePicker } from 'antd';
3
+ import { DatePicker, notification } from 'antd';
4
4
 
5
5
  export default function Home(props) {
6
+ const [duration, setDuration] = useState(null);
7
+
8
+ useEffect(() => {
9
+ function findBox(dataView, type, offset, end) {
10
+ while (offset < end) {
11
+ const size = dataView.getUint32(offset);
12
+ if (size === 0) break;
13
+ const boxType = String.fromCharCode(
14
+ dataView.getUint8(offset + 4),
15
+ dataView.getUint8(offset + 5),
16
+ dataView.getUint8(offset + 6),
17
+ dataView.getUint8(offset + 7),
18
+ );
19
+ if (boxType === type) return offset;
20
+ offset += size;
21
+ }
22
+ return null;
23
+ }
24
+
25
+ function getVideoDuration(buffer) {
26
+ const dataView = new DataView(buffer);
27
+ const fileEnd = buffer.byteLength;
28
+
29
+ let moovOffset = findBox(dataView, 'moov', 0, fileEnd);
30
+ if (moovOffset === null) return null;
31
+
32
+ const moovSize = dataView.getUint32(moovOffset);
33
+ const mvhdOffset = findBox(
34
+ dataView,
35
+ 'mvhd',
36
+ moovOffset + 8,
37
+ moovOffset + moovSize,
38
+ );
39
+ if (mvhdOffset === null) return null;
40
+
41
+ const version = dataView.getUint8(mvhdOffset + 8);
42
+ let timescale, duration;
43
+
44
+ if (version === 0) {
45
+ timescale = dataView.getUint32(mvhdOffset + 12 + 8);
46
+ duration = dataView.getUint32(mvhdOffset + 12 + 12);
47
+ } else {
48
+ const view64 = new DataView(buffer);
49
+ timescale = view64.getUint32(mvhdOffset + 20 + 8);
50
+ const high = view64.getUint32(mvhdOffset + 20 + 12);
51
+ const low = view64.getUint32(mvhdOffset + 20 + 16);
52
+ duration = high * 2 ** 32 + low;
53
+ }
54
+
55
+ return duration / timescale;
56
+ }
57
+
58
+ pm.sendRequest('http://vjs.zencdn.net/v/oceans.mp4', function (err, res) {
59
+ const dur = getVideoDuration(res.rawBody.buffer);
60
+ console.log('视频时长(秒):', dur);
61
+ });
62
+ }, []);
63
+
6
64
  return (
7
65
  <div>
8
- <DatePicker></DatePicker>
66
+ <p>
67
+ 视频时长:
68
+ {duration !== null ? `${duration.toFixed(2)} 秒` : '加载中...'}
69
+ </p>
9
70
  </div>
10
71
  );
11
72
  }
package/src/umi/index.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { createBrowserHistory } from 'history-with-query';
1
+ import { createHashHistory } from 'history-with-query';
2
2
  import { useModel, ModelProvider } from './useModel';
3
3
 
4
- const history = createBrowserHistory();
4
+ const history = createHashHistory();
5
5
 
6
6
  export { history, useModel, ModelProvider };
7
7
  export default history;
package/tsconfig.json CHANGED
@@ -1,9 +1,8 @@
1
1
  {
2
2
  "compilerOptions": {
3
- "module": "ESNext",
4
- "target": "ESNext",
5
- "lib": ["DOM", "ESNext", "DOM.Iterable"],
6
- "moduleResolution": "bundler",
3
+ "module": "esnext",
4
+ "target": "esnext",
5
+ "moduleResolution": "node",
7
6
  "resolveJsonModule": true,
8
7
  "importHelpers": true,
9
8
  "jsx": "react-jsx",
@@ -21,5 +20,11 @@
21
20
  "emitDecoratorMetadata": true,
22
21
  "skipLibCheck": true
23
22
  },
24
- "include": ["mock/**/*", "src/**/*", "routes/**/*", "typings.d.ts", "vite.*.ts"]
23
+ "include": [
24
+ "mock/**/*",
25
+ "src/**/*",
26
+ "routes/**/*",
27
+ "typings.d.ts",
28
+ "vite.*.ts"
29
+ ]
25
30
  }
package/vite.config.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import { defineConfig } from 'vite';
2
- import react from '@vitejs/plugin-react';
2
+ import react from '@vitejs/plugin-react-swc';
3
3
  import { viteMockServe } from 'vite-plugin-mock';
4
- import pluginExternal from 'vite-plugin-external';
5
4
  import path from 'path';
6
5
 
7
6
  export default defineConfig(({ command }) => ({
@@ -17,13 +16,6 @@ export default defineConfig(({ command }) => ({
17
16
  mockPath: 'mock',
18
17
  enable: command === 'serve',
19
18
  }),
20
- pluginExternal({
21
- externals: {
22
- react: 'React',
23
- 'react-dom': 'ReactDOM',
24
- antd: 'antd',
25
- },
26
- }),
27
19
  ],
28
20
  resolve: {
29
21
  alias: {
@@ -31,7 +23,6 @@ export default defineConfig(({ command }) => ({
31
23
  umi: path.resolve(__dirname, './src/umi'),
32
24
  '~antd': path.resolve(__dirname, './node_modules/antd'),
33
25
  },
34
- extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json'],
35
26
  },
36
27
  css: {
37
28
  preprocessorOptions: {
@@ -46,50 +37,6 @@ export default defineConfig(({ command }) => ({
46
37
  },
47
38
  build: {
48
39
  outDir: 'build',
49
- lib: {
50
- entry: path.resolve(__dirname, 'src/main.tsx'),
51
- name: 'CodeSnippet',
52
- formats: ['iife'],
53
- // fileName: (format) => `code-snippet.${format}.js`,
54
- },
55
- // rollupOptions: {
56
- // external: [
57
- // 'react',
58
- // 'react-dom',
59
- // 'antd',
60
- // // 'moment',
61
- // // 'react-router-dom',
62
- // // '@ant-design/pro-layout',
63
- // // '@ant-design/pro-table',
64
- // // '@ant-design/pro-form',
65
- // // '@ant-design/pro-descriptions',
66
- // // '@ant-design/pro-list',
67
- // // '@ant-design/icons',
68
- // // '@ice/stark-app',
69
- // // 'ahooks',
70
- // // 'axios',
71
- // ],
72
- // output: {
73
- // globals: {
74
- // react: 'React',
75
- // 'react-dom': 'ReactDOM',
76
- // antd: 'antd',
77
- // // moment: 'moment',
78
- // // 'react-router-dom': 'ReactRouterDOM',
79
- // // '@ant-design/pro-layout': 'AntDesignProLayout',
80
- // // '@ant-design/pro-table': 'AntDesignProTable',
81
- // // '@ant-design/pro-form': 'AntDesignProForm',
82
- // // '@ant-design/pro-descriptions': 'AntDesignProDescriptions',
83
- // // '@ant-design/pro-list': 'AntDesignProList',
84
- // // '@ant-design/icons': 'AntDesignIcons',
85
- // // '@ice/stark-app': 'IceStarkApp',
86
- // // ahooks: 'ahooks',
87
- // // axios: 'axios',
88
- // // classnames: 'classNames',
89
- // // dayjs: 'dayjs',
90
- // },
91
- // },
92
- // },
93
40
  },
94
41
  server: {
95
42
  port: 8000,
@@ -1,9 +0,0 @@
1
- {
2
- "permissions": {
3
- "allow": [
4
- "Bash(pnpm pub *)",
5
- "Bash(git add *)",
6
- "Bash(git commit *)"
7
- ]
8
- }
9
- }
package/src/main.tsx DELETED
@@ -1,24 +0,0 @@
1
- import React from 'react';
2
- import { createRoot } from 'react-dom/client';
3
- import App from './app-vite';
4
- import './global.css';
5
- import { setLibraryName, isInIcestark } from '@ice/stark-app';
6
-
7
- let root;
8
-
9
- if (!isInIcestark()) {
10
- root = createRoot(document.getElementById('root'));
11
- root.render(<App />);
12
- }
13
-
14
- export function mount(props) {
15
- root = createRoot(props.container);
16
- root.render(<App />);
17
- }
18
-
19
- export function unmount(props) {
20
- root?.unmount();
21
- }
22
-
23
- // 注意:`setLibraryName` 的入参需要与工程配置的 output.library 保持一致
24
- setLibraryName('CodeSnippet');