online-editor 1.0.0 → 1.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 (33) hide show
  1. package/README.md +72 -2
  2. package/dist/{freemarker2-DwY387IE.cjs → freemarker2-3q31Q_S2.cjs} +1 -1
  3. package/dist/{freemarker2-BtZobY0f.js → freemarker2-BUHCD1pt.js} +1 -1
  4. package/dist/{handlebars-DsUez5lW.cjs → handlebars-BlbYjyiu.cjs} +1 -1
  5. package/dist/{handlebars-BQwqRavn.js → handlebars-rV0MBoA8.js} +1 -1
  6. package/dist/{html-B0kQ0bDk.cjs → html-CZCUEc1B.cjs} +1 -1
  7. package/dist/{html-BwW7yNQ4.js → html-Drd4s-sB.js} +1 -1
  8. package/dist/{index-B77o5cnV.js → index-BfxRh_1b.js} +2066 -2040
  9. package/dist/{index-CBTI4pNS.cjs → index-CeMlLD5z.cjs} +94 -94
  10. package/dist/index.cjs +1 -1
  11. package/dist/index.js +1 -1
  12. package/dist/{index3-C0mBFl7i.cjs → index3-DwYRYD41.cjs} +1 -1
  13. package/dist/{index3-DroTBvmq.js → index3-Y0nRCdXH.js} +1 -1
  14. package/dist/{javascript-DOiBWfoH.js → javascript-DG8Gzqnx.js} +1 -1
  15. package/dist/{javascript-Bk_JWkrL.cjs → javascript-DGIzLuBH.cjs} +1 -1
  16. package/dist/{liquid-s3dEdimk.cjs → liquid-BAKcqMEz.cjs} +1 -1
  17. package/dist/{liquid-D7i5C0dB.js → liquid-DYHIOsqt.js} +1 -1
  18. package/dist/{mdx-DJQwZTre.cjs → mdx-B3mtyeVX.cjs} +1 -1
  19. package/dist/{mdx-Bht9Lx9R.js → mdx-BYgy7Ya_.js} +1 -1
  20. package/dist/{python-D8EWWELv.js → python-DMRBi-Dy.js} +1 -1
  21. package/dist/{python-Cm4JfyQk.cjs → python-DsPnOPuE.cjs} +1 -1
  22. package/dist/{razor-BsvQ-jt3.js → razor-B77S1w3R.js} +1 -1
  23. package/dist/{razor-CuaTdf-m.cjs → razor-CuB6Bc01.cjs} +1 -1
  24. package/dist/{typescript-BDrRGmS5.js → typescript-DZkfNH5N.js} +1 -1
  25. package/dist/{typescript-CUjBSVe4.cjs → typescript-TBZKitPx.cjs} +1 -1
  26. package/dist/{xml-CX-m4Epq.cjs → xml-A2kWeeBm.cjs} +1 -1
  27. package/dist/{xml-CC3rLl4D.js → xml-B0TgKOzm.js} +1 -1
  28. package/dist/{yaml-KPEyiK7o.js → yaml-7WAAz9S0.js} +1 -1
  29. package/dist/{yaml-B5rDIKBI.cjs → yaml-BK2yTMka.cjs} +1 -1
  30. package/package.json +1 -1
  31. package/types/components/index.vue.d.ts +6 -2
  32. package/types/index.d.ts +1 -0
  33. package/types/type.d.ts +6 -0
package/README.md CHANGED
@@ -14,7 +14,7 @@
14
14
 
15
15
  ## ✨ 特性
16
16
 
17
- - 🎨 **多框架支持** - 支持 Vue 2/3、React、Svelte、Solid、HTML、JavaScript、TypeScript
17
+ - 🎨 **多框架支持** - 支持 Vue 2/3、React、Svelte、Solid、HTML、JavaScript、TypeScript
18
18
  - 🚀 **Monaco Editor** - 基于 VS Code 同款编辑器,提供专业的代码编辑体验
19
19
  - 📱 **响应式布局** - 同时支持桌面端和移动端
20
20
  - 🎯 **即时预览** - 实时编译运行,所见即所得
@@ -115,10 +115,31 @@ createApp(App).mount('#app');`,
115
115
  | `document` | `string` | - | 文档链接地址 |
116
116
  | `github` | `string` | - | GitHub 地址 |
117
117
 
118
+ ## 📣 事件
119
+
120
+ | 事件名 | 参数 | 说明 |
121
+ |------|------|------|
122
+ | `code-change` | `EditorExportFile[]` | 编辑器内容变化时触发,实时导出全部文件代码 |
123
+
124
+ ```vue
125
+ <script setup lang="ts">
126
+ import OnlineEditor, { type EditorExportFile } from 'online-editor';
127
+ import 'online-editor/style.css';
128
+
129
+ const handleCodeChange = (files: EditorExportFile[]) => {
130
+ console.log(files);
131
+ };
132
+ </script>
133
+
134
+ <template>
135
+ <OnlineEditor @code-change="handleCodeChange" />
136
+ </template>
137
+ ```
138
+
118
139
  ### AppType 类型
119
140
 
120
141
  ```typescript
