Nuxt CodeMirror
Codemirror 作为 Nuxt 模块。演示可以在下面的 playground 中找到
特性
- 🚀 使用几乎每个 API 轻松配置 codemirror 以满足您自己的需求
- 🚠 使用 Typescript 构建
- 🌲 自定义 useNuxtCodeMirror composable 用于创建您自己的编辑器
- 为 CodeMirror 6 及更高版本构建
文档
此模块包含一个组件、一个 Composable 和一些类型供您使用。阅读 CodeMirror 参考指南以获取更多深入信息:CodeMirror 文档
NuxtCodeMirror.vue
此组件是自动导入的,是 CodeMirror 的包装器。
组件 props
interface NuxtCodeMirrorProps
extends Omit<EditorStateConfig, 'doc' | 'extensions'> {
/** value of the auto created model in the editor. Works as underlying logic of a V-Model */
modelValue?: string
/** The height value of the editor. */
height?: string
/** The minimum height value of the editor. */
minHeight?: string
/** The maximum height value of the editor. */
maxHeight?: string
/** The width value of the editor. */
width?: string
/** The minimum width value of the editor. */
minWidth?: string
/** The maximum width value of the editor. */
maxWidth?: string
/** focus on the editor. */
autoFocus?: boolean
/** Enables a placeholder—a piece of example content to show when the editor is empty. */
placeholder?: string | HTMLElement
/**
* `light` / `dark` / `Extension` Defaults to `light`.
* @default light
*/
theme?: 'light' | 'dark' | 'none' | Extension
/**
* Whether to optional basicSetup by default
* @default true
*/
basicSetup?: boolean | BasicSetupOptions
/**
* This disables editing of the editor content by the user.
* @default true
*/
editable?: boolean
/**
* This disables editing of the editor content by the user.
* @default false
*/
readOnly?: boolean
/**
* Controls whether pressing the `Tab` key inserts a tab character and indents the text (`true`)
* or behaves according to the browser's default behavior (`false`).
* @default true
*/
indentWithTab?: boolean
/** Fired whenever a change occurs to the document. */
onChange?(value: string, viewUpdate: ViewUpdate): void
/** Some data on the statistics editor. */
onStatistics?(data: Statistics): void
/** Fired whenever any state change occurs within the editor, including non-document changes like lint results. */
onUpdate?(viewUpdate: ViewUpdate): void
/** The first time the editor executes the event. */
onCreateEditor?(view: EditorView, state: EditorState): void
/** Fired whenever the editor is focused. */
onFocus?(view: ViewUpdate): void
/** Fired whenever the editor is blurred. */
onBlur?(view: ViewUpdate): void
/**
* Extension values can be [provided](https://codemirror.net/6/docs/ref/#state.EditorStateConfig.extensions) when creating a state to attach various kinds of configuration and behavior information.
* They can either be built-in extension-providing objects,
* such as [state fields](https://codemirror.net/6/docs/ref/#state.StateField) or [facet providers](https://codemirror.net/6/docs/ref/#state.Facet.of),
* or objects with an extension in its `extension` property. Extensions can be nested in arrays arbitrarily deep—they will be flattened when processed.
*/
extensions?: Extension[]
/**
* If the view is going to be mounted in a shadow root or document other than the one held by the global variable document (the default), you should pass it here.
* Originally from the [config of EditorView](https://codemirror.net/6/docs/ref/#view.EditorView.constructor%5Econfig.root)
*/
root?: ShadowRoot | Document
/**
* Create a state from its JSON representation serialized with [toJSON](https://codemirror.net/docs/ref/#state.EditorState.toJSON) function
*/
initialState?: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
json: any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
fields?: Record<string, StateField<any>>
}
}
NuxtCodeMirror 组件接受一个模板 ref,并暴露 CodeMirror div 元素、编辑器视图和编辑器状态
interface CodeMirrorRef {
/** Container element of the CodeMirror instance */
container: HTMLDivElement | null
/** The EditorView of the CodeMirror instance */
view: EditorView | undefined
/** The EditorState of the CodeMirror instance */
state: EditorState | undefined
/** Editor element of the CodeMirror instance */
editor: HTMLDivElement | null
}
如果您需要访问底层的 CodeMirror 实例,您可以通过添加一个与您的模板 ref 同名的 ref 来做到这一点。有关如何执行此操作的示例,请在此处找到:🏀 在线 playground
如果由于某种原因您想编写自己的 CodeMirror 组件,那么您也可以这样做 :)
UseNuxtCodeMirror.ts
这个 composable 是 NuxtCodeMirror 组件的底层魔力,也是自动导入的。
它至少需要 3 个 Ref,一个用于 CodeMirror 将连接到的 div 元素,一个用于 CodeMirror 视图,一个用于 CodeMirror 状态
请确保在 onMounted
中执行 composable,否则您会收到错误,因为 codemirror 无法链接到 DOM。
const editor = ref<HTMLDivElement | null>(null)
const container = ref<HTMLDivElement | null>(null)
const view = ref<EditorView>()
const state = ref<EditorState>()
onMounted(() => {
useNuxtCodeMirror({
...props,
container: editor.value,
viewRef: view,
stateRef: state,
containerRef: container,
})
})
有关如何实现此功能的更多信息,请参阅 源代码,以获取您自己版本的灵感
鸣谢
如果没有以下项目,这个 Nuxt 模块是不可能实现的
- @uiw/react-codemirror: https://github.com/uiwjs/react-codemirror
- @surmon-china/vue-codemirror: https://github.com/surmon-china/vue-codemirror
快速设置
使用一个命令将模块安装到您的 Nuxt 应用程序中
npx nuxi module add nuxt-codemirror
就这样!您现在可以在您的 Nuxt 应用程序中使用 Nuxt-codemirror ✨
测试过的扩展
以下是您可以使用的经过测试的扩展列表,版本为 @codemirror/view 6.29.1 和 @codemirror/state 6.4.1
等等...
贡献
如果您有想法或错误,请随时先提出问题。对于想法,请发起讨论。
我欢迎任何形式的贡献,先谢谢您!!
本地开发
# Install dependencies
pnpm i
# Generate type stubs
pnpm dev:prepare
# Develop with the playground
pnpm dev
# Build the playground
pnpm dev:build
# Run ESLint
pnpm lint
# Run Vitest
pnpm test
pnpm test:watch
# Release new version
pnpm release
常见问题
问题
- 使用您的模块启动开发服务器时,出现错误:
Pre-transform error: Failed to resolve import "@codemirror/view"
,Pre-transform error: Failed to resolve import "@codemirror/state"
。
解决方案
- 假设您使用 pnpm 和
shamefully-hoist=false
安装@codemirror/state
和@codemirror/view
,这些错误应该会消失