@fast-crud/editor-code 1.25.13

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 (39) hide show
  1. package/.eslintrc.js +79 -0
  2. package/.prettierrc +5 -0
  3. package/CHANGELOG.md +945 -0
  4. package/dist/d/editor/components/fs-editor-code/validators.d.ts +10 -0
  5. package/dist/d/editor/components/fs-editor-code/worker/index.d.ts +9 -0
  6. package/dist/d/editor/components/fs-editor-code/worker/yaml.worker.d.ts +1 -0
  7. package/dist/d/editor/components/fs-editor-code/workers.d.ts +9 -0
  8. package/dist/d/editor/exports.d.ts +3 -0
  9. package/dist/d/editor/index.d.ts +4 -0
  10. package/dist/d/editor/index.umd.d.ts +4 -0
  11. package/dist/d/editor/type/index.d.ts +4 -0
  12. package/dist/d/editor/type/types.d.ts +12 -0
  13. package/dist/d/index.d.ts +1 -0
  14. package/dist/d/index.umd.d.ts +1 -0
  15. package/dist/editor-code.mjs +9 -0
  16. package/dist/editor-code.mjs.map +1 -0
  17. package/dist/editor-code.umd.js +2 -0
  18. package/dist/editor-code.umd.js.map +1 -0
  19. package/dist/index-4cfdb04c.mjs +132 -0
  20. package/dist/index-4cfdb04c.mjs.map +1 -0
  21. package/dist/index-c2ddaf47.mjs +97 -0
  22. package/dist/index-c2ddaf47.mjs.map +1 -0
  23. package/dist/style.css +1 -0
  24. package/index.d.ts +1 -0
  25. package/index.js +1 -0
  26. package/package.json +84 -0
  27. package/src/editor/components/fs-editor-code/async-import.ts +19 -0
  28. package/src/editor/components/fs-editor-code/index.vue +240 -0
  29. package/src/editor/components/fs-editor-code/validators.ts +37 -0
  30. package/src/editor/components/fs-editor-code/worker/index.ts +64 -0
  31. package/src/editor/components/fs-editor-code/worker/yaml.worker.ts +1 -0
  32. package/src/editor/components/fs-editor-code/workers.ts +45 -0
  33. package/src/editor/exports.ts +3 -0
  34. package/src/editor/index.umd.ts +17 -0
  35. package/src/editor/type/types.ts +7 -0
  36. package/src/index.umd.ts +1 -0
  37. package/src/style.css +1 -0
  38. package/tsconfig.json +49 -0
  39. package/vite.config.js +125 -0
