Nuxt 代码镜像
将 Codemirror 作为 Nuxt 模块。演示可以在下面的游乐场中找到
功能
- 🚀 使用几乎所有 API 轻松配置代码镜像以满足您的需求
- 🚠 使用 TypeScript 构建
- 🌲 自定义 useNuxtCodeMirror 组合式函数以创建您自己的编辑器
- 为 CodeMirror 6 及更高版本构建
文档
此模块包含一个组件、一个组合式函数和一些供您使用的类型。阅读 CodeMirror 参考指南以获取更深入的信息:CodeMirror 文档
NuxtCodeMirror.vue
此组件自动导入,是 CodeMirror 的包装器。
组件属性
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 来实现。有关如何执行此操作的示例,请参见此处:🏀 在线游乐场
如果出于某种原因您想编写自己的 CodeMirror 组件,您也可以这样做 :)
UseNuxtCodeMirror.ts
此组合式函数是 NuxtCodeMirror 组件的底层魔力,也自动导入。
它至少需要 3 个 Ref,一个用于 CodeMirror 将连接到的 div 元素,一个用于 CodeMirror 视图,一个用于 CodeMirror 状态
确保在 onMounted
中执行组合式函数,否则您将收到错误,因为 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
常见问题
问题
- 使用您的模块启动开发服务器时出现错误:
预转换错误:无法解析导入“@codemirror/view”
,预转换错误:无法解析导入“@codemirror/state”
。
解决方案
- 假设您使用 pnpm 且
shamefully-hoist=false
,安装@codemirror/state
和@codemirror/view
,这些错误应该会消失