@tntd/monaco-editor 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 ADDED
@@ -0,0 +1,215 @@
1
+ # @tntd/monaco-editor
2
+
3
+ 基于 Monaco Editor 封装的代码编辑器组件,提供了丰富的编辑功能和自定义主题支持。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ npm install @tntd/monaco-editor --save
9
+ ```
10
+
11
+ ## 特性
12
+
13
+ - 🎨 支持自定义主题
14
+ - 🌍 支持国际化(中文/英文)
15
+ - 🔍 支持关键字高亮
16
+ - 📝 支持公式编辑模式
17
+ - 🔄 支持 Diff 对比模式
18
+ - 🔌 插件化架构
19
+
20
+ ## 基础使用
21
+
22
+ ```jsx
23
+ import MonacoEditor from '@tntd/monaco-editor';
24
+
25
+ const App = () => {
26
+ return (
27
+ <MonacoEditor
28
+ language="javascript"
29
+ defaultValue={code}
30
+ onChange={(value) => console.log(value)}
31
+ />
32
+ );
33
+ };
34
+ ```
35
+
36
+ ## API
37
+
38
+ ### 基础配置项
39
+
40
+ | 参数 | 说明 | 类型 | 默认值 |
41
+ | --- | --- | --- | --- |
42
+ | language | 编辑器语言 | string | 'javascript' |
43
+ | value | 编辑器内容 | string | - |
44
+ | onChange | 内容变化回调函数 | (value: string) => void | - |
45
+ | path | Monaco Editor 资源文件路径 | string | '/vs' |
46
+ | theme | 主题 | 'vs-dark' \| 'tntd-vs-dark' \| string | 'vs-dark' |
47
+ | options | 编辑器配置项 | object | defaultOptions |
48
+ | isDiff | 是否启用对比模式 | boolean | false |
49
+ | isFormula | 是否启用公式编辑模式 | boolean | false |
50
+ | keywords | 需要高亮的关键字列表 | string[] | [] |
51
+
52
+ ### 编辑器事件
53
+
54
+ | 事件名 | 说明 | 参数 |
55
+ | --- | --- | --- |
56
+ | editorWillMount | 编辑器挂载前的回调 | (monaco: any) => object |
57
+ | editorDidMount | 编辑器挂载后的回调 | ({ monaco, editor }) => void |
58
+ | editorWillUnmount | 编辑器卸载前的回调 | (editor: any) => void |
59
+
60
+ ### 默认编辑器配置
61
+
62
+ ```javascript
63
+ const defaultOptions = {
64
+ selectOnLineNumbers: true,
65
+ fontSize: 14,
66
+ lineHeight: 24,
67
+ tabSize: 2,
68
+ minimap: { enabled: false },
69
+ automaticLayout: true,
70
+ scrollBeyondLastLine: false,
71
+ // ... 更多配置项见代码
72
+ }
73
+ ```
74
+
75
+ ### 主题配置
76
+
77
+ 组件内置了 `tntd-vs-dark` 主题,提供了以下默认颜色配置:
78
+
79
+ ```javascript
80
+ const defaultThemeColor = {
81
+ 'editor.background': '#17233D',
82
+ 'editor.foreground': '#BABDC5',
83
+ 'editorGutter.background': '#212c45',
84
+ 'editorLineNumber.foreground': '#BABDC5',
85
+ 'editorCursor.foreground': '#ff9800',
86
+ 'editor.lineHighlightBackground': '#002240',
87
+ // ... 更多颜色配置见代码
88
+ }
89
+ ```
90
+
91
+ ### 实例方法
92
+
93
+ | 方法名 | 说明 | 参数 |
94
+ | --- | --- | --- |
95
+ | getValue | 获取编辑器内容 | () => string |
96
+ | setValue | 设置编辑器内容 | (value: string) => void |
97
+ | getPosition | 获取光标位置 | () => Position |
98
+ | setPosition | 设置光标位置 | (position: Position) => void |
99
+ | focus | 使编辑器获得焦点 | () => void |
100
+ | layout | 重新布局编辑器 | () => void |
101
+ | updateOptions | 更新编辑器配置 | (options: object) => void |
102
+ | insertText | 在光标位置插入文本 | (content: string) => void |
103
+
104
+ ### Diff 模式配置
105
+
106
+ 当 `isDiff={true}` 时,需要提供以下额外配置:
107
+
108
+ ```jsx
109
+ <MonacoEditor
110
+ isDiff={true}
111
+ original="原始代码"
112
+ modified="修改后的代码"
113
+ language="javascript"
114
+ />
115
+ ```
116
+
117
+ ### 公式编辑模式
118
+
119
+ 当 `isFormula={true}` 时,支持以下特性:
120
+
121
+ - 自定义语言高亮规则
122
+ - 特殊的主题配置
123
+ - 通过 `modeField` 配置公式编辑规则
124
+
125
+ ### 插件系统
126
+
127
+ 组件支持通过插件扩展功能:
128
+
129
+ ```javascript
130
+ editor.use({
131
+ name: 'myPlugin',
132
+ apply: (editor) => {
133
+ // 插件逻辑
134
+ return {
135
+ dispose: () => {
136
+ // 清理逻辑
137
+ }
138
+ }
139
+ }
140
+ });
141
+ ```
142
+
143
+ ## 注意事项
144
+
145
+ 1. 确保 Monaco Editor 资源文件路径配置正确
146
+ 2. 在生产环境中,建议使用 `CopyWebpackPlugin` 复制 Monaco Editor 资源文件
147
+ 3. 使用 Diff 模式时,需要同时提供 `original` 和 `modified` 属性
148
+ 4. 自定义主题时,建议继承 `vs-dark` 主题进行扩展
149
+
150
+ ## 示例
151
+
152
+ ### 基础编辑器
153
+
154
+ ```jsx
155
+ import MonacoEditor from '@tntd/monaco-editor';
156
+
157
+ const App = () => {
158
+ const handleEditorDidMount = ({ monaco, editor }) => {
159
+ // 编辑器初始化完成后的操作
160
+ };
161
+
162
+ return (
163
+ <MonacoEditor
164
+ language="javascript"
165
+ value={code}
166
+ onChange={handleChange}
167
+ editorDidMount={handleEditorDidMount}
168
+ options={{
169
+ fontSize: 16,
170
+ minimap: { enabled: true }
171
+ }}
172
+ />
173
+ );
174
+ };
175
+ ```
176
+
177
+ ### 对比模式
178
+
179
+ ```jsx
180
+ import {DiffEditor} from '@tntd/monaco-editor';
181
+
182
+ const App = () => {
183
+ return (
184
+ <DiffEditor
185
+ isDiff={true}
186
+ original={originalCode}
187
+ modified={modifiedCode}
188
+ language="javascript"
189
+ />
190
+ );
191
+ };
192
+ ```
193
+
194
+ ### 公式编辑模式
195
+
196
+ ```jsx
197
+ import {FormulaEditor} from '@tntd/monaco-editor';
198
+
199
+ const App = () => {
200
+ return (
201
+ <FormulaEditor
202
+ fieldList={fieldList || []} // @唤起
203
+ methodList={methodList || []} // #唤起
204
+ readOnly={readOnly}
205
+ cnCodeToEnExtraLogic={(item) => {
206
+ if (item.type) {
207
+ return `α${item.type}`;
208
+ }
209
+ }}
210
+ enCodeToCnExtraLogic={()=>{}}
211
+ cnCodeToEnUniqueLogic={({ group2, group1, list, turnStr }) => {}}
212
+ />
213
+ );
214
+ };
215
+ ```