@@ -0,0 +1,240 @@
1
+ <template>
2
+ <div ref="monacoRef" class="fs-editor-code"></div>
3
+ </template>
4
+
5
+ <script lang="ts" setup>
6
+ import * as monaco from "monaco-editor";
7
+ // import * as monaco from "monaco-editor/esm/vs/editor/editor.api";
8
+ import { onMounted, onUnmounted, ref, watch } from "vue";
9
+ import { cloneDeep, debounce as lodashDebounce } from "lodash-es";
10
+ import {
11
+ importJavascriptContribution,
12
+ importJsonContribution,
13
+ importMonacoYaml,
14
+ importYamlContribution
15
+ } from "./async-import";
16
+ import { initWorkers } from "./workers";
17
+ /**
18
+ * config:
19
+ * value: '', // 编辑器初始文本
20
+ * language: 'javascript', // 语言
21
+ * theme: 'vs', // 主题
22
+ * readOnly: false, // 是否只读
23
+ * minimap: { enabled: false }, // 是否启用小地图
24
+ * fontSize: 14, // 字体大小
25
+ * tabSize: 2, // tab缩进长度
26
+ * automaticLayout: true, // 自动布局
27
+ * lineNumbers: 'off', // 是否启用行号
28
+ * contextmenu: true, // 是否启用上下文菜单
29
+ * folding: true, // 是否启用代码折叠
30
+ * foldingStrategy: 'auto', // 代码折叠策略
31
+ * wordWrap: 'on', // 自动换行设置
32
+ * wrappingIndent: 'indent', // 换行缩进
33
+ * formatOnPaste: true, // 粘贴时是否自动格式化
34
+ * formatOnType: true, // 输入时是否自动格式化
35
+ * dragAndDrop: true, // 是否允许拖放
36
+ * cursorStyle: 'line', // 光标样式
37
+ * cursorBlinking: 'blink', // 光标闪烁方式
38
+ * scrollbar: {
39
+ * vertical: 'auto', // 垂直滚动条的显示方式
40
+ * horizontal: 'auto', // 水平滚动条的显示方式
41
+ * verticalScrollbarSize: 2, // 垂直滚动条的宽
42
+ * horizontalScrollbarSize: 2, // 水平滚动条的高度
43
+ * }
44
+ */
45
+ const props = defineProps<{
46
+ language?: string;
47
+ modelValue?: string;
48
+ config?: any;
49
+ schema?: any;
50
+ debounce?: number;
51
+ init?: any;
52
+ readonly?: boolean;
53
+ disabled?: boolean;
54
+ id?: string;
55
+ }>();
56
+
57
+ export type EditorCodeCtx = {
58
+ // monaco对象
59
+ monaco: any;
60
+ //语言
61
+ language: string;
62
+ //配置
63
+ config: any;
64
+ //editor实例对象
65
+ instance?: any;
66
+
67
+ schema?: any;
68
+
69
+ initialValue?: any;
70
+ };
71
+ initWorkers();
72
+ const monacoRef = ref();
73
+
74
+ let instanceRef: any = null;
75
+
76
+ function disposeEditor() {
77
+ if (instanceRef) {
78
+ instanceRef.dispose(); //使用完成销毁实例
79
+ }
80
+ }
81
+
82
+ onUnmounted(() => {
83
+ disposeEditor();
84
+ });
85
+
86
+ const emits = defineEmits(["update:modelValue", "change", "ready"]);
87
+
88
+ const emitValue = lodashDebounce((value: any) => {
89
+ emits("update:modelValue", value);
90
+ }, props.debounce || 500);
91
+
92
+ async function createEditor(ctx: EditorCodeCtx) {
93
+ disposeEditor();
94
+ const instance = monaco.editor.create(monacoRef.value, {
95
+ automaticLayout: true,
96
+ value: props.modelValue,
97
+ language: ctx.language,
98
+ theme: "vs-dark",
99
+ minimap: { enabled: false },
100
+ readOnly: props.readonly || props.disabled,
101
+ hover: {
102
+ enabled: true
103
+ },
104
+ ...ctx.config
105
+ });
106
+
107
+ // @event `change`
108
+ instance.onDidChangeModelContent((event) => {
109
+ const value = instance.getValue();
110
+ if (props.modelValue !== value) {
111
+ emits("change", value);
112
+ emitValue(value);
113
+ }
114
+ });
115
+
116
+ instanceRef = instance;
117
+ ctx.instance = instance;
118
+ emits("ready", ctx);
119
+ return instance;
120
+ }
121
+
122
+ async function initJavascript(ctx: EditorCodeCtx) {
123
+ await importJavascriptContribution();
124
+ monaco.languages.register({ id: "javascript" });
125
+ }
126
+
127
+ async function initJson(ctx: EditorCodeCtx) {
128
+ await importJsonContribution();
129
+ monaco.languages.register({ id: "json" });
130
+
131
+ const schemas = [];
132
+ if (ctx.schema) {
133
+ schemas.push({
134
+ uri: "http://myserver/foo-schema.json", // id of the first schema
135
+ fileMatch: ["*"], // associate with our model
136
+ schema: {
137
+ ...ctx.schema
138
+ }
139
+ });
140
+ }
141
+
142
+ monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
143
+ validate: true,
144
+ enableSchemaRequest: false,
145
+ schemas
146
+ });
147
+ }
148
+
149
+ async function initYaml(ctx: EditorCodeCtx) {
150
+ await importYamlContribution();
151
+ const { configureMonacoYaml } = await importMonacoYaml();
152
+ monaco.languages.register({ id: "yaml" });
153
+ let id = `fs-editor-code-yaml-${props.id || ""}.yaml`;
154
+ const schemas = [];
155
+ if (ctx.schema) {
156
+ schemas.push({
157
+ fileMatch: ["*"], // associate with our model
158
+ schema: {
159
+ ...ctx.schema
160
+ },
161
+ uri: `http://myserver/${id}`
162
+ });
163
+ }
164
+ configureMonacoYaml(monaco, {
165
+ schemas,
166
+ format: true,
167
+ hover: true,
168
+ completion: true,
169
+ validate: true,
170
+ isKubernetes: false,
171
+ enableSchemaRequest: false
172
+ });
173
+
174
+ // const uri = monaco.Uri.parse(id);
175
+ // const oldModel = monaco.editor.getModel(uri);
176
+ // if (oldModel) {
177
+ // oldModel.dispose();
178
+ // }
179
+ // ctx.config.model = monaco.editor.createModel(props.modelValue, null, uri);
180
+ }
181
+
182
+ async function doInit() {
183
+ disposeEditor();
184
+ const ctx: EditorCodeCtx = {
185
+ monaco,
186
+ language: props.language || "javascript",
187
+ config: cloneDeep(props.config || {}),
188
+ initialValue: props.modelValue || "",
189
+ schema: props.schema
190
+ };
191
+ if (ctx.language === "javascript") {
192
+ await initJavascript(ctx);
193
+ } else if (ctx.language === "yaml") {
194
+ await initYaml(ctx);
195
+ } else if (ctx.language === "json") {
196
+ await initJson(ctx);
197
+ }
198
+
199
+ await createEditor(ctx);
200
+ }
201
+
202
+ onMounted(async () => {
203
+ await doInit();
204
+ watch(
205
+ () => {
206
+ return {
207
+ language: props.language,
208
+ config: props.config
209
+ };
210
+ },
211
+ (value: any) => {
212
+ doInit();
213
+ }
214
+ );
215
+ });
216
+
217
+ watch(
218
+ () => {
219
+ return props.modelValue;
220
+ },
221
+ (value: any) => {
222
+ if (instanceRef && instanceRef.getValue() !== value) {
223
+ instanceRef.setValue(value);
224
+ }
225
+ }
226
+ );
227
+ </script>
228
+
229
+ <style lang="less">
230
+ .fs-editor-code {
231
+ min-height: 100px;
232
+ width: 100%;
233
+ border: 1px solid #eee;
234
+ border-radius: 5px;
235
+ }
236
+
237
+ .monaco-editor .hover-content {
238
+ z-index: 10000 !important;
239
+ }
240
+ </style>
@@ -0,0 +1,37 @@
1
+ import { importJsYaml } from "./async-import";
2
+
3
+ const jsonRule = {
4
+ validator: async (rule: any, value: any) => {
5
+ //校验value json的有效性
6
+ if (value) {
7
+ try {
8
+ JSON.parse(value);
9
+ } catch (e: any) {
10
+ console.error(e);
11
+ throw new Error("json格式错误:" + e.message);
12
+ }
13
+ }
14
+ },
15
+ message: "json格式错误"
16
+ };
17
+
18
+ const yamlRule = {
19
+ validator: async (rule: any, value: any) => {
20
+ //校验value yaml的有效性
21
+ if (value) {
22
+ try {
23
+ const yaml = await importJsYaml();
24
+ yaml.load(value, { schema: yaml.JSON_SCHEMA });
25
+ } catch (e: any) {
26
+ console.error(e);
27
+ throw new Error("yaml格式错误:" + e.message);
28
+ }
29
+ }
30
+ },
31
+ message: "yaml格式错误"
32
+ };
33
+
34
+ export const FsEditorCodeValidators = {
35
+ jsonRule,
36
+ yamlRule
37
+ };
@@ -0,0 +1,64 @@
1
+ import editorWorker from "monaco-editor/esm/vs/editor/editor.worker?worker";
2
+ import jsonWorker from "monaco-editor/esm/vs/language/json/json.worker?worker";
3
+ import cssWorker from "monaco-editor/esm/vs/language/css/css.worker?worker";
4
+ import htmlWorker from "monaco-editor/esm/vs/language/html/html.worker?worker";
5
+ import yamlWorker from "./yaml.worker?worker";
6
+ import tsWorker from "monaco-editor/esm/vs/language/typescript/ts.worker?worker";
7
+ export type WorkerItem = {
8
+ worker: any;
9
+ match: (label: string) => boolean;
10
+ };
11
+ const WorkerBucket: WorkerItem[] = [];
12
+ /**
13
+ * 注册自定义worker
14
+ */
15
+ export function registerWorker(labels: string | string[], worker: any) {
16
+ WorkerBucket.push({
17
+ worker: worker,
18
+ match: (label: string) => {
19
+ if (typeof labels === "string") {
20
+ return labels === label;
21
+ }
22
+ for (const labelItem of labels) {
23
+ if (labelItem === "*") {
24
+ return true;
25
+ }
26
+ if (labelItem === label) {
27
+ return true;
28
+ }
29
+ }
30
+ return false;
31
+ }
32
+ });
33
+ }
34
+
35
+ export function initWorkers() {
36
+ if (window.MonacoEnvironment) {
37
+ return;
38
+ }
39
+
40
+ //@ts-ignore
41
+ window.MonacoEnvironment = {
42
+ //@ts-ignore
43
+ getWorker(_, label) {
44
+ for (const workerLoader of WorkerBucket) {
45
+ if (workerLoader.match(label)) {
46
+ return new workerLoader.worker();
47
+ }
48
+ }
49
+ if (label === "json") {
50
+ return new jsonWorker();
51
+ } else if (label === "css" || label === "scss" || label === "less") {
52
+ return new cssWorker();
53
+ } else if (label === "html" || label === "handlebars" || label === "razor") {
54
+ return new htmlWorker();
55
+ } else if (label === "typescript" || label === "javascript") {
56
+ return new tsWorker();
57
+ } else if (label === "yaml" || label === "yml") {
58
+ //@ts-ignore
59
+ return new yamlWorker();
60
+ }
61
+ return new editorWorker();
62
+ }
63
+ };
64
+ }
@@ -0,0 +1 @@
1
+ export * from "monaco-yaml/yaml.worker.js";
@@ -0,0 +1,45 @@
1
+ export type WorkerItem = {
2
+ worker: any;
3
+ match: (label: string) => boolean;
4
+ };
5
+ const WorkerBucket: WorkerItem[] = [];
6
+ /**
7
+ * 注册自定义worker
8
+ */
9
+ export function registerWorker(labels: string | string[], worker: any) {
10
+ WorkerBucket.push({
11
+ worker: worker,
12
+ match: (label: string) => {
13
+ if (typeof labels === "string") {
14
+ return labels === label;
15
+ }
16
+ for (const labelItem of labels) {
17
+ if (labelItem === "*") {
18
+ return true;
19
+ }
20
+ if (labelItem === label) {
21
+ return true;
22
+ }
23
+ }
24
+ return false;
25
+ }
26
+ });
27
+ }
28
+
29
+ export function initWorkers() {
30
+ if (window.MonacoEnvironment) {
31
+ return;
32
+ }
33
+
34
+ //@ts-ignore
35
+ window.MonacoEnvironment = {
36
+ //@ts-ignore
37
+ getWorker(_, label) {
38
+ for (const workerLoader of WorkerBucket) {
39
+ if (workerLoader.match(label)) {
40
+ return new workerLoader.worker();
41
+ }
42
+ }
43
+ }
44
+ };
45
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./type";
2
+ export * from "./components/fs-editor-code/validators";
3
+ export * from "./components/fs-editor-code/workers";
@@ -0,0 +1,17 @@
1
+ import FsExtendsModelType from "./type";
2
+ export * from "./exports";
3
+ import { utils } from "@fast-crud/fast-crud";
4
+ // @ts-ignore
5
+ const modules = import.meta.glob("./components/*/*.vue", { eager: true });
6
+ const FsExtendsComponents = {
7
+ install(app: any) {
8
+ utils.vite.installSyncComponents(app, modules, null, /^.*\/([^\/]+)\/.*.vue$/, null);
9
+ }
10
+ };
11
+
12
+ export const FsEditorCode = {
13
+ install(app: any, options: any) {
14
+ app.use(FsExtendsModelType, options);
15
+ app.use(FsExtendsComponents);
16
+ }
17
+ };
@@ -0,0 +1,7 @@
1
+ export default function () {
2
+ return {
3
+ "editor-code": {
4
+ form: { component: { name: "fs-editor-code" }, col: { span: 24 } }
5
+ }
6
+ };
7
+ }
@@ -0,0 +1 @@
1
+ export * from "./editor/index.umd";
package/src/style.css ADDED
@@ -0,0 +1 @@
1
+ /*占位,勿删*/
package/tsconfig.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "compilerOptions": {
3
+ "allowJs": true,
4
+ "noImplicitAny": false,
5
+ "target": "esnext",
6
+ "module": "esnext",
7
+ "strict": true,
8
+ "jsx": "preserve",
9
+ "jsxImportSource": "vue",
10
+ "verbatimModuleSyntax":false ,
11
+ "importHelpers": true,
12
+ "moduleResolution": "node",
13
+ "experimentalDecorators": true,
14
+ "skipLibCheck": true,
15
+ "esModuleInterop": true,
16
+ "allowSyntheticDefaultImports": true,
17
+ "strictNullChecks" :false,
18
+ "sourceMap": true,
19
+ "baseUrl": ".",
20
+ "outDir": "./dist/ts",
21
+ "types": [
22
+ "vite/client",
23
+ "mocha",
24
+ "chai",
25
+ "unplugin-vue-define-options/macros-global"
26
+ ],
27
+ "paths": {
28
+ "@/*": [
29
+ "src/*"
30
+ ]
31
+ },
32
+ "lib": [
33
+ "esnext",
34
+ "dom",
35
+ "dom.iterable",
36
+ "scripthost"
37
+ ]
38
+ },
39
+ "include": [
40
+ "src/**/*.ts",
41
+ "src/**/*.tsx",
42
+ "src/**/*.vue",
43
+ "tests/**/*.ts",
44
+ "tests/**/*.tsx"
45
+ ],
46
+ "exclude": [
47
+ "node_modules"
48
+ ]
49
+ }
package/vite.config.js ADDED
@@ -0,0 +1,125 @@
1
+ import vue from "@vitejs/plugin-vue";
2
+ import vueJsx from "@vitejs/plugin-vue-jsx";
3
+ import path from "path";
4
+ import { visualizer } from "rollup-plugin-visualizer";
5
+ import strip from "@rollup/plugin-strip";
6
+ import DefineOptions from "unplugin-vue-define-options/vite";
7
+ import typescript from "@rollup/plugin-typescript";
8
+ import _ from "lodash";
9
+ const { resolve } = path;
10
+ // https://vitejs.dev/config/
11
+ export default ({ command, mode }) => {
12
+ let build = {};
13
+ if (mode === "umd") {
14
+ build = {
15
+ build: {
16
+ emptyOutDir: true,
17
+ lib: {
18
+ entry: resolve(__dirname, "src/index.umd.ts"),
19
+ formats: ["umd"]
20
+ },
21
+ rollupOptions: {
22
+ output: {
23
+ manualChunks: null
24
+ }
25
+ }
26
+ }
27
+ };
28
+ }
29
+
30
+ const options = {
31
+ base: "./",
32
+ plugins: [DefineOptions(), vueJsx(), vue()],
33
+ esbuild: {
34
+ drop: command === "build" ? ["debugger"] : [],
35
+ jsxFactory: "h",
36
+ jsxFragment: "Fragment",
37
+ pure: ["console.log", "debugger"]
38
+ },
39
+ build: {
40
+ emptyOutDir: false,
41
+ lib: {
42
+ entry: resolve(__dirname, "src/index.ts"),
43
+ name: "FsExtends",
44
+ formats: ["es"]
45
+ },
46
+ // cssCodeSplit: true,
47
+ sourcemap: true,
48
+ // minify: false,
49
+ rollupOptions: {
50
+ plugins: [
51
+ strip(),
52
+ visualizer(),
53
+ typescript({
54
+ target: "es2020",
55
+ rootDir: "src",
56
+ declaration: true,
57
+ declarationDir: "dist/d",
58
+ exclude: "./node_modules/**",
59
+ allowSyntheticDefaultImports: true
60
+ })
61
+ ],
62
+ // make sure to externalize deps that shouldn't be bundled
63
+ // into your library
64
+ external: [
65
+ "vue",
66
+ "lodash-es",
67
+ "dayjs",
68
+ "vue-i18n",
69
+ "vuedraggable",
70
+ "ali-oss",
71
+ "cos-js-sdk-v5",
72
+ "axios",
73
+ "qiniu-js",
74
+ "@fast-crud/fast-crud",
75
+ "@fast-crud/ui-interface",
76
+ "monaco-editor",
77
+ "monaco-yaml",
78
+ "js-yaml",
79
+ "monaco-editor/esm/vs/basic-languages/yaml/yaml.contribution",
80
+ "monaco-editor/esm/vs/language/json/monaco.contribution",
81
+ "monaco-editor/esm/vs/basic-languages/javascript/javascript.contribution",
82
+ "monaco-editor/esm/vs/editor/editor.worker?worker",
83
+ "monaco-editor/esm/vs/language/json/json.worker?worker",
84
+ "monaco-editor/esm/vs/language/css/css.worker?worker",
85
+ "monaco-editor/esm/vs/language/typescript/ts.worker?worker",
86
+ "monaco-editor/esm/vs/language/html/html.worker?worker",
87
+ "monaco-yaml/yaml.worker.js",
88
+ "monaco-yaml/yaml.worker.js?worker",
89
+ "monaco-editor/*",
90
+ "monaco-editor/**"
91
+ ],
92
+ output: {
93
+ manualChunks(id) {
94
+ if (id.includes("src/components") && id.lastIndexOf(".vue") > 0) {
95
+ let name = id.substring(id.lastIndexOf("/") + 1);
96
+ name = name.substring(0, name.indexOf("."));
97
+ return "components/" + name;
98
+ }
99
+ },
100
+ // Provide global variables to use in the UMD build
101
+ // for externalized deps
102
+ globals: {
103
+ vue: "Vue",
104
+ "lodash-es": "_",
105
+ dayjs: "dayjs",
106
+ "vue-i18n": "VueI18n",
107
+ axios: "axios",
108
+ vuedraggable: "vuedraggable",
109
+ "@fast-crud/fast-crud": "FastCrud",
110
+ "ali-oss": "OSS",
111
+ "cos-js-sdk-v5": "COS",
112
+ "qiniu-js": "qiniu",
113
+ "@fast-crud/ui-interface": "FsUiInterface",
114
+ "monaco-editor": "monaco",
115
+ "monaco-yaml": "monacoYaml",
116
+ "js-yaml": "jsYaml"
117
+ }
118
+ }
119
+ }
120
+ }
121
+ };
122
+
123
+ _.merge(options, build);
124
+ return options;
125
+ };