43 lines
983 B
TypeScript
43 lines
983 B
TypeScript
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 };
|