121
- type AppType = 'vue' | 'vue2' | 'vue3' | 'react' | 'svelte' | 'solid' | 'html' | 'javascript' | 'js' | 'typescript' | 'ts';
142
+ type AppType = 'vue' | 'vue2' | 'vue3' | 'react' | 'svelte' | 'solid' | 'html' | 'javascript' | 'js' | 'typescript' | 'ts';
122
143
  ```
123
144
 
124
145
  ### Control 类型
@@ -127,6 +148,55 @@ type AppType = 'vue' | 'vue2' | 'vue3' | 'react' | 'svelte' | 'solid' | 'html' |
127
148
  type Control = 'refresh' | 'copy' | 'share' | 'docs' | 'github' | 'format' | 'console';
128
149
  ```
129
150
 
151
+ ## 🏗️ 生产环境构建(Worker 兼容)
152
+
153
+ `online-editor` 依赖 Monaco 等能力,会用到 Web Worker。在某些静态部署场景(相对路径、非根路径、CDN/反向代理等)下,构建产物里的 Worker chunk 地址可能解析不符合预期,导致 Worker 404 或无法启动。
154
+
155
+ 一个通用做法是在构建完成后对产物做一次“构建后补丁”:把特定形式的 `new Worker(new URL(..., import.meta.url))` 改写为 `Blob + importScripts(...)`,让 Worker 使用 `location.href` 来解析并加载对应的 worker chunk。
156
+
157
+ ```js
158
+ const fs = require('fs');
159
+ const path = require('path');
160
+
161
+ const distDir = process.argv[2]
162
+ ? path.resolve(process.cwd(), process.argv[2])
163
+ : path.resolve(process.cwd(), 'dist');
164
+
165
+ function replaceWorkerUrls(filePath) {
166
+ const data = fs.readFileSync(filePath, 'utf8');
167
+ const workerPattern =
168
+ /new Worker\(""\+new URL\(""\+new URL\("([^"]+\.js)",import\.meta\.url\)\.href,self\.location\)\.href\)/g;
169
+
170
+ const newData = data.replace(workerPattern, (match, workerPath) => {
171
+ return `new Worker(URL.createObjectURL(new Blob([\`importScripts("\${new URL("${workerPath}",location.href).href}")\`],{type:"text/javascript"})))`;
172
+ });
173
+
174
+ if (newData !== data) fs.writeFileSync(filePath, newData, 'utf8');
175
+ }
176
+
177
+ function traverseDirectory(dir) {
178
+ const files = fs.readdirSync(dir);
179
+ for (const file of files) {
180
+ const filePath = path.join(dir, file);
181
+ const stat = fs.statSync(filePath);
182
+ if (stat.isDirectory()) traverseDirectory(filePath);
183
+ else if (file.endsWith('.js')) replaceWorkerUrls(filePath);
184
+ }
185
+ }
186
+
187
+ traverseDirectory(distDir);
188
+ ```
189
+
190
+ 构建命令示例:
191
+
192
+ ```json
193
+ {
194
+ "scripts": {
195
+ "build": "vite build && node ./scripts/patch-workers.cjs dist"
196
+ }
197
+ }
198
+ ```
199
+
130
200
  ## 📄 License
131
201
 
132
202
  [MIT](./LICENSE)
@@ -1,4 +1,4 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const f=require("./index-CBTI4pNS.cjs");/*!-----------------------------------------------------------------------------
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const f=require("./index-CeMlLD5z.cjs");/*!-----------------------------------------------------------------------------
2
2
  * Copyright (c) Microsoft Corporation. All rights reserved.
3
3
  * Version: 0.41.0(38e1e3d097f84e336c311d071a9ffb5191d4ffd1)
4
4
  * Released under the MIT license
@@ -1,4 +1,4 @@
1
- import { m as f } from "./index-B77o5cnV.js";
1
+ import { m as f } from "./index-BfxRh_1b.js";
2
2
  /*!-----------------------------------------------------------------------------
3
3
  * Copyright (c) Microsoft Corporation. All rights reserved.
4
4
  * Version: 0.41.0(38e1e3d097f84e336c311d071a9ffb5191d4ffd1)
@@ -1,4 +1,4 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("./index-CBTI4pNS.cjs");/*!-----------------------------------------------------------------------------
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("./index-CeMlLD5z.cjs");/*!-----------------------------------------------------------------------------
2
2
  * Copyright (c) Microsoft Corporation. All rights reserved.
3
3
  * Version: 0.41.0(38e1e3d097f84e336c311d071a9ffb5191d4ffd1)
4
4
  * Released under the MIT license
@@ -1,4 +1,4 @@
1
- import { m as l } from "./index-B77o5cnV.js";
1
+ import { m as l } from "./index-BfxRh_1b.js";
2
2
  /*!-----------------------------------------------------------------------------
3
3
  * Copyright (c) Microsoft Corporation. All rights reserved.
4
4
  * Version: 0.41.0(38e1e3d097f84e336c311d071a9ffb5191d4ffd1)
@@ -1,4 +1,4 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("./index-CBTI4pNS.cjs");/*!-----------------------------------------------------------------------------
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("./index-CeMlLD5z.cjs");/*!-----------------------------------------------------------------------------
2
2
  * Copyright (c) Microsoft Corporation. All rights reserved.
3
3
  * Version: 0.41.0(38e1e3d097f84e336c311d071a9ffb5191d4ffd1)
4
4
  * Released under the MIT license
@@ -1,4 +1,4 @@
1
- import { m as s } from "./index-B77o5cnV.js";
1
+ import { m as s } from "./index-BfxRh_1b.js";
2
2
  /*!-----------------------------------------------------------------------------
3
3
  * Copyright (c) Microsoft Corporation. All rights reserved.
4
4
  * Version: 0.41.0(38e1e3d097f84e336c311d071a9ffb5191d4ffd1)