This commit is contained in:
熊潇 2025-05-12 14:33:04 +08:00
parent 64bc50f1e8
commit 55b1c5fdca
8 changed files with 39 additions and 53462 deletions

View File

@ -8,13 +8,13 @@ node_modules
dist dist
# build # build
/build build
/logs logs
.turbo .turbo
/pack-dist pack-dist
# astro # astro
.astro .astro

View File

@ -1,19 +0,0 @@
import { EditorView } from 'codemirror';
declare let editor: EditorView;
type CreateOpts = {
jsx?: boolean;
typescript?: boolean;
};
/**
*
* @param el
* @returns
*/
declare const createEditorInstance: (el?: HTMLDivElement, opts?: CreateOpts) => EditorView;
/**
*
* @param el
* @returns
*/
export declare const createEditor: (el: HTMLDivElement, opts?: CreateOpts) => EditorView;
export { editor, createEditorInstance };

File diff suppressed because one or more lines are too long

View File

@ -1,15 +0,0 @@
import { EditorView } from 'codemirror';
declare let editor: EditorView;
/**
*
* @param el
* @returns
*/
declare const createEditorInstance: (el?: HTMLDivElement) => EditorView;
/**
*
* @param el
* @returns
*/
export declare const createEditor: (el: HTMLDivElement) => EditorView;
export { editor, createEditorInstance };

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{ {
"name": "@kevisual/codemirror", "name": "@kevisual/codemirror",
"version": "0.0.5", "version": "0.0.7",
"description": "", "description": "",
"main": "dist/editor.js", "main": "dist/editor.js",
"private": false, "private": false,

View File

@ -4,6 +4,7 @@ let editor: EditorView = null;
export type EditorOptions = { export type EditorOptions = {
extensions?: any[]; extensions?: any[];
hasBasicSetup?: boolean;
}; };
/** /**
* *
@ -18,8 +19,12 @@ const createEditorInstance = (el?: HTMLDivElement, opts?: EditorOptions) => {
return editor; return editor;
} }
const extensions = opts?.extensions || []; const extensions = opts?.extensions || [];
const hasBaseicSetup = opts?.hasBasicSetup ?? true;
if (hasBaseicSetup) {
extensions.unshift(basicSetup);
}
editor = new EditorView({ editor = new EditorView({
extensions: [basicSetup, ...extensions], extensions: extensions,
parent: el || document.body, parent: el || document.body,
}); });
editor.dom.style.height = '100%'; editor.dom.style.height = '100%';
@ -33,8 +38,12 @@ const createEditorInstance = (el?: HTMLDivElement, opts?: EditorOptions) => {
*/ */
export const createEditor = (el: HTMLDivElement, opts?: EditorOptions) => { export const createEditor = (el: HTMLDivElement, opts?: EditorOptions) => {
const extensions = opts?.extensions || []; const extensions = opts?.extensions || [];
const hasBaseicSetup = opts?.hasBasicSetup ?? true;
if (hasBaseicSetup) {
extensions.unshift(basicSetup);
}
const editor = new EditorView({ const editor = new EditorView({
extensions: [basicSetup, ...extensions], extensions,
parent: el || document.body, parent: el || document.body,
}); });
editor.dom.style.height = '100%'; editor.dom.style.height = '100%';

View File

@ -10,6 +10,8 @@ type CreateOpts = {
jsx?: boolean; jsx?: boolean;
typescript?: boolean; typescript?: boolean;
type?: 'javascript' | 'json' | 'html' | 'markdown' | 'css'; type?: 'javascript' | 'json' | 'html' | 'markdown' | 'css';
hasBasicSetup?: boolean;
extensions?: any[];
}; };
/** /**
* *
@ -24,27 +26,30 @@ const createEditorInstance = (el?: HTMLDivElement, opts?: CreateOpts) => {
return editor; return editor;
} }
const { type = 'javascript' } = opts || {}; const { type = 'javascript' } = opts || {};
const plugins = [basicSetup]; const extensions = opts?.extensions || [];
const hasBaseicSetup = opts?.hasBasicSetup ?? true;
if (hasBaseicSetup) {
extensions.unshift(basicSetup);
}
switch (type) { switch (type) {
case 'json': case 'json':
plugins.push(json()); extensions.push(json());
break; break;
case 'javascript': case 'javascript':
plugins.push(javascript({ jsx: opts?.jsx, typescript: opts?.typescript })); extensions.push(javascript({ jsx: opts?.jsx, typescript: opts?.typescript }));
break; break;
case 'css': case 'css':
plugins.push(css()); extensions.push(css());
break; break;
case 'html': case 'html':
plugins.push(html()); extensions.push(html());
break; break;
case 'markdown': case 'markdown':
plugins.push(markdown()); extensions.push(markdown());
break; break;
} }
editor = new EditorView({ editor = new EditorView({
extensions: plugins, extensions: extensions,
parent: el || document.body, parent: el || document.body,
}); });
editor.dom.style.height = '100%'; editor.dom.style.height = '100%';
@ -58,26 +63,30 @@ const createEditorInstance = (el?: HTMLDivElement, opts?: CreateOpts) => {
*/ */
export const createEditor = (el: HTMLDivElement, opts?: CreateOpts) => { export const createEditor = (el: HTMLDivElement, opts?: CreateOpts) => {
const { type = 'javascript' } = opts || {}; const { type = 'javascript' } = opts || {};
const plugins = [basicSetup]; const extensions = opts?.extensions || [];
const hasBaseicSetup = opts?.hasBasicSetup ?? true;
if (hasBaseicSetup) {
extensions.unshift(basicSetup);
}
switch (type) { switch (type) {
case 'json': case 'json':
plugins.push(json()); extensions.push(json());
break; break;
case 'javascript': case 'javascript':
plugins.push(javascript({ jsx: opts?.jsx, typescript: opts?.typescript })); extensions.push(javascript({ jsx: opts?.jsx, typescript: opts?.typescript }));
break; break;
case 'css': case 'css':
plugins.push(css()); extensions.push(css());
break; break;
case 'html': case 'html':
plugins.push(html()); extensions.push(html());
break; break;
case 'markdown': case 'markdown':
plugins.push(markdown()); extensions.push(markdown());
break; break;
} }
const editor = new EditorView({ const editor = new EditorView({
extensions: plugins, extensions: extensions,
parent: el || document.body, parent: el || document.body,
}); });
editor.dom.style.height = '100%'; editor.dom.style.height = '100%';