暂存添加编辑器和modal
This commit is contained in:
29
packages/codemirror/package.json
Normal file
29
packages/codemirror/package.json
Normal file
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"name": "@kevisual/codemirror",
|
||||
"version": "0.0.1",
|
||||
"description": "",
|
||||
"main": "dist/editor.js",
|
||||
"scripts": {
|
||||
"build": "rimraf -rf dist && rollup -c"
|
||||
},
|
||||
"keywords": [
|
||||
"kevisual"
|
||||
],
|
||||
"files": [
|
||||
"dist",
|
||||
"src"
|
||||
],
|
||||
"author": "abearxiong",
|
||||
"license": "ISC",
|
||||
"dependencies": {},
|
||||
"devDependencies": {
|
||||
"codemirror": "^6.0.1",
|
||||
"@codemirror/lang-javascript": "^6.2.2",
|
||||
"@codemirror/lang-json": "^6.0.1",
|
||||
"@rollup/plugin-node-resolve": "^15.2.3",
|
||||
"@rollup/plugin-typescript": "^11.1.6",
|
||||
"rollup": "^4.22.2",
|
||||
"tslib": "^2.7.0",
|
||||
"typescript": "^5.6.2"
|
||||
}
|
||||
}
|
||||
6
packages/codemirror/readme.md
Normal file
6
packages/codemirror/readme.md
Normal file
@@ -0,0 +1,6 @@
|
||||
# kevisual codemirror
|
||||
|
||||
```ts
|
||||
const editor = createEditorInstance(ref.current!, { typescript: false });
|
||||
editor.dom.style.height = '100%';
|
||||
```
|
||||
22
packages/codemirror/rollup.config.mjs
Normal file
22
packages/codemirror/rollup.config.mjs
Normal file
@@ -0,0 +1,22 @@
|
||||
import { nodeResolve } from '@rollup/plugin-node-resolve';
|
||||
import typescript from '@rollup/plugin-typescript';
|
||||
|
||||
const entrys = ['editor', 'editor.json'];
|
||||
const configs = entrys.map((entry) => ({
|
||||
input: `./src/${entry}.ts`, // 修改输入文件为 TypeScript 文件
|
||||
output: {
|
||||
file: `./dist/${entry}.js`,
|
||||
},
|
||||
plugins: [
|
||||
nodeResolve(),
|
||||
typescript({
|
||||
tsconfig: './tsconfig.json',
|
||||
compilerOptions: {
|
||||
declaration: true, // 生成声明文件
|
||||
declarationDir: './dist', // 声明文件输出目录
|
||||
},
|
||||
}), // 添加 TypeScript 插件
|
||||
],
|
||||
}));
|
||||
|
||||
export default configs;
|
||||
37
packages/codemirror/src/editor.json.ts
Normal file
37
packages/codemirror/src/editor.json.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { EditorView, basicSetup } from 'codemirror';
|
||||
import { json } from '@codemirror/lang-json';
|
||||
|
||||
let editor: EditorView = null;
|
||||
|
||||
/**
|
||||
* 创建单例
|
||||
* @param el
|
||||
* @returns
|
||||
*/
|
||||
const createEditorInstance = (el?: HTMLDivElement) => {
|
||||
if (editor && el) {
|
||||
editor.dom.appendChild(el);
|
||||
return editor;
|
||||
} else if (editor) {
|
||||
return editor;
|
||||
}
|
||||
editor = new EditorView({
|
||||
extensions: [basicSetup, json()],
|
||||
parent: el || document.body,
|
||||
});
|
||||
return editor;
|
||||
};
|
||||
|
||||
/**
|
||||
* 每次都创建新的实例
|
||||
* @param el
|
||||
* @returns
|
||||
*/
|
||||
export const createEditor = (el: HTMLDivElement) => {
|
||||
return new EditorView({
|
||||
extensions: [basicSetup, json()],
|
||||
parent: el || document.body,
|
||||
});
|
||||
};
|
||||
|
||||
export { editor, createEditorInstance };
|
||||
42
packages/codemirror/src/editor.ts
Normal file
42
packages/codemirror/src/editor.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { EditorView, basicSetup } from 'codemirror';
|
||||
import { javascript } from '@codemirror/lang-javascript';
|
||||
|
||||
let editor: EditorView = null;
|
||||
|
||||
type CreateOpts = {
|
||||
jsx?: boolean;
|
||||
typescript?: boolean;
|
||||
};
|
||||
/**
|
||||
* 创建单例
|
||||
* @param el
|
||||
* @returns
|
||||
*/
|
||||
const createEditorInstance = (el?: HTMLDivElement, opts?: CreateOpts) => {
|
||||
if (editor && el) {
|
||||
editor.dom.appendChild(el);
|
||||
return editor;
|
||||
} else if (editor) {
|
||||
return editor;
|
||||
}
|
||||
const { jsx, typescript } = opts || {};
|
||||
editor = new EditorView({
|
||||
extensions: [basicSetup, javascript({ jsx, typescript })],
|
||||
parent: el || document.body,
|
||||
});
|
||||
return editor;
|
||||
};
|
||||
|
||||
/**
|
||||
* 每次都创建新的实例
|
||||
* @param el
|
||||
* @returns
|
||||
*/
|
||||
export const createEditor = (el: HTMLDivElement, opts?: CreateOpts) => {
|
||||
return new EditorView({
|
||||
extensions: [basicSetup, javascript({ jsx: opts?.jsx, typescript: opts?.typescript })],
|
||||
parent: el || document.body,
|
||||
});
|
||||
};
|
||||
|
||||
export { editor, createEditorInstance };
|
||||
23
packages/codemirror/tsconfig.json
Normal file
23
packages/codemirror/tsconfig.json
Normal file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ESNext",
|
||||
"module": "ESNext",
|
||||
"lib": [
|
||||
"DOM",
|
||||
"ESNext"
|
||||
],
|
||||
"moduleResolution": "Node",
|
||||
"declaration": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"outDir": "./dist",
|
||||
"rootDir": "./src"
|
||||
},
|
||||
"include": [
|
||||
"src"
|
||||
],
|
||||
"exclude": [
|
||||
"node_modules",
|
||||
"dist"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user