|
| 1 | +# 多文件编辑器 |
| 2 | + |
| 3 | +借助此插件可在低码引擎上使用任意层级的文件树进行代码的组织 |
| 4 | + |
| 5 | +## Demo |
| 6 | + |
| 7 | +```ts |
| 8 | +import multipleFileCodeEditorFactory from '@alipay/lowcode-plugin-multiple-editor'; |
| 9 | +import { PrettierPlugin } from '@alilc/lowcode-plugin-multiple-editor/es/plugins/prettier-plugin'; |
| 10 | + |
| 11 | + |
| 12 | +const PLUGIN_NAME = 'multiple-file-code-editor'; |
| 13 | +// 详细参数可见 ts 类型声明 |
| 14 | +const plugin: any = multipleFileCodeEditorFactory({ |
| 15 | + softSave: true, // true 保存代码时将代码保存在插件类上,并不会调用引擎的 importSchema |
| 16 | + es6: true, // 编译选项,true 时仅做 es module 的编译 |
| 17 | + // 可选择使用插进进行代码编辑器功能的拓展 |
| 18 | + plugins: [searchPlugin as any, lintPlugin, new PrettierPlugin()], |
| 19 | + // 内置钩子,在插件初始化时进行一些操作,例如添加类型声明 |
| 20 | + onInstall(controller: EditorController) { |
| 21 | + for (const [path, content] of extraLibList) { |
| 22 | + controller.addExtraLib(content, path); |
| 23 | + } |
| 24 | + controller.addComponentDeclarations(declarations); |
| 25 | + controller.onImportSchema(async (schema) => { |
| 26 | + // Todo sth. |
| 27 | + }); |
| 28 | + window.codeEditorController = controller; |
| 29 | + }, |
| 30 | + defaultFiles: { |
| 31 | + utils: ` |
| 32 | +export function helloWorld() { |
| 33 | + console.log('hello world'); |
| 34 | +} |
| 35 | + ` |
| 36 | + } |
| 37 | +}); |
| 38 | + |
| 39 | +plugin.pluginName = PLUGIN_NAME; |
| 40 | + |
| 41 | +await plugins.register(plugin); |
| 42 | +``` |
| 43 | + |
| 44 | +## 工作原理 |
| 45 | + |
| 46 | +使用该插件时,每次保存时: |
| 47 | + |
| 48 | +1. 编译代码,生成 __initExtra 函数方法,该方法用于初始化各个方法、生命周期的真实定义 |
| 49 | +1. 修改 constructor 函数体,函数体内执行 __initExtra,constructor 将会在渲染引擎内执行,执行完成后即可得到所有方法和生命周期的真实定义 |
| 50 | +1. 在 schema 上保存一个文件 Map,保存位置为 `schema.componentsTree[0]._sourceCodeMap` |
| 51 | + |
| 52 | +## 使用注意事项 |
| 53 | + |
| 54 | +1. 建议所有 project.importSchema 和 project.exportSchema 替换为 codeEditorController.importSchema 和 codeEditorController.exportSchema。原因是插件内部需要对文件内容进行处理 |
| 55 | +2. 不能在 index.js 的 Class 之外进行函数、变量定义,如需定义则在其他文件内定义 |
| 56 | +3. 不能在 index.js Class 的 state 定义中使用表达式 |
| 57 | +4. 如需在 setter 内适用类型定义,请开启 base-editor 的单例模式,仅需在应用入口处调用如下方法即可: |
| 58 | +5. 如果低码项目有使用出码,则需对出码进行定制,将 _sourceCodeMap 中的文件生成到项目中,并对文件的引用进行处理,具体处理方式可自行组织 |
| 59 | + |
| 60 | +```ts |
| 61 | +import { configure } from '@alilc/lowcode-plugin-base-monaco-editor'; |
| 62 | +configure({ singleton: true }); |
| 63 | +``` |
0 commit comments