Nuxt CodeMirror
NuxtモジュールとしてのCodemirror。 デモは下記のプレイグラウンドでご覧いただけます。
機能
- 🚀 ほぼすべてのAPIを使用して、codemirrorを自分のニーズに合わせて簡単に設定できます
- 🚠 TypeScriptで構築
- 🌲 独自のエディターを作成するためのカスタム useNuxtCodeMirror コンポーザブル
- CodeMirror 6以降向けに構築
ドキュメント
このモジュールは、1つのコンポーネント、1つのコンポーザブル、およびいくつかの型で構成されています。 詳細については、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が必要です。1つはCodeMirrorが接続するdiv要素用、1つはCodeMirrorビュー用、もう1つは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
クイックセットアップ
1つのコマンドでモジュールをNuxtアプリケーションにインストールします
npx nuxi module add nuxt-codemirror
これで完了です! NuxtアプリケーションでNuxt-codemirrorを使用できるようになりました ✨
テスト済み拡張機能
以下は、@codemirror/viewバージョン6.29.1および@codemirror/stateバージョン6.4.1で使用できるテスト済み拡張機能のリストです
その他多数...
貢献
アイデアやバグがある場合は、まずIssueを作成してください。 アイデアの場合は、ディスカッションを開始してください。
あらゆる種類の貢献を歓迎します。事前に感謝します!!
ローカル開発
# 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
FAQ
問題
- モジュールを使用して開発サーバーを起動するとエラーが発生します:
プリ変換エラー: "@codemirror/view"のインポートを解決できませんでした
、プリ変換エラー: "@codemirror/state"のインポートを解決できませんでした
。
解決策
shamefully-hoist=false
でpnpmを使用していると仮定して、@codemirror/state
と@codemirror/view
をインストールすると、これらのエラーは解消されるはずです