stream-monaco 0.0.2 → 0.0.3
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/README.md +114 -0
- package/README.zh-CN.md +75 -0
- package/dist/index.cjs +403 -28
- package/dist/index.d.cts +6 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +403 -28
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -38,6 +38,46 @@ Note: If you only use Monaco and pass all `themes` to `createEditor`, typically
|
|
|
38
38
|
|
|
39
39
|
Config: `useMonaco()` does not auto-sync an external Shiki highlighter; if you need external Shiki snippets to follow theme changes, call `getOrCreateHighlighter(...)` and `highlighter.setTheme(...)` yourself.
|
|
40
40
|
|
|
41
|
+
### API Reference
|
|
42
|
+
|
|
43
|
+
#### useMonaco() Returns
|
|
44
|
+
|
|
45
|
+
The `useMonaco()` function returns an object with the following methods:
|
|
46
|
+
|
|
47
|
+
##### Editor Management
|
|
48
|
+
- **`createEditor(container, code, language)`** - Create and mount Monaco editor to a container
|
|
49
|
+
- **`createDiffEditor(container, originalCode, modifiedCode, language)`** - Create and mount Diff editor
|
|
50
|
+
- **`cleanupEditor()`** - Destroy editor and cleanup resources
|
|
51
|
+
- **`getEditorView()`** - Get current editor instance (IStandaloneCodeEditor | null)
|
|
52
|
+
- **`getDiffEditorView()`** - Get current Diff editor instance (IStandaloneDiffEditor | null)
|
|
53
|
+
- **`getEditor()`** - Get Monaco's static editor object for calling static methods
|
|
54
|
+
|
|
55
|
+
##### Code Operations
|
|
56
|
+
- **`updateCode(newCode, language)`** - Update editor content and language (incremental update when possible)
|
|
57
|
+
- **`appendCode(appendText, language?)`** - Append text to the end of editor (optimized for streaming)
|
|
58
|
+
- **`getCode()`** - **Get current code from editor**
|
|
59
|
+
- Returns `string` for normal editor
|
|
60
|
+
- Returns `{ original: string, modified: string }` for diff editor
|
|
61
|
+
- Returns `null` if no editor exists
|
|
62
|
+
- **Use case**: Get the latest code after user manually edits the editor or after programmatic updates
|
|
63
|
+
|
|
64
|
+
##### Diff Editor Operations
|
|
65
|
+
- **`updateDiff(originalCode, modifiedCode, language?)`** - Update both sides of diff editor
|
|
66
|
+
- **`updateOriginal(newCode, language?)`** - Update only the original side
|
|
67
|
+
- **`updateModified(newCode, language?)`** - Update only the modified side
|
|
68
|
+
- **`appendOriginal(appendText, language?)`** - Append to original side (streaming)
|
|
69
|
+
- **`appendModified(appendText, language?)`** - Append to modified side (streaming)
|
|
70
|
+
- **`getDiffModels()`** - Get both diff models: `{ original, modified }`
|
|
71
|
+
|
|
72
|
+
##### Theme & Language
|
|
73
|
+
- **`setTheme(theme)`** - Switch editor theme (returns Promise)
|
|
74
|
+
- **`setLanguage(language)`** - Switch editor language
|
|
75
|
+
- **`getCurrentTheme()`** - Get current theme name
|
|
76
|
+
|
|
77
|
+
##### Performance Control
|
|
78
|
+
- **`setUpdateThrottleMs(ms)`** - Change update throttle at runtime
|
|
79
|
+
- **`getUpdateThrottleMs()`** - Get current throttle value
|
|
80
|
+
|
|
41
81
|
### Install
|
|
42
82
|
|
|
43
83
|
```bash
|
|
@@ -140,6 +180,7 @@ const {
|
|
|
140
180
|
getCurrentTheme,
|
|
141
181
|
getEditor,
|
|
142
182
|
getEditorView,
|
|
183
|
+
getCode,
|
|
143
184
|
cleanupEditor,
|
|
144
185
|
} = useMonaco({
|
|
145
186
|
themes: ['github-dark', 'github-light'],
|
|
@@ -195,6 +236,16 @@ console.log('Monaco editor API:', monacoEditor)
|
|
|
195
236
|
|
|
196
237
|
const editorInstance = getEditorView()
|
|
197
238
|
console.log('Editor instance:', editorInstance)
|
|
239
|
+
|
|
240
|
+
// Get current code from editor (useful after user manually edits)
|
|
241
|
+
function getCurrentCode() {
|
|
242
|
+
const code = getCode()
|
|
243
|
+
if (code) {
|
|
244
|
+
console.log('Current code:', code)
|
|
245
|
+
return code
|
|
246
|
+
}
|
|
247
|
+
return null
|
|
248
|
+
}
|
|
198
249
|
</script>
|
|
199
250
|
|
|
200
251
|
<template>
|
|
@@ -218,6 +269,69 @@ console.log('Editor instance:', editorInstance)
|
|
|
218
269
|
</template>
|
|
219
270
|
```
|
|
220
271
|
|
|
272
|
+
### Get current code (getCode)
|
|
273
|
+
|
|
274
|
+
After creating an editor, you can retrieve the current code content at any time using `getCode()`. This is especially useful when users manually edit the editor content:
|
|
275
|
+
|
|
276
|
+
```vue
|
|
277
|
+
<script setup lang="ts">
|
|
278
|
+
import { onMounted, ref } from 'vue'
|
|
279
|
+
import { useMonaco } from 'stream-monaco'
|
|
280
|
+
|
|
281
|
+
const container = ref<HTMLElement>()
|
|
282
|
+
|
|
283
|
+
const { createEditor, updateCode, getCode, cleanupEditor } = useMonaco({
|
|
284
|
+
themes: ['vitesse-dark', 'vitesse-light'],
|
|
285
|
+
languages: ['javascript', 'typescript'],
|
|
286
|
+
})
|
|
287
|
+
|
|
288
|
+
onMounted(async () => {
|
|
289
|
+
if (container.value) {
|
|
290
|
+
await createEditor(container.value, 'console.log("hello")', 'javascript')
|
|
291
|
+
}
|
|
292
|
+
})
|
|
293
|
+
|
|
294
|
+
// Get current code after updates or user edits
|
|
295
|
+
function handleSubmit() {
|
|
296
|
+
const currentCode = getCode()
|
|
297
|
+
if (currentCode) {
|
|
298
|
+
console.log('Submitting code:', currentCode)
|
|
299
|
+
// Send to API, save to storage, etc.
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// Update code programmatically
|
|
304
|
+
function replaceCode() {
|
|
305
|
+
updateCode('console.log("world")', 'javascript')
|
|
306
|
+
|
|
307
|
+
// Get the new code
|
|
308
|
+
setTimeout(() => {
|
|
309
|
+
const newCode = getCode()
|
|
310
|
+
console.log('Updated code:', newCode)
|
|
311
|
+
}, 100)
|
|
312
|
+
}
|
|
313
|
+
</script>
|
|
314
|
+
|
|
315
|
+
<template>
|
|
316
|
+
<div>
|
|
317
|
+
<div ref="container" class="editor" />
|
|
318
|
+
<button @click="handleSubmit">Submit Code</button>
|
|
319
|
+
<button @click="replaceCode">Replace Code</button>
|
|
320
|
+
</div>
|
|
321
|
+
</template>
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
For Diff editors, `getCode()` returns both sides:
|
|
325
|
+
|
|
326
|
+
```ts
|
|
327
|
+
const { createDiffEditor, getCode } = useMonaco()
|
|
328
|
+
|
|
329
|
+
await createDiffEditor(container, 'old code', 'new code', 'javascript')
|
|
330
|
+
|
|
331
|
+
const codes = getCode()
|
|
332
|
+
// codes = { original: 'old code', modified: 'new code' }
|
|
333
|
+
```
|
|
334
|
+
|
|
221
335
|
### Diff editor quick start (Vue)
|
|
222
336
|
|
|
223
337
|
```vue
|
package/README.zh-CN.md
CHANGED
|
@@ -119,6 +119,7 @@ const {
|
|
|
119
119
|
getCurrentTheme,
|
|
120
120
|
getEditor,
|
|
121
121
|
getEditorView,
|
|
122
|
+
getCode,
|
|
122
123
|
cleanupEditor,
|
|
123
124
|
} = useMonaco({
|
|
124
125
|
// 主题配置 - 至少需要两个主题(暗色/亮色)
|
|
@@ -203,6 +204,16 @@ console.log('Monaco editor API:', monacoEditor)
|
|
|
203
204
|
// 获取编辑器实例
|
|
204
205
|
const editorInstance = getEditorView()
|
|
205
206
|
console.log('Editor instance:', editorInstance)
|
|
207
|
+
|
|
208
|
+
// 获取编辑器当前代码(在用户手动编辑后非常有用)
|
|
209
|
+
function getCurrentCode() {
|
|
210
|
+
const code = getCode()
|
|
211
|
+
if (code) {
|
|
212
|
+
console.log('当前代码:', code)
|
|
213
|
+
return code
|
|
214
|
+
}
|
|
215
|
+
return null
|
|
216
|
+
}
|
|
206
217
|
</script>
|
|
207
218
|
|
|
208
219
|
<template>
|
|
@@ -280,6 +291,69 @@ export function MonacoEditor() {
|
|
|
280
291
|
|
|
281
292
|
说明:Svelte/Solid/Preact 的集成方式与 React 类似——在挂载时创建编辑器实例,卸载时清理即可。
|
|
282
293
|
|
|
294
|
+
### 获取当前代码(getCode)
|
|
295
|
+
|
|
296
|
+
创建编辑器后,您可以随时使用 `getCode()` 获取当前的代码内容。这在用户手动编辑编辑器内容时特别有用:
|
|
297
|
+
|
|
298
|
+
```vue
|
|
299
|
+
<script setup lang="ts">
|
|
300
|
+
import { onMounted, ref } from 'vue'
|
|
301
|
+
import { useMonaco } from 'stream-monaco'
|
|
302
|
+
|
|
303
|
+
const container = ref<HTMLElement>()
|
|
304
|
+
|
|
305
|
+
const { createEditor, updateCode, getCode, cleanupEditor } = useMonaco({
|
|
306
|
+
themes: ['vitesse-dark', 'vitesse-light'],
|
|
307
|
+
languages: ['javascript', 'typescript'],
|
|
308
|
+
})
|
|
309
|
+
|
|
310
|
+
onMounted(async () => {
|
|
311
|
+
if (container.value) {
|
|
312
|
+
await createEditor(container.value, 'console.log("hello")', 'javascript')
|
|
313
|
+
}
|
|
314
|
+
})
|
|
315
|
+
|
|
316
|
+
// 在更新或用户编辑后获取当前代码
|
|
317
|
+
function handleSubmit() {
|
|
318
|
+
const currentCode = getCode()
|
|
319
|
+
if (currentCode) {
|
|
320
|
+
console.log('提交代码:', currentCode)
|
|
321
|
+
// 发送到 API、保存到存储等
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// 程序化更新代码
|
|
326
|
+
function replaceCode() {
|
|
327
|
+
updateCode('console.log("world")', 'javascript')
|
|
328
|
+
|
|
329
|
+
// 获取新代码
|
|
330
|
+
setTimeout(() => {
|
|
331
|
+
const newCode = getCode()
|
|
332
|
+
console.log('更新后的代码:', newCode)
|
|
333
|
+
}, 100)
|
|
334
|
+
}
|
|
335
|
+
</script>
|
|
336
|
+
|
|
337
|
+
<template>
|
|
338
|
+
<div>
|
|
339
|
+
<div ref="container" class="editor" />
|
|
340
|
+
<button @click="handleSubmit">提交代码</button>
|
|
341
|
+
<button @click="replaceCode">替换代码</button>
|
|
342
|
+
</div>
|
|
343
|
+
</template>
|
|
344
|
+
```
|
|
345
|
+
|
|
346
|
+
对于 Diff 编辑器,`getCode()` 返回两侧的代码:
|
|
347
|
+
|
|
348
|
+
```ts
|
|
349
|
+
const { createDiffEditor, getCode } = useMonaco()
|
|
350
|
+
|
|
351
|
+
await createDiffEditor(container, '旧代码', '新代码', 'javascript')
|
|
352
|
+
|
|
353
|
+
const codes = getCode()
|
|
354
|
+
// codes = { original: '旧代码', modified: '新代码' }
|
|
355
|
+
```
|
|
356
|
+
|
|
283
357
|
### Diff 编辑器使用(Vue)
|
|
284
358
|
|
|
285
359
|
#### 快速开始
|
|
@@ -585,6 +659,7 @@ modified?.onDidChangeContent?.(() => { /* ... */ })
|
|
|
585
659
|
| `getEditor` | `() => typeof monaco.editor` | 获取 Monaco 的静态 editor 对象 |
|
|
586
660
|
| `getEditorView` | `() => MonacoEditor \| null` | 获取当前编辑器实例 |
|
|
587
661
|
| `getDiffEditorView` | `() => MonacoDiffEditor \| null` | 获取当前 Diff 编辑器实例 |
|
|
662
|
+
| `getCode` | `() => string \| { original: string, modified: string } \| null` | **获取编辑器当前代码**<br>- 普通编辑器返回 `string`<br>- Diff 编辑器返回 `{ original, modified }`<br>- 无编辑器返回 `null`<br>**用途**:获取用户手动编辑后的最新代码或程序更新后的内容 |
|
|
588
663
|
| `appendOriginal` | `(appendText: string, codeLanguage?: string) => void` | 在 original 末尾追加(显式流式) |
|
|
589
664
|
| `appendModified` | `(appendText: string, codeLanguage?: string) => void` | 在 modified 末尾追加(显式流式) |
|
|
590
665
|
|